NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Reading Serial Data into the ATMega 168

February 13, 2011
by Wads
Wads's Avatar

I'm trying to build a remote temperature sensor. The output of the sensor (built already) is a 4800 BAUD, 16 BIT digital stream. I'm trying to figue out how I can modify the temperature sensor code that came with my NERDKIT (UART enabled already) to read the Serial data and display it to the LCD.

Coding is not my strong point.

Thanks

February 13, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Wads,

The place to start if you are looking to change baud rates and get more fine grained control of the UART module is the datasheet. It starts on page 172, and its a little dense, but definitely worth reading. I assure you if you get through it once, lots of things will start making sense (especially after you dive back into your code).

Humberto

February 19, 2011
by Wads
Wads's Avatar

Ok, I've read the data sheet several times. Can someone please check my code below to confirm I am initializing the uart correctly. I simply modified the uart.c that cam with my nerdkit for 4800 baud and 9 data bits.

Thanks,

void uart_init() {
  // set baud rate
  UBRR0H = 0;
  UBRR0L = 191; // for 4800bps with 14.7456MHz clock
  // enable uart RX, TX, 9 Data Bits, and 9th Bit
  UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(1<<UCSZ02)|(1<<RXB80);
  // set 9N1 frame format
  UCSR0C =(1<<UCSZ01)|(1<<UCSZ00);
February 20, 2011
by Wads
Wads's Avatar

The new parameters aren't getting to the registers (I got rid of the (1<<RXB80). What else has to be done besides just changing the uart.c in the libnerdkits folder. Running the makefile in the tempsensor folder doesn't get the parameters into the registers. All parameters are the same as the original uart.c.

February 20, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Wads,

It looks like you are modifying things in the right place. Make sure you delete the old .hex and .o files in your libnerdkits folder to ensure that you are using the new version of your uart.c, and make sure your new code is calling uart_init().

Humberto

February 20, 2011
by Wads
Wads's Avatar

I wasn't calling uart_init(). Now I have the correct parameters in proper registers. Still not getting good data. It looks like the 9th bit is always high regardless, and the UDR0 is always zero. I think it is time to crack out the O-scope and verify my input stream again.

Thanks,

February 20, 2011
by Wads
Wads's Avatar

I am successfully reading 8 bits of data from my sensor circuit into the MCU. But the ninth bit is giving me a problem. The o-scope shows a 1 for bit 9, but the RXB80 bit is always low. I'm using the sample code from the data sheet for reception of 9 data bits. The UCSZ02,UCSZ01, and UCSZ00 bits are being represented correctly in the UCSR0B and UCSR0C registers. Anyone got experience reading 9 bits via the uart???

Thanks,

unsigned int uart_Receive( void ){

    unsigned int status, resh, resl;

//* Wait for data to be received */

while ( !(UCSR0A & (1<<RXC0)) );

//* Get status and 9th bit, then data */
//* from buffer */

    status = UCSR0A;
    resh = UCSR0B;
    resl = UDR0;

//* If error, return -1 */

    if ( status & ((1<<FE0)|(1<<DOR0)|(1<<UPE0)))

    return -1;

//* Filter the 9th bit, then return */

    resh = (resh >> 1) & 0x01;

    return ((resh << 8) | resl);
February 21, 2011
by 6ofhalfdozen
6ofhalfdozen's Avatar

Just wondering.. the sensor outputs 16bit but you are trying to work with 9bits of data? Is there a particular reason you are trying to send 9bits instead of 8bit groups? I don't know much about your sensor, but most of the sensors I have worked with send in 8N1 format; which is 8 data bits, no parity bits, and one stop bit. the fact that the 9th bit is always high sounds a lot like its trying to send the stop bit instead of a data bit. I am probably totally off base, I don't have access to my datasheets now to check your setup on the mcu, but it might be something to consider.

February 21, 2011
by Wads
Wads's Avatar

I only needed 7 bits for the temp data and was trying to add 2 bits for an RT address so I could have multiple transmitters. I've abandoned that idea and just using 2 8 bit words, one for data and one RT address. My BAUD rate was a little off. I started monitoring the Frame error bit and saw that I was getting frame errors. Made an adjustment and the temperature data is being successfully read ito the MCU.

Thanks for the post.

February 23, 2011
by Wads
Wads's Avatar

My temperature sensor circuit is sending 1 byte for RT address and 1 byte for the temperature. What I want to do is to read the RT Address, decide what the RT address is, and then assign the temperature value that follows to the appropriate variable. I'm trying to monitor the temperature in various rooms of my house from a central location. I don't know how to read the bytes individually. Right now, sometimes I print the RT address to the LCD, sometimes I print the temperature.

Thanks

Post a Reply

Please log in to post a reply.

Did you know that SPDT stands for "Single Pole, Double Throw"? Learn more...