/*--------------------------------------------------------------------
LPC213X硬件SPI接口LCD19232显示C++代码
--------------------------------------------------------------------*/
//__inline
LcdObj::LcdObj(void)
{
LcdSpiInit();//SPI初始化
LcdInit();
}
//__inline
void LcdObj::LcdSpiInit(void)
{
/* 设置MOSI 和SCK 及SS 为输出,其他为输入 */
LCDPORT->IODIR |= (1 << LCDCS);
LCDPORT->IOCLR = (1 << LCDCS);
/*
LCDPORT->IODIR |= (1 << LCDSCK)| (1 << LCDSID);//设置输出方式
LCDPORT->IOSET = (1 << LCDSCK) | (1 << LCDSID);
*/
POWER->P_CONP |= (1 << PCSPI0);
PINSEL->PIN_SEL0 |= ((P0_4_SCK0 << P0_4_PINSEL) | (P0_6_MOSI0 << P0_6_PINSEL));
// SPI->SPI_SPCCR = 0x168; // 设置SPI时钟分频
SPI->SPI_SPCCR = 0x52; // 设置SPI时钟分频
SPI->SPI_SPCR =
(1 << CPHA) | // CPHA = 0, 数据在SCK 的第一个时钟沿采样
(1 << CPOL) | // CPOL = 1, SCK 为低有效
(1 << MSTR) | // MSTR = 1, SPI 处于主模式
(0 << LSBF) | // LSBF = 0, SPI 数据传输MSB (位7)在先
(0 << SPIE); // SPIE = 0, SPI 中断被禁止
}
//__inline
void LcdObj::LcdInit(void)
{
/*---------------------------------------------------
LCD模块上电等待延时
----------------------------------------------------*/
LcdClearBuffer();
LcdSendCommand(0x20);//发送4位控制命令
// LcdSendCommand(0x30);//发送8位控制命令//与8位4位无关!!!
LcdSendCommand(0x02);//发送位址归位命令,设定DDRAM位址计数器为0
LcdSendCommand(0x04);//发送进入点命令
LcdSendCommand(0x0c);//发送开显示关光标命令
LcdSendCommand(0x01);//发送清除显示命令
LcdSendCommand(0x80);//发送设定DDRAM地址0x00命令
}
//__inline
void LcdObj::LcdSend(char cData)
{
/*
unsigned int i;
_delay_loop_(1);
for (i = 0; i < 8; i ++) {
_delay_loop_(1);
LCDPORT->IOCLR = (1 << LCDSCK);
_delay_loop_(1);
if (cData & 0x80) {//MSB最高位为1时
LCDPORT->IOSET = (1 << LCDSID);
}
else {
LCDPORT->IOCLR = (1 << LCDSID);
}
_delay_loop_(1);
cData <<= 1;
LCDPORT->IOSET = (1 << LCDSCK);
_delay_loop_(1);
}
*/
//
SPI->SPI_SPDR = cData; //发送数据(相当于51的SBUF = DATA)
while(!(SPI->SPI_SPSR & (1 << SPIF))); // 等待SPIF置位,即等待数据发送完毕
//
}
/*--------------------------------------------------------
发送8位LCD控制命令
--------------------------------------------------------*/
//__inline
void LcdObj::LcdSendCommand(char cCommand)
{
/*--------------------------------------------------------
发送同步脉冲11111 WR(0) RS(0) 0发送顺序从左至右)
--------------------------------------------------------*/
LCDPORT->IOSET = (1 << LCDCS);//SS=1,启动SPI
LcdSend(0xf8);//发送LCD控制命令
LcdSend(cCommand & 0xf0);//发送高4位LCD控制命令
LcdSend(cCommand << 4);//发送低4位LCD控制命令
LCDPORT->IOCLR = (1 << LCDCS);//SS=0,关闭SPI
if (cCommand == 0x01) _delay_loop_(160);//1.6mS
else _delay_loop_(72);//st7920要求等待72uS
}
/*--------------------------------------------------------
发送8位LCD显示数据(LCD19232)
--------------------------------------------------------*/
//__inline
void LcdObj::LcdSendData(char cData)
{
/*--------------------------------------------------------
发送同步脉冲11111 WR(0) RS(0) 0发送顺序从左至右)
--------------------------------------------------------*/
LCDPORT->IOSET = (1 << LCDCS);//SS=1,启动SPI
LcdSend(0xfa);//发送LCD显示数据
LcdSend(cData & 0xf0);//发送高4位LCD显示数据
LcdSend(cData << 4);//发送低4位LCD显示数据
LCDPORT->IOCLR = (1 << LCDCS);//SS=0,关闭SPI
_delay_loop_(72);//st7920要求等待延时72uS
}
//__inline
void LcdObj::SetLcdDisplayPos(unsigned char row, unsigned char col)
{
row &= 0x01;//2行汉字
if (col > 24) col = 0;//每行12个汉字24个字符
LcdRow = row;
LcdCol = col;
LcdRowWriteEnable[row] = 1;//允许此行刷新汉字显示
}
//__inline
void LcdObj::LcdClearBuffer(void)
{
unsigned char i, j;
for (i = 0;i < 2;i ++) {
for (j = 0;j < 24; j ++) {
LcdBuffer[i][j] = ' ';
}
LcdRowWriteEnable[i] = 1;//允许此行刷新汉字显示
}
LcdRow = 0;
LcdCol = 0;
}
//__inline
void LcdObj::LcdDisplayBuffer(void)
{
unsigned char i, j;
for (i = 0; i < 2; i ++) {//2行汉字
if (LcdRowWriteEnable[i]) {//允许此行刷新汉字显示
LcdSendCommand(0x80 + (i & 1) * 16 + (i >> 1) * 8);//移动光标
for (j = 0; j < 16; j ++) {//每行前8个汉字16个字符
LcdSendData(LcdBuffer[i][j]);//刷新显示字符
}
LcdSendCommand(0x80 + (i & 1) * 16 + ((i + 2) >> 1) * 8);//移动光标
for (j = 16; j < 24; j ++) {//每行后4个汉字8个字符
LcdSendData(LcdBuffer[i][j]);//刷新显示字符
}
LcdRowWriteEnable[i] = 0;//过后不允许此行刷新汉字显示
}
}
}
//__inline
void LcdObj::LcdDisplay(const char * string)
{
while(*string) {
LcdBuffer[LcdRow][LcdCol ++] = *string ++;
}
}
//__inline//(不敢加inline)
//__forceinline
void LcdObj::LcdDisplay(unsigned int Val, unsigned char size1, signed char size0)
{
char str[7], *ptr;
unsigned char i;
ptr = str + 5;
if (size0 > 0) {//有小数
for (i = 0; i < 3; i ++) {
*ptr -- = (Val % 10) + '0';
Val /= 10;
}
ptr[size0 + 1] = 0;
*ptr -- = '.';
}
else {
*ptr -- = 0;
}
for (i = 0; i < size1; i ++) {
if (i && (Val == 0)) {
if (size0 < 0) {
*ptr -- = '0';
}
else {
*ptr -- = ' ';
}
}
else *ptr -- = (Val % 10) + '0';
Val /= 10;
}
LcdDisplay(ptr + 1);
}
评论