⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcd.lst

📁 利用热释红外传感器、12C887和PIC单片机制作的智能电子时钟
💻 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 samsung KS0066. It uses it in 4 bit mode, with

     6:  *  the hardware connected as follows (the standard 16 pin

     7:  *  LCD connector is used):

     8:  *

     9:  *  PORTC bits 4-7 are connected to the LCD data bits 4-7 (high nibble)

    10:  *  PORTA bit 1 is connected to the LCD RS input (register select)

    11:  *  PORTA bit 2 is connected to the LCD EN bit (enable)

    12:  *

    13:  *  To use these routines, set up the port I/O (TRISC, TRISB) then

    14:  *  call lcd_init(), then other routines as required.

    15:  *

    16:  */

    17: 

    18: #include <pic.h>

    19: #include "lcd.h"

    20: #include "delay.h"

    21: 

    22: #define TLCD_RS  TRISA1

    23: #define TLCD_EN  TRISA2

    24: #define TLCD_A   TRISA3

    25: #define LCD_RS   RA1   // 寄存器选择

    26: #define LCD_EN   RA2   // 使能

    27: #define LCD_A    RA3   // 背光,RA4开漏输出,欲输出1,需要外接上拉电阻

    28: 

    29: #define LCD_STROBE  ((LCD_EN = 1),(LCD_EN=0))

    30: #define LCD_CURSOR(x)   lcd_write(((x)&0x7F)|0x80)

    31: 

    32: /* light lcd & dark*/

    33: 

    34: void lcd_light(void)

    35: {

    36:     LCD_A = 0;

    37: }

    38: 

    39: void lcd_dark(void)

    40: {

    41:     LCD_A = 1;

    42: }

    43: 

    44: 

    45: /* write a byte to the LCD in 4 bit mode */

    46: 

    47: void lcd_write(unsigned char c)

    48: {

    49:     PORTC = (PORTC & 0x0F) |  (c &0xF0);

    50:     LCD_STROBE;

    51:     PORTC = (PORTC & 0x0F) |  (c<<4);

    52:     LCD_STROBE;

    53:     DelayUs(40);

    54: }

    55: 

    56: /* Clear and home the LCD */

    57: 

    58: void lcd_clear(void)

    59: {

    60:     LCD_RS = 0;

    61:     lcd_write(0x1);

    62:     DelayMs(2);

    63: }

    64: 

    65: /* write a string of chars to the LCD */

    66: 

    67: void lcd_puts(const char * s)

    68: {

    69:     LCD_RS = 1; // write characters

    70:     while(*s)//当S的值不为0时,执行,故,需要花一个RAM,将其清0作为整个字符数组的结束标记

    71:     {

    72:         lcd_write(*s++);

    73:     }

    74: }

    75: 

    76: 

    77: /* write one character to the LCD */

    78: 

    79: void lcd_putch(char c)

    80: {

    81:     LCD_RS = 1; // write characters

    82:     PORTC = (PORTC & 0x0F) |  (c &0xF0);

    83:     LCD_STROBE;

    84:     PORTC = (PORTC & 0x0F) |  (c<<4);

    85:     LCD_STROBE;

    86:     DelayUs(40);

    87: }

    88: 

    89: /* Go to the specified position */

    90: 

    91: void lcd_goto(unsigned char pos)

    92: {

    93:     LCD_RS = 0;

    94:     LCD_CURSOR(pos);

    95: }

    96: 

    97: /* initialise the LCD - put into 4 bit mode */

    98: #ifndef SPLC780D

    99: void lcd_init(void)

   100: {

   101: 

   102:     TLCD_RS = 0;

   103:     TLCD_EN = 0;

   104:     TLCD_A  = 0;

   105:     LCD_RS = 0;

   106:     LCD_EN = 0;

   107:     LCD_A  = 0;

   108:     LCD_RS = 0;         // write control bytes

   109:     lcd_write(0x2);     // return home

   110:     DelayMs(2);

   111:     lcd_write(0x28);    // 4 bit mode, 2 lines, 5x7 font

   112:     lcd_write(0x0C);    // display on, curson off, blink off

   113:     lcd_clear();        // clear display

   114:     lcd_write(0x06);    // right, increment address

   115: }

   116: #else

   117: void lcd_init(void)

   118: {

   119: 

   120:     TLCD_RS = 0;

   121:     TLCD_EN = 0;

   122:     TLCD_A  = 0;

   123:     LCD_RS = 0;

   124:     LCD_EN = 0;

   125:     LCD_A  = 0;

   126:     LCD_RS = 0;         // write control bytes

   127:     lcd_write(0b00000010);  // return home

   128:     DelayMs(10);

   129:     lcd_write(0b00111111);  // 8 bit interface

   130:     DelayMs(10);

   131:     lcd_write(0b00111111);  // 8 bit interface

   132:     DelayMs(10);

   133:     lcd_write(0b00101000);  // 4 bit interface,2 lines,5*8

   134:     DelayMs(10);

   135:     lcd_write(0b00001100);  // dispaly on

   136:     DelayMs(10);

   137:     lcd_write(0b00000001);  // clear LCD

   138:     DelayMs(10);

   139:     lcd_write(0b00000110);  // entry mode set

   140:     DelayMs(10);

   141: }

   142: #endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -