Production of keil electronic clock based on RASC (Renesas RA) (6)----timer drives digital tube

overview

To make each nixie tube display different numbers, but the nixie tubes must be continuously driven sequentially, and the refresh rate between the nixie tubes should be fast enough, so that the flickering between the nixie tubes cannot be seen. The refresh frequency can be set to 2ms to refresh once, so that the human eye cannot see the flicker.

hardware preparation

First, you need to prepare a development board. Here I am preparing a development board with chip model R7FA2E1A72DFL:

insert image description here

insert image description here

video tutorial

https://www.bilibili.com/video/BV1rj41197iQ/

keil electronic clock production based on RASC (Renesas RA)----(5) drive LED digital tube

select timer

The RA MCU has two timer peripherals: the general-purpose PWM timer (GPT) and the asynchronous general-purpose timer (AGT). When choosing between them, consider the following factors
insert image description here

The clock source set here is PCKLD 48M.
The duty cycle frequency can be modified by modifying this frequency.
insert image description here

Timer as counter configuration

点击Stacks->New Stack->Timers->Timer, General PWM (r_gpt)。

insert image description here

Set the timer to make the counter configuration, for example, an interrupt is generated at 2ms. Since there is no need to capture the rising and falling edges, it only needs to be configured as a counting mode.
Set 2ms to refresh the digital tube once.
Frequency=clock source/period, if the counting time is set to 2ms once, and the frequency is 500Hz, then period=48M/500=96000
insert image description here

After configuration, you need to start the timer in the main program and start counting.

/**********************定时器开启***************************************/
       /* Initializes the module. */
       err = R_GPT_Open(&g_timer0_ctrl, &g_timer0_cfg);
       /* Handle any errors. This function should be defined by the user. */
       assert(FSP_SUCCESS == err);
       /* Start the timer. */
       (void) R_GPT_Start(&g_timer0_ctrl);

insert image description here

timer callback function

The events that can trigger the callback function are as follows, we mainly choose the overflow event TIMER_EVENT_CYCLE_END.

insert image description here

First define the variable to display the value.

//数码管变量
uint8_t num1=0,num2=0,num3=0,num4=0;//4个数码管显示的数值
uint8_t num_flag=0;//4个数码管和冒号轮流显示,一轮刷新五次

insert image description here
Create new timer_smg.c and timer_smg.h files.
Add the following code in the callback function to refresh the nixie tube every 2ms, 4 numbers and a colon, a total of 5 refreshes.

timer_smg.c

/*
 * timer_smg.c
 *
 *  Created on: 2023年6月29日
 *      Author: a8456
 */
#include "timer_smg.h"
//数码管变量
extern uint8_t num1,num2,num3,num4;//4个数码管显示的数值
extern uint8_t num_flag;//4个数码管和冒号轮流显示,一轮刷新五次

void timer0_callback(timer_callback_args_t *p_args)
{
    
    
    /* TODO: add your own code here */
    if (TIMER_EVENT_CYCLE_END == p_args->event)
    {
    
    
        if(num_flag==0)
            smg_1(num1);
        else if(num_flag==1)
            smg_2(num2);
        else if(num_flag==2)
            smg_3(num3);
        else if(num_flag==3)
            smg_4(num4);
        else if(num_flag==4)
            smg_maohao_open(1);   //冒号

        num_flag++;
        if(num_flag==5)
            num_flag=0;
    }
}

timer_smg.h

/*
 * timer_smg.h
 *
 *  Created on: 2023年6月29日
 *      Author: a8456
 */

#ifndef TIMER_SMG_H_
#define TIMER_SMG_H_

#include "hal_data.h"

#endif /* TIMER_SMG_H_ */

Demonstration effect

insert image description here

main program

#include "hal_data.h"
#include <stdio.h>
#include "smg.h"
#include "timer_smg.h"

FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER

//数码管变量
uint8_t num1=0,num2=0,num3=0,num4=0;//4个数码管显示的数值
uint8_t num_flag=0;//4个数码管和冒号轮流显示,一轮刷新五次

fsp_err_t err = FSP_SUCCESS;
volatile bool uart_send_complete_flag = false;
void user_uart_callback (uart_callback_args_t * p_args)
{
    
    
    if(p_args->event == UART_EVENT_TX_COMPLETE)
    {
    
    
        uart_send_complete_flag = true;
    }
}

#ifdef __GNUC__                                 //串口重定向
    #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
    #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

PUTCHAR_PROTOTYPE
{
    
    
        err = R_SCI_UART_Write(&g_uart9_ctrl, (uint8_t *)&ch, 1);
        if(FSP_SUCCESS != err) __BKPT();
        while(uart_send_complete_flag == false){
    
    }
        uart_send_complete_flag = false;
        return ch;
}

int _write(int fd,char *pBuffer,int size)
{
    
    
    for(int i=0;i<size;i++)
    {
    
    
        __io_putchar(*pBuffer++);
    }
    return size;
}





/*******************************************************************************************************************//**
 * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function
 * is called by main() when no RTOS is used.
 **********************************************************************************************************************/
void hal_entry(void)
{
    
    
    /* TODO: add your own code here */

    /* Open the transfer instance with initial configuration. */
       err = R_SCI_UART_Open(&g_uart9_ctrl, &g_uart9_cfg);
       assert(FSP_SUCCESS == err);



/**********************数码管测试***************************************/
              ceshi_smg();
/**********************定时器开启***************************************/
    /* Initializes the module. */
    err = R_GPT_Open(&g_timer0_ctrl, &g_timer0_cfg);
    /* Handle any errors. This function should be defined by the user. */
    assert(FSP_SUCCESS == err);
    /* Start the timer. */
    (void) R_GPT_Start(&g_timer0_ctrl);


       while(1)
       {
    
    
       printf("hello world!123\n");
       R_BSP_SoftwareDelay(1000U, BSP_DELAY_UNITS_MILLISECONDS);
       }



#if BSP_TZ_SECURE_BUILD
    /* Enter non-secure code */
    R_BSP_NonSecureEnter();
#endif
}

Guess you like

Origin blog.csdn.net/qq_24312945/article/details/132010834