|
/*************** ***********************************************************************
* 串口通信控制 *
實(shí)現(xiàn)現(xiàn)象:下載程序后打開(kāi)串口調(diào)試助手,將波特率設(shè)置為4800,根據(jù)串口接收到的數(shù)據(jù)執(zhí)行不同命令。
注意事項(xiàng):無(wú)。
***************************************************************************************/
#include "reg52.h" //此文件中定義了單片機(jī)的一些特殊功能寄存器
#include "string.h"
#include "intrins.h"
typedef unsigned int u16; //對(duì)數(shù)據(jù)類(lèi)型進(jìn)行聲明定義
typedef unsigned char u8;
char jiesuo[5] = {'o','p','e','n','\0'};
char suoche[6] = {'c','l','o','s','\0'};
char receiveDatas[10];
sbit led1 = P2^0;
sbit led2 = P2^1;
sbit da = P1^3;
sbit v = P1^5;
sbit gnd = P1^2;
int br=1;
int i=0;
int s=0;
int t=0;
int m=0;
void Receive(char x);
void result();
void delay500ms();
void delay1s();
sfr WDT_CONTR=0xe1;
/*******************************************************************************
* 函數(shù)名 :UsartInit()
* 函數(shù)功能 :設(shè)置串口
* 輸入 : 無(wú)
* 輸出 : 無(wú)
*******************************************************************************/
void UsartInit()
{
SCON=0X50; //設(shè)置為工作方式1
TMOD=0X21; //設(shè)置計(jì)數(shù)器工作方式2
PCON=0X80; //波特率加倍
TH1=0XF4; //計(jì)數(shù)器初始值設(shè)置,注意波特率是4800的,晶振11.0592
TL1=0XF4;
ES=1; //打開(kāi)接收中斷
EA=1; //打開(kāi)總中斷
TR1=1; //打開(kāi)計(jì)數(shù)器
ET0 = 1;
TR0 = 1;
TH0 = 0x4C;
TL0 = 0x00;
WDT_CONTR=0x35;
}
/*******************************************************************************
* 函 數(shù) 名 : main
* 函數(shù)功能 : 主函數(shù)
* 輸 入 : 無(wú)
* 輸 出 : 無(wú)
*******************************************************************************/
void main()
{
UsartInit(); // 串口初始化
v=1;
da=1;
gnd=0;
k1=0;
k2=0;
k3=0;
k4=0;
while(1)
{
delay1s();
delay500ms();
WDT_CONTR=0x35;
};
}
/*******************************************************************************
* 函數(shù)名 : Usart() interrupt 4
* 函數(shù)功能 : 串口通信中斷函數(shù)
* 輸入 : 無(wú)
* 輸出 : 無(wú)
*******************************************************************************/
void Usart() interrupt 4
{
u8 receiveData;
receiveData=SBUF;//出去接收到的數(shù)據(jù)
Receive(receiveData);
RI = 0;//清除接收中斷標(biāo)志位
SBUF=receiveData;//將接收到的數(shù)據(jù)放入到發(fā)送寄存器
while(!TI); //等待發(fā)送數(shù)據(jù)完成
TI=0; //清除發(fā)送完成標(biāo)志位
}
/**************************************************
將接收到的字符存入字符串
***************************************************/
void Receive(char x)
{
receiveDatas[i]=x ;
i++;
if(i==5){
i=0;
result();
}
}
/*******************************************
對(duì)比字符串執(zhí)行命令
*********************************/
void result()
{
int a= strcmp(receiveDatas,jiesuo) ;
int b= strcmp(receiveDatas,suoche);
int c= strcmp(receiveDatas,qixihuo);
if(a==0)
{led1=1;
}
if(b==0)
{
led2=1;
}
if(c==0)
{led3=1;
}
}
/***********************************************
定時(shí)器中斷
*****************************************/
void Timer0Interrupt(void) interrupt 1
{
TH0 = 0x4C;
TL0 = 0x00;
s++;
t++;
if(s==10){
i=0;
s=0;
}
if(t==30)
{
WDT_CONTR=0x35;
t=0;
}
if(da==0)
{
if (br==1)
{
m++;
if(m==30000)
{ k4=1;
delay1s();
delay500ms();
WDT_CONTR=0x35;
delay500ms();
delay1s();
k1=1;
br=0;
delay500ms();
k1=0;
WDT_CONTR=0x35;
delay1s();
delay500ms();
WDT_CONTR=0x35;
delay500ms();
delay1s();
WDT_CONTR=0x35;
k4=0;
}
}
}
else
{
br=1;
m=0;
}
//add your code here!
}
/*********************************************
延時(shí)0.5秒 和延時(shí)1秒 9秒
*******************************************/
void delay1s() //誤差 -0.000000000227us
{
unsigned char a,b,c;
for(c=13;c>0;c--)
for(b=247;b>0;b--)
for(a=142;a>0;a--);
_nop_(); //if Keil,require use intrins.h
}
void delay500ms() //誤差 -0.000000000114us
{
unsigned char a,b,c;
for(c=98;c>0;c--)
for(b=127;b>0;b--)
for(a=17;a>0;a--);
_nop_(); //if Keil,require use intrins.h
}
|
|