📄 lcd.lst
字号:
1:
2: /* LCD interface example
3: * Uses routines from delay.c
4: * This code will interface to a standard LCD controller
5: * like the Hitachi HD44780. It uses it in 4 bit mode, with
6: * the hardware connected as follows (the standard 14 pin
7: * LCD connector is used):
8: *
9: * PORTB bits 0-3 are connected to the LCD data bits 4-7 (high nibble)
10: * PORTA bit 4 is connected to the LCD RS input (register select)
11: * PORTB bit 3 is connected to the LCD EN bit (enable)
12: *
13: * To use these routines, set up the port I/O (TRISA, TRISB) then
14: * call lcd_init(), then other routines as required.
15: *
16: */
17: #ifndef XTAL FREQ
18: #define XTAL_FREQ 4MHZ /* Crystal frequency in MHz */
19: #endif
20:
21: #include <pic.h>
22: #include "lcd.h"
23: #include "delay.h"
24:
25:
26: #define PORTBIT(adr, bit) ((unsigned)(&adr)*8+(bit))
27:
28: static bit LCD_RS @ PORTBIT(PORTA, 4); // Register select
29: static bit LCD_EN @ PORTBIT(PORTB, 3); // Enable
30: static bit spkr @ PORTBIT(PORTA, 0);
31:
32:
33: void
34: lcd_strobe(void)
35: {
36: LCD_EN = 1 ;
37: DelayUs(1);
38: LCD_EN = 0 ;
39: DelayUs(1);
40: }
41:
42: void
43: lcd_write(unsigned char c)
44: {
45: PORTB = (PORTB & 0x0F) | (c & 0xF0); /*es. A=65 =>0100 0001 bin >>4 avremo 0000 0100 or PORTB&11110000 */
46: lcd_strobe();
47: PORTB = (PORTB & 0x0F) | (c << 4); /* 0100 0001 & 00001111 = 0000 0001 */
48: lcd_strobe();
49: DelayUs(50);
50: }
51:
52: /*
53: * Clear and home the LCD
54: */
55:
56: void
57: lcd_clear(void)
58: {
59: LCD_RS = 0;
60: lcd_write(0x1);
61: DelayMs(2);
62: }
63:
64: /* write a string of chars to the LCD */
65:
66: void
67: lcd_puts(const char * s)
68: {
69: LCD_RS = 1; // write characters
70: while(*s)
71: lcd_write(*s++);
72: }
73:
74: /* write one character to the LCD */
75:
76: void
77: lcd_putch(char c)
78: {
79: LCD_RS = 1; // write characters
80: PORTB = (PORTB & 0x0F) | (c & 0xF0);
81: lcd_strobe();
82: PORTB = (PORTB & 0x0F) | (c << 4);
83: lcd_strobe();
84: DelayUs(40);
85: }
86:
87:
88: /*
89: * Go to the specified position
90: */
91:
92: void
93: lcd_goto(unsigned char pos)
94: {
95: LCD_RS = 0;
96: lcd_write(0x80+pos);
97: }
98:
99: /* initialise the LCD - put into 4 bit mode */
100:
101: void
102: lcd_init(void)
103: {
104: LCD_RS = 0; // write control bytes 'comando'
105: DelayMs(50); // power on delay
106: PORTB = 0x30; // attention!
107: lcd_strobe();
108: DelayMs(5);
109: lcd_strobe();
110: DelayUs(100);
111: lcd_strobe();
112: DelayMs(1);
113: PORTB = 0x20; // set 4 bit mode
114: lcd_strobe();
115: DelayMs(1);
116: lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font
117: lcd_write(0x06); // entry mode
118: lcd_write(0x0C); // display on
119: lcd_clear(); // clear
120:
121: }
122:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -