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

📄 mid_lcd.lst

📁 PICC 16X Code for study and learning
💻 LST
字号:
     1: // MID_LCD.C

     2: // Contain all the necessary LCD subroutines for EVM Board APP001 in Mid Range HIGHTEC C Format

     3: // Must include "MID_LCD.H"

     4: 

     5: #include "mid_lcd.h"

     6: #include <pic.h>

     7: 

     8: //

     9: // Defines for I/O ports that provide LCD data & control

    10: // PORTD[0:3]-->DB[4:7]: Higher order 4 lines data bus with bidirectional

    11: //                                        : DB7 can be used as a BUSY flag

    12: // PORTA.2 --> [E] : LCD operation start signal control 

    13: // PORTD.5 --> [RW]: LCD Read/Write control

    14: // PORTD.4 --> [RS]: LCD Register Select control

    15: //                         : "0" for Instrunction register (Write), Busy Flag (Read)

    16: //                                 : "1" for data register (Read/Write)

    17: //

    18: #define         CPU_SPEED                       16                              // CPU speed is 16 Mhz !!

    19: #define         LCD_RS                          RD4                     // The definition of control pins

    20: #define         LCD_RW                          RD5

    21: #define         LCD_E                           RA2

    22: #define         LCD_DATA                        PORTD                   // PORTD[0:3] as LCD DB[4:7]

    23: #define         DIR_LCD_DATA            TRISD

    24: #define         MASK_DIR_LCD_DATA       0b00000000 

    25: #define         DIR_LCD_RS                      TRISD4

    26: #define         DIR_LCD_RW                      TRISD5

    27: #define         DIR_LCD_E                       TRISA2          

    28: 

    29: 

    30: //  LCD Module commands

    31: #define DISP_2Line_8Bit 0b00111000

    32: #define DISP_2Line_4Bit 0b00101000

    33: #define DISP_ON                 0x00C           // Display on

    34: #define DISP_ON_C               0x00E           // Display on, Cursor on

    35: #define DISP_ON_B               0x00F           // Display on, Cursor on, Blink cursor

    36: #define DISP_OFF                0x008           // Display off

    37: #define CLR_DISP                0x001           // Clear the Display

    38: #define ENTRY_INC               0x006           //

    39: #define ENTRY_INC_S             0x007           //

    40: #define ENTRY_DEC               0x004           //

    41: #define ENTRY_DEC_S             0x005           //

    42: #define DD_RAM_ADDR             0x080           // Least Significant 7-bit are for address

    43: #define DD_RAM_UL               0x080           // Upper Left coner of the Display      

    44: 

    45:  

    46:                 

    47: unsigned char   Temp_CMD ;

    48: unsigned char   Str_Temp ;

    49: unsigned char   Out_Mask ;      

    50: unsigned char   Temp_LCD_DATA ; 

    51: 

    52: void OpenLCD(void)

    53: 

    54: {

    55:         DIR_LCD_DATA = MASK_DIR_LCD_DATA ;      

    56:         DIR_LCD_RS = 0 ;

    57:         DIR_LCD_RW = 0 ;

    58:         DIR_LCD_E = 0 ;

    59: 

    60:         LCD_E=0;        

    61:         

    62:         TRISA2 = 0 ;                                            // RA2 as output                                                                

    63:         

    64:         LCD_DATA = 0x00;                                        // LCD DB[4:7] & RS & R/W --> Low

    65: 

    66:         DIR_LCD_DATA = 0x00;                            // LCD DB[4:7} & RS & R/W are output function

    67:         LCD_E=0;                                        // Set E pin as output

    68: 

    69:         LCD_DATA = 0b00000011 ;

    70:         LCD_CMD_W_Timing() ;

    71:         LCD_L_Delay() ;

    72: 

    73:         LCD_DATA = 0b00000011 ;

    74:         LCD_CMD_W_Timing() ;

    75:         LCD_L_Delay() ;

    76: 

    77:         LCD_DATA = 0b00000011 ;

    78:         LCD_CMD_W_Timing() ;

    79:         LCD_L_Delay() ;

    80: 

    81:         LCD_DATA = 0b00000010 ;

    82:         LCD_CMD_W_Timing() ;

    83:         LCD_L_Delay() ;

    84: 

    85:         WriteCmdLCD(DISP_2Line_4Bit) ;

    86:         LCD_S_Delay() ;

    87: 

    88:         WriteCmdLCD(DISP_ON) ;

    89:         LCD_S_Delay() ;

    90: 

    91:         WriteCmdLCD(ENTRY_INC) ;

    92:         LCD_S_Delay() ;

    93: 

    94:         WriteCmdLCD(CLR_DISP) ;

    95:         LCD_L_Delay() ;

    96: 

    97:                 

    98: }

    99: 

   100: //*********************************************

   101: //     _    ______________________________

   102: // RS  _>--<______________________________

   103: //     _____

   104: // RW       \_____________________________

   105: //                  __________________

   106: // E   ____________/                  \___

   107: //     _____________                ______

   108: // DB  _____________>--------------<______

   109: //***********************************************

   110: // Write Command to LCD module

   111: //

   112: void WriteCmdLCD( unsigned char LCD_CMD) 

   113: {

   114: 

   115:         Temp_LCD_DATA = LCD_DATA ;      

   116: 

   117:         Temp_CMD = (LCD_CMD & 0xF0)>>4 ;                        // Send high nibble to LCD bus

   118:         LCD_DATA= (LCD_DATA & 0xF0)|Temp_CMD ;

   119:         LCD_CMD_W_Timing () ;

   120: 

   121:         Temp_CMD = LCD_CMD & 0x0F ;                             // Send low nibble to LCD bus

   122:         LCD_DATA= (LCD_DATA & 0xF0)|Temp_CMD ;

   123:         LCD_CMD_W_Timing () ;

   124: 

   125:         LCD_DATA = Temp_LCD_DATA ;

   126: 

   127:         LCD_S_Delay() ;                                                 // Delay 100uS for execution

   128: }

   129: 

   130: //***********************************************

   131: // Write Data to LCD module

   132: //

   133: void WriteDataLCD( unsigned char LCD_CMD) 

   134: {

   135:         

   136:         Temp_LCD_DATA = LCD_DATA ;

   137: 

   138:         Temp_CMD = (LCD_CMD & 0xF0)>>4 ;                        // Send high nibble to LCD bus

   139:         LCD_DATA= (LCD_DATA & 0xF0)|Temp_CMD ;

   140:         LCD_DAT_W_Timing () ;

   141: 

   142:         Temp_CMD = LCD_CMD & 0x0F ;                             // Send low nibble to LCD bus

   143:         LCD_DATA= (LCD_DATA & 0xF0)|Temp_CMD ;

   144:         LCD_DAT_W_Timing () ;

   145: 

   146:         LCD_DATA = Temp_LCD_DATA ;

   147: 

   148:         LCD_S_Delay() ;                                                 // Delay 100uS for execution

   149: }

   150: 

   151: void putcLCD(unsigned char LCD_Char)

   152: {

   153:         WriteDataLCD(LCD_Char) ;

   154: 

   155: }

   156: void LCD_CMD_W_Timing( void )

   157: {

   158:         LCD_RS = 0 ;    // Set for Command Input

   159:         #asm

   160:                 nop

   161:                 nop

   162:         #endasm

   163:         LCD_RW = 0 ;

   164:         #asm

   165:                 nop

   166:                 nop

   167:                 nop

   168:                 nop

   169:                 nop

   170:                 nop

   171:                 nop

   172:                 nop

   173:                 nop

   174:                 nop

   175:         #endasm

   176:         LCD_E = 1 ;

   177:         #asm

   178:                 nop

   179:                 nop

   180:                 nop

   181:                 nop

   182:                 nop

   183:                 nop

   184:                 nop

   185:                 nop

   186:                 nop

   187:                 nop

   188:         #endasm

   189:         LCD_E = 0 ;

   190: }

   191: 

   192: void LCD_DAT_W_Timing( void )

   193: {

   194:         LCD_RS = 1 ;    // Set for Data Input

   195:         

   196:         #asm

   197:                 nop

   198:                 nop

   199:         #endasm

   200:         

   201:         LCD_RW = 0 ;    

   202:         

   203:         #asm

   204:                 nop

   205:                 nop

   206:                 nop

   207:                 nop

   208:                 nop

   209:                 nop

   210:                 nop

   211:                 nop

   212:                 nop

   213:                 nop

   214:         #endasm

   215:         LCD_E = 1 ;

   216:         #asm

   217:                 nop

   218:                 nop

   219:                 nop

   220:                 nop

   221:                 nop

   222:                 nop

   223:                 nop

   224:                 nop

   225:                 nop

   226:                 nop

   227:         #endasm 

   228:         LCD_E = 0 ;

   229: }

   230: 

   231: //***********************************************

   232: //     Set Cursor position on LCD module

   233: //                      CurY = Line (0 or 1)

   234: //              CurX = Position ( 0 to 15)

   235: //

   236: void LCD_Set_Cursor(unsigned char CurX, unsigned char CurY)

   237: {

   238:         WriteCmdLCD( 0x80 + CurY * 0x40 + CurX) ;

   239:         LCD_S_Delay( ) ;

   240:         LCD_S_Delay( ) ;

   241:         LCD_S_Delay( ) ;

   242:         LCD_S_Delay( ) ;

   243:         LCD_S_Delay( ) ;

   244: }

   245: 

   246: //***********************************************

   247: //    Put a ROM string to LCD Module

   248: //

   249: void putrsLCD( const char *Str )

   250: {

   251:    while (1)

   252:    {

   253:         Str_Temp = *Str ;

   254: 

   255:                 if (Str_Temp != 0x00 )

   256:                    {

   257:                         WriteDataLCD(Str_Temp) ;

   258:                         Str ++ ;

   259:                    }

   260:                 else

   261:                         break ;

   262:    }

   263: }

   264: 

   265: //***********************************************

   266: //    Put a RAM string to LCD Module

   267: //

   268: void putsLCD( char *Str)

   269: {

   270:    while (1)

   271:    {

   272:         Str_Temp = *Str ;

   273: 

   274:                 if (Str_Temp != 0x00 )

   275:                    {

   276:                         WriteDataLCD(Str_Temp) ;

   277:                         Str ++ ;

   278:                    }

   279:                 else

   280:                         break ;

   281:    }

   282: }

   283: 

   284: 

   285: void puthexLCD(unsigned char HEX_Val)

   286: {

   287:         unsigned char Temp_HEX ;

   288: 

   289:         Temp_HEX = (HEX_Val >> 4) & 0x0f ;

   290: 

   291:         if ( Temp_HEX > 9 )Temp_HEX += 0x37 ;

   292:     else Temp_HEX += 0x30 ;

   293: 

   294:         WriteDataLCD(Temp_HEX) ;

   295: 

   296:         Temp_HEX = HEX_Val  & 0x0f ;

   297:         if ( Temp_HEX > 9 )Temp_HEX += 0x37 ;

   298:     else Temp_HEX += 0x30 ;

   299: 

   300:         WriteDataLCD(Temp_HEX) ;

   301: }

   302: 

   303: // *********************************************************************************

   304: // Delay for atleast 10 ms 

   305: // *********************************************************************************

   306: void    LCD_L_Delay(void)

   307: {

   308:         unsigned int    Loop_L ;

   309:         

   310:         for ( Loop_L = 0 ; Loop_L < 10000 ; Loop_L ++ ) ;

   311: 

   312: }

   313: 

   314: // *********************************************************************************

   315: // Delay for 100 us

   316: // *********************************************************************************

   317: void    LCD_S_Delay(void)

   318: {

   319:         unsigned int    Loop_S ;

   320:                 

   321:         for ( Loop_S = 0 ; Loop_S < 100 ; Loop_S ++ ) ; 

   322: }

   323: 

   324: 

   325: void    Delay10KTCYx( int Loop_10K )

   326: {

   327:                 int     Loop_1 , Loop_2 ;

   328:                 

   329:                 for ( Loop_1 = 0 ; Loop_1 < Loop_10K ; Loop_1++ ) 

   330:                 {

   331:                         for ( Loop_2 = 0 ; Loop_2 < 10000 ; Loop_2++ )

   332:                         {

   333:                                 #asm

   334:                                  nop

   335:                                 #endasm

   336:                         }

   337:                 }

   338:                  

   339: }

   340: 

   341: void    Delay100TCYx( int D_Value )

   342: {

   343:         int     D_Loop , D_LoopA ;

   344:         

   345:                 for ( D_Loop = 0 ; D_Loop < D_Value ; D_Loop++ )

   346:                 {

   347:                         for (D_LoopA = 0 ; D_LoopA < 10 ; D_LoopA ++ ) 

   348:                         {

   349:                                 #asm

   350:                                 nop

   351:                                 #endasm

   352:                         }

   353:                 }

   354: }

   355: 

   356: void    LCD_Put_HEX(unsigned char HEX_Val)

   357: {

   358:         unsigned char Temp_HEX ;

   359: 

   360:         Temp_HEX = (HEX_Val >> 4) & 0x0f ;

   361:         if ( Temp_HEX > 9 )

   362:         Temp_HEX += 0x37 ;

   363:     else

   364:                 Temp_HEX += 0x30 ;

   365: 

   366:         putcLCD(Temp_HEX) ;

   367: 

   368:         Temp_HEX = HEX_Val  & 0x0f ;

   369:         if ( Temp_HEX > 9 )

   370:         Temp_HEX += 0x37 ;

   371:     else

   372:                 Temp_HEX += 0x30 ;

   373: 

   374:         putcLCD(Temp_HEX) ;     

   375: 

   376: }

⌨️ 快捷键说明

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