新手上路
- 积分
- 37
- 金钱
- 37
- 注册时间
- 2017-2-17
- 在线时间
- 5 小时
|
1金钱
#include<stc12c5a60s2.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
#define Buf1_Max 60 //′??ú1?o′?3¤?è
#define Buf2_Max 200 //′??ú2?o′?3¤?è
#define UART1_SendLR() UART1_SendData(0X0D);\
UART1_SendData(0X0A)
static unsigned char *GSM_Way="GSM";//?ìD??DD?o? 1?óú?DD?o?μ??ééü?éò??′′??úμ÷ê?±ê??μ?μú?t2?·?
static unsigned char *phone="15122181657"; //?ìD??óê?·?ê??úo?
static unsigned char *content="Hello-Goouuuu";//·¢?í?ìD??úèY
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
xdata char Uart2_Buf[Buf2_Max];
u8 Times=0,First_Int = 0,shijian=0;
u8 count_20ms;
uchar Timer0_start; //?¨ê±?÷0?óê±???ˉ??êy?÷
uchar Uart2_Start; //′??ú2?aê??óê?êy?Y
uchar Uart2_End; //′??ú2?óê?êy?Y?áê?
void UartInit(void) ;
void UART1_SendData(u8 dat);//串口1发送 1字节
void UART1_SendString(char *s);//串口1发送 字符串
void Timer0Init(void);
void delay_ms(u16 ms);
void delayms(uint z);
void CLR_Buf2(void);
void Second_AT_Command(char *b,char *a,u8 wait_time) ;
void Set_Text_Mode(void);
void Send_Text_Sms(void);
void Wait_CREG(void);
int main(void)
{
UartInit() ;
Timer0Init();
Wait_CREG();
Set_Text_Mode();
Send_Text_Sms();
while(1)
{
;
}
}
void delayms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delay_ms(u16 ms)
{
count_20ms=ms;
while(count_20ms);
}
void UartInit(void) //115200bps@11.0592MHz
{
PCON &= 0x7F; //波特率不倍速
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x04; //独立波特率发生器时钟为Fosc,即1T
BRT = 0xFD; //设定独立波特率发生器重装值
AUXR |= 0x01; //串口1选择独立波特率发生器为波特率发生器
AUXR |= 0x10; //启动独立波特率发生器
ES = 1; //充许串口1中断
EA=1; //开总中断
}
/*----------------------------
UART1 发送串口数据
-----------------------------*/
void UART1_SendData(u8 dat)
{
ES=0; //关串口中断
SBUF=dat;
while(TI!=1); //等待发送成功
TI=0; //清除发送中断标志
ES=1; //开串口中断
}
/*----------------------------
UART1 发送字符串
-----------------------------*/
void UART1_SendString(char *s)
{
while(*s)//检测字符串结束符
{
UART1_SendData(*s++);//发送当前字符
}
}
void Timer0Init(void) //20毫秒@11.0592MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //
TMOD |= 0x01; //设置定时器模式,16位定时器
TL0 = 0x70; //设置定时器初值
TH0 = 0xFC; //设置定时器初值
TF0 = 0; //清TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1; //使能定时器0中断
}
/*******************************************************************************
* 函数名 : Timer0_ISR
* 描述 : 定时器0中断服务入口函数,1ms中断一次
* 输入 :
* 输出 :
* 返回 :
* 注意 :
*******************************************************************************/
void Timer0_ISR() interrupt 1
{
static u8 Time_count=0;
TR0=0;//关定时器
TL0 = 0x70; //重设定时器初值
TH0 = 0xFC; //重设定时器初值
Time_count++;
if(Time_count>=50)
{
Time_count = 0;
if(count_20ms) //20ms延时计数器
count_20ms--;
}
if(Timer0_start)
Times++;
if(Times > (50*shijian))
{
Timer0_start = 0;
Times = 0;
}
TR0=1;//开定时器
}
void CLR_Buf2(void)
{
u16 k;
for(k=0;k<Buf2_Max;k++) //???o′??úèY??á?
{
Uart2_Buf[k] = 0x00;
}
First_Int = 0; //?óê?×?·?′?μ??eê?′?′¢????
}
u8 Find(char *a)
{
if(strstr(Uart2_Buf,a)!=NULL)
return 1;
else
return 0;
}
void Second_AT_Command(char *b,char *a,u8 wait_time)
{
u8 i;
char *c;
c = b; //±£′?×?·?′?μ??·μ?c
CLR_Buf2();
i = 0;
while(i == 0)
{
if(!Find(a))
{
if(Timer0_start == 0)
{
b = c; //??×?·?′?μ??·??b
for (; *b!='\0';b++)
{
UART1_SendData(*b);
}
UART1_SendLR();
Times = 0;
shijian = wait_time;
Timer0_start = 1;
}
}
else
{
i = 1;
Timer0_start = 0;
}
}
CLR_Buf2();
}
void Set_Text_Mode(void)
{
char temp[50]="AT+CSGS=";
Second_AT_Command("ATE0","OK",3); //è???????
Second_AT_Command("AT+CMGF=1","OK",3); //TEXT?£ê?
}
void Send_Text_Sms(void)
{
char temp[50]="AT+CMGS=";
strcat(temp,(char*)phone); //×?·?′??′?óoˉêy£¨?aoˉêy£?
Second_AT_Command(temp,">",3); //μè′y?óê?μ??°>?±2?·¢?í?ìD??úèY
UART1_SendString((char*)content); //·¢?í?ìD??úèY
UART1_SendData(0x1A); //·¢?í?áê?·?
UART1_SendLR();
}
void Wait_CREG(void)
{
u8 i;
u8 k;
i = 0;
CLR_Buf2();
while(i == 0)
{
CLR_Buf2();
UART1_SendString("AT+CREG?"); //2é?ˉ?£?éê?·?×¢2á3é1|
UART1_SendLR();
delayms(50);
for(k=0;k<Buf2_Max;k++)
{
if(Uart2_Buf[k] == ':')
{
if(((Uart2_Buf[k+4] == '1')&&(Uart2_Buf[k+5] != '3'))||(Uart2_Buf[k+4] == '5')) //?μ?÷?£?é×¢2á3é1|
{
i = 1;
break;
}
}
}
}
}
|
|