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.

Uart Problem in Hi-tech compiler

Status
Not open for further replies.

samnang39

Full Member level 2
Joined
Oct 26, 2006
Messages
130
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,296
Location
Cambodia
Activity points
2,246
Dear all

I make program in hi-tech for Monitor Excavator when the excavator work it send SMS to my phone about the time and date. now i got problem
with Uart. i write code and simulation in proteus sofware. i create function for read SMS from user in order to set phone number date time and Report
i already test all function individually in simulation work find. and i start to call all function together to the main function the my Uart function not work
i don't know why?
after that i try to test in hardware not work too

i use hi-tech in Lite mode

this is my code
:???:

- - - Updated - - -

the code is :


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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
/*
 * File:   newmain.c
 * Author: samnang
 *
 * Created on November 18, 2012, 1:36 PM
 */
#include <htc.h>
 
__CONFIG(1, FCMDIS & IESODIS & XT);
__CONFIG(2, BORDIS & BORV45 & PWRTEN & WDTDIS & WDTPS1);
__CONFIG(3, CCP2RB3 & LPT1DIS & MCLRDIS & 0xFDFF);
__CONFIG(4, DEBUGDIS & XINSTDIS & LVPDIS & STVRDIS);
__CONFIG(5, 0xFFFF);
__CONFIG(6, 0xFFFF);
__CONFIG(7, 0xFFFF);
 
__EEPROM_DATA('1','2', '3', '4', '2', '4', '6','4');
__EEPROM_DATA('0','4','6','8','9','"','1','2');
__EEPROM_DATA('2','2','7','1','9','0','"','"');
//__EEPROM_DATA('0','4','6','8','9','"','2','4');
//__EEPROM_DATA('6','4','0','4','6','8','9','"');
#define _XTAL_FREQ 4000000
#define BAUD 9600
 
#define RPM RA0
/*
 *
 */
 
void initPic();
void initModem();
void Usart_char(char data);
void Usart_string(const char *s);
void Usart_str_enter(const char *s);
void Usart_read();
void delayMs(unsigned long Ms);
void _delay(unsigned long cycles);
void ReadSMS();
void SMSanaly();
void Update_password();
void Update_user();
void Update_date();
void write_date();
void findPhone();
void SMSstartEng(int user);
void SMSstopEng(int user);
void SMSstartNote();
void SMSstopNote();
void getDate();
void getCSQ();
struct {
    int ERROR ; // even
    int CMGR ;
    int CMGD ;  // even
    int CMGS ;  // even
    int UsartError ; // even
    int CCLK;
    int CSQ;
    int OK ;    // even
} Response;
struct {
    char phone[16];
    char time[9];       // for send information to the user
    char date[10];      // for send information to the user
    char sigq[3];
    char Password[10];
    char User[30];
    char RTC[20];       // get from SMS
    char tmpRTC[23];    // for send to set time and date to the modem
}tmpData;
 
int SMSleng,tmpSMSleng,y;
int i,End,x; // variable for Usart Read
    
char Datain[400];
void initPic()
{
    TRISA   =   0b00001111;
    TRISB   =   0b00000000;
    TRISC   =   0b00000000;
    INTCON  =   0b00000000;
    INTCON2 =   0b00000000;
    INTCON3 =   0b00000000;
    T0CON   =   0b00000010;
    PIE1    =   0b00100000;
    ADCON1  =   0b00001111;
    USBEN   =   0;
    // Usart configer
    TXSTA   =   0b10100100;
    RCSTA   =   0b10010000;
    SPBRG=(int)(_XTAL_FREQ/(16.0*BAUD)-1);
}
void main(void)
{int i=0;
    char tmp; int y,step;
    initPic();
    initModem();
    step = 0;
   
    while(1)
    {
 
       switch(step)
     {
         
         case 0:
         {
             if(RPM == 0)
             {
                SMSstartEng(1);
                step = 1;
             }
             else
             {
                 step = 0;
             }
             break;
         }
         case 1:
         {
             while(RPM == 0)
             {
                 
             }
             i = 0;
             step = 2;
             break;
         }
         case 2:
         {
             while((RPM == 1)&&(i<=15))
             {
                 delayMs(1000);
 
                 i++;
             }
             if( i > 14)
             {
                SMSstopEng(1);
                step = 0;
                i = 0;
             }
             else
             {
                 step = 1;
                 i = 0;
             }
             i = 0;
             break;
         }
     }
    }
    
}
void initModem()
{
    Usart_str_enter("AT+CMGF=1");          // Set to text mode
    delayMs(1000);
    //Usart_str_enter("AT+CMGD=1,4");        // Delete all message
    delayMs(1000);
    Usart_str_enter("AT+CNMI=2,2,0,1,0");  // Set New Massage Indicater
    delayMs(1000);
    Usart_str_enter("ATE0");               // Echo mode off
    delayMs(1000);
}
void Usart_read(void)
{ 
     while(End == 0)
    {
        while((RCIF==0)&&(i<9001)) // Wait for start bit of Usart
        {
            _delay(10);
            i++;
        }
        if(RCIF = 1)
        {
            Datain[x] = RCREG;
            x++;
            //RCIF = 0;
        }
        if(i>9000)
        {
            End = 1;
        }
        i = 0;
    }
    SMSleng = (x-1);
}
 
void ReadSMS()
{
int i,step;//+CMS ERROR: 321
char tmp;
step = 0; i = 0;
    delayMs(100);
    Usart_str_enter("AT+CMGR=1");
    Usart_read();
    for(i=0;i<SMSleng;i++)
    {
        tmp = Datain[i];
        switch(step)
        {
            case 0:
                {
                    if(tmp == 'M')step = 1;
                    if(tmp == 'E')step = 10;
                    break;
                }
            case 1:
                {
                    if(tmp == 'G')step = 2;
                    else step = 0;
                    break;
                }
            case 2:
                {
                    if(tmp == 'R')step = 3;
                    else step = 0;
                    break;
                }
            case 3:
                {
                    if(tmp == ':')step = 4;
                    else step = 0;
                    break;
                }
            case 4:
                {
                    if(tmp == ' ')
                    {
                        step = 0;
                        findPhone(); // call funtion findPhone
                        i = 500; // exit for loop
                    }
                    break;
                }
            case 10:
                {
                    if(tmp == 'R')step = 11;
                    else step = 0;
                    break;
                }
            case 11:
                {
                    if(tmp == 'R')step = 12;
                    else step = 0;
                    break;
                }
            case 12:
                {
                    if(tmp == 'O')step = 13;
                    else step = 0;
                    break;
                }
            case 13:
                {
                    if(tmp == 'R')Response.ERROR = 1;
                    step = 0;
                    i = 22;
                    break;
                }
        }
    }
}
 
void findPhone()
{
    int i,step,x;//+CMS ERROR: 321   AD",
    char tmp;
    step = 0; i = 0; x = 0;
    for (i=0;i<SMSleng;i++)
    {
        tmp = Datain[i];
 
        switch(step)
        {
            case 0:
            {
                if(tmp == 'A' ){step = 1;}
                break;
            }
            case 1:
            {
                if(tmp == 'D') {step = 2;}
                else step = 0;
                break;
            }
            case 2:
            {
                if(tmp == '"'){step = 3;}
                else step = 0;
                break;
            }
            case 3:
            {
                if(tmp == ','){step = 4;x=0;}
                else step = 0;
                break;
            }
            case 4:
            {
                if(tmp == ',' )
                {
                    step = 0;
                    i = 500; // exit for loop
                    SMSanaly();
                }
                else
                {
                    tmpData.phone[x] = tmp;
                    x++;
                    step = 4;
                }
                break;
            }
        }
    }
}
void SMSanaly()
{
    //>ChangePW(1234,XXXX) //change Pass word
    //
    //>User(XXXX,0977105636,0241111111) //set user
    //>Report
    //>Date(XXXX,yy/MM/dd,hh:mm)   "yy/MM/dd,hh:mm:ss+07" // set Date
    //>SYS ON
    //>SYS OFF
    int i,step,x;//+CMS ERROR: 321
    char tmp;
    step = 0; i = 0; x = 0;
    for (i=0;i<SMSleng;i++)
    {
        tmp = Datain[i];
        switch(step)
        {
            case 0:
            {
                if     (tmp == 'C')step = 1;
                else if(tmp == 'U')step = 10;
                else if(tmp == 'R')step = 20;
                else if(tmp == 'D')step = 30;
                else step = 0;
                break;
            }
            case 1:
            {
                if(tmp == 'h'){step = 2;}
                else step = 0;
                break;
            }
            case 2:
            {
                if(tmp == 'a')step = 3;
                else step = 0;
                break;
            }
            case 3:
            {
                if(tmp == 'n')step = 4;
                else step = 0;
                break;
            }
            case 4:
            {
                if(tmp == 'g')step = 5;
                else step = 0;
                break;
            }
            case 5:
            {
                if(tmp == 'e')step = 6;
                else step = 0;
                break;
            }
            case 6:
            {
                if(tmp == 'P'){step = 7;}
                else step = 0;
                break;
            }
            case 7:
            {
                if(tmp == 'W'){step = 8;}
                else step = 0;
                break;
            }
            case 8:
            {
                if(tmp == '('){step = 9;x = 0;}
                else step = 0;
                break;
            }
            case 9:
            {
                if(tmp == ')')
                {
                    step = 0;
                    x = 0;
                    Update_password();
                }
                else
                {
                    tmpData.Password[x] = tmp;
                    x++;
                    step = 9;
                }
                break;
            }
            case 10:
            {
                if(tmp == 's')step = 11;
                else step = 0;
                break;
            }
            case 11:
            {
                if(tmp == 'e')step = 12;
                else step = 0;
                break;
            }
            case 12:
            {
                if(tmp == 'r')step = 13;
                else step = 0;
                break;
            }
            case 13:
            {
                if(tmp == '('){step = 14;x = 0;}
                else step = 0;
                break;
            }
            case 14:
            {
                if(tmp == ')')
                {
                    step = 0;
                    tmpData.User[x] = tmp;
                    Update_user();
                    x = 0;
                }
                else
                {
                    tmpData.User[x] = tmp;
                    x++;
                    step = 14;
                }
                break;
            }
            case 20:
            {
                if(tmp == 'e')step = 21;
                else step = 0;
                break;
            }
            case 21:
            {
                if(tmp == 'p') step = 22;
                else step = 0;
                break;
            }
            case 22:
            {
                if(tmp == 'o') step = 23;
                else step = 0;
                break;
            }
            case 23:
            {
                if(tmp == 'r') step = 24;
                else step = 0;
                break;
            }
            case 24:
            {
                if(tmp == 't')
                {
                    // Report();
                }
                step = 11;
                break;
            }
            case 30:
            {
                if(tmp == 'a') step = 31;
                else step = 0;
                break;
            }
            case 31:
            {
                if(tmp == 't') step = 32;
                else step = 0;
                break;
            }
            case 32:
            {
                if(tmp == 'e') step = 33;
                else step = 0;
                break;
            }
            case 33:
            {
                if(tmp == '(') {step = 34;x = 0;}
                else step = 0;
                break;
            }
            case 34:
            {
                if(tmp == ')')
                {
                    step = 0;
                    tmpData.RTC[x] = tmp;
                    x = 0;
                    Update_date();
                }
                else
                {
                    tmpData.RTC[x] = tmp;
                    x++;
                    step = 34;
                }
                break;
            }
        }
    }
}
void Update_date()
{
   int i,step,x,y;//+CMS ERROR: 321
   char tmp1,tmp2;
   step = 0; i = 0; x = 0;
   for(i=0;i<20;i++)
   {
       tmp1 = eeprom_read(i);
       delayMs(20);
       tmp2 = tmpData.RTC[i];
       switch(step)
       {
           case 0:
           {
               if(tmp1 == tmp2)
               {
                   step = 1;
               }
               else step = 0;
               break;
           }
           case 1:
           {
               if(tmp1 == tmp2)
               {
                   step = 2;
               }
               else step = 0;
               break;
           }
           case 2:
           {
               if(tmp1 == tmp2)
               {
                   step = 3;
               }
               else step = 0;
               break;
           }
           case 3:
           {
               if(tmp1 == tmp2)
               {
                   step = 4;
               }
               else step = 0;
               break;
           }
           case 4:
           {
               if(tmp2 == ',')
               {
                   step = 5;
                   x = 1;
                   tmpData.tmpRTC[0] = '"';
               }
               else step = 0;
               break;
           }
           case 5:
           {
               if(tmp2 == ')')
               {
                   step = 0;
                   //:00+07"
                   tmpData.tmpRTC[x] = ':';
                   tmpData.tmpRTC[x+1] = '5';
                   tmpData.tmpRTC[x+2] = '9';
                   tmpData.tmpRTC[x+3] = '+';
                   tmpData.tmpRTC[x+4] = '0';
                   tmpData.tmpRTC[x+5] = '7';
                   tmpData.tmpRTC[x+6] = '"';
 
                   write_date();    // write date to modem
               }
               else
               {
                   tmpData.tmpRTC[x] = tmp2;
                   x++;
                   step = 5;
               }
               break;
           }
       }
   }
}
void write_date()
{
    int i;
    char tmp;
    delayMs(100);
    Usart_string("AT+CCLK=");
    for(i=0;i<=21;i++)
    {
        tmp = tmpData.tmpRTC[i];
        Usart_char(tmp);
    }
    Usart_char(0x0D);
}
void Update_user()
{
   int i,step,x,y;//+CMS ERROR: 321
   char tmp1,tmp2;
   step = 0; i = 0; x = 0;
   for(i=0;i<29;i++)
   {
       tmp1 = eeprom_read(i);
       delayMs(20);
       tmp2 = tmpData.User[i];
       switch(step)
       {
           case 0:
           {
               if(tmp1 == tmp2)step = 1;
               break;
           }
           case 1:
           {
               if(tmp1 == tmp2)step = 2;
               else
                   step = 0;
               break;
           }
           case 2:
           {
               if(tmp1 == tmp2)step = 3;
               else
                   step = 0;
               break;
           }
           case 3:
           {
               if(tmp1 == tmp2)step = 4;
               else
                   step = 0;
               break;
           }
           case 4:
           {
               if(tmp2 == '0')
               {
                   step = 5;
                   x = 4;
               }
               else
                   step = 4;
               break;
           }
           case 5:
           {
               if((tmp2 == ',')||(tmp2 == ')'))
               {
                   if(tmp2 == ')')
                   {
                        i = 30; // exit loop
                        step = 0;
                   }
                   else
                   {
                        step = 6;
                        eeprom_write(x,'"');
                        delayMs(20);
                        x = 14;
                   }
               }
               else
               {
                   eeprom_write(x,tmp2);
                   delayMs(20);
                   x++;
                   step = 5;
               }
               break;
           }
           case 6:
           {
               if(tmp2 == '0')
               {
                   step = 7;
                   x = 14;
               }
               else
                   step = 0;
               break;
           }
           case 7:
           {
               if(tmp2 == ')')
               {
                   eeprom_write(x,'"');
                   delayMs(20);
                   step = 0;
                   x = 0;
                   i = 30; // exit loop
               }
               else
               {
                   eeprom_write(x,tmp2);
                   delayMs(20);
                   x++;
                   step = 7;
 
               }
 
               break;
           }
       }
   }
 
}
void Update_password()
{
   int i,step,x;//+CMS ERROR: 321
   char tmp1,tmp2;
   step = 0; i = 0; x = 0;
   for(i=0;i<=4;i++)
   {
       tmp1 = eeprom_read(i);
       delayMs(20);
       tmp2 = tmpData.Password[i];
       switch(step)
       {
           case 0:
           {
               if(tmp1 == tmp2)
               {
                   step = 1;
               }
               else i = 4;
               break;
           }
           case 1:
           {
               if(tmp1 == tmp2)
               {
                   step = 2;
               }
               else
               {
                   i = 4;
               }
               break;
           }
           case 2:
           {
               if(tmp1 == tmp2)
               {
                   step = 3;
               }
               else
               {
                   i = 4;
               }
               break;
           }
           case 3:
           {
               if(tmp1 == tmp2)
               {
                   step = 4;
                   x = 0;
               }
               else
               {
                   i = 4;
               }
               break;
           }
           case 4:
           {
               for(x=0;x<=3;x++)
               {
               tmp2 = tmpData.Password[(x+5)];
               eeprom_write(x,tmp2);
               delayMs(20);
               }
               break;
           }
       }
   }
 
  
}
void SMSstartNote()
{
    delayMs(100);
    Usart_str_enter("Engine   : Start");
    Usart_string("Time     : "); Usart_str_enter(tmpData.time);
    Usart_string("Date     : "); Usart_str_enter(tmpData.date);
    Usart_str_enter("Statue   : Run");
    Usart_string("Signal   : "); Usart_str_enter(tmpData.sigq);
    delayMs(50);
    Usart_char(0x1A); //Ctrl+Z
}
void SMSstopNote()
{
    delayMs(100);
    Usart_str_enter("Engine   : Stop");
    Usart_string("Time     : "); Usart_str_enter(tmpData.time);
    Usart_string("Date     : "); Usart_str_enter(tmpData.date);
    Usart_str_enter("Statue   : Stop");
    Usart_string("Signal   : "); Usart_str_enter(tmpData.sigq);
    delayMs(50);
    Usart_char(0x1A); //Ctrl+Z
}
void getCSQ()
{
    int i,step,x;//+CMS ERROR: 321   AD",
    char tmp;
    step = 0; i = 0; x = 0;
    delayMs(100);
    Usart_str_enter("AT+CSQ");
    Usart_read();
    delayMs(100);
    for (i=0;i<SMSleng;i++)
    {//+CSQ: 18,99
        tmp = Datain[i];
        switch(step)
        {
            case 0:
            {
                if(tmp == 'C')step = 1;
                break;
            }
            case 1:
            {
                if(tmp == 'S')step = 2;
                else step = 0;
                break;
            }
            case 2:
            {
                if(tmp == 'Q')step = 3;
                else step = 0;
                break;
            }
            case 3:
            {
                if(tmp == ':')step = 4;
                else step = 0;
                break;
            }
            case 4:
            {
                if(tmp == ' '){step = 5;x=0;}
                else step = 0;
                break;
            }
            case 5:
            {
                if(tmp == ',')
                {
                    x = 0;
                    i = 500; // exit from for loop
                }
                else
                {
                    tmpData.sigq[x] = tmp;
                    x++;
                }
            }
        }
    }
    
}
void getDate()
{
    int i,step,x;//+CMS ERROR: 321   AD",
    char tmp;
    step = 0; i = 0; x = 0;
    delayMs(100);
    Usart_str_enter("AT+CCLK?");
    Usart_read();
    delayMs(100);
    for (i=0;i<SMSleng;i++)
    {//+CCLK: "00/01/01,00:09:44+00"
        tmp = Datain[i];
        switch(step)
        {
            case 0:
            {
                if(tmp == 'C'){step = 1;}
                break;
            }
            case 1:
            {
                if(tmp == 'L')step = 2;
                else if (tmp == 'C') step = 1;
                else step = 0;
                break;
            }
            case 2:
            {
                if(tmp == 'K')step = 3;
                else step = 0;
                break;
            }
            case 3:
            {
                if(tmp == ':')step = 4;
                else step = 0;
                break;
            }
            case 4:
            {
                if(tmp == ' ')step = 5;
                else step = 0;
                break;
            }
            case 5:
            {
                if(tmp == '"'){step = 6;}
                else step = 0;
                break;
            }
            case 6:
            {
                if(tmp == ',')
                {
                    step = 7;
                    //tmpData.date[x] = tmp;
                    x = 0;
                }
                else
                {
                    tmpData.date[x] = tmp;
                    x++;
                    step = 6;
                }
                break;
            }
            case 7:
            {
                if(tmp == '+')
                {
                    step = 0;
                }
                else
                {
                    tmpData.time[x] = tmp;
                    x++;
                    step = 7;
                }
                break;
            }
        }
    }
   
}
void SMSstartEng(int user)
{
    int i;
    char tmp;
    delayMs(100);
    getDate();
    getCSQ();
 
    Usart_string("AT+CMGS=\"+855");
    if(user == 1)
    {
        i = 4;
    }
    else if(user == 2)
    {
        i = 14;
    }
    do
    {
        tmp = eeprom_read(i);
        delayMs(20);
        Usart_char(tmp);
        i++;
        if(i>30)
        tmp = '"';
    }while(tmp != '"');
    Usart_char(0x0d);
    SMSstartNote();
}
void SMSstopEng(int user)
{
    int i;
    char tmp;
    delayMs(100);
    getDate();
    getCSQ();
 
    Usart_string("AT+CMGS=\"+855");
    if(user == 1)
    {
        i = 4;
    }
    else if(user == 2)
    {
        i = 14;
    }
    do
    {
        tmp = eeprom_read(i);
        delayMs(20);
        Usart_char(tmp);
        i++;
        if(i>30)
        tmp = '"';
    }while(tmp != '"');
    Usart_char(0x0d);
    SMSstopNote();
}
void delayMs(unsigned long Ms)
{
    unsigned long i;
    i = 1;
    while(i<= Ms)
    {
        _delay(500);
        _delay(500);
        i++;
    }
}
void Usart_char(char data)
{
        while(TXIF ==0) continue;
        TXREG=data;
}
void Usart_string(const char *s)
{       
        while((*s)!='\0')
        {        
            while(!TXIF);         
            TXREG=(*s);          
            s++;
        }
}
void Usart_str_enter(const char *s)
{
    while((*s)!='\0')
        {          
            while(!TXIF);           
            TXREG=(*s);           
            s++;
        }
    Usart_char(0x0D);
}

 

Attachments

  • SmartExcavator.txt
    23.2 KB · Views: 62
Last edited by a moderator:

I don't have time to check your code, but I give my code.
Code:
	void uart_init(unsigned int baudrate)
	{
	TRISC7=1;
	TRISC6=0;
	#if defined (_16F877A)  || (_16F777)
		TXSTA=0b00100000;	
		RCSTA=0b10010000;
		if(baudrate<=38400){
			BRGH=0;
			SPBRG=(int)(_XTAL_FREQ/(64.0*baudrate)-1);}
		else{
			BRGH=1;
			SPBRG=(int)(_XTAL_FREQ/(16.0*baudrate)-1);}	
	#elif defined (_16F886) || (_16F887)
		TXSTA=0b00100000;	
		RCSTA=0b10010000;
		if(baudrate<=38400){
			BRGH=0;
			SPBRG=(int)(_XTAL_FREQ/(64.0*baudrate)-1);}
		else{
			BRGH=1;
			SPBRG=(int)(_XTAL_FREQ/(16.0*baudrate)-1);}
		BAUDCTL=0;
		SPBRGH=0;	
	#elif defined (_18F2550) || (_18F4550)
		TXSTA=0b00100000;	
		RCSTA=0b10010000;
		BAUDCON=0;
		SPBRG=(int)(_XTAL_FREQ/(64.0*baudrate)-1);
	#else
		#error No UART Library
	#endif
	}
	
	void uart_write(char data)
	{
	while(TXIF==0) continue;
	TXREG=data;
	}
	
	void uart_string(const char *s)
	{
	while(*s) uart_write(*s++);
	}
	
	void uart_number(unsigned int no, char base, char digit)
	{char i,di[10];
	for(i=0;i<=9;i++) di[i]=0;
	i=0;
	do{
		di[i]=no%base;
		no=no/base;
		i++;}
	while(no!=0);
	for(i=digit;i>0;i--){
		if(di[i-1]<=9) uart_write(di[i-1]+'0');
		else uart_write(di[i-1]-10+'A');}
	}
 
Last edited by a moderator:
Sorry I do not see uart read because my code have problem with my function urat read and to read without use interrupte function
 
Last edited:

Are you saying that problem is in
Code:
 void Usart_read(void)
{ 
while(End == 0)
{
while((RCIF==0)&&(i<9001)) // Wait for start bit of Usart
{
_delay(10);
i++;
}
if(RCIF = 1)
{
Datain[x] = RCREG;
x++;
//RCIF = 0;
}
if(i>9000)
{
End = 1;
}
i = 0;
}
SMSleng = (x-1);
}
?
 

Yes sure. When I test it in individual by simulation it work but when I call all function work together in main function it stop work
 

why this is commented out
Code:
 //RCIF = 0;
?

Can you post the code with the functions called in the while(1) loop? Please post code with code tags.
 
Last edited:

Because in datasheet write that this bit will clear by itself after we read from register pic 18f2550
 

Read the last post its updated. Are you having problem only in uart reading?
 
Yes in simulation problem only uart read only but in hardware not yet test
 

zip and post the hex file and simulation file.
this is my file include Protues file and C file i really thank you that you help me do you know i will graduate by this project
 

Attachments

  • SmartExcavator.rar
    16.4 KB · Views: 56

OK. I will try. I haven't used Hi-Tech C much. I use mikroC. You sen't .c file and not hex file.
Have you pulledup MCLR pin?
 

why you need hex file because you can compile it my hex file not work that is why i am not send to you. you can correct my code and compile to get hex file
 

OK. I will try. Tell me what problems you are facing when you call the functions from main function.
 

after i finish one function i always call to the main for test it work find. and i copy code and save it for backup and than i start call all function to the main in order to compete the project than all function work but Uart read not work. than i delete all code in main and start call one by one agian uart read not work too. so start to copy code that i backup and past it to my code work sheet Uart read work again. i try to call all for work together uart stop agian even i delete all code in main gain and call only uart read it is not work again.
 
Last edited:

So you mean to say all functions except uart_read() is working fine in main() function. Only uard_read() function is not working in the main() function. Is it right?

I am getting these error when I compile your code.

PHP:
Build D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550 for device 18F2550
Using driver F:\Program Files\HI-TECH Software\PICC-18\9.80\bin\picc18.exe

Make: The target "D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.p1" is out of date.
Executing: "F:\Program Files\HI-TECH Software\PICC-18\9.80\bin\picc18.exe" --pass1 "D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c" -q --chip=18F2550 -P --runtime=default --opt=default -D__DEBUG=1 --rom=default --ram=default -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" 
Error   [0] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 11.31 Cannot use literal values (0xFDFF) with __CONFIG(), use __PROG_CONFIG() instead
Error   [0] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 13.31 Cannot use literal values (0xFFFF) with __CONFIG(), use __PROG_CONFIG() instead
Error   [0] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 14.31 Cannot use literal values (0xFFFF) with __CONFIG(), use __PROG_CONFIG() instead
Error   [0] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 15.31 Cannot use literal values (0xFFFF) with __CONFIG(), use __PROG_CONFIG() instead
Warning [1385] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 1085.12 variable "RA0" is deprecated (declared at F:\Program Files\HI-TECH Software\PICC-18\9.80\include\pic18f2550.h:4671)
Warning [1385] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 1098.15 variable "RA0" is deprecated (declared at F:\Program Files\HI-TECH Software\PICC-18\9.80\include\pic18f2550.h:4671)
Error   [192] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 1102.1 undefined identifier "i"
Warning [1385] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 1108.16 variable "RA0" is deprecated (declared at F:\Program Files\HI-TECH Software\PICC-18\9.80\include\pic18f2550.h:4671)
Error   [192] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 1108.20 undefined identifier "i"
Warning [1385] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 1115.9 variable "RB2" is deprecated (declared at F:\Program Files\HI-TECH Software\PICC-18\9.80\include\pic18f2550.h:4691)
Warning [1385] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 1121.9 variable "RB2" is deprecated (declared at F:\Program Files\HI-TECH Software\PICC-18\9.80\include\pic18f2550.h:4691)
Error   [285] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 1131.1 no identifier in declaration
Warning [374] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 1131.1 missing basic type; int assumed
Error   [314] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 1131.1 ";" expected

********** Build failed! **********
 

yes only uart read not work but what i said in simuluation . the real not yet test

- - - Updated - - -

copy it again here

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
#include <htc.h>
 
__CONFIG(1, FCMDIS & IESODIS & XT);
__CONFIG(2, BORDIS & BORV45 & PWRTEN & WDTDIS & WDTPS1);
__CONFIG(3, CCP2RB3 & LPT1DIS & MCLRDIS & 0xFDFF);
__CONFIG(4, DEBUGDIS & XINSTDIS & LVPDIS & STVRDIS);
__CONFIG(5, 0xFFFF);
__CONFIG(6, 0xFFFF);
__CONFIG(7, 0xFFFF);
 
__EEPROM_DATA('1','2', '3', '4', '2', '4', '6','4');
__EEPROM_DATA('0','4','6','8','9','"','1','2');
__EEPROM_DATA('2','2','7','1','9','0','"','"');
//__EEPROM_DATA('0','4','6','8','9','"','2','4');
//__EEPROM_DATA('6','4','0','4','6','8','9','"');
#define _XTAL_FREQ 4000000
#define BAUD 9600
 
#define RPM RA0
/*
 *
 */
 
void initPic();
void initModem();
void Usart_char(char data);
void Usart_string(const char *s);
void Usart_str_enter(const char *s);
void Usart_read();
void delayMs(unsigned long Ms);
void _delay(unsigned long cycles);
void ReadSMS();
void SMSanaly();
void Update_password();
void Update_user();
void Update_date();
void write_date();
void findPhone();
void SMSstartEng(int user);
void SMSstopEng(int user);
void SMSstartNote();
void SMSstopNote();
void getDate();
void getCSQ();
struct {
    int ERROR ; // even
    int CMGR ;
    int CMGD ;  // even
    int CMGS ;  // even
    int UsartError ; // even
    int CCLK;
    int CSQ;
    int OK ;    // even
} Response;
struct {
    char phone[16];
    char time[9];       // for send information to the user
    char date[10];      // for send information to the user
    char sigq[3];
    char Password[10];
    char User[30];
    char RTC[20];       // get from SMS
    char tmpRTC[23];    // for send to set time and date to the modem
}tmpData;
 
int SMSleng,tmpSMSleng,y;
char Datain[400];
void initPic()
{
    TRISA   =   0b00001000;
    TRISB   =   0b00000001;
    TRISC   =   0b10000000;
    INTCON  =   0b00000000;
    INTCON2 =   0b00000000;
    INTCON3 =   0b00000000;
    T0CON   =   0b00000010;
    PIE1    =   0b00100000;
    ADCON1  =   0b00001111;
    USBEN   =   0;
    // Usart configer
    TXSTA   =   0b10100100;
    RCSTA   =   0b10010000;
    SPBRG=(int)(_XTAL_FREQ/(16.0*BAUD)-1);
}
void main(void)
{
    char tmp; int y,step,i;
    initPic();
    initModem();
    while(1)
    {
switch(step)
     {
 
         case 0:
         {
             if(RPM == 0) // excavator start
             {
                SMSstartEng(1); // send sms start to User about start time date signal quality of gsm module
                step = 1;
             }
             else
             {
                 step = 0;
             }
             break;
         }
         case 1:
         {
             while(RPM == 0)
             {
 
             }
             i = 0;
             step = 2;
             break;
         }
         case 2:
         {
             while((RPM == 1)&&(i<=15))// wait 15s after excavator stop if over 15s will send stop sms
             {
                 delayMs(1000);
 
                 i++;
             }
             if( i > 14) // over 15s
             {RB2 = 0;
                SMSstopEng(1); //send sms stop to User about stop time date signal quality of gsm module
                step = 0;
                i = 0;
             }
             else // not over 15s go to wait stop again
             {RB2 = 0;
                 step = 1;
                 i = 0;
             }
             i = 0;
             break;
         }
     }
    }
    }
 
void initModem()
{
    Usart_str_enter("AT+CMGF=1");          // Set to text mode
    delayMs(1000);
    Usart_str_enter("AT+CMGD=1,4");        // Delete all message
    delayMs(1000);
    Usart_str_enter("AT+CNMI=2,2,0,1,0");  // Set New Massage Indicater
    delayMs(1000);
    Usart_str_enter("ATE0");               // Echo mode off
    delayMs(1000);
}
void Usart_read(void)
{
    int i,End,x; // variable for Usart Read
    End = 0; x = 0; i = 0;
     while(End == 0)
    {
        while((RCIF==0)&&(i<9001)) // Wait for start bit of Usart
        {
            _delay(10);
            i++;
        }
        if(RCIF = 1)
        {
            Datain[x] = RCREG;
            x++;
        }
        if(i>9000)
        {
            End = 1;
        }
        i = 0;
    }
    SMSleng = (x-1);
}
 
