Need Help: UART Problem
Need Help: UART Problem
Hi. Im really confused and my head is cracking why my UART is not functioning well. I copied the UART sample code and schematic diagram based from the samples from the HELP tool of MikroC Pro for PIC 2009. I also remodify the code and the schematic just to test if its really working but still not working. I also even triple check the hardware connections to check if there were shorted or open tracks, parts placement and connections and all of them were correct based from the schematic diagram. I also find out that when I unplugged the MAX232, PORTB LED is blinking and when I returned it back and RESET the MCU, the LED isnt blinking anymore. Please help me.
My Code:
/*
* Project name:
UART (Simple usage of UART module library functions)
* Description:
This code tests the UART functions.
* Test configuration:
MCU: PIC18F452
Oscillator: XT, 08.0000 MHz
Ext. Modules: -
SW: mikroC PRO for PIC
http://www.mikroe.com/en/compilers/mikroc/pro/pic/
*PORT Description
PORTD = reserve for LCD Module
PORTB = reserve for Keypad Module
PORTA = reserve for sensors
*/
void main(){
ADCON1 = 7;
CMCON = 7; //Disable comparators
PORTC = 0;
TRISC = 0x80; //set RC7 as input
PORTB = 0;
TRISB = 0;
PORTB = ~PORTB; //Test if program is running
UART1_Init(9600);
Delay_ms(300); //warm up the UART
UART1_Write_text("MikroElektronika");
}
Hyperterminal Settings:
Bits for seconds = 9600
Data bits = 8
Parity = None
Stop Bits = 1
Flow Control = None
My Code:
/*
* Project name:
UART (Simple usage of UART module library functions)
* Description:
This code tests the UART functions.
* Test configuration:
MCU: PIC18F452
Oscillator: XT, 08.0000 MHz
Ext. Modules: -
SW: mikroC PRO for PIC
http://www.mikroe.com/en/compilers/mikroc/pro/pic/
*PORT Description
PORTD = reserve for LCD Module
PORTB = reserve for Keypad Module
PORTA = reserve for sensors
*/
void main(){
ADCON1 = 7;
CMCON = 7; //Disable comparators
PORTC = 0;
TRISC = 0x80; //set RC7 as input
PORTB = 0;
TRISB = 0;
PORTB = ~PORTB; //Test if program is running
UART1_Init(9600);
Delay_ms(300); //warm up the UART
UART1_Write_text("MikroElektronika");
}
Hyperterminal Settings:
Bits for seconds = 9600
Data bits = 8
Parity = None
Stop Bits = 1
Flow Control = None
- Attachments
-
- Schematic of my UART module. LED from PORTB is just to check if the program is running. The 74154 is for the motor controller.
- Uart Schematic module.PNG (32.55 KiB) Viewed 4470 times
Re: Need Help: UART Problem
Maybe a silly suggestion, but you are using a straight through cable correct? not a null modem cable.
have you checked the UART box on the right side of your screen in library manager?
are the project settings correct?
does it compile?
sorry i dont have a version of MikroC on this computer so i cant verify.
did you copy and paste your exact code? i find it odd that your LED is blinking with out any loops in your code.
if it flashes once then that is a different story.
Dave
have you checked the UART box on the right side of your screen in library manager?
are the project settings correct?
does it compile?
sorry i dont have a version of MikroC on this computer so i cant verify.
did you copy and paste your exact code? i find it odd that your LED is blinking with out any loops in your code.
if it flashes once then that is a different story.
Dave
PICFlash2, PIC16f877a, 18F4550, PIC16F883, PIC16F72. Electronics Tech trying hard to be a programmer...
Re: Need Help: UART Problem
Yes its blinking and working fine right now. I just figured out that the only problem is that I didnt put a 100nF ceramic caps b/w the vcc and gnd of the MAX232. Another problem occur to me is the UART1_Read() and UART1_Read_Text() function. I cant figured out how to use it. Can you help me? Objective is that when my MCU receives a string that contain "OK", PORTB will light on.
This was the code I have tried last night but still didnt work.
/*
* Project name:
UART (Simple usage of UART module library functions)
* Description:
This code tests the UART functions.
* Test configuration:
MCU: PIC18F452
Oscillator: XT, 08.0000 MHz
Ext. Modules: -
SW: mikroC PRO for PIC
http://www.mikroe.com/en/compilers/mikroc/pro/pic/
*PORT Description
PORTD = reserve for LCD Module
PORTB = reserve for Keypad Module
PORTA = reserve for sensors
*/
unsigned char* rcv, temp_rcv;
unsigned int i = 0;
void main(){
ADCON1 = 7;
CMCON = 7;
PORTB = 0;
TRISB = 0;
do{ //test if the program is running
PORTB = ~PORTB
}while(i<5);;
UART1_Init(9600); //set baud rate to 9600
Delay_ms(100); //give UART1 to warm up
UART1_Write_Text("OK");
Delay_ms(100);
if(UART1_Data_Ready()==1){
UART1_Read_Text(temp_rcv,"OK",10);
Delay_ms(100);
if(strcmp(temp_rcv,"OK")==0){ //compare the rcv data if match
PORTB = 0xFF;
}
}
}
This was the code I have tried last night but still didnt work.
/*
* Project name:
UART (Simple usage of UART module library functions)
* Description:
This code tests the UART functions.
* Test configuration:
MCU: PIC18F452
Oscillator: XT, 08.0000 MHz
Ext. Modules: -
SW: mikroC PRO for PIC
http://www.mikroe.com/en/compilers/mikroc/pro/pic/
*PORT Description
PORTD = reserve for LCD Module
PORTB = reserve for Keypad Module
PORTA = reserve for sensors
*/
unsigned char* rcv, temp_rcv;
unsigned int i = 0;
void main(){
ADCON1 = 7;
CMCON = 7;
PORTB = 0;
TRISB = 0;
do{ //test if the program is running
PORTB = ~PORTB
}while(i<5);;
UART1_Init(9600); //set baud rate to 9600
Delay_ms(100); //give UART1 to warm up
UART1_Write_Text("OK");
Delay_ms(100);
if(UART1_Data_Ready()==1){
UART1_Read_Text(temp_rcv,"OK",10);
Delay_ms(100);
if(strcmp(temp_rcv,"OK")==0){ //compare the rcv data if match
PORTB = 0xFF;
}
}
}
Re: Need Help: UART Problem
Code: Select all
do{ //test if the program is running
PORTB = ~PORTB
}while(i<5);;
So the part of the code that uses UART never executes...
Code: Select all
do{ //test if the program is running
PORTB = ~PORTB
}while(i++<5); //increments variable 'i' too.
PORTB=0x00; // it is to be sure PORTB will change later to 0xFF if UART code works.
Please use CODE markers to let the code more readable.....
BR.
EDIT: let temp_rcv be unsigned char temp_rcv[3]; Otherwise strcmp will fail.
Re: Need Help: UART Problem
Here is the revised code.
/*
* Project name:
UART (Simple usage of UART module library functions)
* Description:
This code tests the UART functions.
* Test configuration:
MCU: PIC16F877A
Oscillator: XT, 08.0000 MHz
Ext. Modules: -
SW: mikroC PRO for PIC
http://www.mikroe.com/en/compilers/mikroc/pro/pic/
*PORT Description
PORTD = reserve for LCD Module
PORTB = reserve for Keypad Module
PORTA = reserve for sensors
*/
unsigned char* rcv, temp_rcv;
unsigned int i = 0;
void main(){
ADCON1 = 7;
CMCON = 7;
PORTB = 0;
TRISB = 0;
do{ //test if the program starts running
PORTB = ~PORTB
i++;
}while(i<5);;
UART1_Init(9600); //set baud rate to 9600
Delay_ms(100); //give UART1 to warm up
UART1_Write_Text("OK"); //Sends OK to UART
Delay_ms(100);
if(UART1_Data_Ready()==1){
UART1_Read_Text(temp_rcv,"OK",10);
Delay_ms(100);
if(strcmp(temp_rcv,"OK")==0){ //compare the rcv data if match
PORTB = 0xFF;
}
}
}
/*
* Project name:
UART (Simple usage of UART module library functions)
* Description:
This code tests the UART functions.
* Test configuration:
MCU: PIC16F877A
Oscillator: XT, 08.0000 MHz
Ext. Modules: -
SW: mikroC PRO for PIC
http://www.mikroe.com/en/compilers/mikroc/pro/pic/
*PORT Description
PORTD = reserve for LCD Module
PORTB = reserve for Keypad Module
PORTA = reserve for sensors
*/
unsigned char* rcv, temp_rcv;
unsigned int i = 0;
void main(){
ADCON1 = 7;
CMCON = 7;
PORTB = 0;
TRISB = 0;
do{ //test if the program starts running
PORTB = ~PORTB
i++;
}while(i<5);;
UART1_Init(9600); //set baud rate to 9600
Delay_ms(100); //give UART1 to warm up
UART1_Write_Text("OK"); //Sends OK to UART
Delay_ms(100);
if(UART1_Data_Ready()==1){
UART1_Read_Text(temp_rcv,"OK",10);
Delay_ms(100);
if(strcmp(temp_rcv,"OK")==0){ //compare the rcv data if match
PORTB = 0xFF;
}
}
}
Re: Need Help: UART Problem
Ok. But you did not do 2 things:
1. PORTB=0X00; just after the do while loop.
2. declare temp_rcv like I said.
BR.
1. PORTB=0X00; just after the do while loop.
2. declare temp_rcv like I said.
Code: Select all
unsigned char temp_rcv[3];
Re: Need Help: UART Problem
Any progress?
Re: Need Help: UART Problem
Hi. I have not yet tried the code coz a friend of mine borrowed the programmer I'm using. Anyway, do u know anything about the lever shifter by transistor (PIC to SIMCOM340D GSM/GPRS Module). I copied the pdf guide but its not working fine with me.
Guide: http://www.edaboard.com/viewtopic.php?t ... highlight=
I am working on sending a text message using MCU and GSM Module
FORMAT:
AT+CMGF=1<CR>
AT+CMGS="# to be send"<CR>
message<CTRL+Z>
I have tested this code with MC HyperTerminal and PC HyperTerminal and is working fine with me. It follows the format to send a message. And when I connect the PIC with the LEVER Shifter based from the guide, it doesnt work I mean I didnt receive any message. Can you help me? I attached the scematic diagram I am using. Tnx
Guide: http://www.edaboard.com/viewtopic.php?t ... highlight=
I am working on sending a text message using MCU and GSM Module
FORMAT:
AT+CMGF=1<CR>
AT+CMGS="# to be send"<CR>
message<CTRL+Z>
I have tested this code with MC HyperTerminal and PC HyperTerminal and is working fine with me. It follows the format to send a message. And when I connect the PIC with the LEVER Shifter based from the guide, it doesnt work I mean I didnt receive any message. Can you help me? I attached the scematic diagram I am using. Tnx
Code: Select all
/*
MCU: PIC16F877A
SW: MikroC Pro for PIC 2009
OSC: HS,18.432MHz
*/
void main(){
ADCON1 = 7; //set digital I/O
CMCON = 7; //disable comparators
PORTB = 0;
TRISB = 0; //set PORTB as outputs
for(i = 0; i<6; i++){
PORTB = ~PORTB; //test if the program starts running
Delay_ms(200);
}
UART1_Init(9600); //set baud rate 9600
Delay_ms(2000); //warm up UART
UART1_Write_Text("AT");
UART1_Write(0x0D); //send carriage return
Delay_ms(500);
UART1_Write_Text("AT+CMGF=1");
UART1_Write(0x0D);
Delay_ms(500);
UART1_Write_Text("AT+CMGS=");
UART1_Write(0x22); //send double quote
UART1_Write_Text("+639099915069"); //send the number
UART1_Write(0x22); //send double quote
UART1_Write(0x0D); //send carriage return
Delay_ms(500);
UART1_Write_Text("This is a test");
UART1_Write(0x1A); //send CTRL+Z
Delay_ms(1000);
PORTB = 0xFF; //test if the program is finished
Delay_ms(1000);
PORTB = 0;
}
- Attachments
-
- uart_test.PNG (33.36 KiB) Viewed 4390 times
Re: Need Help: UART Problem
Hi. I could help you but I do not know anything about SIMCOM340D GSM/GPRS Module (or any GSM/GPRS module).
Maybe you can provide me a link to the datasheet, as I could not get it. (I got the SIM300DZ_HD_V2.03)
One thing about warm up (300DZ) is waiting 2 to 3 seconds (your code is 2s) before sending any AT command. Have you tried 3s?
Apparentely, the level shifter looks ok to me. If you have an oscilloscope you can view if there is any signal on RX and TX lines and if they are OK. Or maybe the signals should be inverted like MAX232 does, your level shifter doesn't, just a guess.
Another important thing is that you are only sending but not receiving anything in your code. How do you get feedback of the GSM module? I suppose it is going to you cell phone. (Sorry but this may be a silly question, but like I said, I do not know anything about...). If this is the case, maybe some configuration of the module should be done before.
I think the better solution is if somebody already has experience with this and could help you better than me.
BR.
_________________
Living and learning.
Maybe you can provide me a link to the datasheet, as I could not get it. (I got the SIM300DZ_HD_V2.03)
One thing about warm up (300DZ) is waiting 2 to 3 seconds (your code is 2s) before sending any AT command. Have you tried 3s?
Apparentely, the level shifter looks ok to me. If you have an oscilloscope you can view if there is any signal on RX and TX lines and if they are OK. Or maybe the signals should be inverted like MAX232 does, your level shifter doesn't, just a guess.
Another important thing is that you are only sending but not receiving anything in your code. How do you get feedback of the GSM module? I suppose it is going to you cell phone. (Sorry but this may be a silly question, but like I said, I do not know anything about...). If this is the case, maybe some configuration of the module should be done before.
I think the better solution is if somebody already has experience with this and could help you better than me.
BR.
_________________
Living and learning.
Re: Need Help: UART Problem
Hi. I follow your advice the other day with
but cant received the expected output. When I sent two characters, it returns 6 ascii character. Please help
For the SIMCOM datasheet: http://www.edaboard.com/viewtopic.php?t ... highlight=
Here is the code I tried last night:
The result:
Code: Select all
unsigned char* temp_rcv[3];
For the SIMCOM datasheet: http://www.edaboard.com/viewtopic.php?t ... highlight=
Here is the code I tried last night:
Code: Select all
/*This project is to test the UART function of MikroC
MCU = PIC16F877A
SW: MikroC Pro for Pic 2009
OSC: HS, 18.432MHz
PORT DESCRIPTION:
PORTA: for sensors
PORTB: for keypad module
PORTC(RC0-RC5): Motor controller
PORTD: LCD Module
*/
unsigned char* temp_rcv[3];
unsigned int i = 0,j=0;
//void Write_Func(char *write){
// ctr = 0;
// for(ctr = 0; ctr < strlen(write); ctr++){
// UART1_Write(write[ctr]);
// }
//}
void main() {
ADCON1 = 7;
CMCON = 7;
PORTB = 0;
TRISB = 0;
for(i = 1; i<=5; i++){ //test if the program is running
PORTB = 0xFF;
Delay_ms(1000);
PORTB = 0;
Delay_ms(1000);
}
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(3000); // Wait for UART module to stabilize
UART1_Write_Text("START");
UART1_Write(0x0D); //send carriage return
Delay_ms(100);
do{
if(UART1_Data_Ready()==1){
for(j=0; j<3; j++){
temp_rcv[j] = UART1_Read(); //put the received data to temp_rcv array
}
for(i=0; temp_rcv[i]!='\0';i++){ //read the contents of temp_rcv
UART1_Write(temp_rcv[i]);
Delay_ms(40);
}
}
}while(1);;
}
- Attachments
-
- uart1.PNG (32.7 KiB) Viewed 4368 times
Re: Need Help: UART Problem
Ok. Now I've tested in hardware and got something.
This code works:
By the way, you wrote this:
but I said to write this
Did you notice the difference? You defined an array of pointers! Note also I have changed back to a single char variable, not an array.
The array was for UART1_Read_Text() function and it was prepared for receiving "OK", in your first code at this post.
This was the problem about receiving strange characters because you were sending the address of and not the character itself.
I think the problem is that you were reading 3 times the RX buffer and after the for loop ending, there isn't any NULL char at the end of received chars. The problem was in receiving what you sent.
I do not recall but there are some threads about replicating data received. Search this forum or the forum of the old version (non PRO mikroC) about this issue. Good luck.
This code works:
Code: Select all
/*This project is to test the UART function of MikroC
MCU = PIC16F877A
SW: MikroC Pro for Pic 2009
OSC: HS, 18.432MHz
PORT DESCRIPTION:
PORTA: for sensors
PORTB: for keypad module
PORTC(RC0-RC5): Motor controller
PORTD: LCD Module
*/
unsigned char temp_rcv; //******see text******
unsigned int i = 0,j=0;
//void Write_Func(char *write){
// ctr = 0;
// for(ctr = 0; ctr < strlen(write); ctr++){
// UART1_Write(write[ctr]);
// }
//}
void main() {
ADCON1 = 7;
CMCON = 7;
PORTB = 0;
TRISB = 0;
for(i = 1; i<=5; i++){ //test if the program is running
PORTB = 0xFF;
Delay_ms(1000);
PORTB = 0;
Delay_ms(1000);
}
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(3000); // Wait for UART module to stabilize
UART1_Write_Text("START");
UART1_Write(0x0D); //send carriage return
Delay_ms(100);
do{
if(UART1_Data_Ready()==1){ //*****modified - no for loops******
temp_rcv = UART1_Read(); //put the received data to temp_rcv array
UART1_Write(temp_rcv);
}
}while(1);
}
Code: Select all
unsigned char* temp_rcv[3];
Code: Select all
unsigned char temp_rcv[3];
Code: Select all
unsigned char temp_rcv;
This was the problem about receiving strange characters because you were sending the address of and not the character itself.
I think the problem is that you were reading 3 times the RX buffer and after the for loop ending, there isn't any NULL char at the end of received chars. The problem was in receiving what you sent.
I do not recall but there are some threads about replicating data received. Search this forum or the forum of the old version (non PRO mikroC) about this issue. Good luck.
Re: Need Help: UART Problem
I see the difference. What is the exact time or delay for the PIC to read a single character per seconds? Tnx anyway for helping me out. This would help me a lot to filter data sent by the GSM to my PIC.Did you read the datasheet of the SIMCOM I attached last I post a reply?
I read some articles and post from different site but there were so many lever shifter but I dont know what is good or suited for my SIMCOM340D.
I'll try to reprogram my code. Tnx
BR
I read some articles and post from different site but there were so many lever shifter but I dont know what is good or suited for my SIMCOM340D.
I'll try to reprogram my code. Tnx
BR
Re: Need Help: UART Problem
You're wellcome.
1. I don't know. But I think the delay is not the problem.
2. Sorry, I didn't read the datasheet because I was testing the code and it got late so I went to sleep.
3. I tested UART1_Read_Text() and I got some issues, but I have no time now to show you right now. It's early morning here and I got an appointment.
Try something else by yourself and maybe there is somebody that could help you better than me about GSM/GPRS, as I have no way to test it.
BR
1. I don't know. But I think the delay is not the problem.
2. Sorry, I didn't read the datasheet because I was testing the code and it got late so I went to sleep.
3. I tested UART1_Read_Text() and I got some issues, but I have no time now to show you right now. It's early morning here and I got an appointment.
Try something else by yourself and maybe there is somebody that could help you better than me about GSM/GPRS, as I have no way to test it.
BR
Re: Need Help: UART Problem
Ok.
With the code changed like this:
I got this:
Why the characters "34" after "OK" come first? EDIT: And why do they come? I thought the result is the string until "OK" is found (but not "OK"itself), so the char after that should be ignored...
It seems to work nice if "OK" are the last chars....Ignore the first received line once I was testing and there was chars as I sent only "12" before sending "12OK". Is this a bug? ME team?
The example in help file is exactly like this. BTW in help file there is no how variable "output" is declared....I supposed it is an array...
Another thing: why in the terminal "received" comes before what I sent, in this case?
BR.
EDIT: I got this message from edaboard forum (from the link you posted):
With the code changed like this:
Code: Select all
unsigned char temp_rcv[10];
......
do{
if (UART1_Data_Ready()==1){
UART1_Read_Text(temp_rcv,"OK",10);
UART1_Write_Text(temp_rcv);
}
}
}while(1);
It seems to work nice if "OK" are the last chars....Ignore the first received line once I was testing and there was chars as I sent only "12" before sending "12OK". Is this a bug? ME team?
The example in help file is exactly like this. BTW in help file there is no how variable "output" is declared....I supposed it is an array...
Another thing: why in the terminal "received" comes before what I sent, in this case?
BR.
EDIT: I got this message from edaboard forum (from the link you posted):
The topic or post you requested does not exist (it was removed)
Re: Need Help: UART Problem
Hi,
I couldn't recreate your issue here. But let's analyze this example together:
Best regards,
Aleksandar
I couldn't recreate your issue here. But let's analyze this example together:
Code: Select all
Sent: 12OK
Received: 12 // Received string before "OK". Nothing sent afterwards, so this is the only value that fills the array
Sent: 12OK
Received: 12 // Received string before "OK". Nothing sent afterwards, so this is the only value that fills the array
Sent: 12OK
Received: 12 // Received string before "OK". Nothing sent afterwards, so this is the only value that fills the array
Sent: 12OK34
Received: 12 // Received string "12", because it is before "OK". "34" sent afterwards, so this is the value that's left in the input buffer
Sent: 12OK34
Received: 3412 // Received the "34" leftover from previous reception, plus "12" from the new string. Another "34" left in the input buffer
Sent: 12OK34
Received: 3412 // Received the "34" leftover from previous reception, plus "12" from the new string. Another "34" left in the input buffer
Sent: 12OK
Received: 3412 // Received the "34" leftover from previous reception, plus "12" from the new string. Nothing else left in the input buffer
Sent: 12OK
Received: 12 // Received string before "OK". Nothing sent afterwards, so this is the only value that fills the array
Aleksandar
Web Department Manager