code "the most simple usart"

Go To Last Post
100 posts / 0 new

Pages

Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, the inhabitants of this forum. Maybe you read my topic about the problem with 2561, duck here, she did not dare ... the manor does not work. Help please find the simplest code to send something to the serial port (without using the standard library)) Generally, the simplest code is to send one or two digits to the serial port to just make sure that with the standard library all is well and the joint is somewhere else

Last Edited: Thu. Apr 12, 2018 - 12:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In datasphere there is such an example.

#define FOSC 1843200// Clock Speed
#define BAUD 9600
#define (MYUBRR FOSC/16/BAUD-1)
void main( void )
{...
USART_Init ( MYUBRR );
...} // main
void USART_Init( unsigned int ubrr){
/* Set baud rate */
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<USBS)|(3<<UCSZ0);
} // USART_Init
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRnA & (1<<UDREn)) )
;
/* Put data into buffer, sends the data */
UDRn = data;
}

Will this be enough to make your estate work? Unless I would translate this code into the code arduino.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you want people to refer to another thread, then give a link to it:

 

https://www.avrfreaks.net/forum/2561-serial

 

 

To find all the documentation & resources for a chip, go to the Product Page:

 

https://www.microchip.com/wwwproducts/en/ATmega2560 - easily found by putting the part number into google

 

then look on the 'Documents' tab - there you will find the Datasheet, and a list of all the Application Notes.

 

The Datasheet, in the USART section, contains simple examples - in both 'C' and assembler.

 

The example on p207 must be the simplest possible example - it just send a single character.

 

 

Other Application Notes to look at:

 

AN_1451 - AVR306: Using the AVR UART in C on tinyAVR and megaAVR devices

 

AN_2547 - AVR244: UART as ANSI Terminal Interface

 

 

You should also pay attention to:

 

AN2519 - AVR Microcontroller Hardware Design Considerations

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, that is the simplest code to transmit a single byte.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
Yes, that is the simplest code to transmit a single byte.
No it isn't. The 2 stop bits is unusual, it would be easier to simply take the 8N1 default and forget the write to UCSRC

 

Also there is an error here:

#define (MYUBRR FOSC/16/BAUD-1)

The opening parenthesis is in the wrong place. Should be:

#define MYUBRR (FOSC/16/BAUD-1)

(though there are some that might suggest <util/setbaud.h> depending on which compiler is being used).

 

Oh and in :

/* Set baud rate */
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;

there's an implicit cast to uint8_t (unsigned char) anyway as the targets are uint8_t. So this would be fine:

/* Set baud rate */
UBRRH = ubrr >> 8;
UBRRL = ubrr;

This is wrong:

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

"n" is a placeholder. The 2561 has multiple UART so n should be replaced with 0 or 1 or whatever. Something like:

/* Wait for empty transmit buffer */
while ( !( UCSR1A & (1 << UDRE1)) )
;
/* Put data into buffer, sends the data */
UDR1 = data;

and similarly back in:

UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<USBS)|(3<<UCSZ0);

if this is a 2561 all that should be:

UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSR0B = (1 << RXEN0)|(1 << TXEN0);
/* Set frame format: 8data, 2stop bit */
UCSR0C = (1 << USBS0)|(3 << UCSZ0);

(actually I just typed that from memory - you need to look up what the symbols are actually called).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The code in #2 seems to be copied direct from the Datasheet examples.

 

Yes, the datasheet has the error with the #define !!

 

Clearly, they didn't actually test that code!

 

angry

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is why I would actually use the articles in the Tutorial Forum or perhaps Atmel's application notes as a starting point and not the datasheet.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

clawson wrote:

...it would be easier to simply take the 8N1 default and forget the write to UCSRC

 

I thought that best practice was to assume nothing and always set registers how you want them? wink  "|=" anyone?

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

#define MYUBRR (FOSC/16/BAUD-1)
#define FOSC 1843200
#define BAUD 9600
void setup() {
  UBRRH = ubrr >> 8;
  UBRRL = ubrr;
  UCSR0B = (1 << RXEN0) | (1 << TXEN0);
  UCSR0C = (1 << USBS0) | (3 << UCSZ0);

}

