本帖最后由 zhumingxing 于 2010-5-21 12:49 编辑
单片机程序如下,主要功能是对3个继电器的控制,对读状态部分程序有点不理解,对电路图里面LM393的作用也不理解啊:
#include <REG2051.H>
#include<absacc.h>
#include "intrins.h"
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uchar data state;
uchar data cmd;
uchar data ls_data;
uchar data test;
uchar data start_data;
uchar data end_data;
uchar data return_state;
uchar data led_1;
uchar data led_2;
uchar data led_3;
uchar data led_4;
uchar data led_5;
uchar data led_6;
sbit clk =P1^3;
sbit dat =P1^4;
sbit out1 =P1^6;
sbit out2 =P1^7;
sbit out3 =P1^2;
sbit out4 =P1^1;
sbit out5 =P3^4;
sbit out6 =P3^5;
sbit out7 =P3^0;
sbit out8 =P3^1;
sbit cotr1 =P1^5;
sbit cotr2 =P1^0;
sbit cotr3 =P3^7;
void short_delay(void);
void long_delay(void);
void delayx10ms(ulong count);
void delayx1ms(ulong count);
void delay(ulong count);
void send_byte( unsigned char out_byte);
void receive_byte(void);
main()
{
uchar i;
i=20;
for(i=0;i>0;i++)
{
delayx10ms(20);
}
EA=1;
EX0=1; //外中断0允许位
while(1)
{
}
}
void int0() interrupt 0 using 1
{
EX0=0;
receive_byte();
start_data=ls_data;
receive_byte();
cmd=ls_data;
if(start_data==0x0a)
{
if(cmd==0x80)
{
receive_byte();
state=ls_data;
/******************************************************************
电磁阀的状态控制字节STATE,低6位数据有效,对应状态为
0 0 V6 V5 V4 V3 V2 V1
V2V1=01,表示1号电磁阀吸合
*******************************************************************/
led_1=state&0x01;
led_3=state&0x04;
led_5=state&0x10;
if(led_1==0x01)
cotr1=0;//阀1闭合
else
cotr1=1;
if(led_3==0x04)//阀2闭合
cotr2=0;
else
cotr2=1;
if(led_5==0x10)//阀3闭合
cotr3=0;
else
cotr3=1;
}/**/
else if(cmd==0x81)//读状态
{
return_state=0;
if(out6)
return_state+=1;
return_state=return_state*2;
if(out5)
return_state+=1;
return_state=return_state*2;
if(out4)
return_state+=1;
return_state=return_state*2;
if(out3)
return_state+=1;
return_state=return_state*2;
if(out2)
return_state+=1;
return_state=return_state*2;
if(out1)
return_state+=1;
send_byte(return_state);
}
}
EX0=1;
}
/***********************************************************
延时部分
***********************************************************/
void short_delay(void)
{
unsigned char i;
for (i=0;i<16;i++);
}
void long_delay(void)
{
unsigned char i;
for (i=0;i<0x60;i++);
}
void delayx10ms(ulong count)
{
uchar j,k;
while(count--!=0)
{
for(j=0;j<20;j++)
for(k=0;k<26;k++)
;
}
}
void delayx1ms(ulong count)
{
uchar j,k;
while(count--!=0)
{
for(k=0;k<40;k++)
;
}
}
void delay(ulong count)
{
count=count*4;
while(count--!=0);
}
/***********************************************************************
名称: send_byte
用途: 写一个字节子程序
说明:
***********************************************************************/
void send_byte( unsigned char out_byte)
{
unsigned char i;
// dat=0;
for (i=0;i<8;i++)
{
while(clk);
if (out_byte&0x80)
{
dat=1;
}
else
{
dat=0;
}
out_byte=out_byte*2;
while(!clk);
}
dat=1;
}
/***********************************************************************
名称: receive_byte
用途: 读取一个字节子程序
说明:
***********************************************************************/
void receive_byte(void)
{
unsigned char i;
ls_data=0;
for(i=0;i<8;i++)
{
ls_data=ls_data*2;
while(clk);
if (dat)
{
ls_data|=0x01;
}
while(!clk);
} |