⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fengshan.c

📁 遥控风扇
💻 C
字号:
/*************************************************************************
**模块名称: 遥控风扇
**创建人:陈华伟
**日  期:
**修改人:
**日  期:
**功能描述: 通过无线遥控,控制风扇实现定时关机,另外增加控制日光灯。
************设定好的时间可保存,掉电后不用重新设定
**************************************************************************/
 #include "reg52.h"
 #include "intrins.h" #include "stdio.h"
 #include "IR.h"
 #include "18b20.h"
 #define uchar unsigned char
 #define uint unsigned int
 uint t,chu;
 signed char sec,min,hour; 
 uint us1000;
 sbit SDA=P3^7;
 sbit SCL=P3^6;
 sbit P3_1=P3^1;
 sbit P2_0=P2^0;
 sbit P2_1=P2^1;
 sbit P2_3=P2^3;
 sbit P2_4=P2^4;
 sbit P2_5=P2^5;
 sbit P2_6=P2^6;
 sbit P2_7=P2^7;
 sbit P1_0=P1^0;
 sbit P1_1=P1^1;
 sbit P1_2=P1^2;
 sbit P1_3=P1^3;

 bit     flag;
 uchar code tempt[]={0xc0,0xf9,0xa4,0xb0,0x99,      //01234
                    0x92,0x82,0xf8,0x80,0x90,0xff};//56789
 uint    idata  ucSendBuffer[1]=0;
 uint    idata  ucReceData;
 void    ACK();
 void inc_sec();
 void inc_min();
 void inc_hour();

/************************************************************
**函数名:延时函数
**输  入:
**输  出:
*************************************************************/
 void delay(void)
 {
  uint i;
  for(i=100;i>0;i--)
  _nop_();
 }
void delay1(uint n)
{
uint i,j;
for(i=0;i<n;i++)
for(j=0;j<124;j++);
}

/*********************************************************
**名称:I2C_Start
**功能:启动I2C
**输入:无
**返回:无
*********************************************************/
 void I2C_Start()
 {
        SDA=1;
        delay();
        SCL=1;
        delay();
        SDA=0;
        delay();
        SCL=0;                //钳位I2C总线,准备发送数据
 }
/**********************************************************
**名称:I2C_Stop
**功能:停止I2C
**输入:无
**返回:无
**********************************************************/
 void I2C_Stop()
 {
        SDA=0;
        delay();
        SCL=1;
        delay();
        SDA=1;
        delay();
 }
/**********************************************************
**名称:Ack
**功能:应答信号
**输入:无
**返回:无
**********************************************************/
 void Ack()
 {
        SDA=0;
        delay();
        SCL=1;
        delay();
        SCL=0;
        delay();
        SDA=1;
        delay();
 }
/********************************************************
**名称:NoAck
**功能:发送非应答信号
**输入:无
**返回:无
********************************************************/
 void NoAck()
 {
         SDA=1;
        delay();
        SCL=1;
        delay();
        SCL=0;
        delay();
        SDA=0;
        delay();
 }
/********************************************************
**名称:Test_Ack()
**功能:检测应答位
**输入:无
**返回:flag,有应答时flag为0,无应答时flag为1
*********************************************************/
 bit Test_Ack()
 { 
        SCL=0;
        SDA=1;//读入数据
        _nop_();_nop_();_nop_();_nop_();
        SCL=1;
        _nop_();_nop_();_nop_();_nop_();
        if(SDA==0)
                flag=1;
        else        flag=0;
        SCL=0;
        return(flag);
 }
/********************************************************
**名称:SendData()        
**功能:发送一字节数据
**输入:buffer
**返回:
*******************************************************/
 void SendData(uint buffer)
 {
        uint BitCnt=8;//一字节8位
        uint temp=0;
        do
        {
                temp=buffer;
                SCL=0;
                delay();
                if((temp&0x80)==0) //判断最高位是0还是1
                        SDA=0;
                else
                        SDA=1;
                delay();
                SCL=1;
                temp=_crol_(buffer,1);//将buffer中的数据左移一位
                buffer=temp;
                BitCnt--;
        }
        while(BitCnt);
        SCL=0;        
 }
/**************************************************************
**名称:uint ReceiveData()
**功能:接收一字节数据
**输入:
**返回:ucReceData
**说明:将接收的数据存放在ucReceData中
**************************************************************/
 uint ReceiveData()
 {
        uint BitCnt=8;
        uint temp=0;
        SDA=1;//读入数据
        do
        {
                SCL=0;
                delay();
                SCL=1;
                delay();
                if(SDA==1)
                        ucReceData=ucReceData|0x01;  //低位置1
                else
                        ucReceData=ucReceData&0x0fe; //低位清0
                if(BitCnt-1)
                {
                        temp=_crol_(ucReceData,1);   //数据左移一位
                        ucReceData=temp;
                }
                BitCnt--;
        }
        while(BitCnt);
        SCL=0;
        return(ucReceData);
 }
/*************************************************************
**名称:bit WriteNByte()
**功能:主机向24C02中写入多字节数据
**输入:
**返回:
**说明:sla-器件地址 suba-数据地址,*s-写入的数据,n-写入的字节数(n<=8)
**************************************************************/
 bit WriteNByte(uint sla,uint suba,uint *s,uint n)
 {
        uint i;
        I2C_Start();//启动I2C
        SendData(sla);//发送器件地址
        Test_Ack();
        if(flag==0)        return(0);
        SendData(suba);
        Test_Ack();
        if(flag==0)        return(0);
        for(i=0;i<n;i++)//写入8字节数据
        {
                SendData(*(s+i));
                Test_Ack();
                if(flag==0)        return(0);
        }
        I2C_Stop();
        return(1);
 }
/*************************************************************
**名称:bit ReadNByte()
**功能:主机从24C02中读出N字节数据(n<=8)
**输入:
**返回:
**说明:随机地址读操作
**************************************************************/
 bit ReadNByte(uint sla,uint suba,uint *p,uint n)
 {
        uint i;
        I2C_Start();//启动I2C
        SendData(sla);//发送器件地址
        Test_Ack();
        if(flag==0)        return(0);
        SendData(suba);//发送器件内部地址
        Test_Ack();
        if(flag==0)        return(0);

        I2C_Start();
        SendData(sla+1);
        Test_Ack();
        if(flag==0)        return(0);
        for(i=0;i<n-1;i++)//读取字节数据
        {
                *(p+i)=ReceiveData();//读取数据
                ACK();
        }
        *(p+n-1)=ReceiveData();
        
        NoAck();
        I2C_Stop();
        return(1);
 }

/*********************************************************
**函数名:beep()
**输  入:
**输  出:
*********************************************************/
 void beep()
 {
 int h;
 for(h=0;h<600;h++)
 {
 int n;
 P3_1=1;
 for(n=0;n<50;n++);
 P3_1=0;
 for(n=0;n<50;n++);
 }
 }
