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
- 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);
- void adc_init(void) // Function to initialise the ADC feature
- {
- ADCSRA=0X00; // Clear previous ADC results and status flags
- ADMUX |= (1 << REFS1) | (1 << REFS0) | (1 << ADLAR);
- ADCSRA |= (1 << ADSC) | (1 << ADEN) | (1 << ADPS0) | (1 << ADPS2);
- }
- 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;
- }
- 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;
- adc_init();
- while(1){
- result=adc_start(0);
- printf("read: %d \n\r",result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement