|
#include "TM1640.h"
#include "delay.h"
#define DEL 1 //宏定義 通信速率(默認(rèn)為1,如不能通信可加大數(shù)值)
//地址模式的設(shè)置
//#define TM1640MEDO_ADD 0x40 //宏定義 自動(dòng)加一模式
#define TM1640MEDO_ADD 0x44 //宏定義 固定地址模式(推薦)
//顯示亮度的設(shè)置
//#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 //宏定義 亮度 關(guān)
void TM1640_start(){ //通信時(shí)序 啟始(基礎(chǔ)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(){ //通信時(shí)序 結(jié)束(基礎(chǔ)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){ //寫數(shù)據(jù)(低層)
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_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = TM1640_DIN | TM1640_SCLK; //選擇端口號(hào)(0~15或all)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //選擇IO接口工作方式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //設(shè)置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); //設(shè)置數(shù)據(jù),0x40,0x44分別對應(yīng)地址自動(dòng)加一和固定地址模式
TM1640_stop();
TM1640_start();
TM1640_write(TM1640MEDO_DISPLAY); //控制顯示,開顯示,0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f分別對應(yīng)脈沖寬度為:
//------------------1/16, 2/16, 4/16, 10/16, 11/16, 12/16, 13/16, 14/16 //0x80關(guān)顯示
TM1640_stop();
}
void TM1640_led(u8 date){ //固定地址模式的顯示輸出8個(gè)LED控制
TM1640_start();
TM1640_write(TM1640_LEDPORT); //傳顯示數(shù)據(jù)對應(yīng)的地址
TM1640_write(date); //傳1BYTE顯示數(shù)據(jù)
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};//數(shù)字0~9及0~9加點(diǎn)顯示段碼表
//--------------- 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); //傳顯示數(shù)據(jù)對應(yīng)的地址
TM1640_write(buff[date]); //傳1BYTE顯示數(shù)據(jù)
TM1640_stop();
}
void TM1640_display_add(u8 address,u8 date){ //地址自動(dòng)加一模式的顯示輸出
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};//數(shù)字0~9及0~9加點(diǎn)顯示段碼表
//--------------- 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); //設(shè)置起始地址
for(i=0;i<16;i++){
TM1640_write(buff[date]);
}
TM1640_stop();
}
|
|