Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <ctype.h>
- #include <avr/io.h> // include I/O definitions (port names, pin names, etc)
- #include <avr/interrupt.h> // include interrupt support
- #define USART_BAUDRATE 9600
- #define F_CPU 8000000
- #define MYUBRR F_CPU/16/USART_BAUDRATE-1
- static volatile unsigned char temp; //so it is global, because we can not return from interrupt
- int printCHAR(char character, FILE *stream)
- {
- while ((UCSR0A & (1 << UDRE)) == 0) {};
- UDR0 = character;
- return 0;
- }
- FILE uart_str = FDEV_SETUP_STREAM(printCHAR, NULL, _FDEV_SETUP_RW);
- uint16_t samples[64];
- uint8_t sample;
- void setup(void)
- {
- ADMUX=(1<<1); //single ended adc2/pb4
- ADCSRA=(1<<7) | (1<<ADATE)|(1<<ADIE) | (1<<ADPS2)|(1<<ADPS1); //125khz /64
- ADCSRB=0;
- ADCSRA|=(1<<ADSC);
- //RingBuffer_InitBuffer(&txBuffer, txBuffer_Data, sizeof(txBuffer_Data));
- }
- /*unsigned char adc_start(unsigned char channel) // Function to perform an ADC conversion, Takes 0-8 as input
- // to select which input to convert
- {
- unsigned char i;
- ADCH=0x00; // Clear the previous result
- i=channel&0x07; // Decide which line to perform ADC conversion on
- ADMUX=i|0x60; // Enter which line to perform in the ADC control register
- ADCSRA|=1<<ADSC;
- while(ADCSRA & (1<<ADSC)); // wait for conv. to complete
- unsigned char temp=ADCH; //unsigned int temp=ADC; for 10 bits
- return temp;
- }
- */
- ISR(ADC_vect,ISR_NOBLOCK)
- {
- sample++;
- if(sample>63) sample=0;
- samples[sample]=ADCL;
- samples[sample]|=( ((uint16_t)ADCH)<<8 );
- }
- int main (void)
- {
- UCSR0B |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
- UCSR0C |= (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes
- UBRR0H = (MYUBRR >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
- UBRR0L = MYUBRR; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
- stdout = &uart_str;
- printf("Hello World\n\r");
- uint8_t result=0;
- setup();
- sei();//global interrupts enable
- uint16_t a;
- uint8_t i,j;
- ADCSRA|=1<<ADSC;
- while(1)
- {
- //USI_UART_Transmit_Byte('a');
- //ADCSRA|=(1<<ADSC);
- //while(ADCSRA&(1<<ADSC));
- a=0;
- for(i=0;i<64;i++)
- a+=samples[i];
- a|=( ((uint16_t)ADCH)<<8 );
- printf("S: %u\r\n",a);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement