Advertisement
Guest User

Untitled

a guest
Apr 26th, 2012
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <ctype.h>
  4. #include <avr/io.h>         // include I/O definitions (port names, pin names, etc)
  5. #include <avr/interrupt.h>  // include interrupt support
  6. #define USART_BAUDRATE 9600
  7. #define F_CPU 8000000
  8. #define MYUBRR F_CPU/16/USART_BAUDRATE-1
  9.  
  10.  
  11. int printCHAR(char character, FILE *stream)
  12. {
  13.    while ((UCSR0A & (1 << UDRE)) == 0) {};
  14.  
  15.    UDR0 = character;
  16.  
  17.    return 0;
  18. }
  19.  
  20. FILE uart_str = FDEV_SETUP_STREAM(printCHAR, NULL, _FDEV_SETUP_RW);
  21.  
  22.  
  23. void adc_init(void) // Function to initialise the ADC feature
  24. {
  25.     ADCSRA=0X00; // Clear previous ADC results and status flags
  26.     ADMUX |= (1 << REFS1) | (1 << REFS0)  | (1 << ADLAR);
  27.     ADCSRA |= (1 << ADSC) | (1 << ADEN) | (1 << ADPS0) | (1 << ADPS2);
  28. }
  29.  
  30.  
  31.  
  32.  
  33. unsigned char adc_start(unsigned char channel) // Function to perform an ADC conversion, Takes 0-8 as input
  34. // to select which input to convert
  35. {
  36.     unsigned char i;
  37.     ADCH=0x00; // Clear the previous result
  38.  
  39.     i=channel&0x07; // Decide which line to perform ADC conversion on
  40.     ADMUX=i|0x60; // Enter which line to perform in the ADC control register
  41.     ADCSRA|=1<<ADSC;
  42.  
  43.     while(ADCSRA & (1<<ADSC));  // wait for conv. to complete
  44.     unsigned char temp=ADCH; //unsigned int temp=ADC;   for 10 bits
  45.     return temp;
  46. }
  47.  
  48. int main (void)
  49. {
  50.    
  51.    UCSR0B |= (1 << RXEN) | (1 << TXEN);   // Turn on the transmission and reception circuitry
  52.    UCSR0C |= (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes
  53.    UBRR0H = (MYUBRR >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
  54.    UBRR0L = MYUBRR; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
  55.    stdout = &uart_str;
  56.    printf("Hello World\n\r");
  57.    uint8_t result=0;
  58.    adc_init();
  59.  
  60.    while(1){
  61.    
  62.    
  63.     result=adc_start(0);
  64.     printf("read: %d \n\r",result);
  65.  
  66.  
  67.  
  68.    }
  69.    
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement