某年写个番禺龙美游戏机IO板用到了STM32F103VBT6及TM1629.写下了一个驱动程序。希望能帮助到有需要的朋友。不多说了,代码如下:
/* Includes ------------------------------------------------------------------*/
#ifdef GAME
#include "stm32f10x.h"
#include "stdio.h"
/** @addtogroup STM32F10x_StdPeriph_Examples
* @{
*/
/** @addtogroup GPIO_JTAG_Remap
* @{
*/
/*
_________________________________________________
| STM32F103 TM1629 |
| |
| PC0-3 CSA-CSD ----> STB |
| PE14 MISO <---- MISO |
| PC6 SCLK ----> SCLK |
| PC7 MOSI ----> MOSI |
| PC4 KEYLED ----> KEYLED |
| PC5 CPULED ----> CPULED |
|_______________________________________________|
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
//TM1629片选 //STB
#define TM1629_STB GPIOC /* PC0-PC3 */
#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 /*PC6 SCLK*/
#define TM1629_MOSI GPIOC, GPIO_Pin_7 /*PC7 DIN*/
#define TM1629_MISO GPIOE, GPIO_Pin_14 /*PE14 DOUT*/
//键盘指示灯使能
#define KEY_LAMP_EN GPIOC, GPIO_Pin_4 /*PC4 RESVIN*/
#define CPU_LAMP_EN GPIOC, GPIO_Pin_5 /*PC5 LAMP*/
//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 /* 640ns */
//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
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
const unsigned char TM1629_CODE[16]=
/*0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xb 0xC 0xd 0xE 0xF*/
{0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0X7C, 0x39, 0x5E, 0x79, 0x71};
const unsigned char TM1629_CODE_LED[16]=
/*0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF*/
{0xBF, 0x86, 0xDB, 0xCF, 0xE6, 0xED, 0xFD, 0x87, 0xFF, 0xEF, 0xF7, 0XFF, 0xB9, 0xBF, 0xF9, 0xF1};
const unsigned char TM1629_SEGMENT[8]=
/* a b c d e f g h*/
{0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
/* Private function prototypes -----------------------------------------------*/
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);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program.
* @param None
* @retval None
*/
void TM1629_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
uint32_t i,j;
/* Enable GPIOC, GPIOE and AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
/* Enable GPIOC PC0-PC7 output */
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);
/* Enable GPIOE PE14 input */
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<TM1629_DELAY;j++){;} //delay
GPIO_SetBits(TM1629_SCLK); //拉高时钟
for(j=0;j<TM1629_DELAY;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<TM1629_DELAY;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<TM1629_DELAY;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_DELAY;j++){;}//delay
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<length;i++){TM1629_Write_Byte(data[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<digit_limit;i++){data[i]=0x0;}
for(i=0;(led_data>0)&&(i<digit_limit);led_data/=10)
{data[i++]=TM1629_CODE[led_data%10];}
TM1629_Write_CMD(chip,TM1629_ADDR_INC);
TM1629_Write_CMD(chip,TM1629_ADDR_BASE+led_index);
for(i=0;i<digit_limit;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<TM1629_DELAY;j++){;}//delay
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
/**
* @}
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
代码完毕。
离线