Split from: Always define the F_CPU before including delay.h

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am having this problem with my current project. In the delay.h file, it says that the 1MHz F_CPU will only get used if F_CPU isn't defined in the main program, but that doesn't appear to be working, even when I define F_CPU as 20000000UL prior to including delay.h.

 

I believe that the slow CPU (default of 1MHz caused by including delay.h) is causing me headaches with getting USART to work.

 

I am using the ATTiny828 with this program:

 

/*
 * MyATtiny828.cpp
 *
 * Created: 5/15/2019 9:33:57 AM
 * 
 */ 

#define F_CPU 20000000UL

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

void usart_init( unsigned int baud )
{
	/* Set baud rate */
	UBRRH = (unsigned char)(baud>>8);
	UBRRL = (unsigned char)baud;
	/* Enable receiver and transmitter */
	UCSRB = (1<<RXEN)|(1<<TXEN);
	/* Set frame format: 8 data bits, 1 stop bits */
	UCSRC = (0<<USBS)|(1<<UCSZ0)|(1<<UCSZ1);
}

void usart_transmit( unsigned char data )
{
	/* Wait for empty transmit buffer */
	while ( !( UCSRA & (1<<UDRE)) )
	;
	/* Put data into buffer, sends the data */
	UDR = data;
}

unsigned char usart_receive( void )
{
	/* Wait for data to be received */
	// Note that this will BLOCK everything else!
	while ( !(UCSRA & (1<<RXC)) )
	;
	/* Get and return received data from buffer */
	return UDR;
}

int main(void)
{

	usart_init(2400);

	char important_message = 0;
	char received_message = 0;

    while (1)
    {

		usart_transmit(important_message);

		if (important_message <= 255){
			important_message++;
		}
		else if (important_message > 255){
			important_message = 0;
		}

    }
}

 

On my oscilloscope when I send commands with PuTTY set to 2400 baud, I notice those commands are much, much narrower in time (much faster) than the supposedly 2400 baud that I am transmitting from the uC. And when I changed from 9600 baud to 2400 the time duration of the uC's outgoing pulses did not change so I am clearly messing up my clocks here. Help! cryingwink

 

Edit: I used _delay_ms in some functions that I removed for this example, but even when I eliminate the include delay.h I still have this problem with the baud rate appearing to be incorrect. 400ms to transmit 0xFF at 2400 baud seems incorrect, but that's what I see on the 'scope.

 

Last Edited: Wed. Jun 19, 2019 - 08:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Umm, you don't stick your baud rate value into the UBRR registers.  You stick something along the lines of F_CPU/(16*baud) (the exact equation is in the data sheet) into the registers.  The factor of 16 is because the uart samples each bit multiple times.  The data sheet also lists values for many common clock rates and baud rates.

Last Edited: Wed. Jun 19, 2019 - 07:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

:: Morten

 

(yes, I work for Atmel, yes, I do this in my spare time, now stop sending PMs)

Topic locked