void loop() {
  while ( !( UCSR1A & (1 << UDRE1)) ) ;
  UDR1 = data;
}

oh my God ... that is, there are mistakes even in datashits ... how to understand a man who does not speak English well and everything else has not been engaged in angrily programming)) a nightmare ... ... from your post I realized that to me you need to write the following

sketch_apr12a:5: error: 'UBRRH' was not declared in this scope

   UBRRH = ubrr >> 8;

   ^

sketch_apr12a:5: error: 'ubrr' was not declared in this scope

   UBRRH = ubrr >> 8;

           ^

sketch_apr12a:6: error: 'UBRRL' was not declared in this scope

   UBRRL = ubrr;

   ^

sketch_apr12a:7: error: expected primary-expression before '|' token

   UCSR0B = | (1 << RXEN0) | (1 << TXEN0);

            ^

sketch_apr12a:8: error: expected primary-expression before '|' token

   UCSR0C = | (1 << USBS0) | (3 << UCSZ0);

            ^

sketch_apr12a:8: error: 'UCSZ0' was not declared in this scope

   UCSR0C = | (1 << USBS0) | (3 << UCSZ0);

                                   ^

C:\Users\Nikolai\Documents\Arduino\sketch_apr12a\sketch_apr12a.ino: In function 'void loop()':

sketch_apr12a:14: error: 'data' was not declared in this scope

   UDR1 = data;

          ^

exit status 1
'UBRRH' was not declared in this scope

 

Last Edited: Thu. Apr 12, 2018 - 11:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
This is why I would actually use ... Atmel's application notes as a starting point and not the datasheet.

But, if we can't trust Atmel  to get the datasheet right, how can we trust them to get the App Notes any better?

 

angry

 

UNikolai wrote:
datashits

laugh

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@UNikolai please don't keep quoting entire posts - it just clogs the thread and makes it hard to read.

 

just pick out the bits you're specifically referring to.

 

Or use the 'Reply' button if it's a general reply to an entire post - the forum automatically fills-in which post it relates to.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'll keep it on mind. And what of the code that I wrote, why arduino gives an error?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have several types of error, the simplest one is that you did not declare the ubrr and data variables.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

El Tangas wrote:

You have several types of error, the simplest one is that you did not declare the ubrr and data variables.

But in the example they are also not declared. I thought that these are some standard variables.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

El Tangas wrote:
You have several types of error

Actually, it seems to be just one type of error: it is a basic requirement of the C and C++ programming languages that things must be declared before they are used.

 

If you use something before there has been any declaration of it, then you will get an error:

error: 'UBRRH' was not declared in this scope

 

You seem to be getting this for all your peripheral register names: UBRRH, UBRRL, UCSR0B, UCSR0C , etc.

 

So either you are using the wrong names, or your sketch is not properly configured to provide those names.

 

You are also getting the same errors for ubrr, and data - these are your variables - so you need to declare them!

 

This is basic C/C++ language stuff - nothing specific to Arduino or AVR or Atmel.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

UNikolai wrote:
But in the example they are also not declared

The examples are clearly not complete programs!

 

And they also tell you to read the section, "About Code Examples" - did you do that?

 

It says,

These code examples assume that the part specific header file is included before compilation

I don't do Arduino, so I don't know how that happens in Arduino - presumably it's own of the things it tries to be "helpful" by hiding from you?

 

Presumably, you have to tell it what chip you're using when you set up your "Sketch" ?

 

 

 

The datasheet example for transmit does define the 'data' variable - it is parameter to the function:

 

void USART_Transmit( unsigned char data )
{
   /* Wait for empty transmit buffer */
   while ( !( UCSRnA & (1<<UDREn)) )
      ;

   /* Put data into buffer, sends the data */
   UDRn = data;
}

Similarly for ubrr:

void USART_Init( unsigned int ubrr)
{
   /* Set baud rate */
   UBRRH = (unsigned char)(ubrr>>8);
   UBRRL = (unsigned char)ubrr;

   /* Enable receiver and transmitter */
   UCSRB = (1<<RXEN)|(1<<TXEN);

   /* Set frame format: 8data, 2stop bit */
   UCSRC = (1<<USBS)|(3<<UCSZ0);

} // USART_Init

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Apr 12, 2018 - 12:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
mples are clearly not complete programs!  

#define MYUBRR (FOSC/16/BAUD-1)
#define FOSC 1843200
#define BAUD 9600
unsigned char data = 1;
unsigned int ubrr;
void setup() {

  UBRRH = ubrr >> 8;
  UBRRL = ubrr;
  UCSR0B = (1 << RXEN0) | (1 << TXEN0);
  UCSR0C = (1 << USBS0) | (3 << UCSZ0);

}

void loop() {
  while ( !( UCSR1A & (1 << UDRE1)) ) ;
  UDR1 = data;
}
sketch_apr12a:8: error: 'UBRRH' was not declared in this scope

   UBRRH = ubrr >> 8;

   ^

sketch_apr12a:9: error: 'UBRRL' was not declared in this scope

   UBRRL = ubrr;

   ^

sketch_apr12a:11: error: 'UCSZ0' was not declared in this scope

   UCSR0C = (1 << USBS0) | (3 << UCSZ0);

                                 ^

exit status 1
'UBRRH' was not declared in this scope

And now here such error. And in fact there is such register like as in the microcontroller (UBRRH)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Go on.   I am sure that there is an Arduino "core" for the ATmega2561.

If not,   most of the code for a MEGA2560 is going to work on the smaller 2561.

 

If you are using Arduino,   you just use the Serial class.    Exactly the same as any other Arduino.

 

Yes,  you can write directly to the USART if you want.    Note that the Arduino is already set up for interrupt-driven code with the Serial class.    So make sure to disable the USART interrupts for your code.

 

If you are writing directly to AVR registers,   you must read the datasheet like anyone else.   Especially when the datasheet refers to UBRRnH you need to use the specific UBBR0H, UBRR1H, UBRR2H, ...

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

UNikolai wrote:
in fact there is such register like as in the microcontroller (UBRRH)

That's irrelevant.

 

The C or C++ compiler can't read datasheets!

 

So you have to suitably declare these within your source code: as already explained, if there is no declaration, then you will get that error!

 

As the datasheet said, this is normally done by #include-ing the appropriate header file - but this is something that Arduino hides from you.

 

 

EDIT: for convenience, the header file usually follows the same names as the datasheet - but there is no compulsion for it to do so.

 

 

Also pay attention to what David said.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Apr 12, 2018 - 02:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

UNikolai wrote:

datashits

 

laugh

 

+1  yes

 

A translation error? or editorial?

 

Greg Muth

Portland, OR, US

Xplained/Pro/Mini Boards mostly

 

Make Xmega Great Again!

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

Go on.   I am sure that there is an Arduino "core" for the ATmega2561.

If not,   most of the code for a MEGA2560 is going to work on the smaller 2561.

 

If you are using Arduino,   you just use the Serial class.    Exactly the same as any other Arduino.

 

Yes,  you can write directly to the USART if you want.    Note that the Arduino is already set up for interrupt-driven code with the Serial class.    So make sure to disable the USART interrupts for your code.

 

If you are writing directly to AVR registers,   you must read the datasheet like anyone else.   Especially when the datasheet refers to UBRRnH you need to use the specific UBBR0H, UBRR1H, UBRR2H, ...

 

David.

 Yes, there is a kernel. But unfortunately does not work yusart for some reason ...

Last Edited: Thu. Apr 12, 2018 - 02:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@UNikolai please don't keep quoting entire posts - it just clogs the thread and makes it hard to read.

 

just pick out the bits you're specifically referring to.

 

Or use the 'Reply' button if it's a general reply to an entire post - the forum automatically fills-in which post it relates to.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here is an example of how to quote "selectively":

david.prentice wrote:
when the datasheet refers to UBRRnH you need to use the specific UBBR0H, UBRR1H, UBRR2H,

