#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
//函数申明
void delay1(uint x);
void timer_init();
void interrupt time0();
uchar counter,counter1,time_ok,time_1s,mode;
//=======================================//=============
void main() //主程序
{
uchar YiWei,temp,OutBuf;
YiWei = 0xC0; //*赋值为1*//
OutBuf = 0x00;
TRISC=0X00; //C口定义为输出
PORTC=0X3F; //点亮RCO口LED,为共阳
timer_init();
while(1)
{
if(time_1s)
{
time_1s=0;
mode++;
switch(mode)
{
case 1:
PORTC=0X3F;YiWei=0XC0;break;
case 2:
PORTC=0XFC;YiWei=0X03;OutBuf=0;break;
case 3:
PORTC=0XFE;YiWei=0X01;OutBuf=0;break;
case 4:
PORTC=0X3F;YiWei=0XC0;OutBuf=0;break;
case 5:
PORTC=0X7F;YiWei=0X80;OutBuf=0;break;
case 6:
PORTC=0XFC;YiWei=0X03;mode=0;break;
}
}
if(time_ok)
{
time_ok=0;
if(mode==1)
{
//两灯反追
if(PORTC==0XFC)
{
YiWei=0XC0;
PORTC=0X3F;
delay1(500);
}
YiWei>>=1;
PORTC = ~YiWei; //
}
if(mode==0)
{
//两灯顺追
if(PORTC==0X3F)
{
YiWei=0X03;
PORTC=0XFC;
delay1(500);
}
YiWei<<=1;
PORTC = ~YiWei;
}
if(mode==2)
{
//两灯叠加顺追
if(OutBuf==0xFC)//循环一周后清零
{
PORTC=0XFC;
YiWei = 0x03;
OutBuf = 0x00;
}
YiWei<<=1;
temp=YiWei<<1;
if((OutBuf|temp)==OutBuf)
{
OutBuf>>=1;
OutBuf>>=1;
OutBuf += 0xC0;
YiWei = 0x03;
}
PORTC = ~(OutBuf|YiWei);
}
if(mode==3)
{
if(OutBuf==0xFE)//循环一周后清零
{
PORTC=0XFE;
YiWei = 0x01;
OutBuf = 0x00;
}
YiWei<<=1;
if((OutBuf|YiWei)==OutBuf)
{
OutBuf>>=1;
OutBuf += 0x80;
YiWei = 0x01;
}
PORTC = ~(OutBuf + YiWei);
}
if(mode==4)
{
//两灯叠加反追
if(OutBuf==0x3f)//循环一周后清零
{
PORTC=0X3f;
YiWei = 0xc0;
OutBuf = 0x00;
}
YiWei>>=1;
temp=YiWei>>1;
if((OutBuf|temp)==OutBuf)
{
OutBuf<<=1;
OutBuf<<=1;
OutBuf += 0x03;
YiWei = 0xc0;
}
PORTC = ~(OutBuf|YiWei);
}
if(mode==5)
{
//叠加反追
if(OutBuf==0x7f)//循环一周后清零
{
PORTC=0X7f;
YiWei = 0x80;
OutBuf = 0x00;
}
YiWei>>=1;
if((OutBuf|YiWei)==OutBuf)
{
OutBuf<<=1;
OutBuf += 0x01;
YiWei = 0x80;
}
PORTC = ~(OutBuf + YiWei);
}
}
}
}
//=======================================//=============
//TIMR初始化函数
void timer_init()
{
//TIMER0初始化
OPTION=0x87;//1000 0111 //预分频给TM0,分频比1/8
TMR0=60; //(256-60)X256=50mS
GIE=1; //开放全局中断
T0IE=1; //开放TM0中断
//TIMER1初始化
T1CON=0x31;//0011 0001 //预分频比为1/8,启动TMR1
PEIE=1; //使能外设中断
TMR1IE=1; //使能TMR1中断
TMR1H=(65536-62500)/256; //TMR1付初值
TMR1L=(65536-62500)%256; // 62500X8=500mS
GIE=1;
}
//=======================================//=============
//中断服务与汇编一样要判定那种类型中断
// Timer 0 比较匹配中断服务
void interrupt time0()
{
if(T0IE&&T0IF)
{T0IF=0; //TM0中断信号清零
TMR0=60;
//display(); // 调用LED扫描显示
if (++counter >=10) //counter加一,并判断是否大于5
{
counter = 0;
time_ok = 1; // 10ms到
}
}
// Timer 1 比较匹配中断服务
if(TMR1IE&&TMR1IF)
{
TMR1IF=0; //TM0中断信号清零
TMR1H=(65536-62500)/256; //TMR1付初值
TMR1L=(65536-62500)%256; //
if (++counter1 >=60) //counter加一,并判断是否大于5
{
counter1 = 0;
time_1s=1; //
}
}
}
//=======================================//=============
//延时程序
void delay1(uint x) //延时时间为(X)ms
{
uint a,b;
for(a=x;a>0;a--)
for(b=75;b>0;b--); //延时数较准确固定多26us
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
有点问题:
1. temp=YiWei<<1;
if((OutBuf|temp)==OutBuf)
改成if((OutBuf|YiWei<<1))==OutBuf)为什么不行
2.以上花 {MOD}变化有没有简单的程序
一周热门 更多>