void ReadSMS()
{
int i,step;//+CMS ERROR: 321
char tmp;
step = 0; i = 0;
    delayMs(100);
    Usart_str_enter("AT+CMGR=1");
    Usart_read();
    for(i=0;i<SMSleng;i++)
    {
        tmp = Datain[i];
        switch(step)
        {
            case 0:
                {
                    if(tmp == 'M')step = 1;
                    if(tmp == 'E')step = 10;
                    break;
                }
            case 1:
                {
                    if(tmp == 'G')step = 2;
                    else step = 0;
                    break;
                }
            case 2:
                {
                    if(tmp == 'R')step = 3;
                    else step = 0;
                    break;
                }
            case 3:
                {
                    if(tmp == ':')step = 4;
                    else step = 0;
                    break;
                }
            case 4:
                {
                    if(tmp == ' ')
                    {
                        step = 0;
                        findPhone(); // call funtion findPhone
                        i = 500; // exit for loop
                    }
                    break;
                }
            case 10:
                {
                    if(tmp == 'R')step = 11;
                    else step = 0;
                    break;
                }
            case 11:
                {
                    if(tmp == 'R')step = 12;
                    else step = 0;
                    break;
                }
            case 12:
                {
                    if(tmp == 'O')step = 13;
                    else step = 0;
                    break;
                }
            case 13:
                {
                    if(tmp == 'R')Response.ERROR = 1;
                    step = 0;
                    i = 22;
                    break;
                }
        }
    }
}
 
void findPhone()
{
    int i,step,x;//+CMS ERROR: 321   AD",
    char tmp;
    step = 0; i = 0; x = 0;
    for (i=0;i<SMSleng;i++)
    {
        tmp = Datain[i];
 
        switch(step)
        {
            case 0:
            {
                if(tmp == 'A' ){step = 1;}
                break;
            }
            case 1:
            {
                if(tmp == 'D') {step = 2;}
                else step = 0;
                break;
            }
            case 2:
            {
                if(tmp == '"'){step = 3;}
                else step = 0;
                break;
            }
            case 3:
            {
                if(tmp == ','){step = 4;x=0;}
                else step = 0;
                break;
            }
            case 4:
            {
                if(tmp == ',' )
                {
                    step = 0;
                    i = 500; // exit for loop
                    SMSanaly();
                }
                else
                {
                    tmpData.phone[x] = tmp;
                    x++;
                    step = 4;
                }
                break;
            }
        }
    }
}
void SMSanaly()
{
    //>ChangePW(1234,XXXX)
    //
    //>User(XXXX,0977105636,0241111111)
    //>Report
    //>Date(XXXX,yy/MM/dd,hh:mm)   "yy/MM/dd,hh:mm:ss+07"
    //>SYS ON
    //>SYS OFF
    int i,step,x;//+CMS ERROR: 321
    char tmp;
    step = 0; i = 0; x = 0;
    for (i=0;i<SMSleng;i++)
    {
        tmp = Datain[i];
        switch(step)
        {
            case 0:
            {
                if     (tmp == 'C')step = 1;
                else if(tmp == 'U')step = 10;
                else if(tmp == 'R')step = 20;
                else if(tmp == 'D')step = 30;
                else step = 0;
                break;
            }
            case 1:
            {
                if(tmp == 'h'){step = 2;}
                else step = 0;
                break;
            }
            case 2:
            {
                if(tmp == 'a')step = 3;
                else step = 0;
                break;
            }
            case 3:
            {
                if(tmp == 'n')step = 4;
                else step = 0;
                break;
            }
            case 4:
            {
                if(tmp == 'g')step = 5;
                else step = 0;
                break;
            }
            case 5:
            {
                if(tmp == 'e')step = 6;
                else step = 0;
                break;
            }
            case 6:
            {
                if(tmp == 'P'){step = 7;}
                else step = 0;
                break;
            }
            case 7:
            {
                if(tmp == 'W'){step = 8;}
                else step = 0;
                break;
            }
            case 8:
            {
                if(tmp == '('){step = 9;x = 0;}
                else step = 0;
                break;
            }
            case 9:
            {
                if(tmp == ')')
                {
                    step = 0;
                    x = 0;
                    Update_password();
                }
                else
                {
                    tmpData.Password[x] = tmp;
                    x++;
                    step = 9;
                }
                break;
            }
            case 10:
            {
                if(tmp == 's')step = 11;
                else step = 0;
                break;
            }
            case 11:
            {
                if(tmp == 'e')step = 12;
                else step = 0;
                break;
            }
            case 12:
            {
                if(tmp == 'r')step = 13;
                else step = 0;
                break;
            }
            case 13:
            {
                if(tmp == '('){step = 14;x = 0;}
                else step = 0;
                break;
            }
            case 14:
            {
                if(tmp == ')')
                {
                    step = 0;
                    tmpData.User[x] = tmp;
                    Update_user();
                    x = 0;
                }
                else
                {
                    tmpData.User[x] = tmp;
                    x++;
                    step = 14;
                }
                break;
            }
            case 20:
            {
                if(tmp == 'e')step = 21;
                else step = 0;
                break;
            }
            case 21:
            {
                if(tmp == 'p') step = 22;
                else step = 0;
                break;
            }
            case 22:
            {
                if(tmp == 'o') step = 23;
                else step = 0;
                break;
            }
            case 23:
            {
                if(tmp == 'r') step = 24;
                else step = 0;
                break;
            }
            case 24:
            {
                if(tmp == 't')
                {
                    // Report();
                }
                step = 11;
                break;
            }
            case 30:
            {
                if(tmp == 'a') step = 31;
                else step = 0;
                break;
            }
            case 31:
            {
                if(tmp == 't') step = 32;
                else step = 0;
                break;
            }
            case 32:
            {
                if(tmp == 'e') step = 33;
                else step = 0;
                break;
            }
            case 33:
            {
                if(tmp == '(') {step = 34;x = 0;}
                else step = 0;
                break;
            }
            case 34:
            {
                if(tmp == ')')
                {
                    step = 0;
                    tmpData.RTC[x] = tmp;
                    x = 0;
                    Update_date();
                }
                else
                {
                    tmpData.RTC[x] = tmp;
                    x++;
                    step = 34;
                }
                break;
            }
        }
    }
}
void Update_date()
{
   int i,step,x,y;//+CMS ERROR: 321
   char tmp1,tmp2;
   step = 0; i = 0; x = 0;
   for(i=0;i<20;i++)
   {
       tmp1 = eeprom_read(i);
       delayMs(20);
       tmp2 = tmpData.RTC[i];
       switch(step)
       {
           case 0:
           {
               if(tmp1 == tmp2)
               {
                   step = 1;
               }
               else step = 0;
               break;
           }
           case 1:
           {
               if(tmp1 == tmp2)
               {
                   step = 2;
               }
               else step = 0;
               break;
           }
           case 2:
           {
               if(tmp1 == tmp2)
               {
                   step = 3;
               }
               else step = 0;
               break;
           }
           case 3:
           {
               if(tmp1 == tmp2)
               {
                   step = 4;
               }
               else step = 0;
               break;
           }
           case 4:
           {
               if(tmp2 == ',')
               {
                   step = 5;
                   x = 1;
                   tmpData.tmpRTC[0] = '"';
               }
               else step = 0;
               break;
           }
           case 5:
           {
               if(tmp2 == ')')
               {
                   step = 0;
                   //:00+07"
                   tmpData.tmpRTC[x] = ':';
                   tmpData.tmpRTC[x+1] = '5';
                   tmpData.tmpRTC[x+2] = '9';
                   tmpData.tmpRTC[x+3] = '+';
                   tmpData.tmpRTC[x+4] = '0';
                   tmpData.tmpRTC[x+5] = '7';
                   tmpData.tmpRTC[x+6] = '"';
 
                   write_date();    // write date to modem
               }
               else
               {
                   tmpData.tmpRTC[x] = tmp2;
                   x++;
                   step = 5;
               }
               break;
           }
       }
   }
}
void write_date()
{
    int i;
    char tmp;
    delayMs(100);
    Usart_string("AT+CCLK=");
    for(i=0;i<=21;i++)
    {
        tmp = tmpData.tmpRTC[i];
        Usart_char(tmp);
    }
    Usart_char(0x0D);
}
void Update_user()
{
   int i,step,x,y;//+CMS ERROR: 321
   char tmp1,tmp2;
   step = 0; i = 0; x = 0;
   for(i=0;i<29;i++)
   {
       tmp1 = eeprom_read(i);
       delayMs(20);
       tmp2 = tmpData.User[i];
       switch(step)
       {
           case 0:
           {
               if(tmp1 == tmp2)step = 1;
               break;
           }
           case 1:
           {
               if(tmp1 == tmp2)step = 2;
               else
                   step = 0;
               break;
           }
           case 2:
           {
               if(tmp1 == tmp2)step = 3;
               else
                   step = 0;
               break;
           }
           case 3:
           {
               if(tmp1 == tmp2)step = 4;
               else
                   step = 0;
               break;
           }
           case 4:
           {
               if(tmp2 == '0')
               {
                   step = 5;
                   x = 4;
               }
               else
                   step = 4;
               break;
           }
           case 5:
           {
               if((tmp2 == ',')||(tmp2 == ')'))
               {
                   if(tmp2 == ')')
                   {
                        i = 30; // exit loop
                        step = 0;
                   }
                   else
                   {
                        step = 6;
                        eeprom_write(x,'"');
                        delayMs(20);
                        x = 14;
                   }
               }
               else
               {
                   eeprom_write(x,tmp2);
                   delayMs(20);
                   x++;
                   step = 5;
               }
               break;
           }
           case 6:
           {
               if(tmp2 == '0')
               {
                   step = 7;
                   x = 14;
               }
               else
                   step = 0;
               break;
           }
           case 7:
           {
               if(tmp2 == ')')
               {
                   eeprom_write(x,'"');
                   delayMs(20);
                   step = 0;
                   x = 0;
                   i = 30; // exit loop
               }
               else
               {
                   eeprom_write(x,tmp2);
                   delayMs(20);
                   x++;
                   step = 7;
 
               }
 
               break;
           }
       }
   }
 
}
void Update_password()
{
   int i,step,x;//+CMS ERROR: 321
   char tmp1,tmp2;
   step = 0; i = 0; x = 0;
   for(i=0;i<=4;i++)
   {
       tmp1 = eeprom_read(i);
       delayMs(20);
       tmp2 = tmpData.Password[i];
       switch(step)
       {
           case 0:
           {
               if(tmp1 == tmp2)
               {
                   step = 1;
               }
               else i = 4;
               break;
           }
           case 1:
           {
               if(tmp1 == tmp2)
               {
                   step = 2;
               }
               else
               {
                   i = 4;
               }
               break;
           }
           case 2:
           {
               if(tmp1 == tmp2)
               {
                   step = 3;
               }
               else
               {
                   i = 4;
               }
               break;
           }
           case 3:
           {
               if(tmp1 == tmp2)
               {
                   step = 4;
                   x = 0;
               }
               else
               {
                   i = 4;
               }
               break;
           }
           case 4:
           {
               for(x=0;x<=3;x++)
               {
               tmp2 = tmpData.Password[(x+5)];
               eeprom_write(x,tmp2);
               delayMs(20);
               }
               break;
           }
       }
   }
 
   // output
//    Usart_str_enter("welcome to Update Password");
//        y = 0;
//        for (y=0;y<4;y++)
//        {
//            tmp1 = eeprom_read(y);
//            delayMs(20);
//            Usart_char(tmp1);
//        }
//        Usart_char(0x0d);
//
//        Usart_str_enter("End of Update Password");
    // end of output
}
void SMSstartNote()
{
    delayMs(100);
    Usart_str_enter("Engine   : Start");
    Usart_string("Time     : "); Usart_str_enter(tmpData.time);
    Usart_string("Date     : "); Usart_str_enter(tmpData.date);
    Usart_str_enter("Statue   : Run");
    Usart_string("Signal   : "); Usart_str_enter(tmpData.sigq);
    delayMs(50);
    Usart_char(0x1A); //Ctrl+Z
}
void SMSstopNote()
{
    delayMs(100);
    Usart_str_enter("Engine   : Stop");
    Usart_string("Time     : "); Usart_str_enter(tmpData.time);
    Usart_string("Date     : "); Usart_str_enter(tmpData.date);
    Usart_str_enter("Statue   : Stop");
    Usart_string("Signal   : "); Usart_str_enter(tmpData.sigq);
    delayMs(50);
    Usart_char(0x1A); //Ctrl+Z
}
void getCSQ()
{
    int i,step,x;//+CMS ERROR: 321   AD",
    char tmp;
    step = 0; i = 0; x = 0;
    delayMs(100);
    Usart_str_enter("AT+CSQ");
    Usart_read();
    delayMs(100);
    for (i=0;i<SMSleng;i++)
    {//+CSQ: 18,99
        tmp = Datain[i];
        switch(step)
        {
            case 0:
            {
                if(tmp == 'C')step = 1;
                break;
            }
            case 1:
            {
                if(tmp == 'S')step = 2;
                else step = 0;
                break;
            }
            case 2:
            {
                if(tmp == 'Q')step = 3;
                else step = 0;
                break;
            }
            case 3:
            {
                if(tmp == ':')step = 4;
                else step = 0;
                break;
            }
            case 4:
            {
                if(tmp == ' '){step = 5;x=0;}
                else step = 0;
                break;
            }
            case 5:
            {
                if(tmp == ',')
                {
                    x = 0;
                    i = 500; // exit from for loop
                }
                else
                {
                    tmpData.sigq[x] = tmp;
                    x++;
                }
            }
        }
    }
     // output
//    Usart_str_enter("welcome to get CSQ");
//        y = 0;
//        for (y=0;y<2;y++)
//        {
//            tmp = tmpData.sigq[y];
//            Usart_char(tmp);
//        }
//        Usart_char(0x0d);
//
//        Usart_str_enter("End of CSQ");
    // end of output
}
void getDate()
{
    int i,step,x;//+CMS ERROR: 321   AD",
    char tmp;
    step = 0; i = 0; x = 0;
    delayMs(100);
    Usart_str_enter("AT+CCLK?");
    Usart_read();
    delayMs(100);
    for (i=0;i<SMSleng;i++)
    {//+CCLK: "00/01/01,00:09:44+00"
        tmp = Datain[i];
        switch(step)
        {
            case 0:
            {
                if(tmp == 'C'){step = 1;}
                break;
            }
            case 1:
            {
                if(tmp == 'L')step = 2;
                else if (tmp == 'C') step = 1;
                else step = 0;
                break;
            }
            case 2:
            {
                if(tmp == 'K')step = 3;
                else step = 0;
                break;
            }
            case 3:
            {
                if(tmp == ':')step = 4;
                else step = 0;
                break;
            }
            case 4:
            {
                if(tmp == ' ')step = 5;
                else step = 0;
                break;
            }
            case 5:
            {
                if(tmp == '"'){step = 6;}
                else step = 0;
                break;
            }
            case 6:
            {
                if(tmp == ',')
                {
                    step = 7;
                    //tmpData.date[x] = tmp;
                    x = 0;
                }
                else
                {
                    tmpData.date[x] = tmp;
                    x++;
                    step = 6;
                }
                break;
            }
            case 7:
            {
                if(tmp == '+')
                {
                    step = 0;
                }
                else
                {
                    tmpData.time[x] = tmp;
                    x++;
                    step = 7;
                }
                break;
            }
        }
    }
    // output
//    Usart_str_enter("welcome to get date");
//        y = 0;
//        for (y=0;y<9;y++)
//        {
//            tmp = tmpData.date[y];
//            Usart_char(tmp);
//        }
//        Usart_char(0x0d);
//        y = 0;
//        for (y=0;y<9;y++)
//        {
//            tmp = tmpData.time[y];
//            Usart_char(tmp);
//        }
//        Usart_char(0x0d);
//        Usart_str_enter("End of gettime");
    // end of output
}
void SMSstartEng(int user)
{
    int i;
    char tmp;
    delayMs(100);
    getDate();
    getCSQ();
 
    Usart_string("AT+CMGS=\"+855");
    if(user == 1)
    {
        i = 4;
    }
    else if(user == 2)
    {
        i = 14;
    }
    do
    {
        tmp = eeprom_read(i);
        delayMs(20);
        Usart_char(tmp);
        i++;
        if(i>30)
        tmp = '"';
    }while(tmp != '"');
    Usart_char(0x0d);
    SMSstartNote();
}
void SMSstopEng(int user)
{
    int i;
    char tmp;
    delayMs(100);
    getDate();
    getCSQ();
 
    Usart_string("AT+CMGS=\"+855");
    if(user == 1)
    {
        i = 4;
    }
    else if(user == 2)
    {
        i = 14;
    }
    do
    {
        tmp = eeprom_read(i);
        delayMs(20);
        Usart_char(tmp);
        i++;
        if(i>30)
        tmp = '"';
    }while(tmp != '"');
    Usart_char(0x0d);
    SMSstopNote();
}
void delayMs(unsigned long Ms)
{
    unsigned long i;
    i = 1;
    while(i<= Ms)
    {
        //_delay(500);
        _delay(250);
        i++;
    }
}
void Usart_char(char data)
{
        while(TXIF ==0) continue;
        TXREG=data;
}
void Usart_string(const char *s)
{
        // while(*s)
        //uart_transmit(*s++);
 
        while((*s)!='\0')
        {
            //Wait for TXREG Buffer to become available
            while(!TXIF);
            //Write data
            TXREG=(*s);
            //Next goto char
            s++;
        }
}
void Usart_str_enter(const char *s)
{
    while((*s)!='\0')
        {
            //Wait for TXREG Buffer to become available
            while(!TXIF);
            //Write data
            TXREG=(*s);
            //Next goto char
            s++;
        }
    Usart_char(0x0D);
}

 
Last edited by a moderator:

But your proteus circuit is wrong.

pin R7/RX/DT/SDO should go to pin TxD of Virtual terminal. Virtual terminal should be set to echo and then you have to send data from virtual terminal to PIC Rx.

Then it receives data.

Please edit your last post and put your code between code tags. i.e., # button.

With the new code I am getting this

PHP:
Clean: Deleting intermediary and output files.
Clean Warning: File "D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.p1" doesn't exist.
Clean: Done.
Build D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550 for device 18F2550
Using driver F:\Program Files\HI-TECH Software\PICC-18\9.80\bin\picc18.exe

Executing: "F:\Program Files\HI-TECH Software\PICC-18\9.80\bin\picc18.exe" --pass1 "D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c" -q --chip=18F2550 -P --runtime=default --opt=default -D__DEBUG=1 --rom=default --ram=default -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" 
Error   [0] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 5.31 Cannot use literal values (0xFDFF) with __CONFIG(), use __PROG_CONFIG() instead
Error   [0] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 7.31 Cannot use literal values (0xFFFF) with __CONFIG(), use __PROG_CONFIG() instead
Error   [0] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 8.31 Cannot use literal values (0xFFFF) with __CONFIG(), use __PROG_CONFIG() instead
Error   [0] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 9.31 Cannot use literal values (0xFFFF) with __CONFIG(), use __PROG_CONFIG() instead
Warning [1385] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 97.12 variable "RA0" is deprecated (declared at F:\Program Files\HI-TECH Software\PICC-18\9.80\include\pic18f2550.h:4671)
Warning [1385] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 110.15 variable "RA0" is deprecated (declared at F:\Program Files\HI-TECH Software\PICC-18\9.80\include\pic18f2550.h:4671)
Warning [1385] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 120.16 variable "RA0" is deprecated (declared at F:\Program Files\HI-TECH Software\PICC-18\9.80\include\pic18f2550.h:4671)
Warning [1385] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 127.9 variable "RB2" is deprecated (declared at F:\Program Files\HI-TECH Software\PICC-18\9.80\include\pic18f2550.h:4691)
Warning [1385] D:\Users\System Administrator\Desktop\PIC18F2550\pic18f2550_excavator.c; 133.9 variable "RB2" is deprecated (declared at F:\Program Files\HI-TECH Software\PICC-18\9.80\include\pic18f2550.h:4691)

********** Build failed! **********

Should RA0 be changed to LATAbits.RA0?
 

Attachments

  • uart con error.jpg
    uart con error.jpg
    419.4 KB · Views: 88
Last edited:

i used to connect like you said but not work even uart write not work too because now pic is in master mode send data by vitdual com to the GSM module i mean PC work as PIC. any way do you have code in mikroc about GSM model or not? if have could you please send it to me

Untitled.png
it can send data but cannot got data
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top