Advertisement
Guest User

Untitled

a guest
Apr 26th, 2012
45
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. uint16_t samples[64];
  22. uint8_t sample;
  23.  
  24. void setup(void)
  25. {
  26.    
  27.     ADMUX=(1<<1); //single ended adc2/pb4
  28.     ADCSRA=(1<<7) | (1<<ADATE)|(1<<ADIE) | (1<<ADPS2)|(1<<ADPS1); //125khz /64
  29.     ADCSRB=0;
  30.     ADCSRA|=(1<<ADSC);
  31.     //RingBuffer_InitBuffer(&txBuffer, txBuffer_Data, sizeof(txBuffer_Data));
  32. }
  33.  
  34.  
  35.  
  36.  
  37. /*unsigned char adc_start(unsigned char channel) // Function to perform an ADC conversion, Takes 0-8 as input
  38. // to select which input to convert
  39. {
  40.     unsigned char i;
  41.     ADCH=0x00; // Clear the previous result
  42.  
  43.     i=channel&0x07; // Decide which line to perform ADC conversion on
  44.     ADMUX=i|0x60; // Enter which line to perform in the ADC control register
  45.     ADCSRA|=1<<ADSC;
  46.  
  47.     while(ADCSRA & (1<<ADSC));  // wait for conv. to complete
  48.     unsigned char temp=ADCH; //unsigned int temp=ADC;   for 10 bits
  49.     return temp;
  50. }
  51. */
  52. ISR(ADC_vect,ISR_NOBLOCK)
  53. {
  54.     sample++;
  55.     if(sample>63) sample=0;
  56.     samples[sample]=ADCL;
  57.     samples[sample]|=( ((uint16_t)ADCH)<<8 );
  58. }
  59.  
  60. int main (void)
  61. {
  62.    
  63.    UCSR0B |= (1 << RXEN) | (1 << TXEN);   // Turn on the transmission and reception circuitry
  64.    UCSR0C |= (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes
  65.    UBRR0H = (MYUBRR >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
  66.    UBRR0L = MYUBRR; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
  67.    stdout = &uart_str;
  68.    printf("Hello World\n\r");
  69.    uint8_t result=0;
  70.    setup();
  71.    sei();//global interrupts enable
  72.    uint16_t a;
  73.     uint8_t i,j;
  74.    ADCSRA|=1<<ADSC;
  75.   while(1)
  76.     {
  77.         //USI_UART_Transmit_Byte('a');
  78.         //ADCSRA|=(1<<ADSC);
  79.         //while(ADCSRA&(1<<ADSC));
  80.         a=0;
  81.         for(i=0;i<64;i++)
  82.             a+=samples[i];
  83.         a|=( ((uint16_t)ADCH)<<8 );
  84.         printf("S: %u\r\n",a);
  85.    
  86.        
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement