//***************************************************************************** // // File Name : 'a2d.c' // Title : Analog-to-digital converter functions // Author : Pascal Stang - Copyright (C) 2002 // Created : 2002-04-08 // Revised : 2002-09-30 // Version : 1.1 // Target MCU : Atmel AVR series // Editor Tabs : 4 // // Modified by : Markus Joos ([email protected]) // // This code is distributed under the GNU Public License // which can be found at http://www.gnu.org/licenses/gpl.txt //***************************************************************************** #include <avr/io.h> #include <avr/interrupt.h> #include "global.h" #include "a2d.h" // initialize a2d converter //***************/ void a2dInit(void) //Called from sdr.c //***************/ { sbi(ADCSR, ADEN); // enable ADC (turn on ADC power) sbi(ADCSR, ADFR); // free running mode a2dSetPrescaler(ADC_PRESCALE); // set default prescaler a2dSetReference(ADC_REFERENCE_AREF); // set default reference sbi(ADMUX, ADLAR); // set to left-adjusted result }
//----- Begin Code ------------------------------------------------------------ int main(void) { u16 a=0; u08 i=0; // initialize our libraries // initialize the UART (serial port) uartInit(); // make all rprintf statements use uart for output rprintfInit(uartSendByte); // initialize the timer system timerInit(); // turn on and initialize A/D converter a2dInit(); // print a little intro message so we know things are working vt100ClearScreen(); vt100SetCursorPos(1,1); rprintf("Welcome to the a2d test!\r\n"); // configure a2d port (PORTA) as input // so we can receive analog signals DDRA = 0x00; // make sure pull-up resistors are turned off PORTA = 0x00; // set the a2d prescaler (clock division ratio) // - a lower prescale setting will make the a2d converter go faster // - a higher setting will make it go slower but the measurements // will be more accurate // - other allowed prescale values can be found in a2d.h a2dSetPrescaler(ADC_PRESCALE_DIV32); // set the a2d reference // - the reference is the voltage against which a2d measurements are made // - other allowed reference values can be found in a2d.h a2dSetReference(ADC_REFERENCE_AVCC); // use a2dConvert8bit(channel#) to get an 8bit a2d reading // use a2dConvert10bit(channel#) to get a 10bit a2d reading while(1) { // sample all a2d channels and print them to the terminal vt100SetCursorPos(2,1); for(i=0; i<8; i++) { rprintf("Channel %d: %d \r\n", i, a2dConvert8bit(i)); } // print the sample number so far rprintf("Sample # : %d \r\n", a++); } return 0; }
// initialize a2d converter void a2dInit(void) { sbi(ADCSR, ADEN); // enable ADC (turn on ADC power) cbi(ADCSR, ADFR); // default to single sample convert mode a2dSetPrescaler(ADC_PRESCALE); // set default prescaler a2dSetReference(ADC_REFERENCE); // set default reference cbi(ADMUX, ADLAR); // set to right-adjusted result sbi(ADCSR, ADIE); // enable ADC interrupts a2dCompleteFlag = FALSE; // clear conversion complete flag }
void init_peripherals() { // LED DDRC |= (1<<2); // accel pins lis3l_init(); // UART uartInit(); uartSetBaudRate(19200); uartSetFrameFormat(8, 0, 1); rprintfInit(uartAddToTxBuffer); cbi(DDRB, 1); // XBee CTS on PB1 // I2C itg3200_i2cInit(200); sbi(PORTC, 0); // i2c SCL on ATmega163,323,16,32,etc sbi(PORTC, 1); // i2c SDA on ATmega163,323,16,32,etc cbi(TWCR, TWIE); // disable interrupt // itg3200_i2cSetBitrate(200); // todo, check if if w ecan do 200 // SPI mySpiInit(); // a2d a2dInit(); a2dSetReference(ADC_REFERENCE_256V); a2dSetChannel(7); cbi(PORTA, 7); cbi(DDRA, 7); a2dStartConvert(); _delay_ms(50); // accel BOOL accelOkay = lis3l_Reset(); itg3200_init(); // switch xbee to higher baudrate // rprintfStr("+++"); // _delay_ms(55); // rprintfStr("ATBD6,CN\r"); }
// initialize a2d converter void a2dInit(void) { sbi(ADCSR, ADEN); // enable ADC (turn on ADC power) #ifndef atmega328P //cbi(PRR,PRADC); #endif cbi(ADCSR, ADFR); // default to single sample convert mode a2dSetPrescaler(ADC_PRESCALE); // set default prescaler a2dSetReference(ADC_REFERENCE); // set default reference cbi(ADMUX, ADLAR); // set to right-adjusted result sbi(ADCSR, ADIE); // enable ADC interrupts a2dCompleteFlag = FALSE; // clear conversion complete flag sei(); // turn on interrupts (if not already on) }
int main(void) { configure_ports(); a2dInit(); a2dSetPrescaler(ADC_PRESCALE_DIV32); a2dSetReference(ADC_REFERENCE_AVCC); init_servos(); LED_on(); neutral(); hold_pos(); while (1) { move_forward(); // sustain_pos(); } return 0; }
void setup() { uart0Init(); uartSetBaudRate(0, 9600); rprintfInit(uart0SendByte); rprintf("$Id$"); // this goes to the switch common sbi(DDRA, DDA0); // output on PA0 sbi(PORTA, PA0); // take it high cbi(DDRA, DDA1); cbi(DDRA, DDA2); cbi(DDRA, DDA3); cbi(DDRA, DDA4); cbi(DDRA, DDA5); a2dInit(); a2dSetReference(ADC_REFERENCE_AVCC); a2dSetPrescaler(ADC_PRESCALE_DIV128); sbi(DDRB, PB3); // output for the LED sbi(PORTB, PB3); }
void control(void) { reset_timer5(); uartFlushReceiveBuffer(1); a2dSetReference(ADC_REFERENCE_256V); delay_us(100); PORT_OFF(PORTB,7); while(1) { char read = uart1GetByte(); if(read == 'U') { //Rangefinder only for the Hand! We only have 3 uart ports! uart0SendByte(0x22); //sends 4 bytes with the command to read distance (0x22) uart0SendByte(0x00); uart0SendByte(0x00);uart0SendByte(0x22);delay_us(50); uart0GetByte(); //we don't care about this byte rprintf("%d\r\n", 255*uart0GetByte() + uart0GetByte()); uart0GetByte(); //we don't care about this byte } else if(read == 'L') { rprintf("%d\r\n", 255 - a2dConvert8bit(0)); } else if(read == 'R') { rprintf("%d\r\n", 255 - a2dConvert8bit(1)); } else if(read == 'D') { int buff='#'; uart2SendByte('#'); uart2SendByte('o'); uart2SendByte('s'); uart2SendByte('#'); uart2SendByte('f'); delay_ms(60); // Do not change this delay or you'll get in trouble! while(1) { buff = uart2GetByte(); if(buff==10 || buff==13 || buff==-1) { uart1SendByte('\r'); uart1SendByte('\n'); break; } uart1SendByte(buff); } while((buff=uart2GetByte()) != -1); } else if(read == 'E') { int buff='#'; uart3SendByte('#'); uart3SendByte('o'); uart3SendByte('s'); uart3SendByte('#'); uart3SendByte('f'); delay_ms(60); // Do not change this delay or you'll get in trouble! while(1) { buff = uart3GetByte(); if(buff==10 || buff==13 || buff==-1) { uart1SendByte('\r'); uart1SendByte('\n'); break; } uart1SendByte(buff); } while((buff=uart3GetByte()) != -1); } else if(read == 'M') { int buff='#'; uart2SendByte('#'); uart2SendByte('o'); uart2SendByte('t'); uart2SendByte('#'); uart2SendByte('f'); delay_ms(60); // Do not change this delay or you'll get in trouble! while(1) { buff = uart2GetByte(); if(buff==10 || buff==13 || buff==-1) { uart1SendByte('\r'); uart1SendByte('\n'); break; } uart1SendByte(buff); } while((buff=uart2GetByte()) != -1); } else if(read == 'N') { int buff='#'; uart3SendByte('#'); uart3SendByte('o'); uart3SendByte('t'); uart3SendByte('#'); uart3SendByte('f'); delay_ms(60); // Do not change this delay or you'll get in trouble! while(1) { buff = uart3GetByte(); if(buff==10 || buff==13 || buff==-1) { uart1SendByte('\r'); uart1SendByte('\n'); break; } uart1SendByte(buff); } while((buff=uart3GetByte()) != -1); } else if(read == 'P') { PORT_ON(PORTB,6); } else if(read == 'p') { PORT_OFF(PORTB,6); } else if(read == 'Q') { PORT_ON(PORTB,7); } else if(read == 'q') { PORT_OFF(PORTB,7); } else if(read == 'X') { PORT_ON(PORTB,4); } else if(read == 'x') { PORT_OFF(PORTB,4); } else if(read == 'Y') { PORT_ON(PORTB,5); } else if(read == 'y') { PORT_OFF(PORTB,5); } delay_us(50); } }
int main(void) { /****************INITIALIZATIONS*******************/ //other stuff Im experimenting with for SoR uartInit(); // initialize the UART (serial port) uartSetBaudRate(0, 9600); // set UARTE speed, for Bluetooth uartSetBaudRate(1, 115200); // set UARTD speed, for USB connection, up to 500k, try 115200 if it uartSetBaudRate(2, 57600); // set UARTH speed uartSetBaudRate(3, 57600); // set UARTJ speed, for Blackfin //G=Ground, T=Tx (connect to external Rx), R=Rx (connect to external Tx) // initialize rprintf system and configure uart1 (USB) for rprintf rprintfInit(uart1SendByte); configure_ports(); // configure which ports are analog, digital, etc. LED_on(); rprintf("\r\nSystem Warming Up"); // initialize the timer system (comment out ones you don't want) init_timer0(TIMER_CLK_1024); init_timer1(TIMER_CLK_64); init_timer2(TIMER2_CLK_64); init_timer3(TIMER_CLK_64); init_timer4(TIMER_CLK_64); init_timer5(TIMER_CLK_1024); //timer5Init(); a2dInit(); // initialize analog to digital converter (ADC) a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage int i = 0, j = 0; //let system stabelize for X time for(i=0;i<16;i++) { j=a2dConvert8bit(i);//read each ADC once to get it working accurately delay_cycles(5000); //keep LED on long enough to see Axon reseting rprintf("."); } LED_off(); rprintf("Initialization Complete \r\n"); reset_timer0(); reset_timer1(); reset_timer2(); reset_timer3(); reset_timer4(); reset_timer5(); while(1) { control(); delay_cycles(100); //an optional small delay to prevent crazy oscillations } return 0; }
void prvSetupHardware(){ int i, j; //add 1.7s delay for potential power issues delay_cycles(65535); delay_cycles(65535); delay_cycles(65535); delay_cycles(65535); delay_cycles(65535); delay_cycles(65535); delay_cycles(65535); uartInit(); // initialize the UART (serial port) uartSetBaudRate(0, 38400); // set UARTE speed, for Bluetooth uartSetBaudRate(1, 115200); // set UARTD speed, for USB connection, up to 500k, try 115200 if it doesn't work uartSetBaudRate(2, 38400); // set UARTH speed uartSetBaudRate(3, 38400); // set UARTJ speed, for Blackfin //G=Ground, T=Tx (connect to external Rx), R=Rx (connect to external Tx) rprintfInit(uart1SendByte);// initialize rprintf system and configure uart1 (USB) for rprintf configure_ports(); // configure which ports are analog, digital, etc. LED_on(); //rprintf("\r\nSystem Warmed Up"); // initialize the timer system init_timer0(TIMER_CLK_1024); // init_timer1(TIMER_CLK_64); // Timer 1 is initialized by FreeRTOS init_timer2(TIMER2_CLK_64); init_timer3(TIMER_CLK_64); init_timer4(TIMER_CLK_64); init_timer5(TIMER_CLK_64); a2dInit(); // initialize analog to digital converter (ADC) a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage //let system stabelize for X time for(i=0;i<16;i++) { j=a2dConvert8bit(i);//read each ADC once to get it working accurately delay_cycles(5000); //keep LED on long enough to see Axon reseting rprintf("."); } LED_off(); rprintf("Initialization Complete \r\n"); //reset all timers to zero reset_timer0(); reset_timer1(); reset_timer2(); reset_timer3(); reset_timer4(); reset_timer5(); /********PWM Setup***********/ prvPWMSetup(); }