// 智林Z2101/03开发板试验程序
// UART0试验
// 刘笑然
// 智林测控技术研究所
// www.the0.net
// 2006.12.1
#include <stdio.h>
// 引入相关芯片的头文件
#include <LPC2103.H>
#include "TFT018.h"
/*----------------------------------------------------------*\
| TYPE DEFINE |
\*----------------------------------------------------------*/
typedef char S8;
typedef unsigned char U8;
typedef short S16;
typedef unsigned short U16;
typedef int S32;
typedef unsigned int U32;
typedef long long S64;
typedef unsigned long long U64;
typedef unsigned char BIT;
typedef unsigned int BOOL;
// 发光管接在P0.3引脚
#define LED (1<<3)
#define Buf_Max 100 //缓存长度200
#define MQ 0X00000400
#define FD 0X00000800
#define WD 0X00001000
#define RE 0X01<<14
unsigned char Rec_Buf[200];
unsigned char Rec_Buffer[200];
unsigned int i = 0;
unsigned int iRX = 0;
unsigned int FIRST=1;
unsigned int SECOND=1;
unsigned int THIRD=1;
unsigned int FOUR=1;
unsigned int START2=1;
unsigned int START3;
unsigned int START4;
unsigned int START5;
unsigned int START6;
unsigned int START8;
unsigned int b;
unsigned char str1[]="AT\r\n";
unsigned char str2[]="AT+CMGF=1\r\n";
unsigned char str3[]="AT+CMGS=13322489387\r\n";
unsigned char str4[]="LAOPOAINIZHESHIGSMGEINIFADE";
unsigned char str5[]="AT+CMGR=24";
unsigned char str6[]="\r\n";
unsigned char str7[]="AT+CMGD=24";
unsigned int rcv_new;
/*----------------------------------------------------------*\
| Delay ns |
\*----------------------------------------------------------*/
void DelayNS (U32 dly) {
U32 i;
for ( ; dly>0; dly--)
for (i=0; i<5000; i++);
}
/*----------------------------------------------------------*\
| UART0 Initial |
\*----------------------------------------------------------*/
void UART0_Init()
{
unsigned int Fdiv;
PINSEL0 = 0x00000005; //设置串口引脚
U0LCR = 0x83; //置为除数锁存位,进行配置
Fdiv = (60000000 /16) / 9600; // 设置波特率
U0DLM = Fdiv / 256;
U0DLL = Fdiv % 256;
U0LCR = 0x03; //清除除数锁存位,并设置工作模式模式
U0FCR = 0x01; // 使能FIFO,并设置触发点为8字节
U0IER = 0x01; // 允许RBR中断,即接收中断
}
void __irq IRQ_UART0(void)
{
if((U0IIR&0x0F)== 0x04 )
{
rcv_new = 1; // 设置接收到新的数据标志,并清除中断标志位
Rec_Buf[b++] = U0RBR; // 读取FIFO的数据
if(b>200)
b=0;
}
//清除中断
VICVectAddr = 0X00;
}
void Interrupt_Init (void)
{
VICIntSelect = 0x00000000; // 设置所有通道为IRQ中断
VICVectCntl0 = 0x26; // UART0中断通道分配到IRQ slot 0,即优先级最高
VICVectAddr0 = (unsigned int)IRQ_UART0; // 设置UART0向量地址
VICIntEnable = 1<<0X06; // 使能UART0中断
}
/*----------------------------------------------------------*\
| UART0 Send Char |
\*----------------------------------------------------------*/
void UART0_PutCh(unsigned char Ch) {
U0THR = Ch; // 发送数据
while( (U0LSR&0x40)==0 ); // 等待数据发送完毕
}
void UART0_PutStr(unsigned char const *str) {
while(*str)
UART0_PutCh(*str++);
} // 发送数据
unsigned char UART0_GET()
{
while((U0LSR&0X01)==0);
return U0RBR;
}
/*----------------------------------------------------------*\
| UART0 Get Char |
\*----------------------------------------------------------*/
void Delay_ms(unsigned int time)
{
unsigned int j;
for(;time>0;time--)
{
j = 1500;
while(j--);
}
}
/*int Hand(unsigned char *a)
{
if(strstr(Rec_Buf,a)!=NULL)
return 1;
else
return 0;
}*/
void CLR_Buf(void)
{
unsigned char k;
for(k=0;k<Buf_Max;k++) //将缓存内容清零
{
Rec_Buf[k] = 0;
}
i = 0; //接收字符串的起始存储位置
}
void LIANJIE(void)
{
while(FIRST)
{
CLR_Buf();
Delay_ms(600);
UART0_PutStr(str1);
for(i=0;i<5;i++)
Rec_Buffer[i]=UART0_GET();
if(strstr(Rec_Buffer,"OK")!=NULL)
//if(Rec_Buf[0]==0x54&&Rec_Buf[1]==0x41) //判断是否握手成功,如果不成功延时一会,再发送AT握手指令
{
CLR_Buf();
FIRST=0;
Delay_ms(600);
}
else
{ FIRST=1;
Delay_ms(600);
}
}
while(SECOND)
{
CLR_Buf();
Delay_ms(6);
UART0_PutStr(str2);
for(i=0;i<5;i++)
Rec_Buffer[i]=UART0_GET();
if(strstr(Rec_Buffer,"OK")!=NULL)
//if(Rec_Buf[0]==0x54&Rec_Buf[1]==0x41) //判断是否握手成功,如果不成功延时一会,再发送AT握手指令
{
CLR_Buf();
SECOND=0;
//Delay_ms(6);
}
else
{
SECOND=1;
//Delay_ms(6);
}
}
}
void SEND_MSG(void)
{
unsigned int START1=1;
while(START2|START3|START4)
{
while(START1)
{
CLR_Buf();
Delay_ms(600);
UART0_PutStr(str3);
for(i=0;i<5;i++)
Rec_Buffer[i]=UART0_GET();
if(strstr(Rec_Buffer,">")!=NULL)
//if(Rec_Buf[0]==0x54) //判断是否握手成功,如果不成功延时一会,再发送AT握手指令
{
CLR_Buf();
START1=0;
if(START2==1)
{
UART0_PutStr(str4);
Delay_ms(600);
UART0_PutCh(0x1A);
UART0_PutStr(str6);
START2=0;
}
if(START3==1)
{
UART0_PutStr(str4);
Delay_ms(600);
UART0_PutCh(0x1A);
UART0_PutStr(str6);
START3=0;
}
if(START4==1)
{
UART0_PutStr(str4);
Delay_ms(600);
UART0_PutCh(0x1A);
UART0_PutStr(str6);
START4=0;
}
}
else
{
START1=0;
}
}
}
}
void REV_MSG()
{ if(rcv_new==1){
if(strstr(Rec_Buf,"CMTI")!=NULL)
//while(Rec_Buf[0]==0x43)//判断来短信标志位
{ START6=1;
CLR_Buf();
UART0_PutStr(str5);
}
if(START6==1)
{
if(strstr(Rec_Buf,"+KD")!=NULL)
//if(Rec_Bu