/************************************************************
**函数名:显示函数
**输  入:
**输  出:
************************************************************/
void led(uint sdate)
{
 P2_6=0;
 P0=tempt[sdate/10];
 delay();
 P0=0xff;
 P2_6=1;


 P2_7=0;
 P0=tempt[sdate%10];
 delay();
 P0=0xff;
 P2_7=1;
 
}
/*************************************************************
**函数名:time()
**输  入:
**输  出:
**************************************************************/
void time()
{
if(TF1==1)
{
TH1=(65535-1000)/256;
TL1=(65535-1000)%256;
us1000++;
if(us1000==500)
{
us1000=0;
inc_sec();
}
}
}
/*************************************************************
**函数名:主函数
**输  入:
**输  出:
**************************************************************/
 void main()
 {
uint num;
int Bcho;
P3_1=0;
TMOD=0x10;           //设置定时器
TH1=(65535-1000)/256;
TL1=(65535-1000)%256;
 TR1=1;
 EA=1;    //开总中断
 IT0=1;           //外部中断0触发方式控制位,下降沿触发
 EX0=1;           //外部中断0(INT0)中断允许位
ReadNByte(0xa0,0x00,ucSendBuffer,1);
t=ucSendBuffer[0];
ReadNByte(0xa0,0x01,ucSendBuffer,1);
Bcho=ucSendBuffer[0];
hour=t;
min=59;
sec=0;
us1000=0;
if(Bcho!=3) aa[0]=0x41;
if(P1_0==0) aa[0]=0x41;
while(aa[0]==0x41)
{
if(P1_0==0)
{
Bcho++;
if(Bcho>2) Bcho=0;
ucSendBuffer[0]=Bcho;//保存数据
WriteNByte(0xa0,0x01,ucSendBuffer,1);
beep();
aa[2]=0x00;
}
if(P1_1==0)
{
Bcho=4;
ucSendBuffer[0]=Bcho;//保存数据
WriteNByte(0xa0,0x01,ucSendBuffer,1);
}
if(aa[2]==0x1e)
{
Bcho--;
if(Bcho<0) Bcho=2;
ucSendBuffer[0]=Bcho;//保存数据
WriteNByte(0xa0,0x01,ucSendBuffer,1);
beep();
aa[2]=0x00;
}
if(aa[2]==0x1a)
{
Bcho++;
if(Bcho>2) Bcho=0;
ucSendBuffer[0]=Bcho;//保存数据
WriteNByte(0xa0,0x01,ucSendBuffer,1);
beep();
aa[2]=0x00;
}
if(aa[2]==0x12)
{
Bcho=3;
ucSendBuffer[0]=Bcho;//保存数据
WriteNByte(0xa0,0x01,ucSendBuffer,1);
beep();
aa[2]=0x00;
}
if(aa[2]==0x16)
{
Bcho=4;
ucSendBuffer[0]=Bcho;//保存数据
WriteNByte(0xa0,0x01,ucSendBuffer,1);
beep();
aa[2]=0x00;
}
if(aa[2]==0x10)
{
P2_0=0;
Bcho=6;
ucSendBuffer[0]=Bcho;//保存数据
WriteNByte(0xa0,0x01,ucSendBuffer,1);
beep();
aa[2]=0x00;
}
if(aa[2]==0x5a)
{
Bcho=5;
ucSendBuffer[0]=Bcho;//保存数据
WriteNByte(0xa0,0x01,ucSendBuffer,1);
beep();
aa[2]=0x00;
}
if(aa[2]==0x45)
{
Bcho=1;
ucSendBuffer[0]=Bcho;//保存数据
WriteNByte(0xa0,0x01,ucSendBuffer,1);
beep();
aa[2]=0x00;
}
switch(Bcho)
{
case 0:
P3_1=0;
P2=0xc6;
break;
case 1:
 P2_4=1;
 P2_5=1;
P3_1=0;
num=hour;
if(num==0)
{
P2_0=1; 
P0=0xff;   
P2_6=1; 
P2_7=1;
}
else if(num==1)
{
time(); 
P2_0=0;  
led(min);
}
else
{
time(); 
P2_0=0;  
led(hour);
}
break; 
case 2 :
 P3_1=0;
 led(hour);
 hour=t;
 min=59;
 sec=0;
 us1000=0;
 P2_0=1;//关风扇
 P2_4=1;
 P2_5=0;
 P2_3=1;
 P3_1=0;
 if(aa[2]==0x1b)
{ 
  t++;
  if(t>10) t=1;
  ucSendBuffer[0]=t;//保存数据
  WriteNByte(0xa0,0x00,ucSendBuffer,1);
   
  ReadNByte(0xa0,0x00,ucSendBuffer,1);
  t=ucSendBuffer[0];
  beep();
  aa[2]=0x00;
 }
 if(aa[2]==0x1f)
{ 
  t--;
  if(t<1) t=10;
  ucSendBuffer[0]=t;//保存数据
  WriteNByte(0xa0,0x00,ucSendBuffer,1);
   
  ReadNByte(0xa0,0x00,ucSendBuffer,1);
  t=ucSendBuffer[0];
  beep();
  aa[2]=0x00;
 }
 if(P1_2==0)
{
  t++;
  if(t>10) t=1;
  ucSendBuffer[0]=t;//保存数据
  WriteNByte(0xa0,0x00,ucSendBuffer,1);
   
  ReadNByte(0xa0,0x00,ucSendBuffer,1);
  t=ucSendBuffer[0];
  beep();
  aa[2]=0x00;
}
 break;
case 3:
 P2=0xff;
 P3_1=0;
 break;
case 4:
ReadTemperature();
led(sdata);
 P2_4=0;
 P2_5=1;
 P2_3=1;
 P3_1=0;
 break;
case 5:
P2_0=0;
delay1(1000);
P2_0=1;
delay1(2000);
P2_0=0;
delay1(3000);
P2_0=1;
delay1(1000);
break;
default : break;
}
 }
}
/****************************************************
**函数名:inc_sec()
**输  入:
**输  出:
*****************************************************/    
void inc_sec()
{
sec++;
P2_3=!P2_3;
if(sec>59)
{
sec=0;
inc_min();
}
}
/****************************************************
**函数名:inc_min()
**输  入:
**输  出:
*****************************************************/ 
void inc_min()
{
min--;
if(min<0)
{
min=59;
inc_hour();
   }
}
/****************************************************
**函数名:inc_hour()
**输  入:
**输  出:
*****************************************************/ 
void inc_hour()
{
hour--;
if(hour<0) hour=t;
}

  



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -