Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <avr/interrupt.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. #define FOSC 16000000
  8. #define baud_rate 9600
  9. #define baud FOSC/16/baud_rate-1
  10.  
  11. volatile unsigned int adc_val=0;
  12. unsigned char adcval[10]={0};
  13. unsigned char adctext[]="\nADC VAl: ";
  14.  
  15. void Transmit(unsigned char c)
  16. {
  17. UDR0=c;
  18. }
  19.  
  20. void adc_init()
  21. {
  22. sei();
  23. DDRC=0x00;
  24.  
  25. ADMUX = ( (1<<REFS0) | (1<<REFS1) );
  26. ADCSRA = ( (1<<ADEN) | (1<<ADSC) | (1<<ADIE) | (1<<ADIF) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0)); //Enable ADC, ADC Interrupt, Prescaler
  27.  
  28.  
  29. }
  30.  
  31. void usart_init(unsigned int b_rate)
  32. {
  33. UBRR0H=0x00;
  34. UBRR0L=0x00;
  35. UCSR0B=0x00;
  36. UCSR0C=0x00;
  37. UDR0 =0x00;
  38.  
  39. UBRR0H =(unsigned char)b_rate>>8;
  40. UBRR0L =(unsigned char)b_rate;
  41.  
  42. //Enable Rx, Tx, Enable Interrupt on UDR empty
  43. UCSR0B = ( (1<<RXEN0) | (1<<TXEN0) | (1<<UDRIE0) );
  44. UCSR0C = ( (1<<UCSZ00) | (1 << UCSZ01) );
  45. sei();
  46.  
  47. }
  48.  
  49.  
  50. int main (void)
  51. {
  52. usart_init(baud);
  53. adc_init();
  54.  
  55. while(1)
  56. {
  57. _delay_ms(1000);
  58.  
  59. }
  60.  
  61. }
  62.  
  63. ISR(ADC_vect)
  64. {
  65.  
  66. adc_val= ADCL | (ADCH<<8);
  67. //strncat(adctext,itoa(adc_val, adcval,10),10 );
  68. ADCSRA = ( (1<<ADSC));
  69. itoa(adc_val,adcval,10);
  70. }
  71.  
  72. ISR(USART_UDRE_vect)
  73. {
  74. static int i=0;
  75.  
  76.  
  77. if(adcval[i++]!='\0')
  78. Transmit(adcval[i]);
  79. else
  80. { i=0;
  81. Transmit('\n');
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement