số là mình đang làm đồ án về điện tử, đề tài là điều khiển thiết bị từ xa bằng tin nhắn, dùng thiết bị là module sim300CZ để điều khiển tắt hoặc mở hai thiết bị là hai bóng đèn 220v. Cụ thể là khi nhắn tin vào số trong module sim, nó sẽ nhận tin nhắn thi hành lệnh,sau đó sẽ nt lại qua số đt mình nt để xác nhận đã bật hoặc tắt thiết bị. Điều cần nói là mình ko rành về lập trình cho pic,đoạn code bên dưới là đoạn code từ một đồ án tương tự của mình, mình đã thay đổi một vài chi tiết như tên và số đt(số đt của mình là 01683384921), lệnh để tắt,mở thiết bị là bat@1, tat@1, bat@2, tat@2,, tin nhắn trả lời lại là : da bat/tat thiet bị 1/2, bạn nào am hiểu về vấn đề này có thể kiểm tra giúp mình xem có đúng không với, nếu có thể cho mình luôn cả đoạn code thì tôt.
tks mọi người rất nhiều.
#include ".\pic_gsm.h"
#include ".\code\myLCD16x2.c"
#include <math.h>
#define Led0 PIN_C1
#define Led1 PIN_C2
#define GSM_PW PIN_E2
#define GSM_SENSE PIN_E1
#define TEXTMODE 5;
#define PDUMODE 0;
//clear buffer
#define UART_emptyIn() {RxHead=0;RxTail=0;}
#define RXBUFSIZE 70
#define TIME_OUT (int32)5000000
int16 delay_100;
char timed_getc(void);
int1 timeout_error;
int1 frame_get;
int1 new_frame;
void GSM_init(void);
void GSM_start(void);
void GSM_sel_mess_mode(char mode);
int UART_getc_timeout();
int UART_getc_blocking();
unsigned int check_new_message();
int read_message();
int RxBuf[RXBUFSIZE];
int RxHead = 0;
int RxTail = 0;
char new_mess_idx[10];
char cmd_str[30];
//************************************************** ********
// USART Interrupt service
//************************************************** ********
void UART_ISR()//source from MDK8051 project
{
char c;
c=getc();
fputc(c,debug_com);
RxBuf[RxHead] = c;
RxHead++;
if (RxHead > (RXBUFSIZE - 1))
RxHead = 0;
}
#int_TIMER0
TIMER0_isr()
{
if(delay_100 < 40)
delay_100++;
else
{
delay_100=0;
output_toggle(Led0);
}
return 0;
}
#INT_RDA
void rda()
{
UART_ISR();
}
void GSM_init(void)
{
output_bit(GSM_PW,1);
output_bit(Led1,1);
output_float(GSM_SENSE);
}
void GSM_start(void)
{
//start up GSM module
delay_ms(2000);
lcd_init();
lcd_putc("\DO AN TOT NGHIEP\n");
lcd_putc("PHAMQUOCCUONG_08DT1");
//printf("GSM demo \n \r");
output_bit(GSM_PW,0);
output_bit(Led1,0);
delay_ms(2000);
output_bit(GSM_PW,1);
output_bit(Led1,1);
lcd_putc("\nGSM initializing");
delay_ms(5000);
lcd_putc("\nPlease wait.......");
delay_ms(5000);
lcd_putc("\n..to init SIM300");
}
void GSM_sel_mess_mode(char mode)
{
if(mode>0) //text mode
{
output_bit(Led1,0);
printf("AT+CMGF=1");
putc(13);//enter CR
output_bit(Led1,1);
delay_ms(1000);
output_bit(Led1,0);
}
else //PDU mode
{
output_bit(Led1,0);
printf("AT+CMGF=0");
putc(13);//enter CR
output_bit(Led1,1);
delay_ms(1000);
output_bit(Led1,0);
}
}
void main()
{
int1 gsm_ok;
int cnt;
int1 temp;
char c;
char string[20];
char *temp1;
int i;
char rec_str[10];
GSM_init();
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
GSM_start();
GSM_sel_mess_mode(5);
output_bit(Led1,1);
lcd_putc("\nGSM send message ");
printf("AT");
putc(13);//enter CR
delay_ms(1000);
output_bit(Led1,0);
printf("AT+CMGS=");
putc(34);//"
printf("01683384921");
putc(34);//"
putc(13);//enter CR
delay_ms(1000);//cho ki tu ">"
printf("thiet bi san sang");
delay_ms(1000);//
putc(0x1A);//CTRL+Z
output_bit(Led1,1);
delay_ms(2000);//
lcd_putc("\nGSM sent done ");
delay_ms(1000);//
//Vong lap xu ly tin nhan *************************************
while(1)
{
delay_ms(1000);//
check_new_message();
read_message();
}
}
int process_IO(void)
{
if(cmd_str[0]== 'B' || cmd_str[0]== 'b')//bat
{
if(cmd_str[1]== 'a' )//bat
if(cmd_str[2]== 't' )//bat
if(cmd_str[3]== '@' )//bat
if(cmd_str[4]== '1' )//bat
{output_low(PIN_A0);return 0x11;}
else if(cmd_str[4]== '2' )//bat
{output_low(PIN_A1);return 0x21;}
else if(cmd_str[4]== '3' )//bat
{output_low(PIN_A2);return 0x31;}
else if(cmd_str[4]== '4' )//bat
{output_low(PIN_A3);return 0x41;}
else
return 1;
}
else if(cmd_str[0]== 'T' || cmd_str[0]== 't' )//tat
{
if(cmd_str[1]== 'a' )//tat
if(cmd_str[2]== 't' )//tat
if(cmd_str[3]== '@' )//tat
if(cmd_str[4]== '1' )//tat
{output_high(PIN_A0);return 0x10;}
else if(cmd_str[4]== '2' )//tat
{output_high(PIN_A1);return 0x20;}
else if(cmd_str[4]== '3' )//tat
{output_high(PIN_A2);return 0x30;}
else if(cmd_str[4]== '4' )//tat
{output_high(PIN_A3);return 0x40;}
else
return 1;
}
else
return 0;
}
//************************************************** ********************
// Function : UART_getc_blocking
// Description : wait forever and read char from uasrt buffer
//************************************************** ********************
int UART_getc_blocking()
{
unsigned char temp=1;
while (RxHead == RxTail);//wait for incoming char
temp = RxBuf[RxTail];
if (++RxTail > (RXBUFSIZE-1))
RxTail = 0;
return(temp);
}
//************************************************** ********************
// Function : wait new message blocking
// Return : new message index string
// Description : check if response=<cr><lf>s forever
//************************************************** ********************
unsigned int check_new_message()
{
int i,j,k;
char c;
char string[20];
char new_mess[10];
while(1)//doc lien tuc chuoi nhan ve tu sim300CZ
{
c=UART_getc_blocking();//do ki tu /n dau tien
if(c==0x0d)break;
}
while(1)
{
c=UART_getc_blocking();//do ki tu /r dau tien
if(c==0x0a)break;
}
i=0;
while(c!=0x0d)
{
c=UART_getc_blocking();
string[i]=c;
i++;
}
string[i+1]=0;
//fprintf(debug_com,"\n\rhost>:");fprintf(debug_com, string);
lcd_putc("\n ");
lcd_putstring(string,1,2);
new_mess[0]='+';
new_mess[1]='C';
new_mess[2]='M';
new_mess[3]='T';
new_mess[4]='I';
for(j=0;j<5;j++)//so sanh voi +CMTI
{
if(string[j]!=new_mess[j])
{
return 0;
}
}
k=0;
new_mess_idx[0]='#';
while(string[12+k] !=0)//compare to know new mess
{
new_mess_idx[k+1]=string[12+k];
k++;
}
lcd_putc("\n ");
lcd_putstring(new_mess_idx,1,2);
return 1;
}
//************************************************** ********************
// Function : wait new message blocking
// Return : new message index string
// Description : check if response=<cr><lf>s forever
//************************************************** ********************
int read_message()
{
char c;
int i;
char ctrl;
delay_ms(1000);//
printf("AT");
putc(13);//enter CR
delay_ms(1000);//
printf("AT+CMGR="); //doc tin nhan o vi tri bat ky
putc(new_mess_idx[1]);
putc(13);//enter CR
while(1)
{
c=UART_getc_blocking();
//fputc(c,debug_com);
if(c==0x0d)break;
}
while(1)
{
c=UART_getc_blocking();
if(c==0x0a)break;
}
//message content
while(1)
{
c=UART_getc_blocking();
if(c==0x0d)break;
}
while(1)
{
c=UART_getc_blocking();
if(c==0x0a)break;
}
i=0;
while((c!=0x0d) & (i<30))
{
c=UART_getc_blocking();
cmd_str[i]=c;
i++;
}
cmd_str[i-1]=0;
cmd_str[i]=0;
lcd_putc("\n ");
lcd_putstring(cmd_str,1,2);
ctrl = process_IO();
//send confirm sms
delay_ms(1000);//
printf("AT+CMGS=");
putc(34);//"
printf("0974474854");
putc(34);//"
putc(13);//enter CR
delay_ms(1000);//cho ki tu ">"
if (ctrl==0)
{printf("PicGSM1:sai cu phap -> ");printf(cmd_str);}
else if (ctrl==1)
{printf("PicGSM1:thiet bi ko ton tai");}
else if((ctrl&0x01)==1)
{
printf("PicGSM1:da bat thiet bi ");
putc((ctrl>>4)+'0');
}
else if ((ctrl&0x01)==0)
{
printf("PicGSM1:da tat thiet bi ");
putc((ctrl>>4)+'0');
}
delay_ms(1000);//
putc(0x1A);//CTRL+Z
delay_ms(1000);//
}
tks mọi người rất nhiều.
#include ".\pic_gsm.h"
#include ".\code\myLCD16x2.c"
#include <math.h>
#define Led0 PIN_C1
#define Led1 PIN_C2
#define GSM_PW PIN_E2
#define GSM_SENSE PIN_E1
#define TEXTMODE 5;
#define PDUMODE 0;
//clear buffer
#define UART_emptyIn() {RxHead=0;RxTail=0;}
#define RXBUFSIZE 70
#define TIME_OUT (int32)5000000
int16 delay_100;
char timed_getc(void);
int1 timeout_error;
int1 frame_get;
int1 new_frame;
void GSM_init(void);
void GSM_start(void);
void GSM_sel_mess_mode(char mode);
int UART_getc_timeout();
int UART_getc_blocking();
unsigned int check_new_message();
int read_message();
int RxBuf[RXBUFSIZE];
int RxHead = 0;
int RxTail = 0;
char new_mess_idx[10];
char cmd_str[30];
//************************************************** ********
// USART Interrupt service
//************************************************** ********
void UART_ISR()//source from MDK8051 project
{
char c;
c=getc();
fputc(c,debug_com);
RxBuf[RxHead] = c;
RxHead++;
if (RxHead > (RXBUFSIZE - 1))
RxHead = 0;
}
#int_TIMER0
TIMER0_isr()
{
if(delay_100 < 40)
delay_100++;
else
{
delay_100=0;
output_toggle(Led0);
}
return 0;
}
#INT_RDA
void rda()
{
UART_ISR();
}
void GSM_init(void)
{
output_bit(GSM_PW,1);
output_bit(Led1,1);
output_float(GSM_SENSE);
}
void GSM_start(void)
{
//start up GSM module
delay_ms(2000);
lcd_init();
lcd_putc("\DO AN TOT NGHIEP\n");
lcd_putc("PHAMQUOCCUONG_08DT1");
//printf("GSM demo \n \r");
output_bit(GSM_PW,0);
output_bit(Led1,0);
delay_ms(2000);
output_bit(GSM_PW,1);
output_bit(Led1,1);
lcd_putc("\nGSM initializing");
delay_ms(5000);
lcd_putc("\nPlease wait.......");
delay_ms(5000);
lcd_putc("\n..to init SIM300");
}
void GSM_sel_mess_mode(char mode)
{
if(mode>0) //text mode
{
output_bit(Led1,0);
printf("AT+CMGF=1");
putc(13);//enter CR
output_bit(Led1,1);
delay_ms(1000);
output_bit(Led1,0);
}
else //PDU mode
{
output_bit(Led1,0);
printf("AT+CMGF=0");
putc(13);//enter CR
output_bit(Led1,1);
delay_ms(1000);
output_bit(Led1,0);
}
}
void main()
{
int1 gsm_ok;
int cnt;
int1 temp;
char c;
char string[20];
char *temp1;
int i;
char rec_str[10];
GSM_init();
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
GSM_start();
GSM_sel_mess_mode(5);
output_bit(Led1,1);
lcd_putc("\nGSM send message ");
printf("AT");
putc(13);//enter CR
delay_ms(1000);
output_bit(Led1,0);
printf("AT+CMGS=");
putc(34);//"
printf("01683384921");
putc(34);//"
putc(13);//enter CR
delay_ms(1000);//cho ki tu ">"
printf("thiet bi san sang");
delay_ms(1000);//
putc(0x1A);//CTRL+Z
output_bit(Led1,1);
delay_ms(2000);//
lcd_putc("\nGSM sent done ");
delay_ms(1000);//
//Vong lap xu ly tin nhan *************************************
while(1)
{
delay_ms(1000);//
check_new_message();
read_message();
}
}
int process_IO(void)
{
if(cmd_str[0]== 'B' || cmd_str[0]== 'b')//bat
{
if(cmd_str[1]== 'a' )//bat
if(cmd_str[2]== 't' )//bat
if(cmd_str[3]== '@' )//bat
if(cmd_str[4]== '1' )//bat
{output_low(PIN_A0);return 0x11;}
else if(cmd_str[4]== '2' )//bat
{output_low(PIN_A1);return 0x21;}
else if(cmd_str[4]== '3' )//bat
{output_low(PIN_A2);return 0x31;}
else if(cmd_str[4]== '4' )//bat
{output_low(PIN_A3);return 0x41;}
else
return 1;
}
else if(cmd_str[0]== 'T' || cmd_str[0]== 't' )//tat
{
if(cmd_str[1]== 'a' )//tat
if(cmd_str[2]== 't' )//tat
if(cmd_str[3]== '@' )//tat
if(cmd_str[4]== '1' )//tat
{output_high(PIN_A0);return 0x10;}
else if(cmd_str[4]== '2' )//tat
{output_high(PIN_A1);return 0x20;}
else if(cmd_str[4]== '3' )//tat
{output_high(PIN_A2);return 0x30;}
else if(cmd_str[4]== '4' )//tat
{output_high(PIN_A3);return 0x40;}
else
return 1;
}
else
return 0;
}
//************************************************** ********************
// Function : UART_getc_blocking
// Description : wait forever and read char from uasrt buffer
//************************************************** ********************
int UART_getc_blocking()
{
unsigned char temp=1;
while (RxHead == RxTail);//wait for incoming char
temp = RxBuf[RxTail];
if (++RxTail > (RXBUFSIZE-1))
RxTail = 0;
return(temp);
}
//************************************************** ********************
// Function : wait new message blocking
// Return : new message index string
// Description : check if response=<cr><lf>s forever
//************************************************** ********************
unsigned int check_new_message()
{
int i,j,k;
char c;
char string[20];
char new_mess[10];
while(1)//doc lien tuc chuoi nhan ve tu sim300CZ
{
c=UART_getc_blocking();//do ki tu /n dau tien
if(c==0x0d)break;
}
while(1)
{
c=UART_getc_blocking();//do ki tu /r dau tien
if(c==0x0a)break;
}
i=0;
while(c!=0x0d)
{
c=UART_getc_blocking();
string[i]=c;
i++;
}
string[i+1]=0;
//fprintf(debug_com,"\n\rhost>:");fprintf(debug_com, string);
lcd_putc("\n ");
lcd_putstring(string,1,2);
new_mess[0]='+';
new_mess[1]='C';
new_mess[2]='M';
new_mess[3]='T';
new_mess[4]='I';
for(j=0;j<5;j++)//so sanh voi +CMTI
{
if(string[j]!=new_mess[j])
{
return 0;
}
}
k=0;
new_mess_idx[0]='#';
while(string[12+k] !=0)//compare to know new mess
{
new_mess_idx[k+1]=string[12+k];
k++;
}
lcd_putc("\n ");
lcd_putstring(new_mess_idx,1,2);
return 1;
}
//************************************************** ********************
// Function : wait new message blocking
// Return : new message index string
// Description : check if response=<cr><lf>s forever
//************************************************** ********************
int read_message()
{
char c;
int i;
char ctrl;
delay_ms(1000);//
printf("AT");
putc(13);//enter CR
delay_ms(1000);//
printf("AT+CMGR="); //doc tin nhan o vi tri bat ky
putc(new_mess_idx[1]);
putc(13);//enter CR
while(1)
{
c=UART_getc_blocking();
//fputc(c,debug_com);
if(c==0x0d)break;
}
while(1)
{
c=UART_getc_blocking();
if(c==0x0a)break;
}
//message content
while(1)
{
c=UART_getc_blocking();
if(c==0x0d)break;
}
while(1)
{
c=UART_getc_blocking();
if(c==0x0a)break;
}
i=0;
while((c!=0x0d) & (i<30))
{
c=UART_getc_blocking();
cmd_str[i]=c;
i++;
}
cmd_str[i-1]=0;
cmd_str[i]=0;
lcd_putc("\n ");
lcd_putstring(cmd_str,1,2);
ctrl = process_IO();
//send confirm sms
delay_ms(1000);//
printf("AT+CMGS=");
putc(34);//"
printf("0974474854");
putc(34);//"
putc(13);//enter CR
delay_ms(1000);//cho ki tu ">"
if (ctrl==0)
{printf("PicGSM1:sai cu phap -> ");printf(cmd_str);}
else if (ctrl==1)
{printf("PicGSM1:thiet bi ko ton tai");}
else if((ctrl&0x01)==1)
{
printf("PicGSM1:da bat thiet bi ");
putc((ctrl>>4)+'0');
}
else if ((ctrl&0x01)==0)
{
printf("PicGSM1:da tat thiet bi ");
putc((ctrl>>4)+'0');
}
delay_ms(1000);//
putc(0x1A);//CTRL+Z
delay_ms(1000);//
}
Comment