/* - - - - - - - LCD interface - - - - - - - - -
* This code will interface to a standard LCD controller
* It uses it in 4 or 8 bit mode.
*/
#include <pic18.h>
#include "lcd.h"
static bit fourbit; // four or eight bit mode?
#ifdef CHECKBUSY
unsigned char
lcd_read_cmd_nowait(void)
{
unsigned char c, readc;
LCD_DATA_TRIS = INPUT_DATA;
LCD_RW = 1; // Read LCD
asm("nop"); // short propagation delay
asm("nop"); // short propagation delay
if (fourbit)
{
LCD_STROBE_READ(readc); // Read high nibble
// Move 4 bits to high nibble while zeroing low nibble
c = ( ( readc << 4 ) & 0xF0 );
LCD_STROBE_READ(readc); // Read low nibble
c |= ( readc & 0x0F ); // Or in 4 more bits to low nibble
}
else
{
LCD_STROBE_READ(readc);
c = readc;
}
LCD_RW = 0; // Return to default mode of writing LCD
LCD_DATA_TRIS = OUTPUT_DATA; // Return to default mode of writing LCD
return(c);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
void
lcd_check_busy(void) // Return when the LCD is no longer busy,
{ //or we've waiting long enough!
// To avoid hanging forever in event there's a bad or
// missing LCD on hardware. Will just run SLOW, but still run.
unsigned int retry;
unsigned char c;
for (retry=1000; retry-- > 0; ) {
c = lcd_read_cmd_nowait();
if (0==(c&0x80)) break; // Check busy bit. If zero, no longer busy
}
}
#endif
/* - - - - - - send a command to the LCD - - - - - - - - */
void
lcd_cmd(unsigned char c)
{
LCD_WAIT; // may check LCD busy flag, or just delay a little, depending on lcd.h
if (fourbit)
{
LCD_DATA = ( ( c >> 4 ) & 0x0F );
LCD_STROBE();
LCD_DATA = ( c & 0x0F );
LCD_STROBE();
}
else
{
LCD_DATA = c;
LCD_STROBE();
}
}
/* - - - - - - - - - send data to the LCD - - - - - - - -*/
void
lcd_data(unsigned char c)
{
LCD_WAIT; // may check LCD busy flag, or just delay a little, depending on lcd.h
LCD_DATA = 0;
LCD_RS = 1;
if (fourbit)
{
LCD_DATA |= ( ( c >> 4 ) & 0x0F );
LCD_STROBE();
LCD_DATA &= 0xF0;
LCD_DATA |= ( c & 0x0F );
LCD_STROBE();
}
else
{
LCD_DATA = c;
LCD_STROBE();
}
LCD_RS = 0;
}
/* - - - - - - - write a string of chars to the LCD - - - - - - */
void
lcd_puts(const char * s)
{
while(*s)
lcd_data(*s++);
}
/* - - - - - - initialize the LCD - - - - - - - - */
void
lcd_init(unsigned char mode)
{
char init_value;
fourbit = 0;
if (mode == FOURBIT_MODE){
fourbit = 1;
init_value = 0x3;
}else{
init_value = 0x3F;
}
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;
LCD_RS_TRIS = OUTPUT_PIN;
LCD_EN_TRIS = OUTPUT_PIN;
LCD_RW_TRIS = OUTPUT_PIN;
LCD_DATA_TRIS = OUTPUT_DATA;
DelayMs(15);
LCD_DATA = init_value;
LCD_STROBE();
DelayMs(5);
LCD_DATA = init_value;
LCD_STROBE();
DelayUs(200);
LCD_DATA = init_value;
LCD_STROBE();
if (fourbit){
LCD_WAIT; //may check LCD busy flag, or just delay a little, depending on lcd.h
LCD_DATA = 0x2; // Set 4-bit mode
LCD_STROBE();
lcd_cmd(0x28); // Function Set
}else{
lcd_cmd(0x38);
}
lcd_cmd(0xF); //Display On, Cursor On, Cursor Blink
lcd_cmd(0x1); //Display Clear
lcd_cmd(0x6); //Entry Mode
lcd_cmd(0x80); //Initialize DDRAM address to zero
}
/* - - - - - - - - - Delay functions - - - - - - - - - -
*
* See delay.h for details
* Make sure this code is compiled with full optimization!!!
*/
void
DelayMs(unsigned char cnt)
{
unsigned char i;
while (cnt--) {
i=4;
while(i--) {
DelayUs(uS_CNT); /* Adjust for error */
} ;
} ;
}
LCD.rar_pic18f
版权申诉
190 浏览量
2022-09-22
22:00:00
上传
评论
收藏 3KB RAR 举报
APei
- 粉丝: 63
- 资源: 1万+
最新资源
- Python 手写实现 iD3 决策树算法-根据信息增益公式.zip
- 411675952289057车联助手-小窗版(三星)3.5.1.apk
- 三种快速排序方法合并在一个文件中以便直接运行的Python代码示例
- 937712277954201实习5.word
- 2程序语言基础知识pdf1_1716337722703.jpeg
- 简单的Python示例,演示了如何使用TCP/IP协议进行基本的客户端和服务器通信
- 考试.sql
- keil2 + proteus + 8051.exe
- 1961ee27df03bd4595d28e24b00dde4e_744c805f7e4fb4d40fa3f695bfbab035_8(1).c
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