int main() { int x,y,z; adc_init(); lcd_init(); delay(100); data('a'); while(1) { cmd(0x01); // Clear LCD AD0CR = 0x00200600; // ADC Stop, select Channel 0 AD0CR |= 0x01000000; // Start ADC, Channel 0 x = read_adc(); cmd(0x80); dispInt(x); AD0CR = 0x00200602; AD0CR |= 0x01000000; // Start ADC, Channel 0 y = read_adc(); cmd(0x85); dispInt(y); AD0CR = 0x00200604; AD0CR |= 0x01000000; // Start ADC, Channel 0 z = read_adc(); cmd(0x8A); dispInt(z); if(x>600 ) { cmd(0x01); str("ALERT x = "); cmd(0xc0); dispInt(x); delay(1000); cmd(0x01); } if(y<300 ) { cmd(0x01); str("ALERT y = "); cmd(0xc0); dispInt(y); delay(1000); cmd(0x01); } delay(100); } }
void main () { set_tris_a(0xff);//se pone el puerto RA analogo de entrada set_tris_d(0x00);//se pone el puerto RD en salida para prender los led setup_adc_ports(ALL_ANALOG);//configuracion de los puertos analogos setup_adc(ADC_CLOCK_DIV_32);//el osilador del ADC while (true) { set_adc_channel(0); delay_us(10); valor = read_adc(); if(valor){ output_d(0x01); }else{ output_d(0x00); } set_adc_channel(1); delay_us(10); valor = read_adc(); if{ output_d(0x02); }else{ output_d(0x00); } }
void main() { setup_adc_ports(ALL_ANALOG); setup_adc(ADC_CLOCK_INTERNAL); setup_psp(PSP_DISABLED); setup_spi(SPI_SS_DISABLED); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); setup_vref(FALSE); // TODO: USER CODE!! set_adc_channel(0); // set ref valus delay_ms(100); ref_0 =read_adc(); delay_ms(100); set_adc_channel(3); // set ref valus delay_ms(100); ref_3 =read_adc(); delay_ms(100); output_b(0b11111111); delay_ms(700); output_b(0); while(1){ set_adc_channel(0); delay_ms(20); // take readings adc_val_0 =read_adc(); delay_ms(20); if(adc_val_0 > ref_0+cons){ l_0 =1; output_high(pin_d7); delay_ms(500); } else{ l_0=0; output_low(pin_d7); // delay_ms(500); } set_adc_channel(3); delay_ms(20); // take readings adc_val_3 =read_adc(); delay_ms(20); if(adc_val_3> ref_3+cons){ l_3 =1; output_high(pin_d6); delay_ms(500); } else{ l_3=0; output_low(pin_d6); } } }
int main(void){ // UART_Setup(); DDRB |= (1<<LED); PORTB |= (1<<LED); DDRB |= (1<<MB1) | (1<<MC1); PORTE |= (1<<SW1C); DDRH |= (1<<MA)| (1<<MD); ADC_Setup(); TIM0_Setup(); sei(); while(1){ PORTB ^= (1<<LED); _delay_ms(500); vrailing=read_adc(TRRAILLING); if(vrailing<5){ vrailing=5; } vfiller=read_adc(TRFILLER); if(vfiller<5){ vfiller=5; } } }
void main(void) { usart_init(); adc_init(); DDRB.3 = 1; while (1) { #if !TEST_MODE unsigned int Temperature = read_adc(1); //Read ADC Channel number 1 (PA1) Temperature >>= 1; //Divide the reading by 2 to get the temperature printf("Temperature = "); //Print the temperature value of the hyper terminal printf("%u",Temperature); printf("C \n"); delay_ms(2000); //Wait 2 sec to read the temperature again control_motor(Temperature); //Control the motor using the Temperature value #else unsigned int Reading = read_adc(1); //Read ADC Channel number 1 (PA1) printf("Reading = "); //Print the reading printf("%d",Reading); printf("\n"); control_motor_POT(Reading); //Control the motor using the Reading value delay_ms(100); //Wait 100ms to see the change in motor speed #endif }; }
int8_t getkey(void) { uint8_t adcv; //uint8_t c = 0; for(;;){ //if(c > 10) return DIR_NONE; //c++; adcv = read_adc(); if(adcv < (uint8_t)(256.0*0.5/3.035)) return DIR_UP; //if(read_adc() != adcv) continue; //if(adcv == 0) adcv = 1; //else if(adcv == 255) adcv = 254; _delay_ms(1); uint8_t adcv2; adcv2 = read_adc(); //if(adcv2 >= adcv - 2 || adcv2 <= adcv + 2) break; //if(adcv2 >= adcv - 1 || adcv2 <= adcv + 1) break; if(adcv2 == adcv) break; } //if(adcv < (uint8_t)(256.0*0.6/3.035)) return DIR_UP; if(adcv > (uint8_t)(256.0*(1.518-0.2)/3.035) && adcv < (uint8_t)(256.0*(1.518+0.2)/3.035)) return DIR_LEFT; if(adcv > (uint8_t)(256.0*(0.976-0.2)/3.035) && adcv < (uint8_t)(256.0*(0.976+0.2)/3.035)) return DIR_RIGHT; if(adcv > (uint8_t)(256.0*(2.011-0.2)/3.035) && adcv < (uint8_t)(256.0*(2.011+0.2)/3.035)) return DIR_DOWN; return DIR_NONE; }
int16 read_supply() { int8 i; int16 volts; int32 result,reading; result=0; set_adc_channel(0,VSS); for(i=1;i<51;++i){ reading=read_adc(); delay_ms(5); } for(i=1;i<101;++i){ reading=read_adc(); result=(((result*(i-1))+reading)/i); delay_ms(5); } volts=((result*100)/272)+86; //printf("%Ld,%Ld,%Lu\r\n",reading,result,volts); return(volts); }
void sampling_ADC() { float data[50][6]; for(int i = 0; i <= 49; i++) { for(int j = 0; j <= 8; j++) { if (j==4) //Cambiar a AN8 { j=8; set_adc_channel(j); delay_us(20); data[i][j-3] = read_adc(); printf("%g\t",tconv*data[i][j-3]); delay_us(10); } else { set_adc_channel(j); delay_us(20); data[i][j] = read_adc(); printf("%g\t",tconv*data[i][j]); delay_us(10); } } delay_us(10); printf("\r\n"); } }
int16 temp_probe() { int8 i; int16 temp; int32 result,reading; result=0; set_adc_channel(1,VSS); output_bit(PIN_D0, 1); for(i=1;i<51;++i){ reading=read_adc(); delay_ms(5); } for(i=1;i<101;++i){ reading=read_adc(); result=(((result*(i-1))+reading)/i); delay_ms(5); } output_bit(PIN_D0, 0); temp=((result*4133)/100)-20513; //printf("%Ld,%Ld,%Lu\r\n",reading,result,temp); return(temp); }
void main(void) { init(); while (1) { mode=datarx; if ((mode != 57) & (mode > 48)) { mode1=mode; } if (mode == 57) { mode2=mode; } if (mode1 == 49) { tv(); } if (mode1 == 50) { tr(); } if (mode1 == 51) { datatx((read_adc(1)+1)); } if (mode1 == 52) { datatx(0); tv(); datatx((read_adc(1)+1)); } if ((mode1 == 53) & (mode2 == 57)) { tv(); } if ((mode1 == 54) & (mode2 == 57)) { tr(); } if ((mode1 == 55) & (mode2 == 57)) { datatx((read_adc(1)+1)); } if ((mode1 == 56) & (mode2 == 57)) { datatx(0); tv(); datatx((read_adc(1)+1)); } mode2=0; delay_ms(100); } }
int main(void) { SETUP_ADC(ADC_CLOCK_INTERNAL); SETUP_ADC_PORTS(AN0); set_adc_channel(0); while (TRUE) { if (!input(botStart)) { delay_ms(100); if (!input(botStart) && !ctrl) { ctrl = TRUE; cont = 0; while (cont < bufferSize) { vetor[cont++] = read_adc(); while (!adc_done()) ; } for (cont = 0; cont < bufferSize; ++cont) { printf("%lu\n\r", vetor[cont]); } cont = 0; } else if (ctrl) ctrl = FALSE; } } return 0; }
int16 read_analog(int channel) { int i; channel &= 0x1F; delay_us(2); // 2 usec delay output_high(MPH_CLK); // activates SCLK output_high(MPH_SYNC); // activetes SYNC delay_us(1); // 1 usec delay output_low(MPH_SYNC); // resets SYNC for(i=8; i>0; i--) { if ((channel & (1<<(i-1)))==0) output_low(MPH_DIN); else output_high(MPH_DIN); output_low(MPH_CLK); // resets SCLK output_high(MPH_CLK); // activates SCLK } delay_us(50); // 50 usec delay to stabilize the value. return (read_adc()); // reads the analog channel, waits for end of conversion // and returns the value. }
uint8_t DeviceCommands::next_command(void) { static char c; c = getchr(); if (c>0) put(c); if (parse()) { if (strcmp(command.name, "echo") == 0) echo(command.parameters[0]); else if (strcmp(command.name, "set_pwm") == 0) set_pwm((uint8_t)command.parameters[0], command.parameters[1]); else if (strcmp(command.name, "set_motors") == 0) set_motors(command.parameters[0], command.parameters[1], command.parameters[2], command.parameters[3]); else if (strcmp(command.name, "set_led") == 0) set_led((uint8_t)command.parameters[0], (uint8_t)command.parameters[1]); else if (strcmp(command.name, "read_adc") == 0) read_adc(); else if (strcmp(command.name, "stop") == 0) stop(); // comm << cp.command.name<<"\t"<<cp.command.nparameters<<"\n"; } return 0; }
float Eps::read_bus(){ float bus = read_adc(0x1F); bus = (bus * -5.01841) + 5053.055; return bus; }
void main(void) { unsigned char init_result[7],lcd_result[10]; double bss; ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x86; //initiate AD for 125kHz clock, 8 bit result and AREF pin as voltage reference. lcd_init(16); //lcd connect to port D and 16 line mode. while (1) { bss=read_adc(5); bss=bss*(3.3/256); lcd_clear(); lcd_gotoxy(0,0); lcd_putsf("voltage is:"); ftoa(bss,2,init_result); sprintf(lcd_result,"%4s volt.",init_result); lcd_gotoxy(0,1); lcd_puts(lcd_result); delay_ms(350); } }
int main (void){ uint16_t acc_val = 0; char acc_x_val_str[16]; char acc_y_val_str[16]; char acc_z_val_str[16]; //Initialize the USART USART_init(); USART_clean(); //Init ADC adc_init(); while(1) { acc_val = read_adc(6); //itoa(acc_val, acc_z_val_str, 10); //USART_putstring(acc_z_val_str); if (acc_val < 550 && acc_val > 450) USART_putstring("NORMAAL \r"); else if (acc_val < 550) USART_putstring("VOORWAARTS \r"); else if (acc_val > 450) USART_putstring("ACHTERWAARTS\r"); } }
void readvis(void) { if ((tic & 3) == 0) { PORTD |= (1 << PORTD5); delay_ms(50); ad1 = read_adc(0); } if ((tic & 3) == 2) { PORTD &= ~(1 << PORTD5); delay_ms(50); ad0 = read_adc(0); } tic++; }
void main() { init_pic(); initialDisplay(); t = read_adc(); OUTPUT_HIGH(PIN_C0); OUTPUT_HIGH(PIN_C2); OUTPUT_LOW(PIN_C3); while(1) { // if(number_changed) // { lcd_display(noOfPeople,14,1); // number_changed = 0; // } read_temperature(); if(temp_changed) { lcd_display(temperature,13,2); temp_changed = 0; } // phaseControl(); } }
void scanLine() { unsigned char i = 0; unsigned char adcRead[i]; // Variabel pembacaan nilai ADC // JUmlah warna putih dan hitam yang terdeteksi oleh sensor unsigned char blackCount = 0, whiteCount = 0; sensor = 0x00; // Hapus nilai sensor sebelumnya for (; i<8; i++) { adcRead[i] = read_adc(i); // Baca nilai ADC ada bit ke-i if (adcRead[i] > white) // Jika hasil pembacaan > nilai putih blackCount++; // Increment jumlah blok hitam yang terbaca else whiteCount++; // Increment jumlah blok putih yang terbaca } if (whiteCount > blackCount) { // Banyaknya blok warna putih yang terdeteksi > dari blok warna hitam, maka garis nya adalah hitam for (i=0; i<8; i++) { if (adcRead[i] > white) sensor |= (1<<i); } } else { // Banyaknya blok warna putih yang terdeteksi < dari blok warna hitam, maka garis nya adalah putih for (i=0; i<8; i++) { if (adcRead[i] < black) sensor |= (1<<i); } } }
//--------------------------------- void main() { //#byte TRISB=0b11000000; SET_TRIS_B(0xC0); //configura PORTB entrada / 0=salida / 1=entrada OUTPUT_D(0x00); //inicializando PORTD en 0x00 //#byte PORTB=0x00; setup_adc_ports(AN0); setup_adc(ADC_CLOCK_INTERNAL); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);//|RTCC_8_bit); //51.2 us overflow // setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); //13.1 ms overflow // setup_ccp1(CCP_COMPARE_SET_ON_MATCH); // enable_interrupts(INT_RTCC); enable_interrupts(INT_RTCC); enable_interrupts(GLOBAL); cont=0; comp1=0; while(TRUE) { set_adc_channel(0); delay_ms(1);//tiempo de offset necesario comp1=((read_adc())/1024.0*250.0); delay_ms(1);//tiempo de offset necesario } }
/* Calculate backemf and save in g_pos */ void calculate_backemf (uint8_t motor) { int16_t backemf=0; // Read backemf adc_init(3); backemf = read_adc(0x10); if (backemf & 0x200) { backemf = (~(backemf) & (0x03FF))+1; // backemf -= 512; } if (backemf < MOTOR_OFFSET) backemf = 0; g_pos[motor] += backemf; if (g_mode[motor] != MOTOR_STOP) debug_value(backemf,10); //Compare voltage if (((g_angle[motor] > 1)) && ( g_mode[motor] != MOTOR_STOP)) { if ( g_pos[motor] >= (g_angle[motor])) // Check for position { motor_control(1, MOTOR_BRAKE); delay(3); g_mode[motor] = MOTOR_STOP; uart_puts ("Voltage SUM"); debug_value(g_pos[motor],10); g_pos[motor] =0; } } }
void main(void){ int8_t data=0; setup_adc(ADC_CLOCK_DIV_8); //ADCのサンプリング設定 setup_wdt(WDT_1152MS); while(1){ restart_wdt();/* output_high(PIN_A0); output_low(PIN_A1); output_high(PIN_A2); //以上アドレスピン設定 output_low(PIN_A3); output_low(PIN_A4); output_high(PIN_A5); //以上エネーブルピン設定*/ output_a(0b00001111); setup_adc_ports(ALL_ANALOG); set_adc_channel(6); /* ここでADCを適用できるピンと実際に適用するピンを決めます。 ちなみに仕様として各ピンの役割を個別に設定できないようです。 ですがすべてアナログに設定しても問題なくエネーブルピンやアドレスピンは動いてくれているので見なかったことにしましょう。 */ delay_us(20); restart_wdt(); data=read_adc(); setup_adc_ports(NO_ANALOGS);//一応戻しています。 putc((char)data); sleep(); } }
float Eps::read_battery(){ float battery = read_adc(0x20); battery = (-5.10791 * battery) + 5273.393; return battery; }
void main() { int16 valor; float tensao; lcd_init(); setup_adc_ports(AN0|VSS_VDD); setup_adc(ADC_CLOCK_DIV_16); setup_psp(PSP_DISABLED); setup_spi(SPI_SS_DISABLED); setup_wdt(WDT_OFF); setup_timer_0(RTCC_INTERNAL); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); setup_vref(FALSE); set_adc_channel(0); //Setup_Oscillator parameter not selected from Intr Oscillator Config tab // TODO: USER CODE!! while(1) { valor = read_adc(); tensao = valor*5.0/1024.0; printf("ADC = %04ld",valor); printf(" Tensao = %.3fV\r", tensao); printf(lcd_putc,"\fADC = %ld",valor); printf(lcd_putc,"\nTensao = %.3fV",tensao); delay_ms(250); } }
int main() { unsigned int adc; float vol; int n; copy_vec(); irq_init(); button_init(); uart0_init(); timer_init(); // timer4_init(); lcd_init(); lcd_clean(0xffff); adc_ts_init(); ts_init(); while(1); while(1) { adc = read_adc(0); vol = adc * 3.3 / 0x3ff; n = (vol - (int)vol) * 1000; printf("AIN0: adc = %u voltage:%d.%03d\r\n", adc, (int)vol, n); delayms(1000); } return 0; }
int ReadBatteryVoltage( void ) { AdcTempVal = read_adc( 0 ); ftmp = AdcTempVal; vtmp = ftmp / __ADC0_DIV_FACTOR; return( (int)vtmp ); }
void hexbright::read_thermal_sensor() { // do not call this directly. Call get_temperature() // read temperature setting // device data sheet: http://ww1.microchip.com/downloads/en/devicedoc/21942a.pdf thermal_sensor_value = read_adc(APIN_TEMP); }
void main() { BYTE i, j, address, value; int16 q,q1; float p; q1=0; setup_adc_ports(AN0|VSS_VDD); setup_adc(ADC_CLOCK_INTERNAL); set_adc_channel(0); usb_cdc_init(); usb_init(); // while(!usb_cdc_connected()) {} do { usb_task(); if (usb_enumerated()) { delay_ms(500); q = read_adc(); if (q!=q1){ p = 5.0 * q / 1024.0; printf(usb_cdc_putc,"\r Voltage=%01.2fV", p); } q1=q; } } while (TRUE); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(temperature_thread(struct pt *pt)) { PT_BEGIN(pt); init_adc(5); disable_digital_buffer(5); while(1) { cli(); temperature_counter = 0; sei(); PT_WAIT_UNTIL(pt,temperature_counter > 750); temp_result = 0; temp_sampl_cnt = 0; for(temp_sampl_cnt = 0; temp_sampl_cnt < 64; temp_sampl_cnt++) { start_conversion(); PT_WAIT_UNTIL(pt,!is_adc_busy()); temp_result += read_adc(); } dbg(PSTR("> ADC result: %u\r\n"),temp_result >> 6); } PT_END(pt); }
/************************************************************************** * Function name : rd_adc * Returns : Результати оцифровки * Parameters : Канал АЦП * Purpose : Оцифровка по указаному каналу і розрахунок середнього арифметичного з 4х вимірів по указаному каналу АЦП ****************************************************************************/ unsigned int get_average_adc(unsigned char adc_input) { //--------------------------------------------------------------------------------------- unsigned int temp_adc = 0; //_delay_us(500); temp_adc=read_adc(adc_input); _delay_us(10);//_delay_us(50); temp_adc=temp_adc + read_adc(adc_input); _delay_us(10);//_delay_us(50); temp_adc=temp_adc + read_adc(adc_input); _delay_us(10);//_delay_us(50); temp_adc=temp_adc + read_adc(adc_input); temp_adc=temp_adc>>2; return temp_adc; //--------------------------------------------------------------------------------------- };