这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » 基于RD8T37P48的开发板使用定时器控制流水灯

共2条 1/1 1 跳转至

基于RD8T37P48的开发板使用定时器控制流水灯

助工
2024-09-20 20:43:18     打赏

一、硬件准备

1. 新定义RD8T37P48 MCU开发板:板载LED灯、按键等元件。

2. LED灯(多个):作为流水灯的主体,根据需要连接到开发板的多个GPIO端口。

原理图:

图片1.png

下面是LED对应连接IO的位置:

图片2.png

二、软件环境搭建

1. 开发工具Keil C51,用于编写和编译C语言程序。

2. 安装新定义插件(易码魔盒):插件可能提供了一键生成工程文件、配置GPIO等功能,便于快速开发。

3. 使用固件包和示例代码:利用固件包和示例代码,进行开发和学习。

4. 使用易码魔盒或Keil C51创建一个新的工程,并选择RD8T37P48作为主控MCU型号。配置好KEIL的工程与易码魔盒配套开发。

 

具体的方法:

根据开发板的原理图,找到LED灯对应的GPIO端口,并在程序中将其配置为推挽输出模式。

选择一个定时器,并配置其工作模式(如自动重装载模式)。

设置定时器的预分频器和重装载值,以确定流水灯的闪烁频率。

在定时器中断服务例程中编写代码,通过改变LED灯对应GPIO端口的电平状态来实现流水效果。

可以使用循环结构依次点亮每个LED灯,并通过延时函数控制点亮的时间间隔。

LED灯工作状态放在这个中断里面的

BTM_Int(void) interrupt 9

软件代码如下:

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;









关键词: MCU     流水灯    

院士
2024-09-28 16:58:06     打赏
2楼

学习了,谢谢分享。


共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]