📄 lm1602.c
字号:
#include "lm1602.h"
#include "string.h"
#include <macros.h>
/*自定义字符*/
byte CGRAMtab[] = {0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00} ; //度的符号
void LcdInit(void);
void DisplayTime(byte hour,byte minute,byte second);
unsigned char CheckBusy(void)
{
DDRB = 0x00;
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
RS_L;
RW_H;
EN_H;
NOP();
NOP();
NOP();
NOP();
EN_L;
if((PINB&0x80)==0x80)
{
DDRB = 0xFF;
return 1;
}
else
{
DDRB = 0xFF;
return 0;
}
}
void delay(void)
{
unsigned int t;
for(t=0;t<1000;t++);
}
void WriteCmd(byte cmd)
{
RS_L;
RW_L;
DATAPORT = cmd;
EN_H;
//while(CheckBusy());
delay();
EN_L;
}
void WriteData(byte datas)
{
RS_H;
RW_L;
DATAPORT = datas;
EN_H;
delay();
EN_L;
}
void LCD_WriteCGRAM(byte *cgram,byte len) //自定义字符写入CGRAM函数
{
byte i ;
WriteCmd(0x40) ; //写CGRAM
for (i = 0 ; i< len ; i++)
WriteData(*(cgram+i)) ;
}
void ClearLcd(void)
{
WriteCmd(CLR_DIS);
}
void CurReturn(void)
{
WriteCmd(CUR_RET);
}
void SetInputMode(byte curDir, byte textMove)
{
byte setData = 0x04;
if (curDir == 1) //光标右移
setData |= 0x02;
else
setData &= ~0x02;
if (textMove == 1) //文字移动
setData |= 0x01;
else //文字不移动
setData &= ~0x01;
WriteCmd(setData);
}
void SetDisMode(byte allDis, byte curDis, byte curFlash)
{
byte setData = 0x08;
if (allDis == 1) //所有都显示
setData |= 0x04;
else //都不显示
setData &= ~0x04;
if (curDis == 1) //光标显示
setData |= 0x02;
else //光标不显示
setData &= ~0x02;
if (curFlash == 1) //光标闪烁
setData |= 0x01;
else ///光标不闪烁
setData &= ~0x01;
WriteCmd(setData);
}
/*
void SetShift(byte cMove, byte cDir)
{
byte setData = 0x10;
if (cMove == 1) //移动文字
setData |= 0x08;
else //移动光标
setData &= ~0x08;
if (cDir == 1) //右移
setData |= 0x04;
else //左移
setData &= ~0x04;
WriteCmd(setData);
}
*/
void SetFunction(byte numBus, byte lineDis, byte fDis)
{
byte setData = 0x20;
if (numBus == 1) //4位总线
setData |= 0x10;
else //8位总线
setData &= ~0x10;
if (lineDis == 1) //双行显示
setData |= 0x08;
else //单行显示
setData &= ~0x08;
if (fDis == 1) //5*10点阵
setData |= 0x04;
else ///5*7点阵
setData &= ~0x04;
WriteCmd(setData);
}
/*
void SetCGRAMAddr(byte ramAddr)
{
byte setData = 0x40;
setData |= (ramAddr &0x3f);
WriteCmd(setData);
}
*/
void SetDisAddr(byte urow, byte ucol)
{
if (ucol >= 20)
ucol= 20;
if (urow == 0)
WriteCmd( 0x80+ucol);
else
WriteCmd( 0xc0+ucol);
}
void DisCGRAM(byte urow, byte ucol, char ch)
{
SetDisAddr(urow, ucol);
WriteData(ch);
}
/*
void DisChar(byte row, byte line, char ch)
{
SetDisAddr(row, line);
WriteData(ch);
}
*/
void DisString(byte urow, byte ucol, char *str)
{
byte i = 0;
SetDisAddr(urow, ucol);
while (i < strlen(str))
{
WriteData(*(str + i));
i++;
}
}
void DisNum(byte urow, byte ucol, byte num)
{
SetDisAddr(urow, ucol);
WriteData('0'+ num);
}
void DisplayTime(byte hour,byte minute,byte second)
{
DisNum(1, 3, hour/16);
DisNum(1, 4, hour%16);
DisString(1, 5,":");
DisNum(1, 6, minute/16);
DisNum(1, 7, minute%16);
DisString(1, 8,":");
DisNum(1, 10, second & 0x0f);
DisNum(1, 9, (second >> 4) &0x07);
}
void LCD_Dis_Float(byte urow,byte ucol,byte bvalue)
{
byte i;
byte tempdata[4];
tempdata[0] = (bvalue)/100;
if(tempdata[0]== 0) tempdata[0]= ' '-'0';
tempdata[1] = (bvalue)%100/10;
if((tempdata[0] == 0x00)&&(tempdata[1] == 0x00))
{
tempdata[1] = ' '-'0';
}
tempdata[2] = (bvalue)%10;
tempdata[3] = '.'-'0';
for(i = 0;i<4;i++)
DisNum(urow,ucol+i,tempdata[i]);
}
void LCD_Dis_Temperature(byte urow,byte ucol, unsigned int rValue) //温度数据转换和显示函数
{
ClearLcd();
DisString(0,1,"Temperature is:");
if((rValue &0xf800) == 0xf800)
{
DisString(urow,ucol,"-");
LCD_Dis_Float(urow,ucol+1,((byte)(~((rValue>>3)-1)))>>1);
}
else
{
DisString(urow,ucol," ");
LCD_Dis_Float(urow,ucol+1,(rValue>>4)&0xff);
}
if((rValue&0x08)==0x08)
DisString(urow,ucol+5,"5");
else
DisString(urow,ucol+5,"0");
DisCGRAM(urow, ucol+6, 0x00);
}
void LcdInit(void)
{
ClearLcd();
CurReturn();
SetInputMode(1, 0);
SetDisMode(1, 0, 0);
// SetShift(0, 0);
SetFunction(1, 1, 1);
// SetCGRAMAddr();
LCD_WriteCGRAM(CGRAMtab,8);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -