打印

大家帮我看看这个程序,有电路图的。谢谢

[复制链接]
1220|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
raininthes|  楼主 | 2009-3-9 21:18 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <reg52.h>
unsigned char tflag,port1,port2,ZKB;
static unsigned char t;
sfr WDTRST=0xa6;
sbit ctr=P3^0;//定义P0.0为ctr
sbit out=P2^7;//定义P2.7为out
sbit IN1=P1^0; //定义IN的端口
sbit IN2=P1^2;
sbit IN3=P1^3;
sbit IN4=P1^4;
sbit IN5=P1^5;
sbit IN6=P2^0;
sbit IN7=P2^1;
sbit IN8=P2^2;
sbit IN9=P2^3;
sbit IN10=P2^4;
sbit IN11=P2^5;
sbit IN12=P2^6;

void IO_init(void);      //申明初始化端口子程序
void T2_init(void);  //开启T2定时器子程序,作为红外信号的采样时钟
int delay_1ms(void);  //1ms延时子程序
int main(void)      //主程序
{    
   unsigned char count1,count2,count3,count4,count5,count6,count7,count8, count9,count10,count11,count12;
   WDTRST=0x1e;
   WDTRST=0xe1; //初始化看门狗
   IO_init();    //初始化各端口
   T2_init();    //启动T2定时器;
   ZKB=30;        //初始化占空比3:7
       
   count1=0;
   count2=0;     //12路输入
   count3=0;
   count4=0;
   count5=0;
   count6=0;
   count7=0;
   count8=0;
   count9=0;
   count10=0;
   count11=0;
   count12=0;     //12路输入存储变量起始为0;
while(1)    //主程序循环
{    
    WDTRST=0xe1;
    WDTRST=0x1e; 
    if(tflag==1)
    {    tflag=0;
        count1=IN1;
    }
    if(count1==1)
    {
        delay_1ms();
        if(count1==1)
        count1=1;
            //报警    
    }
    //当ctr在T2中断的时候测出是1的时候,进入主程序对所有IN的信号进行处理;
}
}


void IO_init(void)      //初始化端口
{     
    P2=0x7f;//P2.0-P2.6端口做输入,P2.7是输出口初始置为0;
    P1=0x1f;//P1.0-P1.5端口做输入,
    ctr=0;     //初始化CTR脚输出为0,红外灯不工作
    return;
}

void T2_init(void)    //启动作为红外信号的采样时钟T2
{
     RCAP2H=0xee;//11.0592m,1S可以执行的机器周期是11059200/12=921600;50ms的机器周期是46080,1ms的机器周期是921.6 算922,而t2每次溢出的最大机器周期是65536,
                 //因而t2初值应该是65536-922=64614 即fc66;1ms溢出一次,
     RCAP2L=0x00;
     TR2=1;       //t2启动
     ET2=1;       //允许t2中断
     EA=1;       //打开总中断
     return;
}
int delay_1ms(void)    //1ms 延时子程序
{    int i=0;
    for(i=0;i<124;i++);
}

timer2() interrupt 5  //t2中断函数       CTR P0.0高时发送使能
{
        //把rcap2h%rcap2l送t2,重新向上计时
        //20ms,ctr置高,30ms,ctr置低,在20m结束的时候取所有IN口的值送单片机X1=in1;....,
    TF2=0;
     if(ctr==1)          //检测IO放端口状态更改前面。刚从0转变到1的时候,ctr已经使能发射了,但此时接收端并没有反应过来,立即检测的话,可能检测到的就是无信号输入
    {
      tflag=1;//告诉主程序t2,红外工作了,检测IO口
      temp_port1 = port1;   //读取备份 当前IO状态
    }
     t++;
     if(t>=100)
    {
         t=0;
    }
     if(t<=ZKB)
     {
        ctr=1;
    }
        else
         ctr=0;//让单片机的P0^0端口输出一个占空比为3:7的pwm波,周期性使能红外发射信号;
}










主要就是对于接收灯信号的处理我还是感觉自己不行,谢谢各位了哦

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

14

帖子

1

粉丝