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
| // 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
void USART_Init(void);
void USART_Write(unsigned char x);
void USART_Write_Text(char *z);
char AT[] = "AT\r";
char CMGF[] = "AT+CMGF=1\r";
char CMGS[] = "AT+CMGS=\"0795982726\"\r"; //0795982726
char msg[] = "Hi EDABoard";
char buff[25], i = 0, mystr[17];
char response1[] = "AT\r\r\nOK\r\n";
char response2[] = "AT+CMGF=1\r\r\nOK\r\n";
char response3[] = "AT+CMGS=\"0795982726\"\r\r\n> ";
char response4[] = "+CMGS: 170\r\r\nOK\r\n";
int dummy;
void USART_init(void){
TXSTA = 0x22; // low speed communication, asynchronous, Tx enable, 8 bit data
RCSTA = 0x90;// serial port enable, 8 bit data, continuous Rx, no address detection
SPBRG = 12;//9600 bps
TRISC = 0x80; //RC7 is Rx while RC6 is Tx
INTCON = 0xC0; // GIE and PEIE
}
void USART_Write(char x){
while(!TRMT_bit); // wait for previous transmission to finish
TXREG = x;
}
void USART_Write_Text(char *z){
while(*z)USART_Write(*z++);
}
void clearBuffer(){
for(i = 0; i < 25; i++)buff[i] = '\0';
}
void checkChars(){
while(buff[i] != '\0'){
dummy = buff[i] - 0x30;
//if(dummy < 0) dummy = dummy + 48;
IntToStr(dummy, mystr);
LCD_Out(2,1,myStr);
Delay_ms(3000);
LCD_Out(2,1," ");
Delay_ms(2000);
i++;
}
i = 0;
}
void Interrupt(){
if(RCIF_bit){
//if(OERR_bit){
//CREN_bit = 0;
//CREN_bit = 1;
//OERR_bit = 0;
//}
buff[i++] = RCREG;
buff[i] = '\0';
}
RCIF_bit = 0;
}
void main() {
TRISA = 0xFF;
TRISB = 0x00;
PORTB = 0x00;
USART_Init();
Delay_ms(200);
CREN_bit = 1;
RCIE_bit = 1;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
LCD_Out(1,1,"Hello");
while(1){
while(strcmp(buff, response1) != 0){
USART_Write_Text(AT);
Delay_ms(1000);
i = 0;
//checkChars();
}
clearBuffer();
i = 0;
while(strcmp(buff, response2) != 0){
USART_Write_Text(CMGF);
Delay_ms(2000);
i = 0;
//checkChars();
}
clearBuffer();
i = 0;
while(strcmp(buff, response3) != 0){
USART_Write_Text(CMGS);
Delay_ms(2000);
i =0;
//checkChars();
}
clearBuffer();
i = 0;
//while(strcmp(buff, response4) != 0){
USART_Write_Text(msg);
USART_Write(0x1A);
Delay_ms(5000);
i =0;
//}
clearBuffer();
i = 0;
}
} |