初级会员

- 积分
- 73
- 金钱
- 73
- 注册时间
- 2019-5-9
- 在线时间
- 21 小时
|
1金钱
板子是stm32f4探索者开发板,驱动芯片是TM1638,TM1639,TM1640都可以,只需要数码管能亮就行,后续的我自己再改,可以有偿,但不能太多
芯片资料我这边都有,自己写了几个都实现不了,不知道怎么回事
main函数
#include "stm32f4xx.h"
#include "delay.h"
#include "sys.h"
#include "delay.h"
#include "TM1640.h"
int main (void){//主程序
TM1640_Init(); //TM1640初始化
delay_init(168); //初始化延时函数
while(1){
TM1640_display(0,8); //天
TM1640_display(1,8);
TM1640_display(2,8); //时
TM1640_display(3,8);
TM1640_display(4,8); //分
TM1640_display(5,8);
TM1640_display(6,8); //秒
TM1640_display(7,8);
}
}
TM1640.c函数
#include "TM1640.h"
#include "delay.h"
#define DEL 1 //宏定义 通信速率(默认为1,如不能通信可加大数值)
//地址模式的设置
//#define TM1640MEDO_ADD 0x40 //宏定义 自动加一模式
#define TM1640MEDO_ADD 0x44 //宏定义 固定地址模式(推荐)
//显示亮度的设置
//#define TM1640MEDO_DISPLAY 0x88 //宏定义 亮度 最小
//#define TM1640MEDO_DISPLAY 0x89 //宏定义 亮度
//#define TM1640MEDO_DISPLAY 0x8a //宏定义 亮度
//#define TM1640MEDO_DISPLAY 0x8b //宏定义 亮度
#define TM1640MEDO_DISPLAY 0x8c //宏定义 亮度(推荐)
//#define TM1640MEDO_DISPLAY 0x8d //宏定义 亮度
//#define TM1640MEDO_DISPLAY 0x8f //宏定义 亮度 最大
#define TM1640MEDO_DISPLAY_OFF 0x80 //宏定义 亮度 关
void TM1640_start(){ //通信时序 启始(基础GPIO操作)(低层)
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_DIN,(BitAction)(1)); //接口输出高电平1
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_SCLK,(BitAction)(1)); //接口输出高电平1
delay_us(DEL);
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_DIN,(BitAction)(0)); //接口输出0
delay_us(DEL);
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_SCLK,(BitAction)(0)); //接口输出0
delay_us(DEL);
}
void TM1640_stop(){ //通信时序 结束(基础GPIO操作)(低层)
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_DIN,(BitAction)(0)); //接口输出0
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_SCLK,(BitAction)(1)); //接口输出高电平1
delay_us(DEL);
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_DIN,(BitAction)(1)); //接口输出高电平1
delay_us(DEL);
}
void TM1640_write(u8 date){ //写数据(低层)
u8 i;
u8 aa;
aa=date;
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_DIN,(BitAction)(0)); //接口输出0
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_SCLK,(BitAction)(0)); //接口输出0
for(i=0;i<8;i++){
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_SCLK,(BitAction)(0)); //接口输出0
delay_us(DEL);
if(aa&0x01){
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_DIN,(BitAction)(1)); //接口输出高电平1
delay_us(DEL);
}else{
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_DIN,(BitAction)(0)); //接口输出0
delay_us(DEL);
}
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_SCLK,(BitAction)(1)); //接口输出高电平1
delay_us(DEL);
aa=aa>>1;
}
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_DIN,(BitAction)(0)); //接口输出0
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_SCLK,(BitAction)(0)); //接口输出0
}
void TM1640_Init(void){ //TM1640接口初始化
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = TM1640_DIN | TM1640_SCLK; //选择端口号(0~15或all)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //设置IO接口速度(2/10/50MHz)
GPIO_Init(TM1640_GPIOPORT, &GPIO_InitStructure);
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_DIN,(BitAction)(1)); //接口输出高电平1
GPIO_WriteBit(TM1640_GPIOPORT,TM1640_SCLK,(BitAction)(1)); //接口输出高电平1
TM1640_start();
TM1640_write(TM1640MEDO_ADD); //设置数据,0x40,0x44分别对应地址自动加一和固定地址模式
TM1640_stop();
TM1640_start();
TM1640_write(TM1640MEDO_DISPLAY); //控制显示,开显示,0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f分别对应脉冲宽度为:
//------------------1/16, 2/16, 4/16, 10/16, 11/16, 12/16, 13/16, 14/16 //0x80关显示
TM1640_stop();
}
void TM1640_display(u8 address,u8 date){ //固定地址模式的显示输出
const u8 buff[21]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0x00};//数字0~9及0~9加点显示段码表
//--------------- 0 1 2 3 4 5 6 7 8 9 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 无
TM1640_start();
TM1640_write(0xC0+address); //传显示数据对应的地址
TM1640_write(buff[date]); //传1BYTE显示数据
TM1640_stop();
}
void TM1640_display_add(u8 address,u8 date){ //地址自动加一模式的显示输出
u8 i;
const u8 buff[21]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0x00};//数字0~9及0~9加点显示段码表
//--------------- 0 1 2 3 4 5 6 7 8 9 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 无
TM1640_start();
TM1640_write(0xC0+address); //设置起始地址
for(i=0;i<16;i++){
TM1640_write(buff[date]);
}
TM1640_stop();
}
TM1640.H
#ifndef __TM1640_H
#define __TM1640_H
#include "sys.h"
#define TM1640_GPIOPORT GPIOA //定义IO接口
#define TM1640_DIN GPIO_Pin_0 //定义IO接口
#define TM1640_SCLK GPIO_Pin_1 //定义IO接口
#define TM1640_LEDPORT 0xC8 //定义IO接口
void TM1640_Init(void);//初始化
void TM1640_led(u8 date);//
void TM1640_display(u8 address,u8 date);//
void TM1640_display_add(u8 address,u8 date);//
#endif
|
|