小弟希望用atmega48的四个端口,通过4094扩展,控制LCD YM1602C和按键扫描。程序如下:
void YM1602C_Write(UINT8 CD,UINT8 Data) { SiBuffer.YM1602C_RS = CD; SiBuffer.YM1602C_RW = YM1602C_WRITE; SiBuffer.YM1602C_E = YM1602C_DISABLE; SI_Dispatch(); UsDelay(4); SiBuffer.YM1602C_E = YM1602C_ENABLE; SI_Dispatch(); UsDelay(1);
SiBuffer.YM1602C_D = Data; SI_Dispatch(); UsDelay(4); SiBuffer.YM1602C_E = YM1602C_DISABLE; SI_Dispatch(); UsDelay(1); }
BOOL YM1602C_IsBusy() { int i; char ret = FALSE; SiBuffer.YM1602C_RS = 0;//registors option SiBuffer.YM1602C_RW = 1;//Read SiBuffer.YM1602C_E = YM1602C_DISABLE; SiBuffer.YM1602C_D7 = 0; SI_Dispatch(); UsDelay(1);
SiBuffer.YM1602C_E = YM1602C_ENABLE; SI_Dispatch(); UsDelay(2);
if(IS_YM1602C_BUSY) ret = TRUE; SiBuffer.YM1602C_E = YM1602C_DISABLE; SI_Dispatch(); return ret; }
void YM1602C_Init() { UsDelay(4000); YM1602C_Write(YM1602C_CMD, YM1602_CMD_FUN_MASK|YM1602_CMD_2LINES|YM1602_CMD_INTERFACE8|YM1602_CMD_DSP_ON); UsDelay(40); YM1602C_Write(YM1602C_CMD, YM1602_CMD_DCB_MASK|YM1602_CMD_DISPLAY_ON); UsDelay(40); YM1602C_Write(YM1602C_CMD, YM1602_CMD_MODE_MASK|YM1602_CMD_INC_CURSOR);//&(~YM1602_CMD_SHIFT_DISPLAY)); UsDelay(40); YM1602C_Write(YM1602C_CMD, YM1602_CMD_CLEAR_DISPLAYER); UsDelay(1530); YM1602C_Write(YM1602C_CMD, YM1602_CMD_RESET_CURSOR); UsDelay(1530); YM1602C_Write(YM1602C_CMD, YM1602_CMD_SHIFT_MASK|YM1602_CMD_SHIFT_RIGHT); UsDelay(40); }
void YM1602C_DisplayStrAt(UINT8 nLine, UINT8 nCharacter, UINT8 *pChar) { #if 0 YM1602C_Write(YM1602C_CMD, YM1602_CMD_DDRA_MASK|1); YM1602C_Write(YM1602C_DATA,0x3a); #else YM1602C_Write(YM1602C_CMD, YM1602_CMD_DDRA_MASK|(0x40*(nLine-1)+nCharacter-1)); while('\0' != *pChar) { YM1602C_Write(YM1602C_DATA, *pChar++); } #endif }
void YM1602C_SetCursor(UINT8 nLine, UINT8 nColumn) { if(nLine > 0 && nLine<3 && nColumn > 0 && nColumn < 17) YM1602C_Write(YM1602C_CMD,YM1602_CMD_DDRA_MASK|(0x40*(nLine-1)+nColumn-1)); UsDelay(39); }
另外,定时1秒写:
static char n = '0'; int i; YM1602C_SetCursor(1,1); for(i=0; i<16; i++) YM1602C_Write(YM1602C_DATA,n); if(++n > '9') n = '0';
但是LCD显示的是乱码。 希望高手指点迷津。 |