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
| // LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
char msg[21];
const char msg1[] = " WELCOME ";
const char msg2[] = " ";
const char msg3[] = " INITIALISING ";
const char msg4[] = " GSM ";
const char cmd1[] = "AT\r\n";
const char cmd2[] = "AT+CMGF=1\r\n";
const char cmd3[] = "AT+CNMI=2,1\r\n";
const char cmd4[] = "AT+CMGD=4,1\r\n";
const char resp1[] = "\r\nOK\r\n";
const char resp2[] = "+CMTI: \"SM\",";
char gsmBuffer[50], smsIndex[4];
char i = 0;
void interrupt() {
if(OERR_bit) {
OERR_bit = 0;
CREN_bit = 0;
CREN_bit = 1;
}
if(RCIF_bit) {
gsmBuffer[i++] = RCREG;
gsmBuffer[i] = '\0';
RCIF_bit = 0;
}
}
// copy const to ram string
char *CopyConst2Ram(char *dest, const char *src){
char *d;
d = dest;
for(;*dest++ = *src++;);
return d;
}
void Delate_SMS(const char *s1, char s2) {
UART1_Write_Text(CopyConst2Ram(s2, s1));
Delay_ms(2000);
}
void Get_SMS_Index(char *s1, char *s2) {
while(*s1 != ',')
*s1++;
*s1++;
while(*s1 != '\r') {
*s2++ = *s1++;
}
*s2 = '\0';
}
void main() {
CMCON = 0x07;
ADCON1 = 0x87;
TRISA = 0xC0;
TRISB = 0x00;
TRISC = 0x87;
TRISD = 0x00;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
LCD_Init();
LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Cmd(_LCD_CLEAR);
LCD_Out(1,1,CopyConst2Ram(msg, msg1));
LCD_Out(2,1,CopyConst2Ram(msg, msg2));
Delay_ms(2000);
LCD_Out(1,1,CopyConst2Ram(msg, msg3));
LCD_Out(2,1,CopyConst2Ram(msg, msg4));
UART1_Init(9615);
Delay_ms(200);
UART1_Write_Text(CopyConst2Ram(msg, cmd1));
Delay_ms(2000);
UART1_Write_Text(CopyConst2Ram(msg, cmd2));
Delay_ms(2000);
UART1_Write_Text(CopyConst2Ram(msg, cmd3));
Delay_ms(2000);
Delate_SMS(cmd4, msg);
RCIE_bit = 1;
PEIE_bit = 1;
GIE_bit = 1;
RCIF_bit = 0;
while(1) {
if(strstr(gsmBuffer, CopyConst2Ram(msg, resp2))) {
Get_SMS_Index(gsmBuffer, smsIndex);
}
}
} |