Need Help: UART Problem

General discussion on mikroC PRO for PIC.
Author
Message
kill3rb3e
Posts: 31
Joined: 26 Jan 2010 06:52

Need Help: UART Problem

#1 Post by kill3rb3e » 25 Feb 2010 02:54

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
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.
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

zdavesf
Posts: 43
Joined: 10 May 2007 03:52

Re: Need Help: UART Problem

#2 Post by zdavesf » 25 Feb 2010 23:18

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
PICFlash2, PIC16f877a, 18F4550, PIC16F883, PIC16F72. Electronics Tech trying hard to be a programmer...

kill3rb3e
Posts: 31
Joined: 26 Jan 2010 06:52

Re: Need Help: UART Problem

#3 Post by kill3rb3e » 26 Feb 2010 05:38

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;
}
}
}

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

Re: Need Help: UART Problem

#4 Post by MARIO » 26 Feb 2010 11:53

Code: Select all

do{ //test if the program is running
PORTB = ~PORTB
}while(i<5);;
Where is variable 'i' incremented in order to end the loop? If 'i' do not change to reach 5, it will run forever.
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.
I didn't test but I think PORTB will be 0xFF after this loop and even if the UART code is Ok, PORTB will not show because it stays 0xFF.
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.

kill3rb3e
Posts: 31
Joined: 26 Jan 2010 06:52

Re: Need Help: UART Problem

#5 Post by kill3rb3e » 26 Feb 2010 12:56

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;
}
}
}

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

Re: Need Help: UART Problem

#6 Post by MARIO » 26 Feb 2010 15:36

Ok. But you did not do 2 things: :roll:

1. PORTB=0X00; just after the do while loop.
2. declare temp_rcv like I said.

Code: Select all

unsigned char temp_rcv[3];
BR.

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

Re: Need Help: UART Problem

#7 Post by MARIO » 27 Feb 2010 16:54

Any progress?

kill3rb3e
Posts: 31
Joined: 26 Jan 2010 06:52

Re: Need Help: UART Problem

#8 Post by kill3rb3e » 28 Feb 2010 07:57

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

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
uart_test.PNG (33.36 KiB) Viewed 4390 times

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

Re: Need Help: UART Problem

#9 Post by MARIO » 28 Feb 2010 16:12

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.

kill3rb3e
Posts: 31
Joined: 26 Jan 2010 06:52

Re: Need Help: UART Problem

#10 Post by kill3rb3e » 01 Mar 2010 02:43

Hi. I follow your advice the other day with

Code: Select all

unsigned char* temp_rcv[3];
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:

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);;
}
The result:
Attachments
uart1.PNG
uart1.PNG (32.7 KiB) Viewed 4368 times

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

Re: Need Help: UART Problem

#11 Post by MARIO » 01 Mar 2010 05:03

Ok. Now I've tested in hardware and got something.
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);
}
By the way, you wrote this:

Code: Select all

unsigned char* temp_rcv[3];
but I said to write this

Code: Select all

unsigned char temp_rcv[3];
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.

Code: Select all

unsigned char temp_rcv;
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.
image1.JPG
image1.JPG (48.55 KiB) Viewed 4365 times
Good luck.

kill3rb3e
Posts: 31
Joined: 26 Jan 2010 06:52

Re: Need Help: UART Problem

#12 Post by kill3rb3e » 01 Mar 2010 06:55

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

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

Re: Need Help: UART Problem

#13 Post by MARIO » 01 Mar 2010 11:55

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

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

Re: Need Help: UART Problem

#14 Post by MARIO » 01 Mar 2010 12:32

Ok.

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);
I got this:
image2.PNG
image2.PNG (36.18 KiB) Viewed 4355 times
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):
The topic or post you requested does not exist (it was removed)

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

Re: Need Help: UART Problem

#15 Post by anikolic » 01 Mar 2010 14:29

Hi,
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
Best regards,
Aleksandar
Web Department Manager

Post Reply

Return to “mikroC PRO for PIC General”