?? linpro.c
字號:
//接受采用中斷的形式,發(fā)送不用中斷
//*******************************本節(jié)點(diǎn)地址01*****************************//
// 接受來自10地址的數(shù)據(jù)
//*******************************ch的定義格式******************************/
//********** ***************/
//********** 第1位:"1"遠(yuǎn)程幀,"0"為數(shù)據(jù)幀 ***************/
//********** 第2、3位:接收地址 10 ***************/
//********** 第4、5位:發(fā)送地址 01 ***************/
//********** 第6位接收數(shù)據(jù) ** 000:P1.0=0 011:P1.3=0 ***************/
//********** 第7位接收數(shù)據(jù) ** 001:P1.1=0 ***************/
//********** 第8位接收數(shù)據(jù) ** 010:P1.2=0 ***************/
//********** ***************/
//********** 只傳輸一個字節(jié)的數(shù)據(jù) ***************/
//********** ***************/
//*************************************************************************/
#include<reg51.h>
#include <string.h>
bit read_flag=1;
unsigned char ch;//=0x01;//第一位1為遠(yuǎn)程幀,0為數(shù)據(jù)幀
sbit a0=P1^0;
sbit a1=P1^1;
sbit a2=P1^2;
sbit a3=P1^3;
sbit b=P2^0;
LIN_DATA_RECEIVE();
void DelayTime(unsigned char j);
//***********************************串口初始化************************************//
void init89s52(void)
{
SCON = 0x50; //SCON: serail mode 1, 8-bit UART, enable ucvr
TMOD |= 0x20; //TMOD: timer 1, mode 2, 8-bit reload
PCON |= 0x80; //SMOD=1;
TH1 = 0xe8; //設(shè)定定時器初值
TL1 = 0xe8; //Baud:9600 fosc=11.0592MHz
IE |= 0x90; //Enable Serial Interrupt
TR1 = 1; // timer 1 run
}
//**********************向串口發(fā)送一個字符串,strlen為該字符串長度********************/
void LIN_send_DATA()//unsigned char ch)
{
ch=0x48;//01001000
SBUF=ch;
while(TI==0);
TI=0;
}
//**************************************LIN接收函數(shù)**********************************/
void LIN_DATA_Receive()
{
if((ch==0x48)) //根據(jù)接收到的數(shù)據(jù)控制不同的轉(zhuǎn)向燈01001001,P1.0=0
{
a0=0;
DelayTime(50);
a0=1;
}
if((ch==0x55)) //根據(jù)接收到的數(shù)據(jù)控制不同的轉(zhuǎn)向燈01001001,P1.1=0
{
a0=0;
DelayTime(50);
a0=1;
}
if((ch==0x55)) //根據(jù)接收到的數(shù)據(jù)控制不同的轉(zhuǎn)向燈01001001,P1.2=0
{
a0=0;
DelayTime(50);
a0=1;
}
if((ch==0x55)) //根據(jù)接收到的數(shù)據(jù)控制不同的轉(zhuǎn)向燈01001001,P1.3=0
{
a0=0;
DelayTime(50);
a0=1;
}
}
//*************************************串口接收中斷函數(shù)*********************************/
void serial() interrupt 4 using 3
{
if(RI)
{
RI = 0;
ch=SBUF;
LIN_DATA_Receive();
read_flag=1;
}
}
/************************************延時程序1ms函數(shù)****************************/
void DelayTime(unsigned char j)
{
unsigned char k,l;
for(l=0;l<=j;l++)
{
for(k=0;k<=150;k++)
{
;
}
}
}
//*****************************************主函數(shù)**************************************//
main()
{
init89s52(); //初始化串口
b=1;//NSLP為高電平
while(1)
{
if (read_flag) //如果取數(shù)標(biāo)志已置位,就將讀到的數(shù)從串口發(fā)出
{
read_flag=0; //取數(shù)標(biāo)志清0
LIN_send_DATA();//unsigned char ch);//inbuf1,INBUF_LEN);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -