某年写个番禺龙美游戏机IO板用到了STM32F103VBT6及TM1629.写下了一个驱动程序。希望能帮助到有需要的朋友。不多说了,代码如下:
#ifdef GAME
#include "stm32f10x.h"
#include "stdio.h"
//TM1629片选 //STB
#define TM1629_STB GPIOC
#define TM1629_CSA GPIOC, GPIO_Pin_0
#define TM1629_CSB GPIOC, GPIO_Pin_1
#define TM1629_CSC GPIOC, GPIO_Pin_2
#define TM1629_CSD GPIOC, GPIO_Pin_3
//TM1629信号脚
#define TM1629_SCLK GPIOC, GPIO_Pin_6
#define TM1629_MOSI GPIOC, GPIO_Pin_7
#define TM1629_MISO GPIOE, GPIO_Pin_14
//键盘指示灯使能
#define KEY_LAMP_EN GPIOC, GPIO_Pin_4
#define CPU_LAMP_EN GPIOC, GPIO_Pin_5
//TM1629命令
#define TM1629_ADDR_INC 0x40
#define TM1629_ADDR_FIXED 0x44
#define TM1629_ADDR_BASE 0xc0
#define TM1629_DISP_OFF 0x80
#define TM1629_DISP_MINON 0x88
#define TM1629_DISP_MAXON 0x8f
#define TM1629_DISP_ONX 7
#define TM1629_DISP_ON (0x88+TM1629_DISP_ONX)
#define TM1629_KEY_READ 0x42
//TM1629延时
#define TM1629_DELAY 2
//TM1629应用
#define TM1629_LED_DARK 0x00
#define TM1629_LED_BRIGHT 0x7F
#define TM1629_LED_BRIGHT_DOT 0xFF
#define TM1629_CS_MASK 0xFFF0
#ifdef PART5
#define TM1629_CS_NONE 7
#else
#define TM1629_CS_NONE 0
#endif
const unsigned char TM1629_CODE[16]=
{0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0X7C, 0x39, 0x5E, 0x79, 0x71};
const unsigned char TM1629_CODE_LED[16]=
{0xBF, 0x86, 0xDB, 0xCF, 0xE6, 0xED, 0xFD, 0x87, 0xFF, 0xEF, 0xF7, 0XFF, 0xB9, 0xBF, 0xF9, 0xF1};
const unsigned char TM1629_SEGMENT[8]=
{0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
void TM1629_Test(void);
void TM1629_Configuration(void);
void TM1629_Write_Byte(uint8_t wrdata);
void TM1629_Write_CMD(uint8_t chip,uint8_t wrdata);
void TM1629_Write_CMD2(uint8_t chip,uint8_t wrdata);
uint8_t TM1629_Read_Byte(void);
uint32_t TM1629_Read_Keys(unsigned char chip);
void TM1629_Activation(uint8_t chip);
void TM1629_Write_Chip(uint8_t chip,uint8_t *led_data);
void TM1629_Write_Led(uint8_t chip,uint8_t led_index,uint8_t led_data);
void TM1629_Write_Leds(uint8_t chip,uint8_t index,uint32_t *data,uint8_t length);
void TM1629_Write_Digital(uint8_t chip,uint8_t led_index,uint8_t led_data);
void TM1629_Write_3Digital(uint8_t chip,uint8_t led_index,uint32_t led_data);
void TM1629_Write_Digitals(uint8_t chip,uint8_t led_index,uint32_t led_data,uint8_t digit_limit);
void TM1629_Write_Segment(uint8_t chip,uint8_t led_index,uint8_t led_data);
extern void SEND_RS485_STRING(char *s);
extern void Delay(__IO uint32_t nTime);
void TM1629_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
uint32_t i,j;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(KEY_LAMP_EN);
GPIO_ResetBits(CPU_LAMP_EN);
TM1629_Activation(TM1629_CS_NONE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 ;
GPIO_Init(GPIOE, &GPIO_InitStructure);
//全部熄灭
for(i=1;i<=8;i++)
{for(j=0;j<16;j++){TM1629_Write_Led(i,j,TM1629_LED_DARK);}}
GPIO_ResetBits(KEY_LAMP_EN);
}
void TM1629_Write_Byte(uint8_t wrdata)
{
uint8_t i; uint32_t j;
for(i=0;i<8;i++)
{
GPIO_ResetBits(TM1629_SCLK); //拉低时钟
if(wrdata&0x01) //最低位判断
{GPIO_SetBits(TM1629_MOSI); } //最低位为1,则写1到DIN口
else
{GPIO_ResetBits(TM1629_MOSI);} //最低位为0,则写0到DIN口
for(j=0;j //delay
GPIO_SetBits(TM1629_SCLK); //拉高时钟
for(j=0;j //delay
wrdata>>=1; //数据移到下一位
}
}
uint8_t TM1629_Read_Byte(void)
{
uint8_t i,rddata=0x0; uint32_t j;
for(i=0;i<8;i++)
{
GPIO_ResetBits(TM1629_SCLK); //拉低时钟
for(j=0;j //delay
GPIO_SetBits(TM1629_SCLK); //拉高时钟
rddata>>=1; //数据移到下一位
//DOUT为1,最高位付值为1
if(GPIO_ReadInputDataBit(TM1629_MISO)) //最高位判断
{rddata|=0x80;}
//DOUT为0,最高位付值为0
for(j=0;j //delay
}
return rddata;
}
void TM1629_Activation(uint8_t chip)
{
uint16_t temp = (GPIO_ReadOutputData(TM1629_STB)&TM1629_CS_MASK)+chip;
GPIO_Write(TM1629_STB, temp);
}
void TM1629_Write_CMD(uint8_t chip,uint8_t wrdata)
{ uint32_t j;
TM1629_Activation(TM1629_CS_NONE);
for(j=0;j
TM1629_Activation(chip);
TM1629_Write_Byte(wrdata);
}
void TM1629_Write_CMD2(uint8_t chip,uint8_t wrdata)
{
TM1629_Write_CMD(chip,wrdata);
TM1629_Activation(TM1629_CS_NONE);
}
void TM1629_Write_Led(uint8_t chip,uint8_t led_index,uint8_t led_data)
{
TM1629_Write_CMD(chip,TM1629_ADDR_FIXED);
TM1629_Write_CMD(chip,TM1629_ADDR_BASE+led_index);
TM1629_Write_Byte(led_data);
TM1629_Write_CMD2(chip,TM1629_DISP_ON);
}
void TM1629_Write_Digital(uint8_t chip,uint8_t led_index,uint8_t led_data)
{TM1629_Write_Led(chip,led_index,TM1629_CODE[led_data]);}
void TM1629_Write_Segment(uint8_t chip,uint8_t led_index,uint8_t led_data)
{TM1629_Write_Led(chip,led_index,TM1629_SEGMENT[led_data]);}
void TM1629_Write_Leds(uint8_t chip,uint8_t index,uint32_t *data,uint8_t length)
{
uint8_t i;
TM1629_Write_CMD(chip,TM1629_ADDR_INC);
TM1629_Write_CMD(chip,TM1629_ADDR_BASE+index);
for(i=0;i
TM1629_Write_CMD2(chip,TM1629_DISP_ON);
}
void TM1629_Write_3Digital(uint8_t chip,uint8_t led_index,uint32_t led_data)
{
TM1629_Write_CMD(chip,TM1629_ADDR_INC);
TM1629_Write_CMD(chip,TM1629_ADDR_BASE+led_index);
TM1629_Write_Byte(TM1629_CODE[(led_data% 10)/ 1]);
TM1629_Write_Byte(TM1629_LED_DARK);
if(led_data>99)TM1629_Write_Byte(TM1629_CODE[(led_data% 1000)/ 100]);
else TM1629_Write_Byte(TM1629_LED_DARK);
if(led_data> 9)TM1629_Write_Byte(TM1629_CODE[(led_data% 100)/ 10]);
else TM1629_Write_Byte(TM1629_LED_DARK);
TM1629_Write_CMD2(chip,TM1629_DISP_ON);
}
void TM1629_Write_Digitals(uint8_t chip,uint8_t led_index,uint32_t led_data,uint8_t digit_limit)
{
uint8_t i,data[16];
data[0]=TM1629_CODE[0];
for(i=1;i
for(i=0;(led_data>0)&&(i
{data[i++]=TM1629_CODE[led_data];}
TM1629_Write_CMD(chip,TM1629_ADDR_INC);
TM1629_Write_CMD(chip,TM1629_ADDR_BASE+led_index);
for(i=0;i
{TM1629_Write_Byte(data[i]);}
TM1629_Write_CMD2(chip,TM1629_DISP_ON);
}
void TM1629_Write_Chip(uint8_t chip,uint8_t *led_data)
{
uint8_t i;
TM1629_Write_CMD(chip,TM1629_ADDR_INC);
TM1629_Write_CMD(chip,TM1629_ADDR_BASE);
for(i=0;i<16;i++){TM1629_Write_Byte(led_data[i]);}
TM1629_Write_CMD2(chip,TM1629_DISP_ON);
}
uint32_t TM1629_Read_Keys(uint8_t chip)
{
uint32_t i,j;
union {__IO uint32_t u32data;__IO uint8_t u8data[4];} data;
TM1629_Write_CMD(chip,TM1629_KEY_READ);
for(j=0;j
for(i=0;i<4;i++){data.u8data[i]=TM1629_Read_Byte(); }
TM1629_Activation(TM1629_CS_NONE);
return data.u32data;
}
void TM1629_Clear_All(void)
{
uint8_t i,data[16];
for(i=0;i<16;i++){data[i]=0;}
TM1629_Configuration();
for(i=1;i<=9;i++)
{TM1629_Write_Chip(i,data);}
}
#endif
/**
上一篇:stm32 多路ADC+DMA 在内存中各路ADC的数据是如何界定的
下一篇:STM32 DMA详细说明
推荐阅读最新更新时间:2024-03-16 16:16