Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
# include<stdio.h># include<avr/io.h># include<util/delay.h># include<avr/interrupt.h># include<stdlib.h># include<string.h># include<avr/pgmspace.h>#define USART_BAUDRATE 38400 #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL)))- 1) char u[11]="adcvalue\r\n"; char *d; char b[10]; char *p; unsigned int a;void adcinitialize();void uartinitialize();void adcgetvalue();void transferusart();void printvalue(char *); int main(void) { adcinitialize(); uartinitialize(); while(1) { adcgetvalue(); p=itoa(a,b,10); strcat_P(b,PSTR("\r\n")); d=&u[0]; transferusart(); } return(0); } void adcinitialize() { ADCSRA|=_BV(7)|_BV(ADPS2)|_BV(ADPS1); ADMUX=0x60; ADCSRA|=(1<<ADSC); while(!(ADCSRA & (1<<ADIF))); ADCSRA|=(1<<ADIF); TCCR1B|=(1<<CS12); } void uartinitialize() { UCSRB= (1<<RXEN) | (1<<TXEN); UCSRC= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); UBRRL=BAUD_PRESCALE; UBRRH=(BAUD_PRESCALE>>8); } void adcgetvalue() { ADMUX=0x61; ADCSRA|=(1<<ADSC); while(!(ADCSRA & (1<<ADIF))); ADCSRA|=(1<<ADIF); a=ADCH; } void transferusart() { while (*d != '\0') { while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it UDR =*d ; // Echo back the received byte back to the computer d++; } TCNT1=0; while(TCNT1<=60000){}; TCNT1=0; printvalue(p); } void printvalue(char *l) { while (*l != 0x00) { while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it UDR =*l ; // Echo back the received byte back to the computer l++; } TCNT1=0; while(TCNT1<=60000){}; TCNT1=0; }