`在湖人的开发版中,集成了TM1650驱动数码管的硬件,用户可以根据TM1650的数据手册,实现相应的时序,就可以点亮数码管,具体的新建工程的过程,详见我的帖子“ 使用Haawking IDE 新建一个LED闪烁的工程 - 中科昊芯 DSP - 电子技术论坛 - 广受欢迎的专业电子论坛! (elecfans.com)”TM1650数据手册详情看附件
程序的具体实现代码主函数如下:
- /******************************************************************
- 文 档 名:HX_EXAM_DSC28034_IIC_TM1650_V0.0.1
- D S P:dsc28034
- IDE:haawking-workspace-V0.5.0
- 作 用:数码管从0开始加到999循环,S101按下,最左边的数码管加1,S102按下,
- 最左边的数码管减1,S103按下,最左边的数码管显示清零,
- S104按下,数码管的显示亮度增加。
- 说 明:晶振频率为120M,IIC例程
- 版 本:V0.0.1
- 时 间:2021年3月3日 15:48
- 作 者:shentingting
- [url=home.php?mod=space&uid=136761]@mail[/url] :tingting.shen@mail.haawking.com
- ******************************************************************/
-
- #include
- #include "dsc_config.h"
- #include "hx_rv32_dev.h"
- #include "hx_rv32_type.h"
- #include
- #include "xcustom.h"
- #include "IQmathLib.h"
- #include "TM1650_IIC.h"
- #include "timer0_base.h"
- extern char keyVal,keyReg,LigntVal;
- //void INTERRUPT cpu_timer0_isr(void);
- int Tmp = 0;
- char keyReg=0;
- int main(void)
- {
- int i,j;
- // volatile unsigned int i;
- // DisableDog(); //disable watchdog
- EALLOW;
- P_SysCtrlRegs->INTOSC1TRIM.bit.FINETRIM = 0;
- P_SysCtrlRegs->INTOSC2TRIM.bit.FINETRIM = 0;
- EDIS;
- InitSysCtrl();
- // softResetIIC_BUS();
- InitI2C_Gpio(); //io 初始化为IIC
- I2CA_Init(); //HW IIC初始化,100KHz
- LigntVal = 0x11; //BIT6到BIT4为亮度调节,BIT0是 1 开启/0关闭
- TM1650_Send(CMD_SEG, LigntVal); //1级亮度,开启显示
- TM1650_Send(DIG1, SEG7Table[0]); //GID1
- TM1650_Send(DIG2, SEG7Table[0]); //GID2
- TM1650_Send(DIG3, SEG7Table[0]); //GID3
- TM1650_Send(DIG4, SEG7Table[0]); //GID4
- Timer_Config( );
- DINT;
- InitPieCtrl();
- IER_DISABLE(0xffff); //禁能所有中断
- IFR_DISABLE(0xffff); //清所有中断标志位
- InitPieVectTable(); //初始化中断向量表
- EALLOW;
- PieVectTable.TINT0 = &cpu_timer0_isr;
- EDIS;
- IER_ENABLE(M_INT1); //使能中断组1 13 14
- PieCtrlRegs.PIEIER1.bit.INTx7 = 1; //使能中断组 1.7
- EINT; //使能全局中断
- ERTM; // Enable Global realtime interrupt DBGM
- while(1){
- if(timer_count >= 180)
- {
- timer_count = 0;
- Tmp++;
- if(Tmp > 999)
- Tmp = 0;
- TM1650_Send(DIG3,SEG7Table[Tmp/100]); //DIG3
- TM1650_Send(DIG2,SEG7Table[(Tmp/10)%10]); //DIG2
- TM1650_Send(DIG1,SEG7Table[Tmp%10]);
- }
- TM1650_Read(CMD_KEY,&keyVal);
- if(keyVal == 0x44) //S101按下
- {
- keyReg++;
- keyReg&=0x0f;
- TM1650_Send(DIG4,SEG7Table[keyReg]);
- for(i = 0;i < 1000;i++)
- {
- for(j = 0;j < 1000;j++)
- {}
- }
- }
- if(keyVal == 0x4c)//S102按下,
- {
- keyReg--;
- keyReg&=0x0f;
- TM1650_Send(DIG4,SEG7Table[keyReg]);//1级亮度,开启显示
- for(i = 0;i < 1000;i++)
- {
- for(j = 0;j < 1000;j++)
- {}
- }
- }
- if(keyVal == 0x54)//S103按下
- {
- keyReg=0;
- TM1650_Send(DIG4,SEG7Table[keyReg]); //DIG4 显示keyReg 变量
- for(i = 0;i < 1000;i++)
- {
- for(j = 0;j < 1000;j++)
- {}
- }
- }
- if(keyVal == 0x5C)
- {
- LigntVal = (LigntVal+0x10)&0x7F; //TM1650的CMD_SEG寄存器,其高四位为亮度调节,最后1位是开启、关闭显示
- TM1650_Send(CMD_SEG,LigntVal);//调节亮度,开启显示
- for(i = 0;i < 1000;i++)
- {
- for(j = 0;j < 1000;j++)
- {}
- }
- }
- }
- return 0;
- }
- // ----------------------------------------------------------------------------
复制代码
- #include "TM1650_IIC.h"
- #define SLAVE_ADDRESS 0x36
- I2CSlaveMSG TM1650;//定义一个TM1650结构体
- I2CSlaveMSG AT24C02Msg;
- I2CSlaveMSG *PtrMsg[]={&TM1650,&AT24C02Msg};//定义一个指针数组,存放I2C结构体,这里包含TM1650和EEPROM
- char keyVal,LigntVal;
- uint8_t SEG7Table[]={
- //共阴极
- // 0x3f,0x06,0x5b,0x4f,
- // 0x66,0x6d,0x7d,0x07,
- // 0x7f,0x6f,0x77,0x7c,
- // 0x39,0x5e,0x79,0x71
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71
- //共阳极
- // 0xc0,0xf9,0xa4,0xb0,
- // 0x99,0x92,0x82,0xf8,
- // 0x80,0x90,0x88,0x83,
- // 0xc6,0xa1,0x86,0x8e,
- };
- void InitI2C_Gpio()
- {
- EALLOW;
- GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0; // Enable pull-up for GPIO32 (SDAA)
- GpioCtrlRegs.GPBPUD.bit.GPIO33 = 0; // Enable pull-up for GPIO33 (SCLA)
- GpioCtrlRegs.GPBQSEL1.bit.GPIO32 = 3; // Asynch input GPIO32 (SDAA)
- GpioCtrlRegs.GPBQSEL1.bit.GPIO33 = 3; // Asynch input GPIO33 (SCLA)
- GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 1; // Configure GPIO32 for SDAA operation
- GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 1; // Configure GPIO33 for SCLA operation
- EDIS;
- }
- void I2CA_Init(void)
- {
- EALLOW;
- SysCtrlRegs.PCLKCR0.bit.I2CAENCLK = 1; // Turn I2C module clock on
- EDIS;
- I2caRegs.I2CIC_ENABLE.bit.ENABLE = 0; //关闭I2C
- I2caRegs.I2CIC_CON.bit.IC_SLAVE_DISABLE = 1; //禁止 从机模式
- I2caRegs.I2CIC_CON.bit.IC_RESTART_EN = 1; //使能重启模式
- I2caRegs.I2CIC_CON.bit.IC_10BITADDR_MASTER_or_IC_10BITADDR_MASTER_rd_only = 0;//使用 7 bits 地址格式
- I2caRegs.I2CIC_CON.bit.SPEED = 1; //设置标准模式
- I2caRegs.I2CIC_CON.bit.MASTER_MODE = 1; //主设备使能
- // I2caRegs.I2CIC_TAR.bit.IC_TAR = SLAVE_ADDRESS;
- I2caRegs.I2CIC_SS_SCL_HCNT.bit.IC_SS_SCL_HCNT = 0x000a; //设置速度
- I2caRegs.I2CIC_SS_SCI_LCNT.bit.IC_SS_SCL_LCNT = 0x000a;
- //I2caRegs.I2CIC_TAR.bit.IC_TAR = 0;
- I2caRegs.I2CIC_TX_TL.bit.TX_TL = 4; //入口深度
- I2caRegs.I2CIC_RX_TL.bit.RX_TL = 4;
- // I2caRegs.I2CIC_RAW_INTR_STAT.bit.MST_ON_HOLD=1;
- I2caRegs.I2CIC_RAW_INTR_STAT.bit.MST_ON_HOLD=1;
- // I2caRegs.I2CIC_DATA_CMD.bit.STOP = 1;
- // I2caRegs.I2CIC_ACK_GENERAL_CALL.bit.ACK_GEN_CALL = 1; //设置ack应答
- I2caRegs.I2CIC_ENABLE.bit.ENABLE = 1; //开启I2C
- }
- //IIC底层发送函数
- //IIC Maste Mode,发送模式
- //I2caRegs.I2CCNT 为发送的字节长度,不包括从器件的物理地址
- //发送格式:START ADDRESS CONTROL_BYTE DA_BYTE STOP
- //当写入I2caRegs.I2CMDR.all = 0x2E20,硬件IIC进行操作,STOP信号结束
- Uint16 I2CA_Tx_STOP(struct I2CSlaveMSG *msg)
- {
- Uint16 i;
- // if(I2caRegs.I2CIC_RAW_INTR_STAT.bit.STOP_DET == 1)
- // {
- // return I2C_STP_NOT_READY_ERROR;
- // }
- // if(I2caRegs.I2CIC_ENABLE_STATUS.bit.IC_EN ==0)
- I2caRegs.I2CIC_ENABLE.bit.ENABLE = 0; //关闭I2C
- I2caRegs.I2CIC_TAR.bit.IC_TAR = msg->SlavePHYAddress; //设置访问从器件地址,物理地址 //要发送的地址 主机
- I2caRegs.I2CIC_ENABLE.bit.ENABLE = 1; //开启I2C
- for(i=0;i<((msg->Len)-1);i++) // 小于等于4个byte的数据
- {
- I2caRegs.I2CIC_DATA_CMD.bit.DAT = msg->MsgOutBuffer[i];
- }
- // I2caRegs.I2CIC_DATA_CMD.all = 0;
- // I2caRegs.I2CIC_DATA_CMD.all = 0;
- // I2caRegs.I2CIC_DATA_CMD.all = msg->SlavePHYAddress;
- I2caRegs.I2CIC_DATA_CMD.all = (msg->MsgOutBuffer[msg->Len - 1]|0x200);
- while(0 == I2caRegs.I2CIC_STATUS.bit.TFE);
- for(i = 0;i < 2000;i++)
- {
- }
- return I2C_SUCCESS;
- }
- Uint16 I2CA_Rxdata_STOP(struct I2CSlaveMSG *msg)
- {
- I2caRegs.I2CIC_ENABLE.bit.ENABLE = 0;
- I2caRegs.I2CIC_TAR.bit.IC_TAR = msg->SlavePHYAddress; //设置访问从器件地址,物理地址
- I2caRegs.I2CIC_ENABLE.bit.ENABLE = 1;
- I2caRegs.I2CIC_DATA_CMD.bit.DAT = 0X49;
- I2caRegs.I2CIC_DATA_CMD.bit.CMD = 1;
- // I2caRegs.I2CIC_DATA_CMD.all = (0x100|;
- while(0==I2caRegs.I2CIC_STATUS.bit.RFNE);
- return I2C_SUCCESS;
- }
- void TM1650_Send(char addr,char data)
- {
- TM1650.Len = 1; //1个数据(不含物理地址)
- TM1650.SlavePHYAddress = addr;
- TM1650.MsgOutBuffer[0] = data; //8级亮度 开启显示
- I2CA_Tx_STOP(&TM1650); //发送数据,带STOP
- TM1650.IIC_TimerOUT = 0;
- TM1650.MasterStatus = IIC_WRITE;
- }
- void TM1650_Read(char addr,char *data)
- {
- Uint16 i;
- TM1650.Len = 1;
- TM1650.SlavePHYAddress = addr;
- //*data = I2caRegs.I2CIC_DATA_CMD.bit.DAT;
- I2CA_Rxdata_STOP(&TM1650);//发送数据,带STOP
- *data = I2caRegs.I2CIC_DATA_CMD.bit.DAT;
- TM1650.IIC_TimerOUT = 0;
- TM1650.MasterStatus = IIC_READ_STEP2;
- TM1650.MasterStatus = IIC_READ_STEP3;
- // while(0==I2caRegs.I2CIC_STATUS.bit.RFNE)
- // {
- // //*data = I2caRegs.I2CIC_DATA_CMD.bit.DAT;
- // };
- for(i = 0;i < 2000;i++)
- {
- }
- }
- #define DELAY_FOR 10 // 60Mhz 43.5K
- void softResetIIC_BUS()
- {
- Uint16 i,j;
- EALLOW;
- SDA_H();
- SDAMUX = 0;
- SDADIR = 1;
- SDA_L();
- CLKMUX = 0;
- CLKDIR =1;
- EDIS;
- SDA_H();for(j=0;j
- CLK_H();for(j=0;j
- SDA_L();for(j=0;j
- CLK_L();for(j=0;j
- SDA_H();for(j=0;j
- for(i=0;i<9;i++)
- {
- CLK_H();for(j=0;j
- CLK_L();for(j=0;j
- }
- CLK_H();for(j=0;j
- SDA_L();for(j=0;j
- CLK_L();for(j=0;j
- CLK_H();for(j=0;j
- SDA_H();for(j=0;j
- CLK_L();for(j=0;j
- //软件复位完毕之后,要重新初始化IIC IO 为IIC 硬件IO口 调用
- InitI2C_Gpio();
- }
复制代码
timer.c
- #include "timer0_base.h"
- unsigned int timer_count;
- void Timer_Config(void)
- {
- //EALLOW;
- //PieVectTable.TINT0 = &cpu_timer0_isr; //
- InitCpuTimers();
- ConfigCpuTimer(&CpuTimer0, 60, 1000);
- CpuTimer0Regs.TCR.all = 0x4001;
- }
- void INTERRUPT cpu_timer0_isr(void)
- {
- timer_count++;
- PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
- }
复制代码
timer.h
- #ifndef TIMER0_BASE_H_
- #define TIMER0_BASE_H_
- #include "DSC28034_Device.h"
- #include "DSC28034_Examples.h"
- extern unsigned int timer_count;
- void Timer_Config(void);
- void INTERRUPT cpu_timer0_isr(void);
- #endif /* TIMER0_BASE_H_ */
复制代码
实验结果
工程现象数码管显示从0开始加到999循环,S101按下,最左边的数码管加1,S102按下,最左边的数码管减1,S103按下,最左边的数码管显示清零,S104按下,数码管的显示亮度增加。
实验现象如下:
`
|