You are on page 1of 4

#include <18F4520.

h>
#device *=16 adc=8
#FUSES NOWDT
//No Watch Dog Timer
#FUSES WDT128
//Watch Dog Timer uses 1:128 Postscale
#FUSES HS
//Low power osc < 200 khz
#FUSES NOPROTECT
//Code not protected from reading
#FUSES NOBROWNOUT
//No brownout reset
#FUSES BORV20
//Brownout reset at 2.0V
#FUSES NOPUT
//No Power Up Timer
#FUSES NOCPD
//No EE protection
#FUSES STVREN
//Stack full/underflow will cause reset
#FUSES NODEBUG
//No Debug mode for ICD
#FUSES NOLVP
//No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT
//Program memory not write protected
#FUSES NOWRTD
//Data EEPROM not write protected
#FUSES IESO
//Internal External Switch Over mode enabled
#FUSES FCMEN
//Fail-safe clock monitor enabled
#FUSES PBADEN
//PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC
//configuration not registers write protected
#FUSES NOWRTB
//Boot block not write protected
#FUSES NOEBTR
//Memory not protected from table reads
#FUSES NOEBTRB
//Boot block not protected from table reads
#FUSES NOCPB
//No Boot Block code protection
#FUSES LPT1OSC
//Timer1 configured for low-power operation
#FUSES MCLR
//Master Clear pin enabled
#FUSES NOXINST
//Extended set extension and Indexed Addressing mode
disabled (Legacy mode)
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
unsigned char sms[150];
char c=0x00,c1=0x00,c2=0x00,c3=0x00,c4=0x00,c5=0x00;
int8 new_sms=0,i=0,j=0,vi_tri,dk;
// ngt nhn UART
// c tt c d liu t module sim truyn n v lu vo mng sms[]
// nu chui d liu ban u l +CMTI th c tin nhn mi.
#int_RDA
void RDA_isr(void)
{
c=getc();
if(c=='+') c1=c;
if(c=='C') c2=c;
if(c=='M') c3=c;
if(c=='T') c4=c;
if(c=='I') c5=c;
if(c1=='+' && c2=='C' && c3=='M' && c4=='T' && c5=='I') new_sms=1;
if((c!=13)&&(c!=10)){sms[i]=c; i++;}
}
#define
#define
#define
#define

LCD_ENABLE_PIN PIN_D2
LCD_RS_PIN PIN_D0
LCD_RW_PIN PIN_D1
LCD_TYPE 2

#include <lcd.c>
void delete_sms()
{
printf("AT+CMGD=1,4\r\n");//delete all message
delay_ms(500);
}
void reset()
{
i=0;
new_sms=0;
c=0x00,c1=0x00,c2=0x00,c3=0x00,c4=0x00,c5=0x00;
for(j=0;j<90;j++) //Xoa sms[]
{sms[j]=0;}
}
/*
cu trc 1 tin nhn :<AT>+<CMGS>=<s in thoi><CR><LF><ni dung tin
nhn><SUB>
CR tng c vi "\r\" hoc s 13
LF tng ng vi "\n\" hoc s 10
printf("at+cmgs=\"01677613044\"\r\n"); - 01677613044 l s n thoi cn gi
delay_ms(500); // tr 1 khong thi gian
printf(ni dung tin nhn cn gi); v d printf("anh nh em:) ");
delay_ms(500); // tr 1 khong thi gian
putc(26); // k t CTR+Z - SUB, bo hiu kt thc tin nhn
*/
void gui_thong_bao(unsigned char thong_bao)
{
printf("at+cmgs=\"01677613044\"\r\n");
delay_ms(500);
if (thong_bao==1) printf("Khoi Dong ");
else if(thong_bao==2) printf("OK ");
else if(thong_bao==3) printf("Loi");
delay_ms(500);
putc(26);
delay_ms(500);
lcd_gotoxy(1,1);
lcd_putc("OK!
");
lcd_gotoxy(1,2);
lcd_putc("
");
}
void Cho_SIM900(void)
{
lcd_gotoxy(1,1);
lcd_putc("SIM900 init ");
delay_ms(1000);
lcd_gotoxy(1,1);
lcd_putc("SIM900 init. ");
delay_ms(1000);
lcd_gotoxy(1,1);
lcd_putc("SIM900 init.. ");
delay_ms(1000);
lcd_gotoxy(1,1);
lcd_putc("SIM900 init... ");

delay_ms(1000);
lcd_gotoxy(1,1);
lcd_putc("gui tin nhan ");
}
void dieu_khien(unsigned char thiet_bi)
{
// thuc hien lenh tren thiet bi so1
if(
( sms[vi_tri+0]==thiet_bi)&&
((sms[vi_tri+1]=='O')||(sms[vi_tri+1]=='o'))&&
((sms[vi_tri+2]=='N')||(sms[vi_tri+2]=='n')))
{
if(thiet_bi=='1') output_high(PIN_C0);
if(thiet_bi=='2') output_high(PIN_C1);
if(thiet_bi=='3') output_high(PIN_C2);
if(thiet_bi=='4') output_high(PIN_C3);
}
if(
( sms[vi_tri+0]==thiet_bi)&&
((sms[vi_tri+1]=='O')||(sms[vi_tri+1]=='o'))&&
((sms[vi_tri+2]=='F')||(sms[vi_tri+2]=='f')))
{
if(thiet_bi=='1') output_low(PIN_C0);
if(thiet_bi=='2') output_low(PIN_C1);
if(thiet_bi=='3') output_low(PIN_C2);
if(thiet_bi=='4') output_low(PIN_C3);
}
}
void main()
{
lcd_init();
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
set_tris_c(0x80);
output_low(PIN_C0);
output_low(PIN_C1);
output_low(PIN_C2);
output_low(PIN_C3);
Cho_SIM900();
gui_thong_bao(1);
reset();
delete_sms();
reset();
printf("AT+CMGF=1\r\n");// cau hinh doc tin nhan
delay_ms(500);
while(true)
{
if(new_sms==1)
{
reset();
lcd_gotoxy(1,1);
lcd_putc("tin nhan moi");
printf("AT+CMGR=1\r\n");
delay_ms(500);

}
}

// lenh dieu khien nam trong khoang sms[75]~sms[90]


for(i=75;i<90;i++)
{
// tach lenh
if(
((sms[i]=='1')||(sms[i]=='2')||(sms[i]=='3')||(sms[i]=='4'))&&
((sms[i+1]=='O')||(sms[i+1]=='o'))&&
((sms[i+2]=='N')||(sms[i+2]=='n')||(sms[i+2]=='F')||(sms[i+2]=='f')))
{
lcd_gotoxy(1,2);
lcd_putc(sms[i+0]);
lcd_putc('-');
lcd_putc(sms[i+1]);
lcd_putc(sms[i+2]);
vi_tri=i;
dk=sms[i+0];
dieu_khien(dk);
delete_sms();
gui_thong_bao(2);
break;
}
}
if(i==90)
{
lcd_gotoxy(1,2);
lcd_putc("lenh DK loi ");
delete_sms();
gui_thong_bao(3);
}
reset();
new_sms=0;
}

You might also like