We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I am using a Atmel AT89C51 and I am having trouble doing serial communication. Here is what I am trying: #include <AT89X51.H> #include <STDIO.H> #include <intrins.h> int i,j; void main() { PCON |= 0x00; /* don't double baud rate */ SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */ TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */ TH1 = 253; /* TH1: reload value for 9600 baud @ 11.0592 MHz */ TR1 = 1; /* TR1: timer 1 run */ TI = 1; /* TI: set TI to send first char of UART */ do{ P1 = P2; printf("i got a pocket full"); putchar(0x20); for(j=0; j<255; j++); { for(i=0; i<255; i++) { _nop_(); _nop_(); } } printf("yyyyyyy\n\n"); }while(1); } Any suggestions as to what I should try would be appreciated. Thanks.
Precisely what trouble are you having? Please use the <pre> and </pre> tags (as directed!) when posting code!
I just want to have a simple program to print something out using hyperterminal. Also it would be nice to receive a character and echo it back. I have been able to do this in assembly but I can't get it to work in C. Here is the latest code I have tried: #include <AT89X51.H> #include <CTYPE.H> #include <STDIO.H> int i; void main() { SCON = 0x50; //SCON: mode 1, 8-bit UART, enable rcvr TMOD = 0x20; //TMOD: timer 1, mode 2, 8-bit reload TH1 = 0xFD; //TH1: reload value for 9600 baud @ 11.0592MHz TR1 = 1; //TR1: timer 1 run TI = 1; //TI: set TI to send first char of UART while (1) { P1 ^= 0x01; printf ("\n\nHello World\n\n"); i = _getkey(); putchar('$'); putchar(i); } } Is there anything wrong with my code? Or is there something in how I am compiling? I am confident my hardware is fine because I can get it to work in assembly. Any help would be appreciated. Thank you.
I think it should add PCON &=0x7F; TMOD |=0x20; to your init of Uart. Good luck.
Start with my one file uart.c driver at my website. It's free and debugged. Hope it helps. - Mark http://www.embeddedfw.com
Im not an C51 expert, but I do not see a pin set as output anywhere. Best regards, Niels
You don't need to set pins as output, they're all pseudo-bidirectional. You write out to them an they'll attempt to drive that level (port 0 is open drain so you'll need a pull-up, actually, I often pull all my ports up). To read a port just write a one to the pin and then read it. For the UART and TxD the pin just works as an output, nothing for you to do. It's not like the PIC.