Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Pic usart 16f648a UART1_Ready problem

Status
Not open for further replies.

rlucian90

Junior Member level 1
Junior Member level 1
Joined
Sep 21, 2012
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,410
Hello guys.

I have a problem interfacing a phone with pic 16f648a.
The problem is when i try to sync the phone with the pic and after that i can send the command from pic to phone to send sms.
To sync the pic with the phone is very simple, i need to send AT until phone gives me a reply OK., so i thought the easiest way of doing this is using the UART1_READY function from the mikroC.
It's working this way sometimes but not always and i don't know why. Why does the UART1_READY function remains 0 if my phone send OK?
How can i make it?

Thank you so much.

Code:
while(1)
             {

             if(PORTB.B5==1)                                               //button pressed
             {                                                             //if it is still pressed after 50ms then
             Delay_ms(50);                                                 //send SMS
             if(PORTB.B5==1)
             {
             UART1_Init(9600);                                             //init UART
             Delay_ms(3000); 
                                                           //wait for stabilize
             while (!UART1_Data_Ready()) {
                                                            //sync with modem
                              sms_AT();

                              }
                   trash = UART1_Read();                     //clear buffer for next event
                   sms_mode();                                   //the text mode for sms
                   sms_nr();                                       //sms number to call
                   sms_msg();                                    //sms message to send



                   Delay_ms(100);                        //light LED
                   PORTB.B0=1;
                   Delay_ms(100);
                   PORTB.B0=0;
                   Delay_ms(1000);
               }                                           //button deboundcer end
               }                                          //button pressed end
            else
            {
            PORTB.B0=0;
            }
            }                                                 //While end

}
 

try to use this part of code

Code:
while(UART1_Data_Ready() == 0); //wait until data is ready for reading
sms_AT();

instead of

Code:
             while (!UART1_Data_Ready()) {
                                                            //sync with modem
                              sms_AT();

                              }
 

Thank you for the help. You mean, without the ; after the while right?

Code:
while(UART1_Data_Ready() == 0) //wait until data is ready for reading
sms_AT();

I tried the code and it's doing the same.
I can't understand why it's working the 1st or the 2nd time and then doesnt exit the loop.

The problem could be the MAX232? But i'm not sure about it cause when i send some chars from the computer the PIC got them but again only sometimes.

Quite sure the problem is something related with the UART buffer.
Should i put a condition something to read the text recieved and then compare to "OK" and if it's OK to continue?

Thank you so much.
 

no, i mean with semi colon ; like this

Code:
while(UART1_Data_Ready() == 0); //wait until data is ready for reading
sms_AT();

semi colon mean it wait until UART1_Data_Ready() == 1 then execute sms_AT();

try this and tell me the result
 

Thank you for the reply.
Now it doesn't sending anything, nor AT.
Maybe i should ask the phone for the OK reply and when i get a answer from the phone, put it in a char variable and then test it if it's OK then the SMS should be sent.
Can you help me with this please?
Thank you.
 

first use hyper terminal of computer to connect with phone and be sure it's ok. Then go to the second step use hyper terminal with micro. This best way to solve this problem
 

Well first i interfaced the phone with terminal and it was ok, i got reply and sent sms. Then i interfaced the phone with the micro and i recieved sms just once or twice.

The phone is a sagem myx-5m and the pic 16f648a.

This is the full code:
Code:
void sms_AT (){
UART1_Write_Text("AT");
                              UART1_Write(13); //Enter key = CF + LF
                              UART1_Write(10);
                              delay_ms(200);
}

