#include <LPC177x_8x.h>
#include "lcm.h"
extern void write_data(uchar date);
extern void delay(uint32_t tick);
#define LCM_EN_LOW() LPC_GPIO1->CLR |= (1UL<<19); //EN
#define LCM_EN_HIGH() LPC_GPIO1->SET |= (1UL<<19);
#define LCM_RW_LOW() LPC_GPIO1->CLR |= (1UL<<20); //RW
#define LCM_RW_HIGH() LPC_GPIO1->SET |= (1UL<<20);
#define LCM_RS_LOW() LPC_GPIO1->CLR |= (1UL<<21); //RS
#define LCM_RS_HIGH() LPC_GPIO1->SET |= (1UL<<21);
#define LCD_NUM 8
typedef struct
{
uint32_t gpioBaseAddr; // 對應GPIO的基地址
uint32_t pin; // 對應的PIN腳掩碼
}T_LCD; // lcd data引腳的描述信息
// 定義data數據位
const T_LCD g_lcd[LCD_NUM]= {
{LPC_GPIO1_BASE, 1UL << 22}, //DATA:D0-D7
{LPC_GPIO1_BASE, 1UL << 23},
{LPC_GPIO1_BASE, 1UL << 24},
{LPC_GPIO1_BASE, 1UL << 25},
{LPC_GPIO1_BASE, 1UL << 26},
{LPC_GPIO1_BASE, 1UL << 27},
{LPC_GPIO1_BASE, 1UL << 28},
{LPC_GPIO1_BASE, 1UL << 29},
};
void lcd1602_init(void)
{
delay(5000);
write_command(0x30); // 功能設定
delay(100);
write_command(0x04); // 顯示關閉
delay(100);
write_command(0x01); // 清屏
delay(100);
write_command(0x0c); // 顯示開及光標設置
delay(100);
write_command(0x80); // 顯示光標移動設置
delay(100);
//write_command(0x04); //開總顯示,不開開光標,光標不閃爍
//delay(1);
}
// 對lED使用的GPIO管腳進行初始化
void lcdInit(void)
{
LPC_GPIO_TypeDef *pGPIO;
uint32_t i;
for (i = 0; i < LCD_NUM; i++) //D0-D7 配置輸出模式
{
pGPIO = (LPC_GPIO_TypeDef *)g_lcd[i].gpioBaseAddr;
pGPIO->DIR |= g_lcd[i].pin;
}
LPC_IOCON->P1_19 &= 0xFFFFFFF8; // EN
LPC_IOCON->P1_19 |= 0x00000000; //
LPC_GPIO1->DIR |= (1UL<<19); //輸出模式
LPC_IOCON->P1_20 &= 0xFFFFFFF8; // R/W
LPC_IOCON->P1_20 |= 0x00000000; //
LPC_GPIO1->DIR |= (1UL<<20); //輸出模式
LPC_IOCON->P1_21 &= 0xFFFFFFF8; // RS
LPC_IOCON->P1_21 |= 0x00000000; //
LPC_GPIO1->DIR |= (1UL<<21); //輸出模式
lcd1602_init();
}
/*********************************************************************//**
* @brief Delay function
* @param[in] tick - number milisecond of delay time
* @return None
**********************************************************************/
void delay(uint32_t tick)
{
uint32_t i;
uint32_t j;
for (i=tick; i>0; i--)
for (j=0; j<1000; j++);
}
void write_command(uchar command) //寫命令
{
uchar i;
i = command;
LCM_RW_LOW();
LCM_RS_LOW();
LCM_EN_LOW();
delay(5);
LPC_GPIO1->CLR |= (0XFF << 22); //數據八位清零
LPC_GPIO1->SET |= (i << 22); //數據位賦值
delay(5);
LCM_EN_HIGH();
delay(5);
LCM_EN_LOW();
}
void write_data(uchar date) //寫單個字符
{
uchar i;
i = date;
LCM_RW_LOW();
LCM_RS_HIGH();
LCM_EN_LOW();
delay(5);
LPC_GPIO1->CLR |= (0XFF << 22); //數據八位清零
LPC_GPIO1->SET |= (i << 22); //數據位賦值
delay(5);
LCM_EN_HIGH();
delay(5);
LCM_EN_LOW();
}
void dr_screen_print(int x1,int x2,unsigned char *s1,unsigned char *s2) //,int light
{
int j;
if ((x1 < 8) && (x2 < 8))
{
j=0;
write_command(0x80 +j* 16+ x1);//????DDRAM??row * 16 + col??
while(*s1)
{
write_data(*s1);
s1++;
}
++j;
write_command(0x80 +j*16+ x2);//????DDRAM??row * 16 + col??
while(*s2)
{
write_data(*s2);
s2++;
}
}
}
/*
unsigned char set_lcd_position(unsigned char row, unsigned char col)
{
if ((row < 2) && (col < 8)) //?????2?7.5?(????????)
{
write_command(0x80 + row * 16 + col);//????DDRAM??row * 16 + col??
return 1;//????
}
else
return 0;//????
}
void display_string(uchar *p) //寫字符串
{
while(*p)
{
write_data(*p);
p++;
}
}
}
*/
/*void lcd_display(uchar row, uchar col, uchar *p)
{
if (set_lcd_position(row, col))
{
display_string(p);
}
}
*/
/*void gotoxy(unsigned y,unsigned x) //顯示位置 2行16列 Y:1-2; X:0-15
{
if(y ==0) //第一行
write_command(0x80+x);
else if(y ==1) //第二行
write_command(0x80+0x0F+x);
}
*/
|