So do that, and see if it fixes your errors.

 

UNikolai wrote:
 Yes, there is a kernel. But unfortunately does not work yusart for some reason ...

If there is a problem in the Arduino (or 3rd-party) Core, then you need to take that up with Arduino (or the 3rd party)

 

However, the errors you're showing here are plain programming errors on your part - so try again with the proper UBRRnH names ...

 

 

EDIT

 

Fix quote attribution

 

 

EDIT 2

 

add "3rd party" - see #24

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Apr 12, 2018 - 03:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

I do not have a 2561.   And I generally like to stick with genuine Arduino boards.   Then everyone in the world can run the same code on known Arduino boards.

 

There are several third-party cores available e.g. for Tiny85 or Mega2561.

These cores are not officially supported by the Arduino team.

 

If you want to add direct hardware-access code to your project,   you must consider any side effects on the existing Arduino code.

For example.    Timers are already set up for PWM by the Arduino init() code.    USART0 is already set up for Serial class.

 

You must initialise every Timer or USART register before you use Timer or USART for yourself.     Use = and not |= in assignments.

 

Seriously.   You can write your own polled USART functions.   And use them in your program.    Cliff gave you examples.

 

However,   if you are using an "Arduino",  why not stick to the proven Serial class ?

Write Arduino sketches that work on Uno, Mega, Zero, ... by just using the official methods e.g. Serial.begin(),  digitalWrite(), ...

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
There are several third-party cores available e.g. for Tiny85 or Mega2561.

These cores are not officially supported by the Arduino team.

I didn't know that - #23 updated accordingly

 

If you want to add direct hardware-access code to your project,   you must consider any side effects on the existing Arduino code.

And you must have a proper understanding of how the hardware works, and how to do the access in the C++ programming language.

 

 

if you are using an "Arduino",  why not stick to the proven Serial class ?

+1

 

 

Write Arduino sketches that work on Uno, Mega, Zero, ... by just using the official methods e.g. Serial.begin(),  digitalWrite(), ...

+1

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define MYUBRR (FOSC/16/BAUD-1)
#define FOSC 1843200
#define BAUD 9600
unsigned char data = 1;
unsigned int ubrr;
void setup() {

  UBRR0H = ubrr >> 8;
  UBRR0L = ubrr;
  UCSR0B = (1 << RXEN0) | (1 << TXEN0);
  UCSR0C = (1 << USBS0) | (3 << UCSZ00);

}

void loop() {
  while ( !( UCSR1A & (1 << UDRE1)) ) ;
  UDR1 = data;
}

Here it is already compiled, but what variable ubbr should be?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

but this standard serial port is not working ... I'm looking for a reason. So then I generally need this only to debug a sketch, and in the final version of this will not be

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

UNikolai wrote:
what variable ubbr should be?

I think you mean, "what value should the variable ubbr have?" 

 

Look at the code: it is the value that will be loaded into the UBBR register

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is he missing the #include <avr/io.h> header?  Is that the reason why the ubbr is unknown?

 

Jim

 

 

Click Link: Get Free Stock: Retire early!

share.robinhood.com/jamesc3274

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

He's doing Arduino - so shouldn't that be done for him?

 

In #26 he says it is now compiling.

 

Anyhow, surely <avr/io.h> only defines the registers - not user variables like ubbr ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your code initialised USART0 and then wrote to USART1

 

Try this:

#define MYUBRR (FOSC/16/BAUD-1)
//#define FOSC 1843200
#define FOSC F_CPU    //Arduino code "knows" the correct CPU frequency
#define BAUD 9600

char uart_putchar(char c)
{
    while ( !( UCSR0A & (1 << UDRE0)) ) ;
    UDR0 = c;
}

void uart_putstring(char *s)
{
    char c;
    while (c = *s++) {
        uart_putchar(c);
    }
}

void setup()
{
    UBRR0H = MYUBRR >> 8;
    UBRR0L = MYUBRR;
    UCSR0B = (1 << RXEN0) | (1 << TXEN0);
    UCSR0C = (1 << USBS0) | (3 << UCSZ00);
}

