I need to write a program for a project that receive data from serial (RX pin) using HC06 module and then turn a led on according to the received information. I'm using P89v51rd2 microcontroller. Here is the code I write but something is wrong
#include <reg51.h> #include <stdio.h> sbit con1 = 0xa0; void serial_init(void) //Setup the serial port for 9600 baud at 11.0592MHz. { 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 TL1 = 0xFD; TR1 = 1; //* TR1: timer 1 run TI = 1; //* TI: set TI to send first char of UART } void receive() //Function to receive serial data { unsigned int digit; //variable to hold the new character while(RI==0); //Wait for Receive interrupt flag { digit=SBUF; RI=0; //Clear eceive interrupt flag } if (digit==1) { con1=0; } else { con1=1; } void main() { while(1) { serial_init(); receive(); } }
where i'm wrong please help me
I'm sorry for the misunderstanding about the program, so let me explain what the program expected to do.
the program supposed to receive a character from the UART and not a binary value and then depend on the received character turn the led on at the port 0xa0 (P0.0).
the "serial_init" function I copied from the internet and the command "TI = 1;" i left by mistake, I do not want the program sending characters.
I made two change in the code but I still need your help
#include <reg51.h> #include <stdio.h> sbit con1 = 0xa0; void serial_init(void) //Setup the serial port for 9600 baud at 11.0592MHz. { 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 TL1 = 0xFD; TR1 = 1; //* TR1: timer 1 run //delete the TI = 1; } void receive() //Function to receive serial data { unsigned char digit; //variable to hold the new character while(RI==0); //Wait for Receive interrupt flag { digit=SBUF; RI=0; //Clear eceive interrupt flag } if (digit==1) { con1=0; } else { con1=1; } void main() { while(1) { serial_init(); receive(); } }
the "serial_init" function is it correct? what do I need to change in the program that the microcontoller know what character received? thank you very much for your help
So why do you still initialize the UART once per received character? Who have convinced you that this is required or even a good idea since you kept the code unchanged even after I already pointed it out?
Next thing - shouldn't you maybe pick up a book and try to read about the difference between the binary value 1, and the value of the character '1'? Haven't you looked in a ASCII table and considered exactly what value your computer will see when you press the digit '1' on the keyboard?
Quote "led on at the port 0xa0 (P0.0)"
You might want to also check this.
"So why do you still initialize the UART once per received character?" could you please point me for the line because I did not quite understand you
also I loot for ASCII table, so if I type 1 in terminal the micro see it as 31 in Hex? if so I need to write in the code "if (digit==0x31)"?
Mike, check what exactly? I dont think there is a problem with the port, the led power on if "0" in the port and off if "1".
What do you think the following code does?
while (1) { serial_init(); receive(); }
How many characters do you think receive() will look for before it exits?
How many times do you think you need to call serial_init()?
Aren't you trying to run the program in your head, or make use of pen and paper to try to understand exactly what your own code actually does?
Why do you want to play with a hexadecimal constant 0x31, if your goal is to check if the received character is a '1'? You have looked at character constants in a C language text book, haven't you? What would you say would be the main reason for the language to have support for character constants?
Note that the goal with text books is to read them from cover to cover. And then possibly restart from the beginning again to check if you missed something the first time. This is contrary to an encyclopedia, where you normally look up specific facts when needed. It will take forever to learn, if you only look for how C works after you have got stuck - how would you ever know if your code is good or not if you don't know what functionality the language actually has?
I meant, are you sure the address of P0.0 is 0xA0, and not something else (like 0x80)?