| Author |
Message |
|
|
Posted: Mar 28, 2010 - 07:45 PM |
|

Joined: Mar 26, 2010
Posts: 6
|
|
say whatever u guys want to say.
in my present satate, I rebuild the hardware and performing communication successfully via serial port. now I want to change the communication algorithm, I don't know the programming well, so I need a good book on microcontroller UART communication programming in C. atleaset this much help u can do.
any link to ebook of microcontroller c programming will help a lot. i have downloaded 2 ebooks but they r not useful. |
|
|
| |
|
|
|
|
|
Posted: Mar 31, 2010 - 11:29 AM |
|

Joined: Feb 23, 2008
Posts: 162
Location: Bangkok
|
|
This tutorial is very clear, and I think I followed it correctly, but when I try to talk to the microcontroller it replies in Croatian...
I adapted the code to run in an ATTiny 2313 as this:
Code:
#define F_CPU 7372800
#include <avr/io.h>
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
int main (void)
{
char ReceivedByte;
UCSRB |= (1<<RXEN) | (1<<TXEN); // Enable USART Tx & Rx
UCSRC |= (1<<UCSZ0) | (1<<UCSZ1); // Set serial mode as 8 bit, 1 data bits
UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
while(1)
{
while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been recieved and is ready to be read from UDR
ReceivedByte = UDR; // Fetch the recieved byte value into the variable "ByteReceived"
while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
UDR = ReceivedByte; // Echo back the received byte back to the computer
}
}
Which is to say, pretty much a copy/paste of the code in the tutorial. I set the SUT_CKSEL (I'm using AVR Studio by the way) to use an external crystal oscilator, 3 to 8 Mhz, start up time 14CK + 4.1ms.
There's a 7.3728Mhz crystal in between pins 4 and 5, with two 22nF capacitors towards GND. Up to spec as far as I can see.
The problem is, when I try to get an echo using Br@y++ terminal, if I send "Hello", more often than not I get back something like "mlloÿ". Evidently something's got lost in translation here. By the way I'm using a FD232RL based DIY USB to serial thingy, it works just fine crossing the Tx and Rx lines so I don't think the problem is there.
A strange this is that I can't send single digit characters through the terminal, if I send "1" I get nothing, if I send "01" I get "1". Oddly, the binary echo from "01" is "00110001", "02" returns "00110010" and so on and so forth. Puzzling.
Any ideas where things may be getting screwy? |
|
|
| |
|
|
|
|
|
Posted: Mar 31, 2010 - 11:38 AM |
|

Joined: Feb 23, 2008
Posts: 162
Location: Bangkok
|
|
| I suppose I should make clear that I think the problem I have is an spurious half byte, regardless of what I send out, I always get bytes as this xx11xxxx. If there would be a frequency mismatch I would be expecting (not that I'm an expert to diagnose such things!) that the errors wouldn't be so consistent. |
|
|
| |
|
|
|
|
|
Posted: Mar 31, 2010 - 07:28 PM |
|

Joined: Nov 17, 2004
Posts: 13840
Location: Vancouver, BC
|
|
Well, it is certainly not the code since all it does is send out what it receives.
Quote:
Oddly, the binary echo from "01" is "00110001"
Why is that odd? 0x00110001 is the ASCII representation of the character '1'. The only thing to figure out is what is happening to the '0'. |
_________________ Regards,
Steve A.
The Board helps those that help themselves.
|
| |
|
|
|
|
|
Posted: Apr 01, 2010 - 01:03 AM |
|

Joined: Feb 23, 2008
Posts: 162
Location: Bangkok
|
|
|
Koshchi wrote:
Well, it is certainly not the code since all it does is send out what it receives.
Quote:
Oddly, the binary echo from "01" is "00110001"
Why is that odd? 0x00110001 is the ASCII representation of the character '1'. The only thing to figure out is what is happening to the '0'.
What's odd is that with any character I send out I get back xx11xxxx, as if doing "Byte |= 0b0011000;". |
|
|
| |
|
|
|
|
|
Posted: Apr 01, 2010 - 05:03 AM |
|

Joined: Sep 05, 2009
Posts: 51
|
|
| Can i transmit a temperature value using 'unsigned char' data type in the transmit function?Do i have to make it int? |
|
|
| |
|
|
|
|
|
Posted: Apr 01, 2010 - 02:24 PM |
|

Joined: Aug 14, 2009
Posts: 4
|
|
|
Quote:
What's odd is that with any character I send out I get back xx11xxxx, as if doing "Byte |= 0b0011000;".
What Steve means is that "1" is 0x31 in hex, 49 in decimal, and 0b00110001 in binary. In other words, your terminal program is sending chars/strings, not bytes, hexidecimals, or decimals. So, the fact that you are getting back 0bxx11xxxx is expected, you're just missing the first byte for some reason.
Something I noticed. If you OR 'H' with 'e' you get 'm'.
0b01001000 'H'
0b01100101 'e'
0b01101101 'm'
why your code would generate that, I don't know, but that's what I see. |
|
|
| |
|
|
|
|
|
Posted: Apr 16, 2010 - 04:24 AM |
|

Joined: Apr 16, 2010
Posts: 4
|
|
|
|
|
|
|
Posted: Apr 17, 2010 - 09:07 AM |
|

Joined: Oct 23, 2009
Posts: 5
|
|
| more of an electrical question, but i get errors sending except for when i touch the pin that on the max chip thats sending(pin 14). touching any other pin doesnt do anything. ive tried a bunch of different grounds. any ideas? |
|
|
| |
|
|
|
|
|
Posted: Apr 17, 2010 - 09:19 AM |
|


Joined: Jan 23, 2010
Posts: 840
Location: Edmonton, Alberta
|
|
| drunkentiger, touching the pin likely adds a small amount of capacitance to the pin, stabilizing the signal (or clock, if your finger is near XTAL or the potted processor itself). What clock source are you using, and baud rate? UART communication requires precise timing -- often more precise than what the default internal 1MHz clock can provide. |
|
|
| |
|
|
|
|
|
Posted: Apr 17, 2010 - 09:58 AM |
|

Joined: Oct 23, 2009
Posts: 5
|
|
| 14.7456 crystal. 20 pf caps. 2400 baud rate |
|
|
| |
|
|
|
|
|
Posted: Apr 24, 2010 - 07:28 AM |
|

Joined: Oct 23, 2009
Posts: 5
|
|
just incase someone else has problems,
dont leave the capacitor off from Vs- to gnd |
|
|
| |
|
|
|
|
|
Posted: Apr 29, 2010 - 01:26 AM |
|

Joined: Apr 29, 2010
Posts: 6
|
|
| I know responding to make a correction to the original post is rather silly. Just something I noticed. Shouldn't when setting UBRR the high byte be written first instead of the other way around? I'm using AVR ATMEGA164P and 644P and (soon to be PA) and at least on those chips if you write the low bit first it immediately counts the whole register as being written and trying to write the high bit afterward will just cause problems. Is this just something that is true of the chips I'm using or is the guide wrong? |
|
|
| |
|
|
|
|
|
Posted: Apr 29, 2010 - 09:48 AM |
|


Joined: Jul 18, 2005
Posts: 62299
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Quote:
Shouldn't when setting UBRR the high byte be written first instead of the other way around?
Nope, while some 16 bit registers on AVR have a specific read sequence to trigger a buffering mechanism there is no limit on writing most 16 bit register pairs. You can write UBRRH and UBRRL in whatever order you like. In fact it's pretty unusual for UBRRH to be anything but 0 so often it's simply a waste of time to rewrite the default 0 that it holds.
Cliff |
_________________
|
| |
|
|
|
|
|
Posted: May 09, 2010 - 08:24 AM |
|

Joined: Mar 24, 2010
Posts: 12
|
|
This is taken from the Atmega88 datasheet,
Code:
#define FOSC 1843200 // Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
void USART_Init( unsigned int ubrr)
{
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
//UCSR0A = 0x00;
/*Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 2stop bit */
UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) )
;
/* Put data into buffer, sends the data */
UDR0= data;
}
In my main,
Code:
while(1)
{
//PORTC=0x00;
USART_Transmit('p');
}
I connect it to window's hyper terminal via the STK500...
The rx and tx are connected to PD0 and PD1 repestively...
I disconnect the CTRL rs232, connect it to the spare rs232.
Setting on hyperterminal is
9600 baud
8 data bit
2 stop bit
none parity bit
flow control none
I got nothing from this setup, whats wrong? |
|
|
| |
|
|
|
|
|
Posted: May 09, 2010 - 12:49 PM |
|


