Go Down

Topic: Setting Serial Data, Parity and Stop bits. (Read 21 times) previous topic - next topic

drspectro

Also, you can test as follows.
1) initialize the serial port as usual
2) set the baud/parity/stop bits
3) loop and send a string repeatedly, (delay a second each time makes it a little easier)

4) start your sketch, then EXIT the Arduino GUI.  Start Hyperterm you should receive your test string.  You should have to set hyperterm to match your serial settings.  Make sure you select the right com port in hyperterm.

Darren Chapman

#6
Jun 14, 2009, 05:55 am Last Edit: Jun 14, 2009, 05:56 am by dcbeast Reason: 1
Hi,

I hope you can help... What you have here looks like the solution to my problems.

I need to interface to a device that uses 2 stop bits, I went searching and found this post.

I fixed SerialExtension.h as shown here:
Code: [Select]
#include <WProgram.h>

#ifndef cbi
  #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
  #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

void SetSerial(long baud,char parity, int wordlength, int stop);


The corresponding SerialExtension.cpp is as follows:
Code: [Select]
#include <SerialExtension.h>

void SetSerial(long baud, char parity, int wordlength, int stop)
{
// set baud rate
// code lifted straight from wiring_serial.c
#if defined(__AVR_ATmega168__)
     UBRR0H = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8;
     UBRR0L = ((F_CPU / 16 + baud / 2) / baud - 1);

     // enable rx and tx
     sbi(UCSR0B, RXEN0);
     sbi(UCSR0B, TXEN0);

     // enable interrupt on complete reception of a byte
     sbi(UCSR0B, RXCIE0);

#else
     UBRRH = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8;
     UBRRL = ((F_CPU / 16 + baud / 2) / baud - 1);

     // enable rx and tx
     sbi(UCSRB, RXEN);
     sbi(UCSRB, TXEN);

     // enable interrupt on complete reception of a byte
     sbi(UCSRB, RXCIE);
#endif
     // defaults to 8-bit, no parity, 1 stop bit

//clear parity, stop bits, word length
//UCSR0B bit 2=0 for all wordlengths except 9
//Note: Serial.read routines wont work with 9 bit data as written
UCSR0C = UCSR0C & B11000001;
UCSR0B = UCSR0B & B11111011;  

//set parity
if ((parity == 'O')|(parity == 'o'))
 {
   UCSR0C = UCSR0C | B00110000;
 }
else if ((parity == 'E')|(parity == 'e'))
 {
   UCSR0C = UCSR0C | B00100000;
 }
else // ((parity == 'N')|(parity == 'n')))
 {
   UCSR0C = UCSR0C | B00000000;
 }

//set word length
if (wordlength == 5)
 {
   UCSR0C = UCSR0C | B00000000;
 }
else if (wordlength == 6)
 {
   UCSR0C = UCSR0C | B00000010;
  }
else if (wordlength == 7)
 {
   UCSR0C = UCSR0C | B00000100;
 }
else if (wordlength == 9)
 {
   UCSR0C = UCSR0C | B00000110;
   UCSR0B = UCSR0B | B00000100;
 }
else // (wordlength == 8)
 {
   UCSR0C = UCSR0C | B00000110;
 }

//set stop bits
if (stop == 1)
 {
   UCSR0C = UCSR0C | B00000100;
 }
else // (stop == 2)
 {
   UCSR0C = UCSR0C | B00000000;
 }
}




But I'm getting (kinda expected, from code inspection) the following compile errors:

Code: [Select]




SerialExtension.cpp: In function 'void SetSerial(long int, char, int, int)':
SerialExtension.cpp:19: error: 'UBRRH' was not declared in this scope
SerialExtension.cpp:20: error: 'UBRRL' was not declared in this scope
SerialExtension.cpp:23: error: 'UCSRB' was not declared in this scope
SerialExtension.cpp:23: error: 'RXEN' was not declared in this scope
SerialExtension.cpp:24: error: 'TXEN' was not declared in this scope
SerialExtension.cpp:27: error: 'RXCIE' was not declared in this scope
E:\Internet Downloads\arduino-0015-win\arduino-0015\hardware\cores\arduino\SerialExtension.cpp: In function 'void SetSerial(long int, char, int, int)':


E:\Internet Downloads\arduino-0015-win\arduino-0015\hardware\cores\arduino\SerialExtension.cpp:19: error: 'UBRRH' was not declared in this scope


E:\Internet Downloads\arduino-0015-win\arduino-0015\hardware\cores\arduino\SerialExtension.cpp:20: error: 'UBRRL' was not declared in this scope


E:\Internet Downloads\arduino-0015-win\arduino-0015\hardware\cores\arduino\SerialExtension.cpp:23: error: 'UCSRB' was not declared in this scope


E:\Internet Downloads\arduino-0015-win\arduino-0015\hardware\cores\arduino\SerialExtension.cpp:23: error: 'RXEN' was not declared in this scope


E:\Internet Downloads\arduino-0015-win\arduino-0015\hardware\cores\arduino\SerialExtension.cpp:24: error: 'TXEN' was not declared in this scope


E:\Internet Downloads\arduino-0015-win\arduino-0015\hardware\cores\arduino\SerialExtension.cpp:27: error: 'RXCIE' was not declared in this scope


I get the same results in versions 15 & 16 of the Arduino environment.

Help would be *greatly* appreciated.

Cheers,



Darren

drspectro

The stop bits code I posted is wrong anyway.......

Quick and dirty just
1) set baud rate with the usual command
2) add the following to your setup code:
UCSR0C = UCSR0C | B00001000;

I am pretty sure UCSR0C is predefined (note that is a ZERO not an O).
Word length is also UCSR0C (except for 9 bit).

I double checked with the AVR datasheet and the code I posted doesnt set stop bits right anyway.  Pretty embarrasing. Between the compile errors and that I am going to go retest.

Darren Chapman

#8
Jun 16, 2009, 02:14 pm Last Edit: Jun 16, 2009, 02:14 pm by dcbeast Reason: 1
Hey drspectro,

Thank you !!!!!!!!!!!!!!!!

I'm posting very simple code here for anyone else who hits this problem.  You don't need any other library, just the code below.

This is working built using Arduino 16 talking to hyper-terminal on Windows XP.

Code: [Select]


byte high = 0;

void setup()
{
 Serial.begin(9600);
 // 2 Stop-bits please!
 UCSR0C = UCSR0C | B00001000;
 pinMode(13, OUTPUT);
}

void loop()
{
 if (high == 0)
 {
   digitalWrite(13, HIGH);
   high = 1;
 }
 else
 {
   digitalWrite(13, LOW);
   high = 0;
 }
 if (high == 0)
 {
   Serial.print("HELLO"); // The text
 }
 else
 {
   Serial.print("WORLD"); // The text
 }
 delay(500);
}

drspectro

glad it worked

I still want to get the library fixed because it covers all the parameters at once.  I would hope that would be easier in the long run.....  Also dont be afraid to go straight to the AVR datasheet for stuff like this.  It is usually pretty clear which registers to mess with and you can look at the wiring code to figure out the syntax.

Go Up