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
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)?