Joined: Jul 18, 2005
Posts: 62299
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Quote:
I got nothing from this setup, whats wrong?
If you've read this or any of the other threads about using UART you will now know that 99.9% of the problems people have are because their AVR is not running at the clock speed they have calculated the UBRR value for. Your code above says:
Code:
#define FOSC 1843200 // Clock Speed
so have you really setup your STK500/AVR to run at 1.8432MHz? Did that include changing CKSEL fuses and clearing the CKDIV8 fuse? |
_________________
|
| |
|
|
|
|
|
Posted: May 11, 2010 - 03:17 PM |
|

Joined: Mar 24, 2010
Posts: 12
|
|
|
Quote:
so have you really setup your STK500/AVR to run at 1.8432MHz? Did that include changing CKSEL fuses and clearing the CKDIV8 fuse?
Hi clawson.
I have mine setup, default clock speed is 1000000...
Thats for Atmega88, that comes with a 8hz internal ocillator, by default, it is set to div8...
So I figure out that its 1mhz and it works sweet!
Cheers! |
|
|
| |
|
|
|
|
|
Posted: May 11, 2010 - 03:22 PM |
|


Joined: Jul 18, 2005
Posts: 62299
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
| But technically 9600 baud does not work on 1MHz?!? If you find it does it's simply because the inaccuracy of your internal oscillator has an error so far in the right direction to allow it - not a great design! |
_________________
|
| |
|
|
|
|
|
Posted: May 11, 2010 - 03:45 PM |
|

Joined: Mar 24, 2010
Posts: 12
|
|
I set it to 2400 baud rate...
Works fine, cheers! |
|
|
| |
|
|
|
|
|
Posted: May 27, 2010 - 04:45 PM |
|

Joined: May 17, 2010
Posts: 5
|
|
Hello everyone,
I am using the atmega8515 and can open up brays terminal and I believe everything is working fine. My questions are when i send a phrase like hello, it echoes it back, but when i check the hex that is being sent it doesn't match up with the ascii? my second question is why can i only open it up in brays terminal and not in hyper terminal.
Code:
#include <avr/io.h>
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((4000000 / (USART_BAUDRATE * 16UL))) - 1)
int main (void)
{
char ReceivedByte;
UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
UCSRC = (1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ0); /* Set frame format: 8data, 1stop bit */
UBRRL = BAUD_PRESCALE;
UBRRH = (BAUD_PRESCALE >> 8);
for(;;)
{
while((UCSRA & (1<< RXC)) == 0) {};
ReceivedByte = UDR; // fetch back byte
while ((UCSRA & (1 << UDRE)) == 0) {}; //Wait for UDR, when ready more data can be written
UDR = ReceivedByte; //echo back byte
}
}
|
|
|
| |
|
|
|
|
|