Go Down

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

Jeff Craighead

Hi all, I've just started working with the Arduino and have not been able to find a way to set the Data, Parity and Stop bits for the serial libraries.  I've got a motor controller that needs 7-E-1, not 8-N-1.

Thanks!

drspectro

I wrote a library for this, I put off posting it because I wanted to add some more interesting stuff for error checking.   Its short so I am putting the code in line.  I didnt find this elsewhere, but its pretty simple so it may be on the site already.  Note if you dont want to use the library, you can simply lift the setserial sub.
FYI - I have worked out a couple of tricks to check for parity errors if you get into that.  The best one requires editing wiring_serial.c, but I found a kluge that can be done from a program.

File must be named SerialExtension.cpp
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;
 }
}



File must be named SerialExtension.h
Code: [Select]
#include <WConstants.h>
//#include <wiring_private.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);

#endif

Jeff Craighead

#2
Jun 02, 2009, 11:09 pm Last Edit: Jun 02, 2009, 11:36 pm by jeffcraighead Reason: 1
The #endif at the bottom of SerialExtension.h is missing the associated if, and if I remove it there are a few other errors because it looks like something is missing or redefining something.  What is the #if line at the top of the file?

I'm getting this error now:

In file included from /Applications/arduino-0015/hardware/cores/arduino/WProgram.h:4,

/Applications/arduino-0015/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/include/stdlib.h:111: error: expected unqualified-id before 'int'

/Applications/arduino-0015/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/include/stdlib.h:111: error: expected `)' before 'int'

/Applications/arduino-0015/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/include/stdlib.h:111: error: expected `)' before 'int'

In file included from /Applications/arduino-0015/hardware/cores/arduino/WProgram.h:6,


Thanks!


drspectro

Sorry for the syntax error.  I dont know if I grabbed a bad version from my archive or if it somehow compiled as-is.  Either way thanks for the catch.  


Go Up