前言
早就听说中科昊芯是一家专注于数字信号处理器(DSP)领域的创新企业,其产品在工业控制、新能源汽车、光伏储能等领域有广泛应用。于是抱着试试看的心理申请了中科昊芯Core_DSC280025C
开发板,没想到竟然通过了,收到开发板心里很是激动,红色的开发板和米黄色的
仿真器各有一个彩色包装盒,从这里可以看出厂方很大气,仿真器做的也很大气。
Core_DSC280025C核心板使用了昊芯HXS320F28025C RISC-VDSP芯片,该芯片集成了吴芯自主研发的H28x内核。核心板包括:14针的下载、调试接口、供电
电源接口、复位、指示灯,板载一个按键,2个LED,以及两侧的对外扩展PIN脚等,除JTAG等IO外,所有外设IO全部引出,开发者可通过杜邦跳线进行功能验证,便于进行不同应用开发。
IO口引出这一点有点像Arduino,方便开发者使用,但习惯于用51单片机写流水灯这类程序的初学者可能不太习惯,随着不断的使用,你会习惯这种用法的。
开心归开心,学习一门新技术可不是一件容易的事情,花了近半个月的时间研究这款开发版,才初步有进展,能点亮LED,会使用按键中断,可以用模拟SPI驱动74HC595,会定时器的最简单应用,能驱动8位数码管。笔者参考厂家的例程,学习了定时器的用法,并参考
STM32单片机的资料,编写了定时器计时,用软件模拟SPI驱动数码管显示程序,代码如下。
程序
程序的编写要借助厂方的例程模板。
1、主函数
- //min.c
- /******************************************************************
- 文 档 名: HX_DSC280025_CPUtiMER
- 开 发 环 境: Haawking IDE V2.3.10Pre
- 开 发 板:
- D S P: DSC280025
- 使 用 库:
- 作 用: 定时器0中断秒计时,74HC595驱动8位数码管的段码,模拟SPI驱动,
- 扫描位码经PNP管倒相驱动,从87650000秒开始显示,后4位为秒加1计数,满
- 10000秒,后四位清0,主程序扫描显示
- 硬件: //GPIO0接595的Pin_14数据
- //GPIO1接595的Pin_11移位脉冲
- //GPIO2接595的Pin_12锁存,并出
- //74HC595的PIN_10接电源
- //GPIO4~GPIO11驱动位码
- 说 明: FLASH工程
- -------------------------- 例程使用说明 --------------------------
- 功能描述:芯片主频160MHz,定时器中断触发
- 版 本: moni_SPI_2
- 与moni_SPI_1比较,定义了timer.h和timer.c文件,使主程序代码精简了
- 时 间: 2025年7月10日
- 作 者:
- @ mail:
- ******************************************************************/
- //
- // Included Files
- //
- #include "driverlib.h"
- #include "device.h"
-
- #include
- #include "IQmathLib.h"
- #include "hx_intrinsics.h"
- #include "driverlib.h"
- #include "device.h"
- #include "hx_fintdiv.h"
- #include "moni_SPI.h" //自定义的模拟SPI头文件
- #include "timer.h" //自定义的模拟timer头文件
- //0~9,熄灭共阳极数码管字段码
- unsigned char disp[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xff};
- unsigned char buff[8]={1,2,3,4,5,6,7,8}; //显示缓冲
- unsigned int t0_sec = 0; //timer0中断次数,秒计时
- //unsigned int cnt = 0;
- //声明3个定时器中断次数
- uint16_t cpuTimer0IntCount; //定时器用的变量为何不能放timer.h定义???
- uint16_t cpuTimer1IntCount;
- uint16_t cpuTimer2IntCount;
- //=============分离变量=================
- void fenli_data(unsigned int data)
- {
- unsigned char i;
- for(i=0;i<4;i++) //共4位数码
- {
- buff[i] = data % 10; //先分离个位
- data /= 10;
- }
- /******************
- //采用4位显示,高位为0,不点亮
- if(0 == buff[3]) //如果千位为0
- {
- buff[3] = 10; //千位不显示
- if(0 == buff[2]) //如果百位为0
- {
- buff[2] = 10; //百位不显示
- if(0 == buff[1]) //如果十位为0
- buff[1] = 10; //十位不显示
- }
- }
- ******************/
- }
- void SEG_dit_Config(void) //8个扫描位初始化
- {
- unsigned char x;
- GPIO_setPinConfig(GPIO_4_GPIO4); //在gpio.h声明,在gpio.c中定义
- GPIO_setPinConfig(GPIO_5_GPIO5); //在gpio.h声明,在gpio.c中定义
- GPIO_setPinConfig(GPIO_6_GPIO6); //在gpio.h声明,在gpio.c中定义
- GPIO_setPinConfig(GPIO_7_GPIO7); //在gpio.h声明,在gpio.c中定义
- GPIO_setPinConfig(GPIO_8_GPIO8); //在gpio.h声明,在gpio.c中定义
- GPIO_setPinConfig(GPIO_9_GPIO9); //在gpio.h声明,在gpio.c中定义
- GPIO_setPinConfig(GPIO_10_GPIO10); //在gpio.h声明,在gpio.c中定义
- GPIO_setPinConfig(GPIO_11_GPIO11); //在gpio.h声明,在gpio.c中定义
- for(x=4;x<12;x++) //IO.04~IO.11的设置
- {
- GPIO_setDirectionMode(x, GPIO_DIR_MODE_OUT); //在gpio.h声明,在gpio.c中定义
- GPIO_setPadConfig(x, GPIO_PIN_TYPE_PULLUP); //在gpio.h声明,在gpio.c中定义
- GPIO_setQualificationMode(x, GPIO_QUAL_SYNC); //在gpio.h声明,在gpio.c中定义
- }
- }
- int main(void)
- {
- unsigned char x,y;
- Device_init(); //device文件夹下device.c里定义
- /* 模拟SPI端口初始化 */
- moni_SPI_Config();
- //扫描位初始化
- SEG_dit_Config();
- /*GPIO配置,用于显示中断状态*/
- GPIO_config();
- /*初始化PIE与清PIE寄存器,关CPU中断*/
- Interrupt_initModule();
- /*初始化中断向量表*/
- Interrupt_initVectorTable();
- timer_ISR_peizhi( ); //定时器配置
- /*打开全局中断*/
- EINT;
- while (1)
- //IO011~IO4自左向右接数码管扫描位1~8,倒相,段码74Hc595驱动
- {
- fenli_data(t0_sec); //把秒分离填充到右边4位显示缓冲区
- for(y = 0; y < 30; y++) //刷新扫描30次,可以480ms刷新一次数据
- {for(x = 0; x < 8; x++) //8个数码管动态显示
- {
- hc595_display(disp[buff[7-x]]); //模拟SPI发送函数,发送段码
- switch(x) //扫描位,显示清晰
- {
- case 0:
- GPIO_writePin(4,0); //开通扫描位,倒相
- DEVICE_DELAY_US(2000); //延时2ms
- GPIO_writePin(4,1); //熄灭扫描位
- break;
- case 1:
- GPIO_writePin(5,0); //开通扫描位,倒相
- DEVICE_DELAY_US(2000); //延时2ms
- GPIO_writePin(5,1); //熄灭扫描位
- break;
- case 2:
- GPIO_writePin(6,0); //开通扫描位,倒相
- DEVICE_DELAY_US(2000); //延时2ms
- GPIO_writePin(6,1); //熄灭扫描位
- break;
- case 3:
- GPIO_writePin(7,0); //开通扫描位,倒相
- DEVICE_DELAY_US(2000); //延时2ms
- GPIO_writePin(7,1); //熄灭扫描位
- break;
- case 4:
- GPIO_writePin(8,0); //开通扫描位,倒相
- DEVICE_DELAY_US(2000); //延时2ms
- GPIO_writePin(8,1); //熄灭扫描位
- break;
- case 5:
- GPIO_writePin(9,0); //开通扫描位,倒相
- DEVICE_DELAY_US(2000); //延时2ms
- GPIO_writePin(9,1); //熄灭扫描位
- break;
- case 6:
- GPIO_writePin(10,0); //开通扫描位,倒相
- DEVICE_DELAY_US(2000); //延时2ms
- GPIO_writePin(10,1); //熄灭扫描位
- break;
- case 7:
- GPIO_writePin(11,0); //开通扫描位,倒相
- DEVICE_DELAY_US(2000); //延时2ms
- GPIO_writePin(11,1); //熄灭扫描位
- break;
- }
- }
- /*****************
- { //类似这样的代码共8组,显示时最左边的那位不该亮的段也有点亮,即消隐不良
- //hc595_display(disp[10]); //清除段码,为何不清屏也行
- hc595_display(disp[buff[0]]); //模拟SPI发送函数,发送段码
- GPIO_writePin(4,1); //熄灭
- GPIO_writePin(4,0);
- GPIO_writePin(5,1);
- GPIO_writePin(6,1);
- GPIO_writePin(7,1);
- GPIO_writePin(8,1);
- GPIO_writePin(9,1);
- GPIO_writePin(10,1);
- GPIO_writePin(11,1);
- DEVICE_DELAY_US(3000); //延时1ms
- }
- ***********************************************/
- }
- }
- }
2、moni_SPI.c
- /*
- * moni_SPI.c
- *
- * Created on: 2025年7月10日
- * Author: 于
- */
- #include "moni_SPI.h"
- #include "system.h"
- //74HC595的Pin_10接VCC
- #define HC595_SDA_PIN 0 //GPIO0接595的Pin_14数据
- #define HC595_SFT_PIN 1 //GPIO1接595的Pin_11移位脉冲
- #define HC595_LCK_PIN 2 //GPIO2接595的Pin_8锁存,并出
- #define HC595_SDA_1 GPIO_writePin(HC595_SDA_PIN,1 )
- #define HC595_SDA_0 GPIO_writePin(HC595_SDA_PIN,0)
- #define HC595_SFT_1 GPIO_writePin(HC595_SFT_PIN ,1 )
- #define HC595_SFT_0 GPIO_writePin(HC595_SFT_PIN ,0 )
- #define HC595_LCK_1 GPIO_writePin(HC595_LCK_PIN,1 )
- #define HC595_LCK_0 GPIO_writePin(HC595_LCK_PIN,0 )
- void moni_SPI_Config(void)
- {
- GPIO_setPinConfig(GPIO_0_GPIO0); //在gpio.h声明,在gpio.c中定义
- GPIO_setDirectionMode(0, GPIO_DIR_MODE_OUT); //在gpio.h声明,在gpio.c中定义
- GPIO_setPadConfig(0, GPIO_PIN_TYPE_PULLUP); //在gpio.h声明,在gpio.c中定义
- GPIO_setQualificationMode(0, GPIO_QUAL_SYNC); //在gpio.h声明,在gpio.c中定义
- GPIO_setPinConfig(GPIO_1_GPIO1); //在gpio.h声明,在gpio.c中定义
- GPIO_setDirectionMode(1, GPIO_DIR_MODE_OUT); //在gpio.h声明,在gpio.c中定义
- GPIO_setPadConfig(1, GPIO_PIN_TYPE_PULLUP); //在gpio.h声明,在gpio.c中定义
- GPIO_setQualificationMode(1, GPIO_QUAL_SYNC); //在gpio.h声明,在gpio.c中定义
- GPIO_setPinConfig(GPIO_2_GPIO2); //在gpio.h声明,在gpio.c中定义
- GPIO_setDirectionMode(2, GPIO_DIR_MODE_OUT); //在gpio.h声明,在gpio.c中定义
- GPIO_setPadConfig(2, GPIO_PIN_TYPE_PULLUP); //在gpio.h声明,在gpio.c中定义
- GPIO_setQualificationMode(2, GPIO_QUAL_SYNC); //在gpio.h声明,在gpio.c中定义
- }
- //系统时钟24MHZ
- void hc595_display(unsigned char dat) //模拟SPI发送函数
- {
- unsigned char i=0;
- for(i=0; i<8; i++)
- {
- if((dat&0x80) == 0) //发送高位
- HC595_SDA_0;
- else
- HC595_SDA_1;
- dat <<= 1; //次高位移至高位
- //HC595_SFT_0; //移位时钟拉低
- //DEVICE_DELAY_US(1); //延时有必要可加长
- HC595_SFT_1; //移位时钟上升沿时,数据移入595
- DEVICE_DELAY_US(1); //延时1us
- HC595_SFT_0;
- DEVICE_DELAY_US(1); //延时1us
- }
- //HC595_LCK_0;
- //DEVICE_DELAY_US(1); //延时1us
- HC595_LCK_1; //上升沿将数据送到输出锁存器
- DEVICE_DELAY_US(1); //延时1us
- HC595_LCK_0;
- DEVICE_DELAY_US(1); //延时1us
- }
3、moni_SPI.h
- /*
- * moni_SPI.h
- *
- * Created on: 2025年7月10日
- * Author: 于
- */
- #ifndef SRC_MONI_SPI_H_
- #define SRC_MONI_SPI_H_
- void moni_SPI_Config(void); //模拟SPI初始化
- void hc595_display(unsigned char dat); //模拟SPI发送函数
- #endif /* SRC_MONI_SPI_H_ */
4、system.h
- /*
- * system.h
- *
- * Created on: 2025年7月10日
- * Author: 于
- */
- #ifndef SRC_SYSTEM_H_
- #define SRC_SYSTEM_H_
- #include "driverlib.h"
- #include "device.h"
- #endif /* SRC_SYSTEM_H_ */
5、timer.c
- /*
- * timer.c
- *
- * Created on: 2025年7月12日
- * Author: 于
- */
- #include "timer.h"
- //声明3个定时器中断次数,这三个变量main.c中不用,可以只在此声明
- extern uint16_t cpuTimer0IntCount; //定时器用的变量为何不能放timer.h定义???
- extern uint16_t cpuTimer1IntCount;
- extern uint16_t cpuTimer2IntCount;
- //
- // initCPUTimers - This function initializes all three CPU timers
- // to a known state.
- //
- void initCPUTimers(void)
- {
- /*时钟周期初始化,配置成最大值*/
- CPUTimer_setPeriod(CPUTIMER0_BASE, 0xFFFFFFFF);
- CPUTimer_setPeriod(CPUTIMER1_BASE, 0xFFFFFFFF);
- CPUTimer_setPeriod(CPUTIMER2_BASE, 0xFFFFFFFF);
- /*时钟周期分频配置:基地址CPUTIMERx_BASE,n+1分频*/
- CPUTimer_setPreScaler(CPUTIMER0_BASE, 0);
- CPUTimer_setPreScaler(CPUTIMER1_BASE, 0);
- CPUTimer_setPreScaler(CPUTIMER2_BASE, 0);
- /*CpuTimer定时器暂停,便于配置:基地址CPUTIMERx_BASE*/
- CPUTimer_stopTimer(CPUTIMER0_BASE);
- CPUTimer_stopTimer(CPUTIMER1_BASE);
- CPUTimer_stopTimer(CPUTIMER2_BASE);
- /*CpuTimer定时器暂停,便于配置:基地址CPUTIMERx_BASE*/
- CPUTimer_reloadTimerCounter(CPUTIMER0_BASE);
- CPUTimer_reloadTimerCounter(CPUTIMER1_BASE);
- CPUTimer_reloadTimerCounter(CPUTIMER2_BASE);
- /*重置定时器CpuTimer定时器中断计数*/
- cpuTimer0IntCount = 0;
- cpuTimer1IntCount = 0;
- cpuTimer2IntCount = 0;
- }
- //
- // configCPUTimer - This function initializes the selected timer to the
- // period specified by the "freq" and "period" parameters. The "freq" is
- // entered as Hz and the period in uSeconds. The timer is held in the stopped
- // state after configuration.
- //
- void configCPUTimer(uint32_t cpuTimer, float freq, float period)
- {
- uint32_t temp;
- //
- // Initialize timer period:
- //
- temp = (uint32_t)((freq / 1000000) * period);
- CPUTimer_setPeriod(cpuTimer, temp);
- //
- // Set pre-scale counter to divide by 1 (SYSCLKOUT):
- //
- CPUTimer_setPreScaler(cpuTimer, 0);
- //
- // Initializes timer control register. The timer is stopped, reloaded,
- // free run disabled, and interrupt enabled.
- // Additionally, the free and soft bits are set
- //
- CPUTimer_stopTimer(cpuTimer);
- CPUTimer_reloadTimerCounter(cpuTimer);
- CPUTimer_setEmulationMode(cpuTimer,
- CPUTIMER_EMULATIONMODE_STOPAFTERNEXTDECREMENT);
- CPUTimer_enableInterrupt(cpuTimer);
- //
- // Resets interrupt counters for the three cpuTimers
- //
- if (cpuTimer == CPUTIMER0_BASE)
- {
- cpuTimer0IntCount = 0;
- }
- else if(cpuTimer == CPUTIMER1_BASE)
- {
- cpuTimer1IntCount = 0;
- }
- else if(cpuTimer == CPUTIMER2_BASE)
- {
- cpuTimer2IntCount = 0;
- }
- }
- //==========================================================
- //定时器配置
- void timer_ISR_peizhi(void)
- {
- /*初始化定时器时钟cpuTimer*/
- initCPUTimers();
- /*配置定时器时钟cpuTimer:基地址CPUTIMERx_BASE,频率MHz,周期值us*/
- configCPUTimer(CPUTIMER0_BASE, DEVICE_SYSCLK_FREQ, 1000000); //1s
- configCPUTimer(CPUTIMER1_BASE, DEVICE_SYSCLK_FREQ, 2000000); //2s
- configCPUTimer(CPUTIMER2_BASE, DEVICE_SYSCLK_FREQ, 4000000); //4s
- /*定时器时钟cpuTimer使能:基地址CPUTIMERx_BASE*/
- CPUTimer_enableInterrupt(CPUTIMER0_BASE);
- CPUTimer_enableInterrupt(CPUTIMER1_BASE);
- CPUTimer_enableInterrupt(CPUTIMER2_BASE);
- /*cpuTimer相应的定时器时钟PIE与CPU中断使能:中断INT_TIMERx*/
- Interrupt_enable(INT_TIMER0);
- Interrupt_enable(INT_TIMER1);
- Interrupt_enable(INT_TIMER2);
- /*cpuTimer定时器启动:基地址CPUTIMERx_BASE*/
- CPUTimer_startTimer(CPUTIMER0_BASE);
- CPUTimer_startTimer(CPUTIMER1_BASE);
- CPUTimer_startTimer(CPUTIMER2_BASE);
- /*中断入口INT_TIMER0指向相应中断服务程序,执行cpuTimer0ISR*/
- Interrupt_register(INT_TIMER0, &cpuTimer0ISR);
- /*中断入口INT_TIMER1指向相应中断服务程序,执行cpuTimer1ISR*/
- Interrupt_register(INT_TIMER1, &cpuTimer1ISR);
- /*中断入口INT_TIMER2指向相应中断服务程序,执行cpuTimer2ISR*/
- Interrupt_register(INT_TIMER2, &cpuTimer2ISR);
- }
- //==========================================================
- //
- // cpuTimer0ISR - Counter for CpuTimer0
- //
- extern unsigned int t0_sec; //timer0中断次数,秒计时
- __interrupt void cpuTimer0ISR(void)
- {
- cpuTimer0IntCount++;
- // /*GPIO31翻转闪灯,每进一次中断,翻转一次*/
- //GPIO_togglePin(31);
- t0_sec++;
- if(10000 == t0_sec)
- t0_sec = 0;
- //
- // Acknowledge this interrupt to receive more interrupts from group 1
- //
- Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP1);
- }
- //
- // cpuTimer1ISR - Counter for CpuTimer1
- //
- __interrupt void cpuTimer1ISR(void)
- {
- cpuTimer1IntCount++;
- // /*GPIO34翻转闪灯,每进一次中断,翻转一次*/
- //GPIO_togglePin(34);
- }
- //
- // cpuTimer2ISR - Counter for CpuTimer2
- //
- __interrupt void cpuTimer2ISR(void)
- {
- cpuTimer2IntCount++;
- // /*GPIO31与GPIO34翻转闪灯,每进一次中断,翻转一次*/
- //GPIO_togglePin(31);
- //GPIO_togglePin(34);
- }
- void GPIO_config(void) //普通IO配置
- {
- EALLOW;
- /*GPIO31的IO功能配置*/
- GPIO_setPinConfig(GPIO_31_GPIO31);
- /*GPIO31的IO方向配置:GPIO_DIR_MODE_OUT输出,GPIO_DIR_MODE_IN输入*/
- GPIO_setDirectionMode(31, GPIO_DIR_MODE_OUT);
- /*GPIO31的上拉翻转配置:
- * GPIO_PIN_TYE_STD上拉翻转输出或浮点输入,GPIO_PIN_TYPE_PULLUP-上拉翻转输入
- * GPIO_PIN_TYPE_INVERT翻转极性输入,GPIO_PIN_TYPE_OD开漏输出*/
- GPIO_setPadConfig(31, GPIO_PIN_TYPE_STD);
- /*GPIO31的采样配置:GPIO_QUAL_SYNC同步采样,GPIO_QUAL_3SAMPLE-3采样
- * GPIO_QUAL_6SAMPLE-6采样,GPIO_QUAL_ASYNC异步采样*/
- GPIO_setQualificationMode(31, GPIO_QUAL_SYNC);
- GPIO_setPinConfig(GPIO_34_GPIO34);
- GPIO_setDirectionMode(34, GPIO_DIR_MODE_OUT);
- GPIO_setPadConfig(34, GPIO_PIN_TYPE_STD);
- GPIO_setQualificationMode(34, GPIO_QUAL_SYNC);
- EDIS;
- }
- //
- // End of File
- //
6.timer.h
- /*
- * timer.h
- *
- * Created on: 2025年7月12日
- * Author: 于
- */
- #ifndef SRC_TIMER_H_
- #define SRC_TIMER_H_
- #include "driverlib.h"
- #include "device.h"
- //
- // Globals
- //
- //
- // Function Prototypes
- //
- __interrupt void cpuTimer0ISR(void);
- __interrupt void cpuTimer1ISR(void);
- __interrupt void cpuTimer2ISR(void);
- void initCPUTimers(void);
- void configCPUTimer(uint32_t, float, float);
- void GPIO_config(void); //普通IO配置
- //uint16_t bss; //为何不屏蔽此句会出错?h文件里不能声明变量?
- void initCPUTimers(void);
- void configCPUTimer(uint32_t cpuTimer, float freq, float period);
- void timer_ISR_peizhi(void); //定时器配置
- #endif /* SRC_TIMER_H_ */