專注差異化嵌入式產(chǎn)品解決方案 給智能產(chǎn)品定制注入靈魂給予生命
提供開(kāi)發(fā)工具、應(yīng)用測(cè)試 完善的開(kāi)發(fā)代碼案例庫(kù)分享
從全面的產(chǎn)品導(dǎo)入到強(qiáng)大技術(shù)支援服務(wù) 全程貼心伴隨服務(wù),創(chuàng)造無(wú)限潛能!
十年單片機(jī)開(kāi)發(fā)方案公司深圳英銳恩分享PIC18F452單片機(jī)GC864發(fā)短信控制流水燈方案,基于QL200開(kāi)發(fā)板。
//------------------------------------------------------------
// 實(shí)驗(yàn)介紹: 利用QL200開(kāi)發(fā)板與GSM模塊實(shí)現(xiàn)發(fā)送信息控制流水燈,
// GSM模塊為GC864,用三根線連接232的收發(fā)腳和地。
// MCU: PIC18F452
// 撥碼: S7撥到ON,S11,S12的高四位撥到ON,其余為OFF
// 現(xiàn)象: 發(fā)送“開(kāi)機(jī)”到GSM模塊(一定要插SIM卡),流水燈
// 循環(huán)跑,發(fā)送“關(guān)機(jī)”到GSM模塊后流水燈熄滅,可反復(fù)
// 操作。
// 基于本實(shí)驗(yàn)可發(fā)揮想象,拓展功能 :-)
// 詳細(xì)信息參見(jiàn)所用GSM模塊的AT指令集
//------------------------------------------------------------
//宏定義及配置位
#include "pic18.h" //18頭文件
__CONFIG(1,OSCSEN&HS); //振蕩方式為HS
__CONFIG(2,BORDIS&PWRTDIS); //4.2V復(fù)位
__CONFIG(4,DEBUGDIS&LVPDIS&STVRDIS); //關(guān)調(diào)式
__CONFIG(5,CPALL);
__CONFIG(6,WPA&WRTEN);// 寫保護(hù)
__CONFIG(7,TRPA&TRPALL&SWDTDIS);// 關(guān)看門狗
#define LED0 PORTC //燈組1
#define LED1 PORTB //燈組2
//------------------------------------------------------------
//定義變量
static unsigned char rc_buff[100]; //信息接收緩存
static unsigned char rxter; //信息長(zhǎng)度計(jì)數(shù)
static unsigned char *ptr;
static unsigned char on_counter; //開(kāi)機(jī)信息核對(duì)
static unsigned char of_counter; //關(guān)機(jī)信息核對(duì)
static unsigned char TAB_counter; //換行計(jì)數(shù)器
static unsigned char rC_inter; //延時(shí)計(jì)數(shù)器
static unsigned char sC_inter;
const unsigned char exe_on[]={0x35,0x46,0x30,0x30,0x36,0x37,0x33,0x41,0x0D,0x0A}; //開(kāi)機(jī)的Unicode
const unsigned char exe_of[]={0x35,0x31,0x37,0x33,0x36,0x37,0x33,0x41,0x0D,0x0A}; //關(guān)機(jī)的Unicode
static volatile unsigned char flag @ 0x1a9; //定義標(biāo)志位
static volatile bit open_flag @ (unsigned) &flag*8+0; //流水燈運(yùn)行標(biāo)志
static volatile bit new_flag @ (unsigned) &flag*8+1; //信息取出正確
static volatile bit get_flag @ (unsigned) &flag*8+2; //有新信息標(biāo)志
//------------------------------------------------------------
//函數(shù)申明
void ExeData(void);
void SysInit(void);
void TimeDelay(unsigned int kk);
void RegistInit(void);
void data_send0(void);
void RegistInit(void);
void data_send1(void);
void UartReceive(void);
void JudgeStat(void);
void comsend(unsigned char kk);
//------------------------------------------------------------
//主函數(shù)
void main(void)
{
SysInit(); //初始化
RegistInit();
for(;;)
{
UartReceive();
if(open_flag) //指令正確后開(kāi)機(jī)
ExeData();
}
}
//------------------------------------------------------------
//流水燈子函數(shù),轉(zhuǎn)圈跑
void ExeData(void)
{
unsigned char k;
for(k=0x08;k!=0;k>>=1) //點(diǎn)亮PORTC高四位
{
LED0 |= k;
TimeDelay(5000);
LED0 = 0;
}
for(k=0X01;k!=0X10;k<<=1) //點(diǎn)亮PORTB高四位
{
LED1 |= k;
TimeDelay(5000);
LED1 = 0;
}
}
//------------------------------------------------------------
//中斷服務(wù)
void interrupt isr(void)
{
if(TMR2IF) //定時(shí)2中斷處理
{
TMR2IF = 0;
if(rC_inter<100)
rC_inter++;
if(sC_inter<250)
sC_inter++;
}
if(RCIE&&RCIF) //接收中斷處理
{
rC_inter = 0;
if (OERR == 0)
{
rc_buff[rxter] = RCREG;
rxter++;
}
else
{
CREN = 0;
asm("nop");
CREN = 1;
}
}
}
//------------------------------------------------------------
//系統(tǒng)初始化
void SysInit(void)
{
flag = 0; //清標(biāo)志位
on_counter = 0;
of_counter = 0;
rxter = 0;
sC_inter = 0;
rC_inter = 0;
TAB_counter = 0;
TRISA = 0Xf0;
TRISB = 0Xf0;
TRISC = 0Xf0;
PORTA = 0X00;
PORTB = 0X00;
PORTC = 0X00;
}
//------------------------------------------------------------
//特殊功能寄存器初始化
void RegistInit(void)
{
SPBRG = 0XC; //設(shè)置波特率為19200BPS
TXSTA = 0X24; //使能串口發(fā)送,選擇高速波特率
RCSTA = 0X90; //使能串口工作,連續(xù)接收
RCIE = 0X1; //使能接收中斷
GIE = 0X1; //開(kāi)放全局中斷
PEIE = 0X1; //使能外部中斷
TMR2IE = 1; //TMR2中斷允許
TMR2IF = 0;
PR2 = 249; //定時(shí)計(jì)數(shù)為250US進(jìn)中斷
T2CON = 0x04; //開(kāi)啟定時(shí)器2,分頻比為0
RBIE = 0; //禁止B口中斷
}
//------------------------------------------------------------
// 延時(shí)子程序
void TimeDelay(unsigned int kk) //軟件延時(shí)
{
unsigned int k;
for(k=kk;k>0;k--)
{
asm("nop");
asm("nop");
asm("nop");
asm("nop");
}
}
//------------------------------------------------------------
// 提取信息子程序
void data_send0(void)
{
static unsigned char ask[]={'A','T','+','C','M','G','L',0x0d}; //AT+CMGL 回車
unsigned char i;
for(i=0;i<8;i++) //循環(huán)發(fā)送8個(gè)字節(jié)
{
TXREG = ask[i];
NOP();
NOP();
TXIE = 0;
while(!TXIF);
}
}
//------------------------------------------------------------
// 刪除信息1子程序
void data_send1(void)
{
static unsigned char ask[]={'A','T','+','C','M','G','D','=','1',0x0d}; //AT+CMGL 回車
unsigned char i;
for(i=0;i<10;i++) //循環(huán)發(fā)送10個(gè)字節(jié)
{
TXREG = ask[i];
NOP();
NOP();
TXIE = 0;
while(!TXIF);
}
}
//------------------------------------------------------------
//調(diào)試串口發(fā)送子程序
void comsend(unsigned char kk)
{
TXREG = kk;
TXEN = 1;
TXIE = 0;
while(!TXIF);
}
//------------------------------------------------------------
// 接收信息
void UartReceive(void)
{
if(rxter>15&&rC_inter>80)// 接收長(zhǎng)度大于15且間隔大于20MS認(rèn)為接收完成
{
JudgeStat(); //對(duì)信息進(jìn)行操作
rxter = 0;
rC_inter = 0;
}
if(get_flag)
{
if(sC_inter >= 100)
{
data_send0(); //25MS后發(fā)送AT+CMGL指令提取信息
get_flag = 0;
}
}
}
//------------------------------------------------------------
// 處理信息
void JudgeStat(void)
{
unsigned char i = 0;
ptr = rc_buff;
while(*ptr != 0x2B) //掃描信息串里有無(wú)'+'
{
ptr++;
}
if(*(ptr+4) == 0x49) //+CMTI:提示有新的信息
{
get_flag = 1;
sC_inter = 0;
}
else if(*(ptr+4) == 0x4C) //+CMGL:有信息列出
new_flag = 1;
if(new_flag)
{
new_flag = 0;
ptr = rc_buff; //重新指向緩存區(qū)
while(TAB_counter < 2)
{
while(*ptr != 0x0A) //掃描兩次換行
{
ptr++;
}
ptr++;
TAB_counter++;
}
for(i=0;i<8;i++,ptr++) //提取漢字信息
{
if(*ptr == exe_on[i])
on_counter++;
if(*ptr == exe_of[i])
of_counter++;
}
//comsend(on_counter);
//comsend(of_counter);
if(on_counter == 0x08) //開(kāi)機(jī)8個(gè)字節(jié)全正確
open_flag = 1; //開(kāi)機(jī)標(biāo)志位置位
if(of_counter == 0x08) //關(guān)機(jī)8個(gè)字節(jié)全正確
open_flag = 0; //關(guān)機(jī)標(biāo)志位置清零
on_counter = 0; //計(jì)數(shù)器清零
of_counter = 0;
TAB_counter = 0;
for(i=0;i<100;i++)
rc_buff[i] = 0; //清空接收緩存
data_send1(); //刪除此條信息
}
}
//------------------------------------------------------------
// END
//------------------------------------------------------------