没有芯片和设备,所以程序没调,有问题可以再q我。
光敏传感器是个检测设备,你要怎么控制它。
#include
#define u16 unsigned int
#define u8 unsigned char
sbit PHOTORESISTOR=P3^7;
sbit RELAY=P3^6;
sbit BEEP=P4^5;
u8 ds[8]={0,1,2,3,4,5,6,7,}; //数码管显示缓存
u8 smg_zt=1;
//共阳
code u8 w[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,}; //位码
code u8 d[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,};//断码
void dispaly(){//显示函数
static u8 a;
if(++a>=8)a=0;
P0=0xff;
P2=w[a];
P0=d[ds[a]];
}
/*
//共阴
code u8 w[]={~0x01,~0x02,~0x04,~0x08,~0x10,~0x20,~0x40,~0x80,}; //位码
code u8 d[]={~0xc0,~0xf9,~0xa4,~0xb0,~0x99,~0x92,~0x82,~0xf8,~0x80,~0x90,0x00,};//断码
void dispaly(){//显示函数
static u8 a;
if(++a>=8)a=0;
P0=0x00;
P2=w[a];
P0=d[ds[a]];
}
*/
#define UART_RX_SIZE 30
u8 uartRx_buf[UART_RX_SIZE];
u8 uartRx_count=0;
u8 uartRx_outtime=0;
void UartInit(void) //9600bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0xE0; //设定定时初值
TH1 = 0xFE; //设定定时初值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
}
void Send_byte(u8 val){
SBUF=val;
while(!TI);
TI=0;
}
void Send_Uart(u8 *p,u8 size){
while(size-->0){
Send_byte(*p);
p++;
}
}
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void IO_Init(){
P0M1=0x00;//P0口和P2口设为强推挽输出
P0M0=0xff;
P2M1=0x00;
P2M0=0xff;
}
void init() //初始化函数
{
IO_Init();
UartInit();
Timer0Init();
}
//判断两个字符串是否相等
u8 puanduan(u8 *p,u8 size,u8 *pp){
u8 i;
for(i=0;pp[i]!=0;i++){
if(i>=size)return 0;
if(p[i]!=pp[i])return 0;
}
return 1;
}
//串口解析程序
void UART_Protocol(){
if(uartRx_outtime>=10&&uartRx_count>=3){
if(puanduan(uartRx_buf,uartRx_count,"lightsensor:on")!=0){
}else if(puanduan(uartRx_buf,uartRx_count,"lightsensor:off")!=0){
}else if(puanduan(uartRx_buf,uartRx_count,"digital:on")!=0){
smg_zt=1;
}else if(puanduan(uartRx_buf,uartRx_count,"digital:off")!=0){
smg_zt=0;
}else if(puanduan(uartRx_buf,uartRx_count,"relay:on")!=0){
RELAY=0;
}else if(puanduan(uartRx_buf,uartRx_count,"relay:off")!=0){
RELAY=1;
}else if(puanduan(uartRx_buf,uartRx_count,"beep:on")!=0){
BEEP=1;
}else if(puanduan(uartRx_buf,uartRx_count,"beep:off")!=0){
}
uartRx_count=0;
}
}
void main()//主函数
{
u8 i;
init();
while(1){
UART_Protocol();
if(smg_zt==1){
for(i=0;i<8;i++)ds[i]=i;
}else {
for(i=0;i<8;i++)ds[i]=10;
}
}
}
void timer0() interrupt 1//定时器,实现倒计时
{
dispaly();
if(++uartRx_outtime>=100){
uartRx_outtime=0;
uartRx_count=0;
}
}
void uart0() interrupt 4
{
if(RI){
u8 t=SBUF;RI=0;
uartRx_outtime=0;
if(uartRx_countuartRx_buf[uartRx_count++]=t;
}
}
}
(2),(3),(4)段程序错了