Hollis Scriber
Hollis Scriber

Reputation: 159

Interpreting Atmega328p data sheet

I'm trying to program an Arduino Uno with AVR code because I'm not allowed to use the Arduino libraries for my senior project that starts in September. I found the data sheet, got the digital pins working, and moved on to trying to talk to my computer through the USB Serial connection. I've read chapter 19 of this manual (1) entirely too many times, and I am still lost. I copy and pasted code from the library documentation itself and the data sheet, and it still says things are unresolved and/or not found. Any help is appreciated.

Code:

#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#include <util/setbaud.h>

#define analog1 PC0
#define LED PB5
#define BAUD 9600
//#define F_CPU 16000000

void init_io(){
    DDRB |= (1<<LED);
    DDRC |= (0<<analog1);

}

static void
   uart_9600(void)
    {
   UBRRH = UBRRH_VALUE;
   UBRRL = UBRRL_VALUE;
   #if USE_2X
   UCSRA |= (1 << U2X);
   #else
   UCSRA &= ~(1 << U2X);
   #endif
   }

void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRnA & (1<<UDREn)) )
;
/* Put data into buffer, sends the data */
UDRn= data;
}

int main(){
    int analog_value = 1000;
    while(1){
        //analog_value = PINC;

        if(analog_value > 500){
            PORTB |= (1<<LED);
            _delay_ms(500);
            PORTB &= ~(1<<LED);
            _delay_ms(500);
            putchar(analog_value);

        }
    }
}

Thanks a lot!

Upvotes: 0

Views: 992

Answers (1)

TRON
TRON

Reputation: 341

The first issue you have in your code is the AVR-libc one. You are using the util/setbaud.h file incorrectly. I suggest you take a look at the documentation here. To make things brief, you need to #define BAUD 9600 before you #include <util/setbaud.h>. Then, as others have pointed out, your register names are incorrect. The USART control registers are UCSRnA, UCSRnB and UCSRnC; the baud registers are UBRRnL and UBRRnH, where n is the number of the serial port in use on the device, indexed from 0.

You also need to define a function to handle stdout, as AVR-libc provides you none. See the documentation for stdio.h in order to learn how to write one.

Cheers.

Upvotes: 1

Related Questions