void sms_mode(){
                               UART1_Write_Text("AT+CMGF=1");                        //text mode sms
                               UART1_Write(13); //Enter key = CF + LF
                               UART1_Write(10);
                               delay_ms(500);
}
void sms_nr(){
                                UART1_Write_Text("AT+CMGS=\"0720050752\"");        //phone number
                                UART1_Write(13); //Enter key = CF + LF
                                UART1_Write(10);
                                delay_ms(500);
}
void sms_msg(){
                                UART1_Write_Text("Senzor activ!");       //sms text
                                UART1_Write(0x1A); // <ctrl-z>
                                delay_ms(5000);
}
unsigned char trash[70];
     void main() {
      int i;

   UART1_Init(9600);              // Initialize UART module at 9600 bps
    Delay_ms(6000);                 // Wait for UART module to stabilize
     //CMCON = 0x07;
     TRISA = 0xFF;
     TRISB = 0xFE;
	 Delay_ms(100);                        //light LED
     PORTB.B0=1;
     Delay_ms(1000);
     PORTB.B0=0;
     while(1)
             {

             if(PORTB.B5==1)                                               //button pressed
             {                                                             //if it is still pressed after 50ms then
             Delay_ms(50);                                                 //send SMS
             if(PORTB.B5==1)
             {
             UART1_Init(9600);                                             //init UART
             Delay_ms(3000); 
                                                           //wait for stabilize

             while (!UART1_Data_Ready()) {
                                                            //sync with modem

                              sms_AT();

                              }
                  // trash = UART1_Read();                     //clear buffer for next event


                   sms_mode();
                   sms_nr();
                   sms_msg();



                   Delay_ms(100);                        //light LED
                   PORTB.B0=1;
                   Delay_ms(100);
                   PORTB.B0=0;
                   Delay_ms(1000);
               }                                           //button deboundcer end
               }                                          //button pressed end
            else
            {
            PORTB.B0=0;
            }
            }                                                 //While end

}
Below it's the connection between PIC and phone.
n6rfr.jpg


Thank you.
 
Last edited:

You received SMS once means code and baudrate is ok. Problem in your Modem or interface with uC. Post your exact circuit. Modem interfaces with uC using TTL or RS232?
 
This is the circuit.
2h4cm0h.jpg

Modem interfaces with Uc through MAX232 like in the schematic so i think it's TTL.
 

Same results. The problem isn't the button cause if i press the button the led lights. The problem is somehow the Uart_Ready function , that buffer that i don't know isn't cleared maybe or something. I think i need a syntax to extract the whole reply from the phone to empty the buffer, but i don't know how.
 

Try to comment out the second UART1_Init (in the main while loop), this is unnecessary I think.
Furthermore, the too long delay routines can cause errors too because the communication does not using any hw handshaking.
In my humble opinion ...
zuisti
PS:
the line
CMCON = 0x07;
is a must to switch off comparators
 
Last edited:
Are you trying to send SMS using Proteus or real hardware? If Proteus then you don't need to use MAX232 but you have to use COMPIM. Add these to lines before UART1_Init();


Code C - [expand]
1
2
TRISB.F1 = 1;
TRISB.F2 = 0;

 
why you connect in MAX232 14 with 7 and 13 with 8 ??

make the phone take the output direct from 14 & 13
 
After looking at your wiring diagram also seems to me that the MAX232 is totally unnecessary to use so, because in this case it is only reproducing the PIC TX RX signals (at TTL level). However, the phone's TX pin may be an open collector output, so try to apply a 10k pullup resistor on it.

The PIC's UART buffer may be cleared by some dummy reading of the RCREG internal register (eg i = RCREG; i = RCREG;).
 
@zuisti I eliminate the UART_INIT from the while loop and reduced the delay for every AT command and seems it works better. I recieved 5 SMS but when i pressed the button for the 6th one the led showed that the sms was sent but it doesnt. I connected the computer to see what is sending the uC and i saw that the part with AT was skipped so the UART_Ready was always 1 and sent to phone only the command to send SMS without being sync. I think i'm close.
@jayanth.devarayanadurga I'm trying on real hardware. Where to add those line in the while loop or before the INIT from the beginning?
@#MAAM# I connected that way to have a TTL signal for both phone and uC. Isn't the same thing if i connect it to 14 and 13 ? I thought those outputs are only for the computer which is +10V and -10V logical.
 

