Author Topic: uart Library  (Read 2061 times)

0 Members and 1 Guest are viewing this topic.

Offline macdad-Topic starter

  • Robot Overlord
  • ****
  • Posts: 132
  • Helpful? 5
    • LoneWolf's Circuits
uart Library
« on: March 17, 2011, 02:33:14 PM »
Hey I've been fooling around with the UART and trying to get my bearings with it, but I've hit a snag. The simple program I wrote is to accept one key input from HyperTerminal and output the data through Port C. I have been going back and forth and haven't found an answer.

Should I use uartGetByte() or uartReceiveByte()? And do they return their data as an ASCII encoding(E.G the byte is returned 65 for A)?

Thanks

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: uart Library
« Reply #1 on: March 18, 2011, 02:23:32 AM »
Quote
Should I use uartGetByte() or uartReceiveByte()?
It would be helpful if you let us know which micro and which library you are using... ;) Webbotlib (version 1.28) uses uartGetByte() if that's any use.
Any library worth it's salt should have documentation that tells you which commands are available.  Take a peek at the documentation for the library you are using, if there is no clear information on the available commands then switch library!

Quote
...And do they return their data as an ASCII encoding(E.G the byte is returned 65 for A)?
If you sent ASCII data to your micro (which you did using HyperTerminal) you will get ASCII data returned. The code just grabs a byte from the buffer. It doesn't care about, or change, the encoding. You are correct, a received ASCII A has a decimal value of 65, but to relate it to port pin values you need to be thinking in binary/hex (0b01000001 / 0x41).





Offline macdad-Topic starter

  • Robot Overlord
  • ****
  • Posts: 132
  • Helpful? 5
    • LoneWolf's Circuits
Re: uart Library
« Reply #2 on: March 18, 2011, 09:09:39 AM »
I am using the WinAVR library for the Atmega88, I'll continue to test it to work out the bugs.

Thank you

Offline macdad-Topic starter

  • Robot Overlord
  • ****
  • Posts: 132
  • Helpful? 5
    • LoneWolf's Circuits
Re: uart Library
« Reply #3 on: March 20, 2011, 10:19:40 AM »
Ok, I'm still having problems. I'm using the UART library as provided by Admin in his UART tutorial. I thought I had solved the receive problem when I added the buffer initialization function, but that quieted the UART completely as the mcu didn't send any notification data to send the data. Basically the code is as follows, I send a single byte through HyperTerminal and the mcu outputs the data as a 4 Bit number(Only allowing the numbers 0-9) which is sent to a 7-Segmented decoder to display on an LED display:
Code: [Select]
//UART_Test 2011 Nick Winston
//Tests Tx and Rx with the built-in UART, outputs
//sent data on two-digit Numeric LED display.

//Headers
#include "global.h"
#include "stdint.h"
#include "io.h"
#include "iom88.h"
#include "uart.h"
#include "delay.h"
#include "interrupt.h"
#include "power.h"
#include "buffer.h"
#include "rprintf.h"

//Pin Definitions
#define RunButton PB0

//Main Task
int main(void) {
//IO Setup
DDRD = 0xFF;
DDRC = 0xFF;
DDRB = 0x00;

//Variable Setup
int UARTByteIn; //For accepting ASCII Input

//Disable unneccessary modules
power_adc_disable();
power_twi_disable();

//Program Start Button
while((PINB & _BV(RunButton)) != 0) {
PORTD=0b00100000; //Red
_delay_ms(250);
PORTD=0b01000000; //Green
_delay_ms(250);
}

PORTD=0b01000000;

uartInit();  // initialize UART
uartSetBaudRate(9600); // set UART baud rate
uartInitBuffers(); //Enable Buffers to read bytes from the Rx Buffer
rprintfInit(uartSendByte);
rprintf("Input data now, hit the A Key to shut down.\n");
while(UARTByteIn != 65) {
if(uartReceiveBufferIsEmpty() != TRUE) {
UARTByteIn=uartGetByte();
switch(UARTByteIn) {
case 48:
PORTC=0b00000000;
case 49:
PORTC=0b00000001;
case 50:
PORTC=0b00000010;
case 51:
PORTC=0b00000011;
case 52:
PORTC=0b00000100;
case 53:
PORTC=0b00000101;
case 54:
PORTC=0b00000110;
case 55:
PORTC=0b00000111;
case 56:
PORTC=0b00001000;
case 57:
PORTC=0b00001001;
}
uartFlushReceiveBuffer();
}
}
rprintf("End of Line");
PORTD=0b00000000;
}

Thank you

Offline macdad-Topic starter

  • Robot Overlord
  • ****
  • Posts: 132
  • Helpful? 5
    • LoneWolf's Circuits
Re: uart Library
« Reply #4 on: March 20, 2011, 01:38:40 PM »
Update: I put in a debug section and I think I have nailed down the core of the issue, but I still need help.

I added in this little nugget:
Code: [Select]
while(UARTByteIn != -1) {
UARTByteIn=uartGetByte();
}
if((PINB & _BV(RunButton)) == 0) {
rprintf("Sent: %d", UARTByteIn);
}
to replace:
Code: [Select]
UARTByteIn=uartGetByte();
And dropped the whole uartReceiveBufferIsEmpty condition check.

Something is definitely wrong with the UART Receive buffer, or I've coded something wrong, thanks again.

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: uart Library
« Reply #5 on: March 20, 2011, 02:58:59 PM »
Try
Code: [Select]
u08 myData;



if (uartReceiveByte( &myData )) {
    rprintf("received %c\r\n", myData);
}

And get rid of the uartInitBuffers() call, if you haven't already, as it is already called from uartInit().

Offline macdad-Topic starter

  • Robot Overlord
  • ****
  • Posts: 132
  • Helpful? 5
    • LoneWolf's Circuits
Re: uart Library
« Reply #6 on: March 22, 2011, 08:05:20 AM »
Thanks hopslink, that was the code I needed. Also turns out that HyperTerminal wasn't displaying the data sent by the MCU for some odd reason. I actually used the Debug Terminal on my BASIC Stamp IDE and it was able to fully send/receive data to/from the MCU. Might be some formatting problems in HyperTerminal that I'm not aware of, but thanks again.

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: uart Library
« Reply #7 on: March 22, 2011, 11:07:48 AM »
Glad to hear it's working. For HyperTerminal check that the flow control setting for your connection is set to 'none'. Better still switch to a more advanced terminal like RealTerm or TeraTerm.