Beginners: please read this post and this post before posting to the forum.
0 Members and 1 Guest are viewing this topic.
#define F_CPU 1000000UL#define USART_BAUDRATE 9600#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)#include <avr/io.h>int main(void){ UCSR0B |= (1 << RXEN0) | (1 << TXEN0); //Turn ON transmission and reception circuitry UCSR0C |= (1 << UCSZ01) | (1 << UCSZ00); //Data length of 8-Bit UBRR0H = (BAUD_PRESCALE >> 8); //Load upper 8-bits of BAUD_PRESCALE value UBRR0L = BAUD_PRESCALE; void sendByte(uint8_t inputChar) { while((UCSR0A & (1 << UDRE0)) == 0){} //Do nothing until SEND register is empty UDR0 = inputChar; //Send value of "inputChar" } uint8_t receiveByte() { while((UCSR0A & (1 << RXC0)) == 0){} //Do nothing until data is received return UDR0; } while(1) { uint8_t newChar = receiveByte(); sendByte(newChar); }}
[All GUI stuff goes here] private void sendBtn_Click(object sender, EventArgs e) { if ((sendBox.Text != "") && (serialPort1.IsOpen)) { byte[] newByte = { Convert.ToByte(Convert.ToDecimal(sendBox.Text)) }; serialPort1.Write(newByte, 0, 1); System.Console.WriteLine("Byte sent!"); } } private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { System.Console.WriteLine("Byte received!"); byte[] newByte = new byte[1]; serialPort1.Read(newByte, 0, 1); getBox.Text = ((int)newByte[0]).ToString(); }
Code: [Select] void sendByte(uint8_t inputChar) { while((UCSR0A & (1 << UDRE0)) == 0){} //Do nothing until SEND register is empty UDR0 = inputChar; //Send value of "inputChar" } uint8_t receiveByte() { while((UCSR0A & (1 << RXC0)) == 0){} //Do nothing until data is received return UDR0; }[ ... ]
void sendByte(uint8_t inputChar) { while((UCSR0A & (1 << UDRE0)) == 0){} //Do nothing until SEND register is empty UDR0 = inputChar; //Send value of "inputChar" } uint8_t receiveByte() { while((UCSR0A & (1 << RXC0)) == 0){} //Do nothing until data is received return UDR0; }[ ... ]
while(! (UCSR0A & (1 << RXC0)) == 0) ;
Why are you defining functions inside of main(). Does that actually compile?
Have you tried just communication with hyperterm?
Quote from: joe61 on February 01, 2012, 07:28:03 PMWhy are you defining functions inside of main(). Does that actually compile?It does compile. You can write functions in a function making them accessible only to the parent function and not whole class
Quote from: joe61 on February 01, 2012, 07:28:03 PMHave you tried just communication with hyperterm?I have tried it with RealTerm (free alternative of Hyperterminal) and output was the same.Any other suggestions?
#include <stdio.h>#include <fcntl.h>int main(){ int i; int fd = open ("/dev/ttyUSB0", O_RDWR); if (fd == -1) { perror ("open"); return 1; } for (i=30; i<255; ++i) { char b; printf ("writing %d\n", i); write (fd, (unsigned char*)&i, 1); read (fd, &b, 1); printf ("read %d\n", b); }}
Byte sent = 2 --> in HEX = 02Byte received = 130 --> in HEX = 82
Solved!All that data misinterpretation was due to comm error as I tried Baud Rate of 9600 running uC @ 1MHz. I looked it up in ATmega48 datasheet and it stated that 9600 Baud Rate produces -7.0% Error when running @ 1MHz. Then I changed uC clock to 8MHz and Baud Rate (in uC and PC) to 250k (which according to datasheet produces 0.0% - and that solved the problem Thanks for Your help joe61
Actually I realize now that I didn't really run your code unmodified. I'm using a board that has a 20MHz resonator on it, and I changed F_CPU to match that.
According to datasheet 9600 Baud Rate @ 20MHz produces only 0.2% Error, that's why it gave good results.
Again, thanks for Your help.
Except that it didn't, until I got the sign right on the char.
In C one unsigned byte is char or uint8_t,
in C# it's simply called byte and that was what I was using from the start, so binary data interpretation was not the issue here.
No, it's implementation defined whether a char is signed or unsigned. [...] It's something to keep in mind.