void start_task(void) { uint16_t dtycyl, adc_val; init_display(); while(1) { adc_val = adc_read(0); dtycyl = duty_cycle(adc_val); if (red != dtycyl) { red = dtycyl; VT100_cur_pos(ROW_A, COL_A + 4); printf("%04d", adc_val); Timer1_set_dtcyl(red); } adc_val = adc_read(1); dtycyl = duty_cycle(adc_val); if (green != dtycyl) { green = dtycyl; VT100_cur_pos(ROW_B, COL_B + 4); printf("%04d", adc_val); Timer2_set_dtcyl(green); } adc_val = adc_read(2); dtycyl = duty_cycle(adc_val); if (blue != dtycyl) { blue = dtycyl; VT100_cur_pos(ROW_C, COL_C + 4); printf("%04d", adc_val); Timer0_set_dtcyl(blue); } } }
bool dbg_ports(void) { int line; lcd_setfont(FONT_SYSFIXED); while(1) { lcd_clear_display(); line = 0; _DEBUG_PRINTF("GPIO_PADR: %02x",(unsigned char)GPIO_PADR); _DEBUG_PRINTF("GPIO_PACON: %02x",(unsigned char)GPIO_PACON); _DEBUG_PRINTF("GPIO_PBDR: %02x",(unsigned char)GPIO_PBDR); _DEBUG_PRINTF("GPIO_PBCON: %02x",(unsigned char)GPIO_PBCON); _DEBUG_PRINTF("GPIO_PCDR: %02x",(unsigned char)GPIO_PCDR); _DEBUG_PRINTF("GPIO_PCCON: %02x",(unsigned char)GPIO_PCCON); _DEBUG_PRINTF("GPIO_PDDR: %02x",(unsigned char)GPIO_PDDR); _DEBUG_PRINTF("GPIO_PDCON: %02x",(unsigned char)GPIO_PDCON); _DEBUG_PRINTF("ADC0: %d", adc_read(0)); _DEBUG_PRINTF("ADC1: %d", adc_read(1)); _DEBUG_PRINTF("ADC2: %d", adc_read(2)); _DEBUG_PRINTF("ADC3: %d", adc_read(3)); lcd_update(); if (button_get_w_tmo(HZ/10) == (DEBUG_CANCEL|BUTTON_REL)) break; } lcd_setfont(FONT_UI); return false; }
void main() { int x,y; trisb=0; trisd=0; portd=0; pie1=0b00000001; intcon=0b11000000; t1con=0b00000101; vdelay_init(); tmr1l=0xDF; tmr1h=0xB1; while(1) { x=adc_read(0); y=adc_read(1); if(x<100) portd=0b00001001; else if(x>800) portd=0b00000110; if(y<100) portd=0b00000101; else if(y>800) portd=0b00001010; else portd=0; time=adc_read(2); time1=adc_read(3); } }
void cmd_readadc(char *line) { adc_init(PIN_MAP[12].adc_device); // all on ADC1 adc_set_extsel(PIN_MAP[12].adc_device, ADC_SWSTART); adc_set_exttrig(PIN_MAP[12].adc_device, true); adc_enable(PIN_MAP[12].adc_device); adc_calibrate(PIN_MAP[12].adc_device); adc_set_sample_rate(PIN_MAP[12].adc_device, ADC_SMPR_55_5); gpio_set_mode (PIN_MAP[12].gpio_device,PIN_MAP[12].gpio_bit, GPIO_INPUT_ANALOG); gpio_set_mode (PIN_MAP[19].gpio_device,PIN_MAP[19].gpio_bit, GPIO_INPUT_ANALOG); gpio_set_mode (PIN_MAP[20].gpio_device,PIN_MAP[20].gpio_bit, GPIO_INPUT_ANALOG); uint16 value1 = adc_read(PIN_MAP[12].adc_device,PIN_MAP[12].adc_channel); uint16 value2 = adc_read(PIN_MAP[19].adc_device,PIN_MAP[19].adc_channel); uint16 value3 = adc_read(PIN_MAP[20].adc_device,PIN_MAP[20].adc_channel); char values[50]; sprintf(values,"PA6 ADC Read: %u\r\n",value1); serial_write_string(values); sprintf(values,"PC4 ADC Read: %u\r\n",value2); serial_write_string(values); sprintf(values,"PC5 ADC Read: %u\r\n",value3); serial_write_string(values); }
extern struct pwm_command read_combo_command(uint8_t channel) { struct pwm_command command; uint8_t index_a = channel * 2; uint8_t index_b = channel * 2 + 1; command.command_a = adc_to_combo_pwm(accelerate(index_a, adc_read(index_a))); command.command_b = adc_to_combo_pwm(accelerate(index_b, adc_read(index_b))); if (!(PINC & (1 << channel))) { // break switch pressed command.command_a = 0b1000; is_break[channel] = true; } command.is_neutral_position = (command.command_b == 0 && command.command_a == 0); if (command.command_a == 0 && is_break[channel]) { // release break if joystick is in neutral halt position is_break[channel] = false; } else if (is_break[channel]) { // only release break after reaching neutral position of joystick, // override command. command.command_a = 0b1000; } command.has_command_changed = (command.command_a != last_command[index_a] || command.command_b != last_command[index_b]); last_command[index_a] = command.command_a; last_command[index_b] = command.command_b; return command; }
// Private helper functions int batteryUnsafe() { // Returns 1 if the battery is unsafe, 0 if safe. // Read input from pin. Use Vcc as analog reference // Pins PB1,2,3 correspond to IO1,2,3 uint8_t ref = 0x0F; // uint8_t muxPB1 = (ref & A_IN1); // PB1 uint8_t muxPB2 = (ref & A_IN2); // PB2 uint8_t muxPB3 = (ref & A_IN3); // PB3 // Return value ranges from 0x000 to 0x3FF; // Division converts everything to same scaling // If voltage = V, input I = (V*1024)/(V_ref*12) ~= 17*V int IO1 = adc_read(muxPB1) / 6; int IO2 = adc_read(muxPB2) / 3; int IO3 = adc_read(muxPB3) / 2; // Check if any cell voltages are above threshold = 3.5V int threshold = 60; // 17*3.5 ~= 60 if ((IO1 < threshold) | (IO2 - IO1 < threshold) | (IO3 - IO2 < threshold)) { return 1; } return 0; }
/** \brief This is the task function for monitoring the button and joysticks. * \details This function monitors the "Target Set" button, and when it is pressed, * updates shared variables "motor1_power_SHARED", and "motor2_power_SHARED" with adc_channel * readings. If the button is NOT pressed, the motor power shareds are set to the effective * "zero power" value of 512.(1024 adc values corresponding to full forward(1023), and * full reverse(0); */ void task_sensors(void* pvParameters){ uint8_t default_sensor_prio = uxTaskPriorityGet(NULL); portTickType xLastWakeTime; xLastWakeTime = xTaskGetTickCount(); adc_init(); uint16_t joystick_y; uint16_t joystick_x; button_init(); while(1) { if(button_pressed()){ joystick_y = adc_read(ADC_JOYSTICK_Y); joystick_x = adc_read(ADC_JOYSTICK_X); vTaskPrioritySet(NULL, configMAX_PRIORITIES - 1); motor1_power_SHARED = joystick_y; motor2_power_SHARED = joystick_x; vTaskPrioritySet(NULL, default_sensor_prio); } else{ vTaskPrioritySet(NULL, configMAX_PRIORITIES - 1); motor1_power_SHARED = 512; motor2_power_SHARED = 512; vTaskPrioritySet(NULL, default_sensor_prio); } vTaskDelayUntil(&xLastWakeTime, 100/portTICK_RATE_MS); } }
int main(){ uint16_t k, min, max, krok, poziom1, poziom2; DDRB= LED3|LED4; //odpowiednie piny portu B jako wyjœcia DDRD= LED1|LED2; //odpowiednie piny portu D jako wyjœcia DDRE= ~SWITCH; //odpowiedni pin portu E jako wejœcie PORTB = 0x00; //zerowanie wartoœci wyjœæ PORTD = 0x00; PORTE |= SWITCH; //podci¹gniêcie pinu ze switchem do Vcc przez wewnêtrzny rezystor adc_init(); //inicjalizacja ADC while(1){ PORTD=LED1|LED2; PORTB=LED3|LED4; if (!(PINE&SWITCH)){ //oczekiwanie na naciœniêcie switcha PORTB=0x00; PORTD=0x00; min=adc_read(3); //w przypadku naciœniêcia odczyt wartoœci ADC do zmiennej min _delay_ms(1000); while(1){ if (!(PINE&SWITCH)){ //oczekiwanie na naciœniêcie switcha max=adc_read(3); //w przypadku naciœniêcia odczyt wartoœci ADC do zmiennej max krok=(max-min)/3; //obliczenia granic zapalania siê kolejnych diód poziom1=min+krok; poziom2=min+2*krok; while(1){ //dzia³anie programu - odczyt napiêcia z ADC PORTB=0x00; PORTD=0x00; // i zapalanie odpowiedniej iloœci diód k=adc_read(3); if(k<min){ PORTB=0x00; PORTD=0x00; } else if(k>=min&&k<poziom1){ PORTB=0x00; PORTD=LED1; } else if(k>=poziom1&&k<poziom2){ PORTB=0x00; PORTD=LED1|LED2; } else if(k>=poziom2&&k<max){ PORTB=LED3; PORTD=LED1|LED2;; } else{ PORTD=LED1|LED2; PORTB=LED3|LED4; } _delay_ms(200); } } } } } }
void main() { int v1,v2,v3,x1,x2,i; char txt[7]; lcd_init(); lcd_cmd(_lcd_clear); // clear lcd lcd_cmd(_lcd_cursor_off ); lcd_out(1,1,"Sun Tracker"); trisc=0b00000000; trisd=0b00000011; while(1) { delay_ms(200); v1=adc_read(1); v2=adc_read(2); v3=adc_read(3); i=adc_read(4)*150; x1=v2-v1; x2=v2-v3; inttostr(i,txt); lcd_out(2,1,"I= "); lcd_out(2,4,txt); lcd_out_cp(" uA"); while(i>=1500) { en=0; lcd_out(2,1,"Motor Off "); delay_ms(2000); i=adc_read(4)*150; } if(rd0_bit==0) { en=1; in1=1; in2=0; while(rd1_bit==1); en=0; in1=0; in2=0; } if(x1<-20) { en=1; in1=1; in2=0; } else if(x2<-20) { in1=0; in2=1; en=1; } else { en=0; } } }
void wind_data_read(double *speed, double *dir) { double voltage; adc_read(&voltage, WSPEED); //Leser spenning. *speed = (voltage * 50) / 5; //Konverterer til hastighet mellom 0-50 m/s adc_read(&voltage, WDIR); //Leser spenning. *dir = (voltage * 360) / 5; //Konverterer til grader mellom 0-360. }
static void send_packet(void *ptr) { static int seq_id; char buf[MAX_PAYLOAD_LEN]; seq_id++; PRINTF("DATA send to %d 'Hello %d'\n", server_ipaddr.u8[sizeof(server_ipaddr.u8) - 1], seq_id); sprintf(buf, "ACLL X: %d, Y:%d", adc_read(1), adc_read(2)); uip_udp_packet_sendto(client_conn, buf, strlen(buf), &server_ipaddr, UIP_HTONS(UDP_SERVER_PORT)); }
int button_read_device(void) { int btn = BUTTON_NONE; int data; /* check F1..F3 and UP */ data = adc_read(ADC_BUTTON_ROW1); if (data >= 150) { if (data >= 545) if (data >= 700) btn = BUTTON_F3; else btn = BUTTON_UP; else if (data >= 385) btn = BUTTON_F2; else btn = BUTTON_F1; } /* Some units have mushy keypads, so pressing UP also activates the Left/Right buttons. Let's combat that by skipping the AN5 checks when UP is pressed. */ if(!(btn & BUTTON_UP)) { /* check DOWN, PLAY, LEFT, RIGHT */ data = adc_read(ADC_BUTTON_ROW2); if (data >= 150) { if (data >= 545) if (data >= 700) btn |= BUTTON_DOWN; else btn |= BUTTON_RIGHT; else if (data >= 385) btn |= BUTTON_LEFT; else btn |= BUTTON_PLAY; } } if ( adc_read(ADC_BUTTON_ON) < 512 ) btn |= BUTTON_ON; if ( adc_read(ADC_BUTTON_OFF) > 512 ) btn |= BUTTON_OFF; return btn; }
// Arduino compatible pin input int16_t analogRead(uint8_t pin) { #if defined(__AVR_ATmega32U4__) static const uint8_t PROGMEM pin_to_mux[] = { 0x00, 0x01, 0x04, 0x05, 0x06, 0x07, 0x25, 0x24, 0x23, 0x22, 0x21, 0x20}; if (pin >= 12) return 0; return adc_read(pgm_read_byte(pin_to_mux + pin)); #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) if (pin >= 8) return 0; return adc_read(pin); #else return 0; #endif }
unsigned int measure_voltage(void) { int i =0; unsigned int voltage = 0; //Atsakymas isduodamas milivoltais// //TODO: isduoda butent tiek voltu kiek mato, perdaryti, kad paverstu i itampa gaunama is jegaines for (i = 0; i < 10; i++) { voltage += (adc_read(VOLTAGE_MEASUREMENT_DIFF1_ADC_PIN) - adc_read(VOLTAGE_MEASUREMENT_DIFF2_ADC_PIN))*5; } voltage /= 10; return voltage; }
int main(void) { adc_init();//initialize the LDR input DDRB = 0x00; DDRB |= (1<<PB4);//set led OUTPUT //PB2 input LDR ledBlink(5); // Retrieve, increment and store a randseed to eeprom - so the number sequence isn't the same each time! uint8_t randSeed = eeprom_read_byte ((uint8_t*)1); ++randSeed; eeprom_write_byte ((uint8_t*)1, randSeed); srand (randSeed); while (1 == 1) { raw = adc_read(1);//reading change in light value delay_ms(10); if ( raw < 100){ ledON( rng(2,3) , 100); pauseOFF(rng(3,10), 10); } } return 1; }
void adc_test(uint8_t channel) { // disable COMPA isr - prevents flickering TIMSK1 &= ~_BV(OCIE1A); TOGGLE_LED; delay(50); TOGGLE_LED; delay(50); TOGGLE_LED; delay(50); TOGGLE_LED; DISPLAY_OFF; spi_transfer(0x01); // show LSB LATCH; DISPLAY_ON; delay(200); while(1) { uint8_t tmp = adc_read(channel); DISPLAY_OFF; spi_transfer(tmp); // show the data LATCH; DISPLAY_ON; DISPLAY_OFF; spi_transfer(0x00); // turn it off to make it less blindingly bright LATCH; DISPLAY_ON; } }
unsigned char read_current(unsigned char channel) { float xdata current; /* read current channel */ if (!adc_read(channel*2+1, ¤t)) return 0; /* correct opamp gain, divider & curr. resist, microamp */ current = current / CUR_MULT * DIVIDER / RCURR * 1E6; /* correct for unbalanced voltage dividers */ current -= user_data[channel].cur_vgain * user_data[channel].u_meas; /* correct for offset */ current -= user_data[channel].cur_offset; /* calibrate gain */ current = current * user_data[channel].cur_gain; /* 0.1 uA resolution */ current = floor(current*10 + 0.5)/10; DISABLE_INTERRUPTS; user_data[channel].i_meas = current; ENABLE_INTERRUPTS; return 1; }
unsigned char read_hv(unsigned char channel) { float xdata hv; /* read voltage channel */ if (!adc_read(channel*2, &hv)) return 0; /* convert to HV */ hv *= DIVIDER; /* apply calibration */ hv = hv * user_data[channel].adc_gain + user_data[channel].adc_offset; /* 0.01 resolution */ hv = floor(hv * 100) / 100.0; led_mode(channel, !(hv > 40)); DISABLE_INTERRUPTS; user_data[channel].u_meas = hv; ENABLE_INTERRUPTS; return 1; }
void init(){ adc_init();//initialize the LDR input DDRB = 0x00; DDRB |= (1<<PB3);//set led OUTPUT DDRB |= (1<<PB2);//set led OUTPUT DDRB |= (1<<PB1);//set led OUTPUT DDRB |= (1<<PB0);//set led OUTPUT step_delay = 60L * 1000L / 100 / 25;//max 75 cli(); /* interrup setup */ // prescale timer0 to 1/8th the clock rate // overflow timer0 every 0.256 ms TCCR0B |= (1<<CS01); // enable timer overflow interrupt TIMSK |= 1<<TOIE0; // Enable global interrupts sei(); timer = millis() + 3000; timer2 = millis() + 5000; readLDR = adc_read(2) ; oldLDRVal = readLDR; }
bool dbg_hw_info_adc(void) { lcd_setfont(FONT_SYSFIXED); while(1) { int button = get_action(CONTEXT_STD, HZ / 25); switch(button) { case ACTION_STD_NEXT: case ACTION_STD_PREV: case ACTION_STD_OK: case ACTION_STD_MENU: lcd_setfont(FONT_UI); return true; case ACTION_STD_CANCEL: lcd_setfont(FONT_UI); return false; } lcd_clear_display(); /* add battery readout in mV, this it is not the direct output of a channel */ lcd_putsf(0, 0, "Battery(mV) %d", _battery_voltage()); for(unsigned i = 0; i < NUM_ADC_CHANNELS; i++) { lcd_putsf(0, i + 1, "%s %d", imx233_adc_channel_name[i], adc_read(i)); } lcd_update(); yield(); } }
/* This will read the Temperature and return something useful. Polling is being used. */ float STM32ADC::readTemp(){ unsigned int result = 0; float temperature = 0.0; result = adc_read(_dev, 16); temperature = (float)((_V25-result)/_AverageSlope)+ 25.0; return temperature; }
int main(void) { adc_init(); /* Setup for our PWM output */ /* Output enable PD5 */ DDRD |= (1 << DDD5); /* PWM SETUP */ TCCR0A |= (1<<COM0B1) | (1<<WGM00) | (1<<WGM01); TCCR0B |= (1<<CS00); /* Variables used to write to PWM pin */ uint8_t pwm = 0x00; OCR0B = 0; while(1) { /* ~~~~~ Original code ~~~~~ */ /* Read from the sensor */ pwm = adc_read(0) & 0xFF; /* Control the actuator by writing to PWM */ OCR0B = pwm; _delay_ms(10); } }
/***************************************************************************//** * @brief adc_pn_mon *******************************************************************************/ int32_t adc_pn_mon(adc_core core, enum adc_pn_sel sel) { uint8_t index; uint32_t reg_data; int32_t pn_errors = 0; for (index = 0; index < core.no_of_channels; index++) { adc_write(core, ADC_REG_CHAN_CNTRL(index), ADC_ENABLE); adc_set_pnsel(core, index, sel); } mdelay(1); for (index = 0; index < core.no_of_channels; index++) { adc_write(core, ADC_REG_CHAN_STATUS(index), 0xff); } mdelay(100); for (index = 0; index < core.no_of_channels; index++) { adc_read(core, ADC_REG_CHAN_STATUS(index), ®_data); if (reg_data != 0) { pn_errors = -1; xil_printf("ADC PN Status: %d, %d, 0x%02x!\n", index, sel, reg_data); } } return pn_errors; }
int main(void) { volatile SAMPLE values[NR_CHANNELS]; volatile uint16_t seqno = 0; volatile uint16_t count = 0; int idle, channel; ocp_init(); shm_init(); WR_MAGIC( 0x0ADCDA8A ); WR_SEQNO( seqno ); while (1) { // Read ADC data into values values[0] = adc_read( 0, 0, 0 ); values[1] = adc_read1( 0, 0, 0 ); values[2] = adc_read2( 0, 0, 0 ); values[3] = adc_read3( 0, 0, 0 ); // Write into shared memory for ( channel = 0; channel < NR_CHANNELS; ++channel ) { WR_DATA( seqno, channel, count, values[channel] ); } // Increment and update sample counts count++; WR_COUNT( count ); if ( count >= SAMPLES_PER_MSG ) { count = 0; WR_COUNT( count ); seqno++; WR_SEQNO( seqno ); // Signal to host that data is ready for reading for ( idle=0; idle < 2; ++idle ) { seqno = seqno; } generate_host_interrupt( PRU0_ARM_DONE_INTERRUPT + 16 ); } // Idle some until time for the next sample // // We want a delay of abuot 100us (10kHz) between samples. Since // the PRU runs at 200MHz (5ns per op), we want 20000 ops for // an approximate delay of 100us. Since one iteration is // approximately 10 assembly instructions, counting up to 2000 // for now. for ( idle=0; idle < 2000; ++idle ) { seqno = seqno; } } __halt(); return 0; }
float adc_check(void) { uint16_t adc_buf[ADC_CHECK_COUNT]; uint32_t adc_sum = 0; uint16_t adc_avg = 0; uint16_t i = 0; for (i = 0; i < ADC_CHECK_COUNT; i++) { delay(10); adc_read(ADC_CHANNEL_VBASE, &adc_buf[i]); adc_sum += adc_buf[i]; } adc_avg = adc_sum / ADC_CHECK_COUNT; if (adc_avg > ADC_VBASE_STD_VALUE) { if ((adc_avg - ADC_VBASE_STD_VALUE) > ADC_WAVE_VALUE) adc_avg = ADC_VBASE_STD_VALUE; } else { if ((ADC_VBASE_STD_VALUE - adc_avg) > ADC_WAVE_VALUE) adc_avg = ADC_VBASE_STD_VALUE; } return (ADC_VBASE_STD_VALUE / (float)adc_avg); }
int main(int argc, char * argv[]) { long kickthreshold = 0xFFFFFE4A; while (1) { adc_read(0); if ( (adc_sum < kickthreshold) && (kick_down == 0) ) { duart_a_write(0x99); duart_a_write(0x24); // note 36 duart_a_write(0x45); kick_down = 1; } if (adc_sum > kickthreshold) { kick_down = 0; } } return (0); }
/***************************************************************************//** * @brief adc_setup *******************************************************************************/ int32_t adc_setup(uint32_t adc_addr, uint32_t dma_addr, uint8_t ch_no) { uint8_t index; uint32_t status; adc_baseaddr = adc_addr; adc_dmac_baseaddr = dma_addr; adc_write(ADC_REG_RSTN, 0x00); adc_write(ADC_REG_RSTN, 0x03); mdelay(100); for(index = 0; index < ch_no; index++) { adc_write(ADC_REG_CHAN_CNTRL(index), 0x51); } adc_read(ADC_REG_STATUS, &status); if(status == 0x0) { xil_printf("ADC Core Status errors.\n\r"); return -1; } else { xil_printf("ADC Core successfully initialized.\n"); return 0; } }
int main(void) { cli(); TCCR1A = 0x00; TCNT1=0x0000; TCCR1B = 0x04; TIMSK1=0x01; sei(); int c=0; //used to toggle the channel DDRD = (1<<DDD6)|(1<<DDD5); adc_init(); while(1) { if(c==0){ //it works for channel 0 adc_read(); a&=0b00000000; c=1; } else{ //it works for channel 1 adc_read1(); a&=0b00000001; c=0; } _delay_ms(500); //enterSleep(); } }
int main(void) { bano_info_t info; uint8_t i; uint8_t j; uint16_t x; info = bano_info_default; info.disable_mask = BANO_DISABLE_ALL; info.disable_mask &= ~BANO_DISABLE_ADC; bano_init(&info); nrf905_set_pa_pwr(3); nrf905_cmd_wc(); adc_setup(); adc_start_free_running(); for (j = 0; j != 4; ++j) { x = 0; for (i = 0; i != 8; ++i) x += adc_read(); x /= i; bano_send_set(0x2a2b, x); bano_send_set(0x2a2b, x); } bano_loop(); bano_fini(); return 0; }
void read_current(unsigned char channel) { float xdata current; /* read current channel */ if (!adc_read(channel*2+1, ¤t)) return; /* correct opamp gain, divider & curr. resist, microamp */ if (user_data[0].status & STATUS_LOWCUR) current = current / CUR_MULT_LC * DIVIDER / RCURR_LC * 1E6; else current = current / CUR_MULT_HC * DIVIDER / RCURR_HC * 1E6; /* correct for unbalanced voltage dividers */ current -= user_data[channel].cur_vgain * user_data[channel].u_meas; /* correct for offset */ current -= user_data[channel].cur_offset; /* calibrate gain */ current = current * user_data[channel].cur_gain; /* 1 uA resolution */ current = floor(current + 0.5); DISABLE_INTERRUPTS; user_data[channel].i_meas = current; ENABLE_INTERRUPTS; }