// Header: tc35 *** 作函数
// File Name: TC35.C
// Author: 兴创科技
#include "hardware.h"
#include "SMS.h"
extern uchar numbuff[]
void delay(uint m) //延时m*0.2ms
{
uchar n
for(m>0m--)
for(n=100n>0n--)
}
void Sendcmd(uchar *p) //发送AT命令
{
while(*p)
{
SBUF=*(p++)
while(TI==0)
TI=0
}
}
void Sendchar(uchar c) /伍橘拍/发送一个字符
{
TI=0
SBUF=c
while(TI==0)
TI=0
}
void Init_tc35(void) //判断是否连接TC35
{
uint i=0xffff
rcount=0
receive_ok=0
Sendcmd("AT\r\n")
while(receive_ok==0)
{
i--
if(i==0) //超时还未响应则返回
{
rcount=0
receive_ok=0
return
}
}
receive_ok=0
rcount=0
delay(4000)
i=0xffff
Sendcmd("AT&F\r\n") //初始化
while(receive_ok==0)
{
i--
if(i==0) //超时还未响应则返回
{
rcount=0
receive_ok=0
return
}
}
receive_ok=0
rcount=0
delay(4000)
i=0xffff
Sendcmd("AT+IPR=4800\r\n") //设置波特率
while(receive_ok==0)
{
i--
if(i==0) //超时还未响应则返回
{
rcount=0
receive_ok=0
return
}
}
receive_ok=0
rcount=0
delay(4000)
i=0xffff
Sendcmd("AT+CMGF=1\r\n") //设置短消息格式
while(receive_ok==0)
{
i--
if(i==0) //超时还未响应则返回
{
rcount=0
receive_ok=0
return
}
}
receive_ok=0
rcount=0
delay(4000)
i=0xffff
Sendcmd("AT+CSCS=") //设置字符集为“GSM”
Sendchar('"')
Sendcmd("GSM")
Sendchar('"')
Sendchar(0x0D)
Sendchar(0x0A)
while(receive_ok==0)
{
i--
if(i==0) //超伍洞时还未响应腔羡则返回
{
rcount=0
receive_ok=0
return
}
}
receive_ok=0
rcount=0
tc35_init_ok=1 //全部初始化完毕置标志
}
void Sendsms(uchar *q) //向指定的号码发送短消息
{
ulong i=0x1ffff
rcount=0 //数据缓冲区指针回0
Sendcmd("AT+CMGS=")
Sendchar('"')
Sendcmd(numbuff)
Sendchar('"')
Sendchar(0x0D)
Sendchar(0x0A)
while(rbuff[rcount-2]!='>')
{
i--
if(i==0)
{
rcount=0
receive_ok=0
return //超时返回
}
}
Sendcmd(q)
Sendchar(0x1A)//发送短信
Sendchar(0x0D)
Sendchar(0x0A)
receive_ok=0
i=0x5fffff //发送短消息超时处理发送等待5s比较安全
while(receive_ok==0)
{
i--
if(i==0) //超时还未响应则返回
{
rcount=0
receive_ok=0
return
}
}
receive_ok=0
rcount=0
}
smc.h
// Header: tc35头文件
// File Name: SMS.H
// Author: Yoran
// Date: 2008/05/12
#ifndef __SMS_H__
#define __SMS_H__
#include <reg52.H>
#include <hardware.H>
extern uchar idata rbuff[]
extern uchar data databuff[]
extern uchar rcount
extern bit tc35_init_ok
extern bit receive_ok
extern bit new_sms
void Init_tc35(void) //判断是否连接TC35
void Sendcmd(uchar *cmd) //发送AT命令
void Sendchar(uchar c) //发送一个字符
void Sendsms(uchar *q) //向指定的号码发送短消息
#endif
main.c
// Header: XC-TC35短信猫测试程序
// File Name: 发送英文短信.C
// Author: 兴创科技
// P2^4---//发短信
#include <reg52.h>
#include <string.h>
#include "hardware.h"
#include "sms.h"
#include "sms.c"
//LCD Start
uchar code table[]="Welcome...."
uchar code table1[]="No:1 is ON"
uchar code table2[]="No:2 is ON"
uchar code table3[]="No:1 2 are ON"
uchar i
//以下三个是定义LCD的引脚
sbit lcden=P2^2 //6
sbit lcdwrite=P2^1//5
sbit lcdrs=P2^0//4
//lcd的写指令
void write_com(uchar com)
{
lcdrs=0
lcden=0
P0=com
delay(5)
lcden=1
delay(5)
lcden=0
}
//lcd的写数据
void write_data(uchar da)
{
lcdrs=1
lcden=0
P0=da
delay(5)
lcden=1
delay(5)
lcden=0
}
//初始化
void init()
{
lcdwrite=0
lcden=0
write_com(0x38)//16*2显示,5*7点阵,8位数据
write_com(0x0c)//显示开,关光标
write_com(0x06)//移动光标
write_com(0x01)//清除LCD的显示内容
}
//LCD END
uchar idata rbuff[110] //串口接收数据缓存区
uchar numbuff[]="13217832221" //此处修改为需要接收短信的手机号码,注意要定义为字符串(\0)
uchar databuff[]="000000" //数据缓存区,注意要定义为字符串(\0)
uchar rcount //串口接收数据指针
bit tc35_init_ok //TC35注册成功初始化完成标志
bit receive_ok //收到模块“ok”标志
bit send_error
bit send_ok
void initUart(void)
{ // RXD=1
//TXD=1
TMOD=0x20 //T1工作方式2,8位自动重装
SCON=0x40 //串行口工作方式1,8位UTRA
TH1=0xfa
TL1=0xfa //波特率发生器,波特率为4800
REN=1 //允许串行接收
TR1=1 //开波特率发生器T1
ES=1 //开串行口中断
EA=1 //开总中断
// R=1
// T=1
}
void RECEIVE_DATA(void) interrupt 4 //接收tC35发送的数据并缓存
{
ES=0
if(RI)
{
rbuff[rcount++]=SBUF
RI=0
}
//判断是否接收完毕
if((rbuff[rcount-1]==0x0a)&&(rbuff[rcount-2]==0x0d)&&(rbuff[rcount-3]==0x4b)&&(rbuff[rcount-4]==0x4f))
{ //换行 //回车
receive_ok=1
}
//判断是否为新短消息指示
if((rbuff[rcount-14]=='C')&&(rbuff[rcount-13]=='M')&&(rbuff[rcount-12]=='T')&&(rbuff[rcount-11]=='I'))
{ //换行
new_sms=1
}
else new_sms=0
if(((rbuff[rcount-17]=='C')&&(rbuff[rcount-16]=='M')&&(rbuff[rcount-15]=='G')&&(rbuff[rcount-14]=='S')&&(rbuff[rcount-4]==0x4f)&&(rbuff[rcount-3]==0x4b)&&(rbuff[rcount-2]==0x0d)&&(rbuff[rcount-1]==0x0a))|((rbuff[rcount-16]=='C')&&(rbuff[rcount-15]=='M')&&(rbuff[rcount-14]=='G')&&(rbuff[rcount-13]=='S')&&(rbuff[rcount-4]==0x4f)&&(rbuff[rcount-3]==0x4b)&&(rbuff[rcount-2]==0x0d)&&(rbuff[rcount-1]==0x0a)))
{ //换行
send_ok=1
}
else send_ok=0
if((rbuff[rcount-7]=='E')&&(rbuff[rcount-6]=='R')&&(rbuff[rcount-5]=='R')&&(rbuff[rcount-4]=='O')&&(rbuff[rcount-3]=='R'))
{ //换行
send_error=1
}
else send_error=0
if(rcount>109)rcount=0
ES=1
}
void keyboard()
{
if(!(K1&K2&K3&K4))
{
delay(10)
if(!(K1&K2&K3&K4))
{
if(K1==0)
{
Sendcmd("ATD13217832221\r\n") //按键1拨打电话测试语音通话
}
if(K3==0&&K4==0)
{
Sendsms("The Light No:1 and 2 are ON!") //发送英文短信
write_com(0x01)
delay(30)
for(i=0i<13i++)write_data(table3[i])
delay(2000)delay(2000)delay(2000)delay(2000)delay(2000)
while(K3==0&&K4==0)
}
if(K3==0)
{
Sendsms("The Light No:1 is ON!") //发送英文短信
write_com(0x01)
delay(30)
for(i=0i<10i++)write_data(table1[i])
delay(2000)delay(2000)delay(2000)delay(2000)delay(2000)
while(K3==0)
}
if(K4==0)
{
Sendsms("The Light No:2 is ON!") //发送英文短信
write_com(0x01)
delay(30)
for(i=0i<10i++)write_data(table2[i])
delay(2000)delay(2000)delay(2000)delay(2000)delay(2000)
while(K4==0)
}
write_com(0x01)
write_com(0x80+1)
for(i=0i<11i++)write_data(table[i])
rcount=0
while(!(K1&K2&K3&K4)) //按键释放
}
}
}
void main(void)
{
uchar times=0
uchar m=0
tc35_init_ok=0
init()//LCD
write_com(0x80+1)
for(i=0i<11i++)write_data(table[i])
initUart() //初始化串口
Init_tc35() //开启TC35
delay(5000)
delay(5000)
delay(5000)
delay(5000)
while(tc35_init_ok==0) //初始化TC35
{
Init_tc35()
delay(4000)
times++
if(times>15)
{
times=0
delay(1000)
delay(500)
delay(1000)
delay(4000)delay(4000)delay(4000)
}
}
if(tc35_init_ok)
{
BEEP=0 //初始化成功鸣叫一声
delay(2000)
BEEP=1
delay(2000)
BEEP=0
}
while(1)
{
keyboard()
}
}
编液困前写短信发送:即先提示输入text: string textcin>>text。然闹清后提示输入phonenum。string phonenumcin>>phonenum.time的话,利用系统提供的函数获得。然后实例化一个结构。再初始化。sms.phonenum=phonenum,sms.smstext=text然后提示用户发送短信,如果发送,则添尺郑加到发件箱。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)