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.
Zip and post your Proteus file and mikroC project files. I don't have time to draw it.

Try this.


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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
void sms_AT (){
        UART1_Write_Text("AT");
        UART1_Write(13); //Enter key = CF + LF
        UART1_Write(10);
        //delay_ms(2000);
}
 
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(13);
        UART1_Write(0x1A); // <ctrl-z>
        UART1_Write(13);        
}
 
unsigned char uart_rd[3], send_sms = 0;
unsigned int i = 0, m = 1;
 
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();
                    
                    // If data is ready, read it:
                    if (UART_Data_Ready() == 1){ 
                                uart_rd[i++] = UART_Read();
                                uart_rd[i] = '\0';
                    }
                    
                    m = strcmp(uart_rd, "OK");
                    
                    if(m == 0){
                    
                            sms_mode();
                            sms_nr();
                            sms_msg();
                    
                            PORTB.B0 = 1;
                            Delay_ms(1000);
                            PORTB.B0 = 0;
                    
                    }
                    
                    send_sms = 0;
                    m = 1;                    
                 
             }       
                
         }
}

 
Last edited:

Thank you so much for the help.
Here it is the project.

- - - Updated - - -

The code is nice but it has two problems:
1.i need an AT loop until the phone reply, one AT isn't sufficient.
2.if once the reply is OK then always after the AT command will send SMS even if reply isn't OK. i tested it on Proteus

- - - Updated - - -

This is the real hardware circuit:
IMAG0711.jpgIMAG0712.jpgIMAG0713.jpg
 

Attachments

  • 16f648a.rar
    40.8 KB · Views: 77

Try these codes. If these still doesn't work then you have to use Serial Interrupt code to receive the modem response.
 

Attachments

  • SMS PIC16F648A.rar
    32.1 KB · Views: 76
  • SMS PIC16F648A rev3.rar
    49.3 KB · Views: 93
  • SMS PIC16F648A rev5.rar
    33.2 KB · Views: 94
Last edited:

First:
Unfortunately I do not have Proteus v8 (only v7.10) so I cannot open your DSN.
But as I wrote I recommend completely remove the MAX232 ...
Second:
I absolutely not know the GSM communication, but based on what you read so far, this program I wrote . Try it!



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//zuisti's SMS program (not tried)
 
void sms_AT() {
     UART1_Write_Text("AT\x0D\x0A");
}
 
void sms_mode() {
     UART1_Write_Text("AT+CMGF=1\x0D\x0A"); //text mode sms
}
 
void sms_nr() {    
     UART1_Write_Text("AT+CMGS=\"0720050752\"\x0D\x0A"); //phone number
}
 
void sms_msg() {
     UART1_Write_Text("Senzor activ!\x1A"); //sms text + <ctrl-z>
}
 
 
unsigned char t;
 
//***********
 
void main() {
 
     CMCON = 0x07;       //switch off comparators
     PORTA = 0;          //all low, not used
     TRISA = 0;          //all output, not used
     TRISB = 0b00100010; //only bit 1 (Rx) and bit 5 (button) are inputs
     //PORTB bit 0 (LED), bit 1 (Rx), bit 2 (Tx) and bit 5 (button) are high:
     PORTB = 0b00100111;
 
     UART1_Init(19200);  // Initialize UART module at 19200 bps
     //LED lit for one sec and also wait for UART module to stabilize
     Delay_100ms();
     PORTB.B0 = 0; //switch off it
     
     while (1) { //main loop
 
           //wait for the button depressed
           if (!PORTB.B5)
              continue;      //if button does NOT pressed (pin is low)
 
           //button IS pressed (pin is high now)
           Delay_100ms();     //debounce time
           if (!PORTB.B5)
              continue;      //if it is NOT pressed after 100 ms, was a noise only
           
    //wait for the buttons's release
    while (PORTB.B5); //comment out if you want
           
           //now try to send the SMS in an infinite loop (more attempts)
           while (1) {
               //first send the empty AT command only
               sms_AT();
 
               //wait for the phone's response ("OK")
               t = 255; //with appr 510 ms timeout
               while (!UART1_Data_Ready() && t) { //sync with modem
                  Delay_ms(2);
                  --t;
               }
               if (!t)
                  continue; //timeout is occured, new attempt
 
               //receive (wait for) the "OK": two chars
               if ( UART1_Read() != 'O') {
                  Delay_100ms(); //time for other (wrong) char(s)
                  continue;
               }
               t = 10; //wait fot the second char with appr 20 ms timeout
               while (!UART1_Data_Ready() && t) {
                  Delay_ms(2);
                  --t;
               }
               if (!t)
                  continue; //timeout is occured, again
 
               if ( UART1_Read() == 'K')
                  break; //SUCCESS
               Delay_100ms(); //time for other (wrong) chars, new attempt
           }
           
           //phone response was OK, now send the SMS
           //and lit the LED for a while
           PORTB.B0 = 1; //switch on it
 
           sms_mode();
           Delay_100ms();
           sms_nr();
           Delay_100ms();
           sms_msg();
           Delay_100ms();
 
           PORTB.B0 = 0; //switch off the LED
     } //While loop end
} // END

 

do as jayanth.devarayanadurga and zuisti said then tell us the result
 

Ok. Guys thank you very much for the help , i will try the codes but tell me how to wire directly phone to uC without MAX232. RX>TX TX>RX and on the TX pin from the phone a 10k pullup to 5V and that's it?
Thank you !
 

tell me how to wire directly phone to uC without MAX232. RX>TX TX>RX and on the TX pin from the phone a 10k pullup to 5V and that's it?
Well, I do not know your phone, but I'm sure it does not tolerate the official RS232 voltages (+-12V), produced by the MAX232. That's why you used twice the MAX232 internal interface circuits now, which in turn is meaningless, since in this case it is only reproducing the PIC Tx Rx signals at the same (TTL) level.

In addition to the proposed PIC Rx pullup, for safe (I think the phone's power voltage is appr 3.3 V) apply a small value resistor (1 but max 4.7 k) in serial, between the PIC Tx and the phone Rx pins, but you have to try because a too large value may disturb the communication.

zuisti
 

Well then the MAX232 was a safe choice.
I measured with multimeter the phone rx and tx voltage it's 2.8-3V and the PIC is about 4.8V. So i think i should use the MAX232.
 

Please measure the voltage of your MAX232 pin 9 (used for the phone Rx) but with disconnected phone. I think it will be somewhere between 4 and 5 V (unloaded TTL level). Is this safe?
Look at my previous post!
 

I think your module can't work properly, because baud rate is not exactly.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top