Add after

Code C - [expand]
1
TRISB = 0xFE;



Try this code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
void sms_AT (){
        UART1_Write_Text("AT");
        UART1_Write(13); //Enter key = CF + LF
        UART1_Write(10);
        delay_ms(1000);
}
 
void sms_mode(){
        UART1_Write_Text("AT+CMGF=1");                        //text mode sms
        UART1_Write(13); //Enter key = CF + LF
        UART1_Write(10);
        delay_ms(1000);
}
void sms_nr(){
        UART1_Write_Text("AT+CMGS=\"0720050752\"");        //phone number
        UART1_Write(13); //Enter key = CF + LF
        UART1_Write(10);
        delay_ms(2000);
}
void sms_msg(){
        UART1_Write_Text("Senzor activ!");       //sms text
        UART1_Write(0x0D);
        UART1_Write(0x1A); // <ctrl-z>
        UART1_Write(0x0D);
        delay_ms(3000);
}
 
unsigned char send_sms = 0;
 
void main(){
    
        
        TRISA = 0xFF;
        TRISB = 0xFA;
        
        PORTB.B0 = 1;
        Delay_ms(1000);
        PORTB.B0 = 0;
     
        UART1_Init(9600);              // Initialize UART module at 9600 bps
        Delay_ms(100);                 // Wait for UART module to stabilize
        
        while(1){
 
             if(PORTB.B5){                                               
                    Delay_ms(50);                                                 
                    if(PORTB.B5){
                            send_sms = 1;
                    }
             }
             
             if(send_sms){
                 
                    sms_AT();
                    sms_mode();
                    sms_nr();
                    sms_msg();
                    
                    PORTB.B0 = 1;
                    Delay_ms(1000);
                    PORTB.B0 = 0;
                    
                    send_sms = 0;                    
                 
             }                   
         }
}

 
Last edited:
@zuisti I eliminate the UART_INIT from the while loop and reduced the delay for every AT command and seems it works better. I recieved 5 SMS but when i pressed the button for the 6th one the led showed that the sms was sent but it doesnt. I connected the computer to see what is sending the uC and i saw that the part with AT was skipped so the UART_Ready was always 1 and sent to phone only the command to send SMS without being sync. I think i'm close.
@jayanth.devarayanadurga I'm trying on real hardware. Where to add those line in the while loop or before the INIT from the beginning?
@#MAAM# I connected that way to have a TTL signal for both phone and uC. Isn't the same thing if i connect it to 14 and 13 ? I thought those outputs are only for the computer which is +10V and -10V logical.

add sms_AT(); function again before sms_mode(); function and try again
 
Thank you for the code jayanth.devarayanadurga but id doesn't work. I need a loop sending AT for syncronize with phone and when i get OK reply from phone i know it's working. All seems random now, i don't know why. If i don't make the sync with phone i can't send the SMS. Any other ideas what i need to do?
@#MAAM# one AT command it doesn't work . I don't know why the phone is desyncronizing that fast but every command i need to seen must be only after i get the OK reply.
@zuisti i put the TX pullup 10k resistor from 5V to the max232 pin , same result.
I tried to make a clear for the RX buffer,it seems it worked but after the 1st OK no SMS is sending.
Code:
while (UART1_Data_Ready()) {                 //sync with modem
                              trash[i]=RCREG;
                              i=i+1;
                              }
                              i=0;
                              
                          //    UART1_Write_Text(trash);
                          //   UART1_Write(13); //Enter key = CF + LF         //those Uart_Writes were for me to see.
                           //    UART1_Write(10);

Also i interface the terminal and the uC. I pressed the switch, the AT loop started, i send OK, the uC send me the SMS AT code. I do it one more time and even i sent OK the loop AT keep going. I don't know why really.
Any ideas?

Thank you all of you for the help !
Lucian
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top