瑞萨单片机流水灯实例

本文介绍了一款基于瑞萨R5F211B4微控制器的流水灯实验,详细阐述了硬件配置、程序设计及实现方法。通过定时器控制LED灯按预设模式有序点亮,演示了多种灯光效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**********************************************************************                                                                                                         
硬件资源:瑞萨R5F211B4 
程序编制:林华电子商行 
淘宝店网址:http://shop70189212.taobao.com 
硬件说明: 
 学习板灯名称L            R5F211B4MCU板                芯片引脚编号 
    L0                  p1_0                            18PIN 
    L1                  p1_1                            17PIN 
    L2                  p1_2                            15PIN 
    L3                  p1_3                            14PIN 
    L4                  p1_4                            13PIN 
    L5                  p1_5                            12PIN 
    L6                  p1_6                            11PIN 
    L7                  p1_7                            10PIN 
功能说明:8个LED灯按照一定顺序亮(流水灯实验) 
晶振:12MHZ 
/***********************************************************************/  
#include "sfr_R81B.h"                     //Definition of R8C/1B SFR  
#define _Disableint asm("Fclr I")   //I标志位为0,则禁止所有的可屏蔽中断  
#define _Enableint  asm("Fset I")   //I标志位置1,表示打开总中断控制  
#define uchar unsigned char  
#define uint unsigned int  
#define ulong unsigned long  
#define PORT3_DIS p1    //定义P1端口  
uint    count0; //中断次数计时  
uchar dis_count0;//显示计数  
uchar dis_count1;//显示功能计数  
_Bool time_05s_bz0;//定时0.5S标志0 如果时间到则置1  
/************************************************************************ 
灯位置顺序 
L0  L1  L2  L3  L4  L5  L6  L7  L8 
************************************************************************/  
uchar far display_light0[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//从左到右  
uchar far display_light1[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//从右到左  
/******************************************************************* 
灯位置顺序 
L0  L1  L2  L3  L4  L5  L6  L7  L8 
*******************************************************************/  
uchar far display_light2[]={0xe7, 0xdb, 0xbd, 0x7e};//从中间到两边  
uchar far display_light3[]={0x7e, 0xbd, 0xdb, 0xe7};//从两边到中间  
//左边逐渐到全亮  
uchar far display_light4[]={0xfe,0xfc,0xf8,0xf0, 0xe0, 0xc0,0x80, 0x00};//  
//右边逐渐到全亮  
uchar far display_light5[]={0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00}; //  
uchar far display_light6[]={0xe7,0xc3,0x81,0x00};//从中间逐渐向两边全亮  
uchar far display_light7[]={0x7e,0x3c,0x18,0x00};//从两边逐渐向中间全亮  
void led_display(void);//流水灯显示程序  
void InitClock(void);//初始化时钟  
void Init_IO(void);//初始化IO口  
void InitTimerX(void);//初始化X定时器  
void main(void)  
 {  
    _Disableint;  //禁止全局中断                          
    InitClock();                               //外部部高速振荡器  
    Init_IO();                                 //IO口初始化  
    InitTimerX();                              //定时器X初始化  
    _Enableint; //允许全局中断                 
    while(1)  
    {  
        asm("NOP\nNOP\nNOP\nNOP");  
        led_display();//流水灯显示子程序  
     }    
  }  
/************************************************************************ 
程序名称:系统时钟初始化 选主时钟 1分频 
************************************************************************/  
void InitClock(void)  
{  
        prcr=0x01;                          //关闭和时钟相关的保护寄存器,允许修改时钟   
        cm13=1;                             //XIN-XOUT引脚  
        cm15 =1;                            //XIN-XOUT引脚使用高驱动能力  
        cm05=0;                             //开始振荡  
        asm("NOP\nNOP\nNOP\nNOP");  
        asm("NOP\nNOP\nNOP\nNOP");  
        ocd2=0x00;                          //使用主时钟作为系统时钟   
        cm16=0;cm17=0;cm06=0;               //f1  
        prcr=0;                             //打开保护寄存器  
}  
/************************************************************************ 
*函数原型: Init_IO 
*功能 :   初始化IO口 pd1_0 = 1 output =0 input 默认0 input 
************************************************************************/  
void Init_IO(void)  
{                         
    pd1 = 0xff;     //p1=0xff为输出口  
    p1 = 0xff;      //输出1       
}  
/************************************************************************ 
 定时器X:定时1ms初始化设置 
************************************************************************/  
void InitTimerX(void)  
{  
    txs=0;                                 //定时器X停止计数  
    txmr=0x00;                             //定时器X寄存器txmr写0  
    tcss |= 0x03;                       //f2  
    prex=120-1;                            //t=((200*120)/12M)s  
    tx=200-1;  
    txic=6;                                //中断优先级6  
    txs=1;                                 //定时器开始计数  
 }  
 /************************************************************************** 
 程序名称:定时器X中断处理 
 **************************************************************************/  
#pragma INTERRUPT TimerXInt(vect=22)  
void TimerXInt(void)  
{  
    ir_txic=0;      //无中断请求标志  
    count0 += 1;    //中断1次计时+1  
    if(count0 > 20) //计时次数到吗  
    {  
        count0 = 0;     //是 清零 重新开始  
        time_05s_bz0 = 1;//置0.15秒时间标志  
    }     
 }  
 /************************************************************************************************* 
程序名称:流水灯显示 功能描述:查表显示不同顺序 
输入变量: 输出变量: 
影响标志位: 
**************************************************************************************************/  
void led_display(void)  
{  
    if(time_05s_bz0 == 1)  
    {  
        time_05s_bz0 = 0;//清时间到标志0    
        switch(dis_count1)  
        {                         
            case 0://第0种花样  
                PORT3_DIS = display_light0[dis_count0];//送数据到P3口显示  
                dis_count0++;//查表指针+1  
                if(dis_count0 > 7)//判断指针次数到吗  
                {  
                    dis_count0 = 0;//查表指针清零,重新开始查表  
                    dis_count1 = 1;//转第1种花样  
                }  
                break;  
            case 1: //第1种花样  
                PORT3_DIS = display_light1[dis_count0];//送数据到P3口显示  
                dis_count0++;//查表指针+1  
                if(dis_count0 > 7)//判断指针次数到吗  
                {  
                    dis_count0 = 0;//查表指针清零,重新开始查表  
                    dis_count1 = 2;//转第2种花样  
                }  
                break;  
            case 2://第2种花样  
                PORT3_DIS = display_light2[dis_count0];//送数据到P3口显示  
                dis_count0++;//查表指针+1  
                if(dis_count0 > 3)//判断指针次数到吗  
                {  
                    dis_count0 = 0;//查表指针清零,重新开始查表  
                    dis_count1 = 3;//转第3种花样  
                }  
                break;  
            case 3://第3种花样  
                PORT3_DIS = display_light3[dis_count0];//送数据到P3口显示  
                dis_count0++;//查表指针+1  
                if(dis_count0 > 3)//判断指针次数到吗  
                {  
                    dis_count0 = 0;//查表指针清零,重新开始查表  
                    dis_count1 = 4;//转第4种花样  
                }  
                break;  
            case 4://第4种花样  
                PORT3_DIS = display_light4[dis_count0];//送数据到P3口显示  
                dis_count0++;//查表指针+1  
                if(dis_count0 > 7)//判断指针次数到吗  
                {  
                    dis_count0 = 0;//查表指针清零,重新开始查表  
                    dis_count1 = 5;//转第5种花样  
                }  
                break;  
            case 5://第5种花样  
                PORT3_DIS = display_light5[dis_count0];//送数据到P3口显示  
                dis_count0++;//查表指针+1  
                if(dis_count0 > 7)//判断指针次数到吗  
                {  
                    dis_count0 = 0;//查表指针清零,重新开始查表  
                    dis_count1 = 6;//花样计数单元清零 重新开始,也可以后续添加花样种类  
                }  
                break;  
            case 6:  
                PORT3_DIS = display_light6[dis_count0];//送数据到P3口显示  
                dis_count0++;//查表指针+1  
                if(dis_count0 > 3)//判断指针次数到吗  
                {  
                    dis_count0 = 0;//查表指针清零,重新开始查表  
                    dis_count1 = 7;//花样计数单元清零 重新开始,也可以后续添加花样种类  
                }  
                break;  
            case 7:  
                PORT3_DIS = display_light7[dis_count0];//送数据到P3口显示  
                dis_count0++;//查表指针+1  
                if(dis_count0 > 3)//判断指针次数到吗  
                {  
                    dis_count0 = 0;//查表指针清零,重新开始查表  
                    dis_count1 = 0;//花样计数单元清零 重新开始,也可以后续添加花样种类  
                }  
                break;
            default:  
            asm("NOP\nNOP\nNOP\nNOP");  
            break;  
        }             
    }  
}  
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值