void loop()
{
    uart_putstring("David Prentice");
    uart_putchar('\n');     //newline
    delay(5000);
}

But you could also try this regular Arduino code:

void setup()
{
    // put your setup code here, to run once:
    Serial.begin(9600);
}

void loop()
{
    // put your main code here, to run repeatedly:
    Serial.println("David Prentice");
    delay(5000);
}

If your "Arduino" board is configured wrong,  you might need to force 1843200 if that is your correct clock frequency.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

In the first example in #31 a mix of arduino and C is used. It takes more than 600b flash.

I would prefer to write either pure arduino or pure C code.

 

Arduino sketch in C:


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

#define MYUBRR (FOSC/16/BAUD-1)
//#define FOSC 1843200
#define FOSC F_CPU    //Arduino code "knows" the correct CPU frequency
#define BAUD 9600

char uart_putchar(char c)
{
    while ( !( UCSR0A & (1 << UDRE0)) ) ;
    UDR0 = c;
}

void uart_putstring(char *s)
{
    char c;
    while (c = *s++) {
        uart_putchar(c);
    }
}

void setup()
{
    UBRR0H = MYUBRR >> 8;
    UBRR0L = MYUBRR;
    UCSR0B = (1 << RXEN0) | (1 << TXEN0);
    UCSR0C = (1 << USBS0) | (3 << UCSZ00);
}

int main()
{   setup();
    uart_putstring("David Prentice");
    uart_putchar('\n');     //newline
    _delay_ms(5000);
}

takes only 256 bytes of flash. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I guess this is not so simple after all...................frown

 

JIm

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
I guess this is not so simple after all...................frown
My 2 cents:

You have to put Uber from #32 under the FOSC and BAUD definitions:

//#define FOSC 1843200
#define FOSC F_CPU    //Arduino code "knows" the correct CPU frequency
#define BAUD 9600
#define MYUBRR (FOSC/16/BAUD-1)

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, some people just work way too hard...

 

JC

 

'Simple USART Comm's Test
'File: SUCT V1.bas

   $regfile = "m328pdef.dat"    'Specify the uC
   $crystal = 16000000          'uC Clock MHz

   $hwstack = 64                'Hardware Stack
   $swstack = 64                'Software Stack
   $framesize = 64              'Frame Space
   $Baud = 19200                'Baud Rate

Main:
   do
      Print "Testing 1, 2, 3"
      waitms 1000
   loop

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My 2 cents:

You have to put Uber from #32 under the FOSC and BAUD definitions:

Surprisingly the compiler does not care about the order of #defs.

I have tested in real with arduino board.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@Visovian,

 

You do not need to worry about basic system include files if built as a INO file.

You do not need to worry about forward declarations if built as a INO file.

 

The Arduino Build processes INO into CPP file.   It adds "Arduino.h" and adds any forward declarations.

 

If you save the source file as C or CPP,   it is your responsibility to include system file like avr/io.h

 

Your example does not use a loop().    So it only prints "David Prentice" once.

 

I would not worry whether an executable is 600 bytes or 250 bytes in a 256kB MCU.

I would choose whether to write straight C, C++ or to take advantage of the Arduino INO processing.

 

The C preprocessor does worry about what it "knows" at the time of expansion.

MYUBBR is only evaluated after it knows the MYUBRR define and the BAUD, FOSC defines.

 

I removed any global variables like ubrr and data.   They serve no purpose.

 

@docJC,

 

The Arduino code is the simplest of all.    But the Basic is simpler than the C code.

In practice,   the m2561 has got plenty of Flash memory.

 

David.

Last Edited: Thu. Apr 12, 2018 - 08:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


C:\Users\Nikolai\Documents\Arduino\sketch_apr12a\sketch_apr12a.ino:2:0: warning: "FOSC" redefined

 #define FOSC F_CPU    //Arduino code "knows" the correct CPU frequency

 ^

C:\Users\Nikolai\Documents\Arduino\sketch_apr12a\sketch_apr12a.ino:1:0: note: this is the location of the previous definition

 #define FOSC 1843200

 ^

C:\Users\Nikolai\Documents\Arduino\sketch_apr12a\sketch_apr12a.ino: In function 'char uart_putchar(char)':

C:\Users\Nikolai\Documents\Arduino\sketch_apr12a\sketch_apr12a.ino:13:1: warning: no return statement in function returning non-void [-Wreturn-type]

 }

 ^

C:\Users\Nikolai\Documents\Arduino\sketch_apr12a\sketch_apr12a.ino: In function 'void uart_putstring(char*)':

C:\Users\Nikolai\Documents\Arduino\sketch_apr12a\sketch_apr12a.ino:18:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

     while (c = *s++) {

                  ^

C:\Users\Nikolai\Documents\Arduino\sketch_apr12a\sketch_apr12a.ino: In function 'int main()':

C:\Users\Nikolai\Documents\Arduino\sketch_apr12a\sketch_apr12a.ino:33:36: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

     uart_putstring("David Prentice");

                                    ^

Compiling libraries...
Compiling core...
Using precompiled core
Linking everything together...
"C:\Users\Nikolai\Desktop\arduino-1.8.5-windows\arduino-1.8.5\hardware\tools\avr/bin/avr-gcc" -Wall -Os -Wl,--gc-sections -mmcu=atmega2561  -o "C:\Users\Nikolai\AppData\Local\Temp\arduino_build_869229/sketch_apr12a.ino.elf" "C:\Users\Nikolai\AppData\Local\Temp\arduino_build_869229\sketch\sketch_apr12a.ino.cpp.o" "C:\Users\Nikolai\AppData\Local\Temp\arduino_build_869229/..\arduino_cache_490980\core\core_MegaCore-master_avr_2561_BOD_1v8,LTO_Os,clock_8MHz_internal_789f896693846094e303cbac6561f2a9.a" "-LC:\Users\Nikolai\AppData\Local\Temp\arduino_build_869229" -lm
"C:\Users\Nikolai\Desktop\arduino-1.8.5-windows\arduino-1.8.5\hardware\tools\avr/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0  "C:\Users\Nikolai\AppData\Local\Temp\arduino_build_869229/sketch_apr12a.ino.elf" "C:\Users\Nikolai\AppData\Local\Temp\arduino_build_869229/sketch_apr12a.ino.eep"
"C:\Users\Nikolai\Desktop\arduino-1.8.5-windows\arduino-1.8.5\hardware\tools\avr/bin/avr-objcopy" -O ihex -R .eeprom  "C:\Users\Nikolai\AppData\Local\Temp\arduino_build_869229/sketch_apr12a.ino.elf" "C:\Users\Nikolai\AppData\Local\Temp\arduino_build_869229/sketch_apr12a.ino.hex"
Скетч использует 382 байт (0%) памяти устройства. Всего доступно 261120 байт.
Глобальные переменные используют 16 байт (0%) динамической памяти, оставляя 8176 байт для локальных переменных. Максимум: 8192 байт.
C:\Users\Nikolai\Desktop\arduino-1.8.5-windows\arduino-1.8.5\hardware\tools\avr/bin/avrdude -CC:\Users\Nikolai\Desktop\arduino-1.8.5-windows\arduino-1.8.5\hardware\MegaCore-master\avr/avrdude.conf -v -patmega2561 -cusbasp -Pusb -Uflash:w:C:\Users\Nikolai\AppData\Local\Temp\arduino_build_869229/sketch_apr12a.ino.hex:i 

avrdude: Version 6.3, compiled on Jan 17 2017 at 12:00:53
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Users\Nikolai\Desktop\arduino-1.8.5-windows\arduino-1.8.5\hardware\MegaCore-master\avr/avrdude.conf"

         Using Port                    : usb
         Using Programmer              : usbasp
         AVR Part                      : ATmega2561
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    10     8    0 no       4096    8      0  9000  9000 0x00 0x00
           flash         65    10   256    0 yes    262144  256   1024  4500  4500 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : usbasp
         Description     : USBasp, http://www.fischl.de/usbasp/

avrdude: auto set sck period (because given equals null)
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x1e9803 (probably m2561)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: auto set sck period (because given equals null)
avrdude: reading input file "C:\Users\Nikolai\AppData\Local\Temp\arduino_build_869229/sketch_apr12a.ino.hex"
avrdude: writing flash (382 bytes):

Writing | ################################################## | 100% 3.99s

avrdude: 382 bytes of flash written
avrdude: verifying flash memory against C:\Users\Nikolai\AppData\Local\Temp\arduino_build_869229/sketch_apr12a.ino.hex:
avrdude: load data flash data from input file C:\Users\Nikolai\AppData\Local\Temp\arduino_build_869229/sketch_apr12a.ino.hex:
avrdude: input file C:\Users\Nikolai\AppData\Local\Temp\arduino_build_869229/sketch_apr12a.ino.hex contains 382 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 2.47s

avrdude: verifying ...
avrdude: 382 bytes of flash verified

avrdude done.  Thank you.

as you wrote)) thanks for the help) but that's what writes arduin in response to this sketch. Tomorrow I am soldering the new board, there will be all capacitors in place, I will also use external quartz to definitely exclude all variants of the "wrong printed circuit board"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I apologize for the fuzzy photo, but in general everything is visble. And unfortunately, nothing works at all. Even the elementary light-emitting diode will not be able to blink ... on any of the ports. I xs what's up. It's somehow wildly sad

Last Edited: Mon. Apr 16, 2018 - 06:52 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

n this case sketches are loaded, fyuzes are read and written. Tried to light the LEDs and standard blink, as well as ddrd portd

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wouldn't life be easier if you simply got an Arduino? If you really need that many IO and that much flash get a "Mega".

 

Arduino boards just let you get on and design stuff without worrying about dodgy hardware.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That would be my advice too. Develop software on the Arduino MEGA2560. Final tests of the completed software on your 2561 target hardware.
.
The 2561 is just a 2560 with missing ports i.e. to fit in TQFP-64 package instead of TQFP-100.
.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

no, it does not work out, I have very strict limitations on the size of the case and its shape.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Maybe it's worth it ... I think it would be easier ... but that's only one thing we have about $ 10 ... not that I'm sorry, it's just that something can not happen again ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Our advice was for "project development" in a pleasant environment.    Size does not matter.   Convenience of using Serial, JTAG, ... is more important.   

 

Of course your final hardware will be made on a small pcb.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

From the "fuzzy photo" it seems that many of the pins near the crystal are shorting out.

 

Also I can see VCC being routed to the top half of the chip but not GND.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

UNikolai wrote:
I apologize for the fuzzy photo

Go on. You managed to get it in-focus in your previous thread:

 

https://www.avrfreaks.net/commen...

 

So try again until you get it.

 

Also, either get a good "square-on" shot, or add some other angles - so that we can clearly see everything.

 

Remember: this is the only way we get to see what hardware you have - so it's up to you to give us a good view!

 

Also, please post the schematic.

 

Maybe even the bare board layout.

 

Again, the better the info you provide, the more likely it is that you will get good replies.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have nothing and is not confined anywhere

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, that's better - but:

js wrote:
it seems that many of the pins near the crystal are shorting out.

I still can't see that clearly due to the angle of the photo - but it does seem to be the case.

 

That's why I asked for some other angles!

 

If you're not going to post the photos, then you're just going to have to inspect it yourself - and confirm that there are not shorts.

 

And what about the schematic?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But you see everything there. And the components are signed

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

UNikolai wrote:
But you see everything there.

But not clearly.

 

Again, it looks like you have solder bridges between pins 29 & 30 and 31 & 32 - but the photo is not clear enough to tell for sure.

 

Due to the angle, it looks like nearly all the pins on the 49-64 side could be shorted.

 

 

And the components are signed

The legends on your layout are hard to read - due to both the colour and being too crammed together - and make it hard to see the detail of the tracks.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...

Pages