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!
  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. static volatile unsigned char temp; //so it is global, because we can not return from interrupt
  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 << ADLAR) | (1 << REFS0);
  27.     ADCSRA |= (1 << ADSC) | (1 << ADEN) | (1 << ADPS1) | (1 << ADPS2)|(1<<ADIE); //interrpts now enabled
  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. ISR(ADC_vect){
  49. // NOT USING ADC CHANNEL SELECTOR FOR NOW
  50. //
  51. //
  52. //      unsigned char i;
  53. //  ADCH=0x00; // Clear the previous result
  54. //  i=channel&0x07; // Decide which line to perform ADC conversion on
  55. //  ADMUX=i|0x60; // Enter which line to perform in the ADC control register
  56.  // wait for conv. to complete
  57.     temp = ADCH; //unsigned int temp=ADC;   for 10 bits
  58.     printf("read: %d \n\r", temp);
  59.     ADCSRA|=1<<ADSC;
  60. }
  61.  
  62. int main (void)
  63. {
  64.    
  65.    UCSR0B |= (1 << RXEN) | (1 << TXEN);   // Turn on the transmission and reception circuitry
  66.    UCSR0C |= (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes
  67.    UBRR0H = (MYUBRR >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
  68.    UBRR0L = MYUBRR; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
  69.    stdout = &uart_str;
  70.    printf("Hello World\n\r");
  71.    uint8_t result=0;
  72.    adc_init();
  73.    sei();//global interrupts enable
  74.  
  75.    while(1){
  76.    }
  77.    
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement