void ADC_Task(void* pData){ int ch = 0; float volt = 0; alt_u16 data16; printf("Monitoring ADC value. Press KEY0 or KEY1 to terminal the monitor process.\r\n"); ADC_Read(ch); while(!bKeyPressed){ //if (next_ch >= 8) // next_ch = 0; data16 = ADC_Read(ch); // 12-bits resolution volt = (float)data16 * 3.3 / 4095.0; printf("CH%d=%.2f V\r\n", ch, volt); if (volt < 2.0 && volt > 1.0) //close to an object { OSQPost(MainQueue, BLOCKED_FRONT); //post and alert navigation task of the left and right } } //read from CH0, switch to CH1 or CH2 when obstacle detected }
void Main() { double accele_x_bias = 0.0, accele_y_bias = 0.0; double jyro_x_bias = 0.0, jyro_y_bias = 0.0; double accele_x = 0.0, accele_y = 0.0, accele_z = 0.0; double jyro_x = 0.0, jyro_y = 0.0; double theta_x = 0.0, theta_y = 0.0; double roll = 0.0, pitch = 0.0; int i = 0; SetUp(); for(i = 0; i < 100; i ++){ accele_x_bias += ADC_Read(ANAROG_PIN_ACCELE_X); accele_y_bias += ADC_Read(ANAROG_PIN_ACCELE_Y); jyro_x_bias += ADC_Read(ANAROG_PIN_JYRO_X); jyro_y_bias += ADC_Read(ANAROG_PIN_JYRO_Y); } accele_x_bias /= 100.0; accele_y_bias /= 100.0; jyro_x_bias /= 100.0; jyro_y_bias /= 100.0; while(1){ TMR0 = 0; accele_x = (ADC_read(ANAROG_PIN_ACCELE_X) - accele_x_bias) * ACCELE_COEFFICIENT; accele_y = (ADC_read(ANAROG_PIN_ACCELE_Y) - accele_y_bias) * ACCELE_COEFFICIENT; accele_z = (ADC_read(ANAROG_PIN_ACCELE_Z) - ACCELE_Z_BIAS) * ACCELE_COEFFICIENT; jyro_x = (ADC_read(ANAROG_PIN_JYRO_X) - jyro_x_bias) * JYRO_COEFFICIENT; jyro_y = (ADC_read(ANAROG_PIN_JYRO_Y) - jyro_y_bias) * JYRO_COEFFICIENT; theta_x = atan(accele_x / sqrt(accele_y * accele_y + accele_z * accele_z)) * (180.0 / PI); theta_y = atan(accele_y / sqrt(accele_x * accele_x + accele_z * accele_z)) * (180.0 / PI); roll += (jyro_x * DT) - (W * roll * DT) + (W * theta_x * DT); //相補フィルタ pitch += (jyro_y * DT) - (W * pitch * DT) + (W * theta_y * DT); if(-0.15 < roll && roll < 0.15) roll = 0.0; if(-0.15 < pitch && pitch < 0.15) pitch = 0.0; #if DEBUG_MODE #if ACCELE_CHECK_MODE accele_x = ADC_read(ANAROG_PIN_ACCELE_X); accele_y = ADC_read(ANAROG_PIN_ACCELE_Y); accele_z = ADC_read(ANAROG_PIN_ACCELE_Z); AcceleCheck(accele_x, accele_y, accele_z); #else Debug(roll, pitch); #endif #else OutPut(pitch - roll, pitch + roll); //right, left #endif } }
void InitSensores() { rede = 0; bateria = 0; int i = 0; for (i = 0; i < 10; i++) { rede = ADC_Read(ADC0_BASE); bateria = ADC_Read(ADC1_BASE); LCD_Process(); } }
void main() { InitMain(); current_duty = 200; // initial value for current_duty current_duty1 = 200; // initial value for current_duty1 TRISA = 0xFF; // PORTA is input TRISB = 0xFF; // Set PORTB as input //PORTB = 0x00; PWM1_Start(); // start PWM1 PWM2_Start(); // start PWM2 PWM1_Set_Duty(0); // Set current duty for PWM1 PWM2_Set_Duty(0); // Set current duty for PWM2 adcon1 = 0b1000010; while (1) { // endless loop temp_res = ADC_Read(0); temp_res=temp_res*255.0/1024.0; newduty=temp_res; if(RB0_bit==1 && RB1_bit==0){ PWM1_Set_Duty(newduty); PWM2_Set_Duty(0); }else if(RB0_bit==0 && RB1_bit==1){ PWM1_Set_Duty(0); PWM2_Set_Duty(newduty); } else{ PWM1_Set_Duty(0); PWM2_Set_Duty(0); } Delay_ms(5); // slow down change pace a little } }
void SysTick_Handler(void){static unsigned char i=0; unsigned char x; unsigned char y; //adc stuff ADCMail = ADC_Read(); //call ADC_In to get sample and put into global variable, ADCMail NewData = ADCMail; if(NewData!=OldData){ Ship_Position = ConvertX(NewData); //get the converted position into pixels OldData = NewData; //update the new data } else{ Ship_Position = ConvertX(OldData); //not new, so get old data into pixels } //switch stuff if((GPIO_PORTE_DATA_R&0x01)==1){ //if switch is pressed //fire missile if(x!=0){ if(Missile[i].life==0){ //if missile doesn't exist then create it Missile[i] .x = (Ship_Position+(Ship_Position+28))/2; Missile[i] .y = 208; //top of the ship Missile[i] .life = 1; //give the missile life //sounds Sound_Play(Pokeball_Sound,4080); x=0; } } i = (i+1)%1; Missle_Move(); } else{ Missle_Move(); //dontfiremissle } //move sprites Enemy1_Move(); //test for hit for(y=0;y<4;y++){ for(x=0;x<1;x++){ if((Missile[x].x+16)>=Enemy1[y].x){ if(Missile[x].x<=(Enemy1[y].x+33)){ if(Missile[x].y<=(Enemy1[y].y+32)){ if(Missile[x].y>=Enemy1[y].y){ if(Missile[x].life==1){ if(Enemy1[y].life>0){ Missile[x].life=0; Enemy1[y].life-=2; hits++; if(Enemy1[y].life==0){ Sound_Play(Pikachu_Sound,10000); kill++; } } } } } } } } } ADCStatus = 1; //ADCStatus=1 to indicate fresh sample in ADCMail Semaphore = 1; // trigger }
void main(){ ADCON1 = 0b00001110; // HABILITA CANAL AN0 E AN1 TRISA.RA0 = 1; Lcd_Init (); // START LCD Lcd_Cmd(_Lcd_Clear); // CLEAR LCD Lcd_Cmd(_LCD_CURSOR_OFF); // TURN OFF LCD CURSOR ADC_Init(); while(1){ adc_rd = ADC_Read(0); // GET ADC VALUE FROM 1ST CHANNEL EEPROM_Write(0x08, adc_rd); //ESCREVE "adc_rd" NA POSICAO 0x08 DA EEPROM IntToStr(adc_rd, adcprint); // CONVERSION TO STRING Lcd_out(1,1,"COUNTS..: "); Lcd_Out_Cp(adcprint); // PRINTS ADC STEPS IN LCD FIRST ROW if (diff != adc_rd){ BarSegmentNum = adc_rd /(1023 / 16); // CALCULATE NUMBERS OF BAR GRAPH SEGMENTS Lcd_out(2,1," "); // CLEARS LCD SECOND ROW for (i = 0; i < BarSegmentNum; i++){ // LOOP TO PRINT EACH BAR SEGMENT IN SECOND ROW CustomChar(2, i+1);}} // PRINT CUSTOM CHARACTER FOR BARGRAPH SEGMENT Delay_ms(750); // STABILIZE LCD DISPLAY diff = adc_rd; // LCD SHOWS EEPROM VALUE ADCeer = EEPROM_Read(0x08); // LE A POSICAO 0x08 DA EEPROM ShortToStr (ADCeer, ADCeerSTR); //Lcd_Cmd(_LCD_CLEAR); // LIMPA A TELA DO LCD Lcd_Out(2, 1, ADCeerSTR); Delay_ms(2000); Lcd_Cmd(_LCD_CLEAR); // LIMPA A TELA DO LCD } // END WHILE } // END MAIN
void TIM3_IRQHandler() { // Checks whether the TIM3 interrupt has occurred or not if (TIM_GetITStatus(TIM3, TIM_IT_Update)) { // Read ADC value (10-bit PWM) adcValue = ADC_Read() >> 2; // Add audio effect if (effect & LOW_PASS) { adcValue = low_pass(adcValue); } if (effect & PITCH_UP) { adcValue = pitch_up(adcValue); } if (effect & PITCH_DOWN) { adcValue = pitch_down(adcValue); } // Write to PWM PWM_Write(adcValue); // Clears the TIM3 interrupt pending bit TIM_ClearITPendingBit(TIM3, TIM_IT_Update); } }
/******************************************************************************* **函 数: ADC_ReadAverage **功 能: 读取ADC转换平均值 **参 数: times -- 平均值次数 ** *ADC_Result -- 返回读取的ADC平均值 **返 回: 1:成功 0:失败 *******************************************************************************/ u8 ADC_ReadAverage(u8 times,u16 *adcAverage) { u32 temp_val=0; u16 ADC_val = 0; u8 t; ADC1->CR2 |= 1<<0; //ADCON POWER ON for(t=0;t<times;t++) { if(1==ADC_Read(&ADC_val)) { temp_val += ADC_val; delay_us(500); } else { ADC1->CR2 &= ~(1<<0); //ADCON POWER OFF return 0; } } *adcAverage = temp_val/times; ADC1->CR2 &= ~(1<<0); //ADCON POWER OFF return 1; }
uint8_t Atomizer_ReadBoardTemp() { uint8_t i; uint16_t thermAdcSum, lowerBound, higherBound; uint32_t thermRes; // Sample and average thermistor resistance // A power-of-2 sample count is better for // optimization. 16 is also the biggest number // of samples that will fit in a uint16_t. thermAdcSum = 0; for(i = 0; i < 16; i++) { thermAdcSum += ADC_Read(ADC_MODULE_TEMP); } thermRes = ATOMIZER_ADC_THERMRES(thermAdcSum / 16); // Handle corner cases if(thermRes >= Atomizer_boardTempTable[0]) { return 0; } else if(thermRes <= Atomizer_boardTempTable[20]) { return 99; } // Look up lower resistance bound (higher temperature bound) for(i = 1; i < 20 && thermRes < Atomizer_boardTempTable[i]; i++); // Interpolate lowerBound = Atomizer_boardTempTable[i]; higherBound = Atomizer_boardTempTable[i - 1]; return 5 * (i - 1) + (thermRes - lowerBound) * 5 / (higherBound - lowerBound); }
int16_t axis_offset(uint8_t channel, uint16_t scaling) { int32_t value = ADC_Read(channel); value-=504; //char tmp[21]; //sprintf(tmp,"%ld ",value); //lcd_gotoxy(0,0); ////lcd_puts(tmp); if(value < -10 || value > 10) { value/=scaling; //char tmp[21]; //sprintf(tmp,"%ld ",value); //lcd_gotoxy(0,1); //lcd_puts(tmp); return value; } return 0; }
void doMeas(uint8_t meas[ADC_CHNS]) { uint8_t i; for(i = 0; i < ADC_CHNS; i++){ meas[i] = ADC_Read(i); } }
int isLeftSafe(){ ir_value = ADC_Read(IRSensorLeft); if(250<=ir_value && ir_value<800){ // ~5cm return FALSE; } return TRUE; }
int isMiddleSafe(){ ir_value = ADC_Read(IRSensorCenter); if(250<=ir_value && ir_value<800){ // ~5cm return FALSE; } return TRUE; }
void DEMO_ADC(void){ int ch = 0, next_ch=0; alt_u16 data16; printf("Monitor ADC Value. Press KEY0 or KEY1 to terminal the monitor process.\r\n"); ADC_Read(next_ch); while(!bKeyPressed){ next_ch++; if (next_ch >= 8) next_ch = 0; data16 = ADC_Read(next_ch); // 12-bits resolution printf("CH%d=%.2f V\r\n", ch, (float)data16 * 3.3 / 4095.0); ch = next_ch; } }
void DAS(void){ unsigned short input; if(NumSamples < RUNLENGTH){ // finite time run input = ADC_Read(1); DASoutput = Filter(input); FilterWork++; // calculation finished } }
void DEMO_ADC(void) { int ch = 0, next_ch=0; alt_u16 data16; float volt = 0; printf("Monitor ADC Value. Press KEY0 or KEY1 to terminal the monitor process.\r\n"); ADC_Read(next_ch); while(!bKeyPressed) { //next_ch++; //if (next_ch >= 8) // next_ch = 0; data16 = ADC_Read(next_ch); // 12-bits resolution volt = (float)data16 * 3.3 / 4095.0; printf("CH%d=%.2f V\r\n", ch, volt); usleep(1000000); //ch = next_ch; } }
void LeSensores() { rede = ADC_Read(ADC0_BASE); bateria = ADC_Read(ADC1_BASE); if(rede <= 18) LCD_BlackLight_Disable(); else LCD_BlackLight_Enable(); if(rede == ADC_ERROR_TIMEOUT || bateria == ADC_ERROR_TIMEOUT) { LCD_Clear(); LCD_Write("ERRO ADC!", 1); SysCtlDelay(halfseg*4); } }
void main() { int i; unsigned char chTemp = 0b00000000; char chNotificar = 'N'; unsigned char chUmbrLuz = chUmbralLuz; unsigned char chTmpStat = chNoHayLuz; unsigned char chStatLuz = chNoHayLuz; // Limpieza del Tri-estado, en cada etapa de la configuracion se cambiara el valor de este registro TRISA = 0b00000000; // Configuracion de los dispositivos. ADC_Init(); UART_Init(); //------------------------------------------------ // Aviso que vamos al bucle principal - Enciende y apaga el RA4 PORTA = 0b00000100; // ------------------------------------------------- // El bucle principal while(1) { // Obtenemos el valor de si hay luz o no if(ADC_Read() > chUmbrLuz) { chStatLuz = chSiHayLuz ; PORTA = LATA | 0b00010000; } else { chStatLuz = chNoHayLuz ; PORTA = LATA & 0b00000111; } // Evaluamos si hubo cambio en el valor de la luz chNotificar =(chStatLuz == chTmpStat)? 'N' : 'S' ; // Volvemos a colocar los dos valores iguales chTmpStat = chStatLuz; // Si hubo cambio en las condiciones de la luz notificamos if (chNotificar == 'S') { // chTemp = chTemp == 0b00000000? 0b00000001 : chTemp++; // TXREG = chTemp++; //mensaje[i]; TXREG = msgID; __delay_ms(300); TXREG = chStatLuz; //mensaje[i]; i = i > 9? 0 : i++; // enviaMensaje(chStatLuz); } __delay_ms(10); } }
uint32_t createSeed32(void){ uint8_t seedCount=0; uint32_t seed[1]={0}; while(seedCount<32){ uint16_t adcval1 = ADC_Read(); uint16_t adcval2 = ADC_Read(); if((getLSB(adcval1) && !getLSB(adcval2))){ SetBit(seed,seedCount); seedCount++; } if((!getLSB(adcval1) && getLSB(adcval2))){ ClearBit(seed,seedCount); seedCount++; } } return seed[0]; }
int main(void) { every_gpio_set_dir(LED_PORT, LED_PIN, OUTPUT); ADC_Init(); EVERY_GPIO_SET_PULL(ADC_PORT, ADC_PIN, PULL_UP); EVERY_GPIO_SET_FUNCTION(ADC_PORT, ADC_PIN, ADC, IOCON_IO_ADMODE_ANALOG); every_gpio_write(LED_PORT, LED_PIN, true); ADC_Read(5); while (true) { int i = ADC_Read(5); //int i = 512; every_gpio_write(LED_PORT, LED_PIN, false); delay(50 * (i+100)); every_gpio_write(LED_PORT, LED_PIN, true); delay(50 * (1124-i)); } return 0; }
void generate_ADCFrame() { uint16_t adcval; adcval=ADC_Read(AD_RIGHT); //right adcval=adcval-ADC_Read(AD_LEFT); //left -> value = right - left CIM_frame.data[0]=adcval&0xff; CIM_frame.data[1]=adcval>>8; adcval=ADC_Read(AD_DOWN); //down adcval=adcval-ADC_Read(AD_UP); //up -> value = down-up CIM_frame.data[2]=adcval&0xff; CIM_frame.data[3]=adcval>>8; adcval=ADC_Read(AD_PRESSURE); //pressure CIM_frame.data[4]=adcval&0xff; CIM_frame.data[5]=adcval>>8; CIM_frame.data_size=6; }
char Temperature_read(void) { u32 Temperature; u8 Valor_bit; Valor_bit = ADC_Read(); /*Read the convertion of the ADC*/ Temperature=(Valor_bit*100/545); Temperature=((unsigned u32)(Valor_bit*3300)<<7)/255; Temperature=Temperature>>7; return Temperature; }
int adcdump (int argc, char * argv[], environment * env) { unsigned int x,c; ADCsample_t sample; c = ADC_Channels(); for (x = 0; x < c; x++) { sample = ADC_Read(x); printf_P(PSTR("%.2d - % 0d\r\n"), x, sample); } printf_P(PSTR("\r\n")); return 0; }
int main() { int ADC_Val,i; while(1) { for(i=0;i<8;i++) { ADC_Val=ADC_Read(i); printf("ADC value %d",ADC_Val); } for(i=0;i<10000;i++); } return 0; }
int adc (int argc, char * argv[], environment * env) { unsigned int chan; ADCsample_t sample; if (argc < 2) { printf_P(PSTR("Usage: %s <ADC channel>\r\n"), argv[0]); return 0; } chan = strtoul(argv[2], 0, 0); sample = ADC_Read(chan); printf_P(PSTR("%d\r\n"), sample); return 0; }
void generate_ADCFrame() { uint16_t adcval; uint8_t i; for (i=0;i<6;i++) { adcval=ADC_Read(i); CIM_frame.data[i<<1]=(uint8_t)(adcval&0xff); CIM_frame.data[(i<<1) +1]=(uint8_t)(adcval>>8); } CIM_frame.data_size=12; }
void checkBankA(void) { for (int i=0; i < 4;i++) { int bankAValue = ADC_Read(BANK_A_PIN); int rawValue = bankAValue - buttonValues[i]; rawValue = (rawValue < 0) ? -rawValue : rawValue; if (rawValue <= THRESHOLD) { //then this must be button 1 + 1 pressed- 1,2,3,4 buttonPressedState = BUTTON_DOWN; buttonVal = i + 1; break; } } }
void Atomizer_ReadInfo(Atomizer_Info_t *info) { uint32_t vSum, iSum; uint16_t savedTargetVolts = 0; uint8_t wasOff = 0, numSamples, i; if(Atomizer_curState == POWEROFF) { // Power on at 1.00V for measurement wasOff = 1; savedTargetVolts = Atomizer_targetVolts; Atomizer_SetOutputVoltage(1000); Atomizer_Control(1); Timer_DelayMs(2); } // Sample V and I vSum = iSum = 0; numSamples = wasOff ? 50 : 1; for(i = 0; i < numSamples; i++) { Timer_DelayUs(10); iSum += ADC_Read(ADC_MODULE_CURS); Timer_DelayUs(10); vSum += ADC_Read(ADC_MODULE_VATM); } info->resistance = ATOMIZER_ADC_RESISTANCE(vSum, iSum); if(wasOff) { info->voltage = info->current = 0; // Power off and restore previous target voltage Atomizer_Control(0); Atomizer_SetOutputVoltage(savedTargetVolts); } else { info->voltage = ATOMIZER_ADC_VOLTAGE(vSum); info->current = ATOMIZER_ADC_CURRENT(iSum); } }
bool ReadADCChannels(alt_u8 *uiChannels, alt_u8 uiChannelCount, alt_u32 *uiValues) { if(uiChannelCount < 1) return false; // Start Read for the first channel ADC_Read(uiChannels[0]); // Read rest for(int i = 0; i < uiChannelCount-1; i++) { uiValues[i] = ADC_Read(uiChannels[i+1]); uiValues[i] *= 100; uiValues[i] /= 1311; } // Read last uiValues[uiChannelCount-1] = ADC_Read(0); uiValues[uiChannelCount-1] *= 100; uiValues[uiChannelCount-1] /= 1311; return true; }
int everypio_analog_read(every_pin pin){ // check this is an adc pin // 8 pins support adc, they are mapped to "channels" 0 - 7, lookup // channel uint8_t channel = _everypio_map_pin_to_channel(pin); if (channel == 8) { return -1; } if (everypio_analog_config & ( 1 << channel)) { return ADC_Read(channel); } else { return -1; } }