TM1638八位数码管八按键驱动程序(stm32f103驱动,使用了stm32 3.5库)
来源:互联网 发布:金字塔股票交易软件 编辑:程序博客网 时间:2024/06/06 10:49
/************************************************************************************************Program Assignment: Driver for TM1638 digital tube**Author : Wuwang**Date : 2014.8.26 9:00**Description : This is a driver for the board which is controled by thechip of tm1638. The board has eight digital tubes which have eight segments and eight keys.***********************************************************************************************/#include "main.h" //#include "stm32f10x.h" /*********************define and global variables*********************************************/#define STB GPIO_Pin_0 //chip-select line#define CLK GPIO_Pin_1 //clock line#define DIO GPIO_Pin_2 //data line#define Set(x) GPIO_SetBits(GPIOA,(x))//Sets the selected data port bits#define Reset(x) GPIO_ResetBits(GPIOA,(x))//Resets the selected data port bits#define Get(x) GPIO_ReadInputDataBit(GPIOA,(x))==SET //Read the specified input port pinuint16_t const tm_dat[2][14]={{'0','1','2','3','4','5',//the char and its segment code '6','7','8','9','.','-','_',' '},{0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x80,0x40,0x08,0x00}};/************************************************************************************************Function Name: RCC_Config*Purpose : Configration Clock***********************************************************************************************/void RCC_Config(){RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);}/************************************************************************************************Function Name: GPIO_Config*Purpose : Configration GPIO***********************************************************************************************/void GPIO_Config(){GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Pin=STB|CLK|DIO;GPIO_Init(GPIOA,&GPIO_InitStructure);}/************************************************************************************************Function Name: Write_Byte*Purpose : Write one byte to the data port*params : byte -------8-bits byte *return : none***********************************************************************************************/void Write_Byte(uint8_t byte){uint8_t i=0;for(i=0;i<8;i++){Reset(CLK);if(byte&0x01){Set(DIO);}else{Reset(DIO);}Set(CLK);byte>>=1;}}/************************************************************************************************Function Name: Read_Byte*Purpose : Read one byte from data port*params : none*return : the 8-bits byte which is read from data port***********************************************************************************************/int8_t Read_Byte(){uint8_t i=0;uint8_t temp=0x00;for(i=0;i<8;i++){Set(CLK);temp>>=1;if(Get(DIO)){temp|=0x80;}Reset(CLK);}return temp;}/************************************************************************************************Function Name: Write_Cmd*Purpose : Write a conmand to the data port*params : cmd -------8-bits byte,the conmand,check the data sheet to find the conmand *return : none***********************************************************************************************/void Write_Cmd(uint8_t cmd){Set(STB);Reset(STB);Write_Byte(cmd);}/************************************************************************************************Function Name: Read_Key*Purpose : Read the key number which has been pressed*params : none*return : the number of the key. 0-8. "return 0" represents no key has been pressed.***********************************************************************************************/int8_t Read_Key(){uint8_t i=0;uint8_t key1=0x00;uint16_t key2=0x00;Write_Cmd(0x42);Set(DIO); //this is obligatory, check the data sheet,GPIOfor(i=0;i<4;i++){key1=Read_Byte();key2|=(key1<<i);}key2>>=1;for(i=0;i<8;i++){if(0x01<<i==key2)return i+1;}return 0;}/************************************************************************************************Function Name: Write_Dat*Purpose : Write data to the location specified*params : addr ------the address,0x00 to 0x0fdat ------the data,segment code*return : none***********************************************************************************************/void Write_Dat(uint8_t addr,uint8_t dat){Write_Cmd(0x44);Write_Cmd(0xc0|addr);Write_Byte(dat);}/************************************************************************************************Function Name: TM1638_SendData*Purpose : Write data to the location specified*params : i ------the bit code of digtal tube,0 to 7str ------the string,the char which was not in tm_data will be replace with "''".*return : none***********************************************************************************************/void TM1638_SendData(uint8_t i,char * str){int j=0,k=0;unsigned char chr;for(;i<8;i++){k=0;for(j=0;j<14;j++){if(*str==tm_dat[0][j]){chr=tm_dat[1][j];k=1;break;}}if(k==0){chr=0x00;}if(*(str+1)=='.'){chr|=0x80;Write_Dat(i*2,chr);str++;}else{Write_Dat(i*2,chr);}str++;if(*str=='\0')break;}}/************************************************************************************************Function Name: TM1638_Init*Purpose : the initialization of tm1638*params : none*return : none***********************************************************************************************/void TM1638_Init(){int i=0;RCC_Config();GPIO_Config();Write_Cmd(0x8a);Write_Cmd(0x40);for(i=0;i<16;i++){Write_Byte(0x00);}}
2 0
- TM1638八位数码管八按键驱动程序(stm32f103驱动,使用了stm32 3.5库)
- TM1638八位数码管八按键驱动程序(stm32f103驱动,使用了stm32 3.5库)
- stm32+DS1302+TM1638驱动程序
- STM32+CH452驱动8位数码管
- 嵌入式驱动编写-按键驱动程序
- 介绍2款使用过的按键和数码管驱动IC
- Linux驱动程序开发之三----按键驱动(Tiny6410)
- BS8116A-3驱动程序(使用STM32--STM32cubemx)
- 【STM32小案例 06 】STM32使用按键驱动L298N电机模块控制电机正反转
- 字符设备驱动之按键处理一(查询方式的按键驱动程序)
- 字符设备驱动之按键处理二(中断处理的按键驱动程序)
- 按键驱动程序(1)
- 【stm32f103】stm32 外部中断(寄存器版)
- 【stm32f103】stm32 外部中断(寄存器版)
- 【连载】【FPGA黑金开发板】Verilog HDL那些事儿--数码管电路驱动(八)
- 基于中颖SH88F516驱动TM1640的LED数码管驱动程序
- 【Linux设备驱动】TQ2440按键驱动程序设计
- display32bit(扩展板上的八位数码管的显示模块)
- 看操作系统虚拟化原理总结篇——内存管理机制
- java.lang.IllegalArgumentException: Document base *** does not exist or is not a readable
- 9种基于Web的演讲稿灯片解决方案
- Mac 修改hosts 文件
- 深入理解java动态代理
- TM1638八位数码管八按键驱动程序(stm32f103驱动,使用了stm32 3.5库)
- pip 直接安装tar.gz zip文件包 (windows linux mac 可用)
- CALayer 属性不显示错误
- <context:component-scan base-package=com.xxx.xxx/ >包扫描注解
- 在Mac OSX系统下安装Tomcat
- 解决方案:svn: Can't open file 'F:\临时存放\myeclipse\DBTVinterface\src\com\dbt\vintegration\vgnrebatereceipt
- vim 一起一路走下去
- 【c++】C++编写OCX控件
- CSS position详解