TM1621是一個多功能的LCD驅動器,帶有蜂鳴器驅動功能。通訊采用四線串行接口
TM1621的難點在於字節序和顯存跟屏幕的映射關系上,下面是寫寄存器的代碼
void Delay_us(uint8_t us) { uint16_t i = 0,z = 0; for(i = 0; i < us; i++) { for(z = 0; z < 50; z++); } } /************************************************************************************** * FunctionName : TM1621_SendBitMsb() * Description : 發送發送多位[高位在前] * EntryParameter : None * ReturnValue : None **************************************************************************************/ void TM1621_SendBitMsb(uint8_t dat, uint8_t cnt) { for (uint8_t i=0; i<cnt; i++) { (dat & 0x80) ? TM1621_DATA_HIG() : TM1621_DATA_LOW(); dat <<= 1; TM1621_WR_LOW(); Delay_us(3); TM1621_WR_HIG(); } } /************************************************************************************** * FunctionName : TM1621_SendBitLsb() * Description : 發送多位[低位在前] * EntryParameter : None * ReturnValue : None **************************************************************************************/ void TM1621_SendBitLsb(uint8_t dat, uint8_t cnt) { for (uint8_t i=0; i<cnt; i++) { (dat & 0x01) ? TM1621_DATA_HIG() : TM1621_DATA_LOW(); dat >>= 1; TM1621_WR_LOW(); Delay_us(3); TM1621_WR_HIG(); } } /************************************************************************************** * FunctionName : TM1621_SendCmd() * Description : 發送命令 * EntryParameter : None * ReturnValue : None **************************************************************************************/ void TM1621_SendCmd(uint8_t cmd) { TM1621_CS_LOW(); TM1621_SendBitMsb(0x80, 3); // 前面3位命令代碼 TM1621_SendBitMsb(cmd, 9); // 后面10位: a5~a0[RAM地址]+d3~d0[RAM數據] TM1621_CS_HIG(); } /************************************************************************************** * FunctionName : HTBSendNDat() * Description : 發送N數據 * EntryParameter : None * ReturnValue : None **************************************************************************************/ void TM1621_SendNDat(uint8_t addr, uint8_t *pDat, uint8_t cnt, uint8_t bitNum) { TM1621_CS_LOW(); TM1621_SendBitMsb(0xA0, 3); // 前面3位命令代碼 TM1621_SendBitMsb(addr<<2, 6); // a5~a0[RAM地址] for (uint8_t i=0; i<cnt; i++) { TM1621_SendBitMsb(*pDat++, bitNum); // RAM數據 } TM1621_CS_HIG(); } /** * @brief TM1621 Write CMD. * @param cmd 指向寫入的命令. * @return void */ void TM1621_Write_CMD(uint8_t cmd) { TM1621_CS_LOW(); TM1621_SendBitMsb(0x80, 4); // 前面3位命令代碼 TM1621_SendBitMsb(cmd, 8); // a5~a0[RAM地址] TM1621_CS_HIG(); }
有了寫寄存器的代碼以后,下面我們來看顯存跟屏幕的映射關系
從手冊上的RAM映象圖上可以看出,TM1621一個地址是4bit。SEG0....SEG31是寄存器地址對應SEG0....SEG31管腳,COM0~COM3是TM1621的公共端
假設我們要讓屏幕的第一個8顯示“0”,(TM1621是共陰的所以采用陰碼)陰碼的“0”是0x3f,如果我們直接把0x3f給寄存器0~1,顯示的效果就是亂碼。
從這個表可以看出,如果要正常顯示應該把G跟F調換(Lcd_ram是緩存數組,單獨擦寫每一位太過麻煩。所以建立一個緩存區,一次刷新整屏幕)
uint8_t BIT_Reverse(uint8_t num) { uint8_t bit = num; if((bit & 0x10) == 0) bit = bit & 0xf7; else bit = bit | 0x08; if((bit & 0x80) == 0) bit = bit & 0xfe; else bit = bit | 0x01; bit = bit << 4; bit = bit & 0xF0; return bit; } void TM1621_display(uint8_t cnt, uint8_t num) { Lcd_ram[cnt] = BIT_Reverse(Lcd_table[num] & 0xF0); Lcd_ram[cnt] |= (Lcd_table[num] & 0x0F); TM1621_SendNDat(0x00,Lcd_ram,10,8); }



