来个流水灯,这个开发板有很多可以做的,先易后难,本来想弄个花样灯的,结果太晚了,弄个流水灯。
原理图:
下面是LED对应连接IO的位置:
代码:
定时器初始
TMCON = 0X07; //------111 ;Timer0、Tiemr1和Tiemr2选择时钟Fsys
//T0设置,现象:P02输出频率为1K的方波(主频:32M)
TMOD |= 0x01; //0000 0001;Timer0设置工作方式1
TL0 = (65536 - 16000)%256; //时钟为Fsys,则溢出时间=16000*(1/Fsys);
TH0 = (65536 - 16000)/256;
TR0 = 0;
ET0 = 1;//定时器0允许
TR0 = 1;//打开定时器0
//T1设置,现象为P03输出频率为100K的方波(主频:32M)
TMOD |= 0x20; //0010 0000;Timer1设置工作方式2
TL1 = 256 - 160; //时钟为Fsys,则溢出时间=160*(1/Fsys);
TH1 = 256 - 160;
TR1 = 0;
ET1 = 1;//定时器1允许
TR1 = 1;//打开定时器1
//T2设置,设置为捕获模式,读取变量count可以知道P22第10次到第11次下降沿的定时器计数值
TXINX = 0x02; //选择定时器T2
TXCON = 0X09; //使能EXT2,16位捕获模式
TXMOD = 0X80;
THX = 0X00;
TLX = 0X00;
RCAPXH = 0X00;
RCAPXL = 0X00;
ET2 = 1;
TRX = 1;//打开定时器2
//T3设置,现象:P02输出频率为1K的方波(主频:32M)
TXINX = 0x03; //选择定时器T3
TXMOD = 0x80;
TXCON = 0x00; //设置为16位重载寄存器
RCAPXH = (65536-16000)/256; //时钟为Fsys,则溢出时间=32000*(1/Fsys);
RCAPXL = (65536-16000)%256;
TRX = 0;
IE1 |= 0x40; //定时器3允许
IP1 |= 0X40;
TRX = 1; //打开定时器3
//T4设置
TXINX = 0x04; //选择定时器T4
TXMOD = 0x80;
TXCON = 0x00; //设置为16位重载寄存器
RCAPXH = (65536-32000)/256; //时钟为Fsys,则溢出时间=32000*(1/Fsys);
RCAPXL = (65536-32000)%256;
TRX = 0;
IE1 |= 0x80;//定时器4允许
IP1 |= 0X80;
TRX = 1; //打开定时器4
EA = 1;
流水灯控制
i++;
if(i==0)
{
P00 = 0;
P01 = 0;
P02 = 0; //IO口翻转
P03 = 0;
P05 = 0; //IO口翻转
P06 = 0;
P07 = 0; //IO口翻转
P22 = 0; //IO口翻转
P23 = 0; //IO口翻转
P24 = 0; //IO口翻转
P25 = 0; //IO口翻转
P26 = 0; //IO口翻转
P31 = 0; //IO口翻转
P32 = 0; //IO口翻转
P33 = 0; //IO口翻转
P37 = 0; //IO口翻转
P40 = 0; //IO口翻转
P41 = 0; //IO口翻转
P46 = 0; //IO口翻转
P50 = 0; //IO口翻转
P51 = 0; //IO口翻转
}
else if(i==2)
{
P00 = ~P00;
P01 = ~P01;
P02 = ~P02; //IO口翻转
P03 = ~P03;
P05 = ~P05; //IO口翻转
P06 = ~P06;
P07 = ~P07; //IO口翻转
P22 = ~P22; //IO口翻转
P23 = ~P23; //IO口翻转
P24 = ~P24; //IO口翻转
P25 = ~P25; //IO口翻转
P26 = ~P26; //IO口翻转
P31 = ~P31; //IO口翻转
P32 = ~P32; //IO口翻转
P33 = ~P33; //IO口翻转
P37 = ~P37; //IO口翻转
P40 = ~P40; //IO口翻转
P41 = ~P41; //IO口翻转
P46 = ~P46; //IO口翻转
P50 = ~P50; //IO口翻转
P51 = ~P51; //IO口翻转
}
else if(i==3)
{
P00 = 0; //0 1 2 3 5 24 25 26
P01 = 1;
P02 = 1; //IO口翻转
P03 = 1;
P05 = 1; //IO口翻转
P06 = 0;
P07 = 0; //IO口翻转
P22 = 0; //IO口翻转
P23 = 1; //IO口翻转
P24 = 1; //IO口翻转
P25 = 1; //IO口翻转
P26 = 1; //IO口翻转
P31 = 0; //IO口翻转
P32 = 0; //IO口翻转
P33 = 0; //IO口翻转
P37 = 0; //IO口翻转
P40 = 0; //IO口翻转
P41 = 0; //IO口翻转
P46 = 0; //IO口翻转
P50 = 0; //IO口翻转
P51 = 0; //IO口翻转
}
else if(i==4)
{
P00 = 1; //0 1 2 3 5 24 25 26
P01 = 1;
P02 = 1; //IO口翻转
P03 = 1;
P05 = 1; //IO口翻转
P06 = 1;
P07 = 0; //IO口翻转
P22 = 0; //IO口翻转
P23 = 1; //IO口翻转
P24 = 1; //IO口翻转
P25 = 1; //IO口翻转
P26 = 0; //IO口翻转
P31 = 0; //IO口翻转
P32 = 0; //IO口翻转
P33 = 0; //IO口翻转
P37 = 0; //IO口翻转
P40 = 0; //IO口翻转
P41 = 0; //IO口翻转
P46 = 0; //IO口翻转
P50 = 0; //IO口翻转
P51 = 0; //IO口翻转
}
else if(i==5)
{
P00 = 1;
P01 = 1;
P02 = 1; //IO口翻转
P03 = 1;
P05 = 1; //IO口翻转
P06 = 1;
P07 = 0; //IO口翻转
P22 = 1; //IO口翻转
P23 = 1; //IO口翻转
P24 = 1; //IO口翻转
P25 = 0; //IO口翻转
P26 = 1; //IO口翻转
P31 = 0; //IO口翻转
P32 = 0; //IO口翻转
P33 = 0; //IO口翻转
P37 = 0; //IO口翻转
P40 = 0; //IO口翻转
P41 = 0; //IO口翻转
P46 = 0; //IO口翻转
P50 = 1; //IO口翻转
P51 = 0; //IO口翻转
}
else if(i==6)
{
P00 = 1;
P01 = 1;
P02 = 1; //IO口翻转
P03 = 1;
P05 = 1; //IO口翻转
P06 = 1;
P07 = 1; //IO口翻转
P22 = 1; //IO口翻转
P23 = 1; //IO口翻转
P24 = 0; //IO口翻转
P25 = 1; //IO口翻转
P26 = 1; //IO口翻转
P31 = 0; //IO口翻转
P32 = 0; //IO口翻转
P33 = 0; //IO口翻转
P37 = 0; //IO口翻转
P40 = 0; //IO口翻转
P41 = 0; //IO口翻转
P46 = 0; //IO口翻转
P50 = 1; //IO口翻转
P51 = 1; //IO口翻转
}
else if(i==7)
{
P00 = 1;
P01 = 0;
P02 = 1; //IO口翻转
P03 = 1;
P05 = 1; //IO口翻转
P06 = 1;
P07 = 1; //IO口翻转
P22 = 1; //IO口翻转
P23 = 1; //IO口翻转
P24 = 1; //IO口翻转
P25 = 1; //IO口翻转
P26 = 1; //IO口翻转
P31 = 0; //IO口翻转
P32 = 0; //IO口翻转
P33 = 0; //IO口翻转
P37 = 0; //IO口翻转
P40 = 0; //IO口翻转
P41 = 0; //IO口翻转
P46 = 1; //IO口翻转
P50 = 1; //IO口翻转
P51 = 1; //IO口翻转
}
else if(i==8)
{
P00 = 1;
P01 = 1;
P02 = 0; //IO口翻转
P03 = 1;
P05 = 1; //IO口翻转
P06 = 1;
P07 = 1; //IO口翻转
P22 = 1; //IO口翻转
P23 = 1; //IO口翻转
P24 = 1; //IO口翻转
P25 = 1; //IO口翻转
P26 = 1; //IO口翻转
P31 = 0; //IO口翻转
P32 = 0; //IO口翻转
P33 = 0; //IO口翻转
P37 = 0; //IO口翻转
P40 = 0; //IO口翻转
P41 = 1; //IO口翻转
P46 = 1; //IO口翻转
P50 = 1; //IO口翻转
P51 = 1; //IO口翻转
} else if(i==9)
{
P00 = 1;
P01 = 1;
P02 = 1; //IO口翻转
P03 = 0;
P05 = 1; //IO口翻转
P06 = 1;
P07 = 1; //IO口翻转
P22 = 1; //IO口翻转
P23 = 1; //IO口翻转
P24 = 1; //IO口翻转
P25 = 1; //IO口翻转
P26 = 1; //IO口翻转
P31 = 0; //IO口翻转
P32 = 0; //IO口翻转
P33 = 0; //IO口翻转
P37 = 0; //IO口翻转
P40 = 1; //IO口翻转
P41 = 1; //IO口翻转
P46 = 1; //IO口翻转
P50 = 1; //IO口翻转
P51 = 1; //IO口翻转
}else if(i==10)
{
P00 = 1;
P01 = 1;
P02 = 1; //IO口翻转
P03 = 1;
P05 = 0; //IO口翻转
P06 = 1;
P07 = 1; //IO口翻转
P22 = 1; //IO口翻转
P23 = 1; //IO口翻转
P24 = 1; //IO口翻转
P25 = 1; //IO口翻转
P26 = 1; //IO口翻转
P31 = 0; //IO口翻转
P32 = 0; //IO口翻转
P33 = 0; //IO口翻转
P37 = 1; //IO口翻转
P40 = 1; //IO口翻转
P41 = 1; //IO口翻转
P46 = 1; //IO口翻转
P50 = 1; //IO口翻转
P51 = 1; //IO口翻转
}else if(i==11)
{
P00 = 1;
P01 = 1;
P02 = 1; //IO口翻转
P03 = 1;
P05 = 1; //IO口翻转
P06 = 1;
P07 = 1; //IO口翻转
P22 = 1; //IO口翻转
P23 = 1; //IO口翻转
P24 = 1; //IO口翻转
P25 = 1; //IO口翻转
P26 = 1; //IO口翻转
P31 = 1; //IO口翻转
P32 = 1; //IO口翻转
P33 = 1; //IO口翻转
P37 = 1; //IO口翻转
P40 = 1; //IO口翻转
P41 = 1; //IO口翻转
P46 = 1; //IO口翻转
P50 = 1; //IO口翻转
P51 = 1; //IO口翻转
i=0;
把LED灯工作状态放在这个中断里面的
BTM_Int(void) interrupt 9
流水灯工作视频:
https://www.bilibili.com/video/BV1kG4y1V7Gz/?vd_source=0e4686609dd9c60a63b3f7fe54080c03
|