網頁

2016年4月7日 星期四

DIY - PIC24:C30 LCD1602 8BIT 液晶模組程式(十三)

DIY - PIC24C30 LCD1602 8BIT 液晶模組程式(十三):

LCD 液晶顯示器顯示程式,程式開始初始化 LCD160216x2)模組,設定不閃動及不顯示游標。初始化後,將 8BIT 資料傳送到液晶屏上,然後顯示資料後,閃動 LED 燈作程式完成。
 
Microchip PIC24FJ64GA008 LCD1602 液晶模組
Microchip PIC24FJ64GA008 LCD1602 8B 主程式:
//.............................................................
//                        FILES INCLUDED IN THE PROJECT
//.............................................................
#include "p24FJ64GA008.h"
#include "system\lcd\lcd.h"         // XLCD1 LCD header
#include "system\lcd\delay.h"
//.............................................................
//                             CONFIGURATION  BITS
//.............................................................
// Config1 Code Protect off, Write protect off,
_CONFIG1(JTAGEN_OFF &               // JTAG port is disabled
         GCP_OFF &
         GWRP_OFF &
         FWDTEN_OFF                  // Watchdog Timer is disabled
        );
// Config1 Fast RC Oscillator w/ divider and PLL
_CONFIG2(   FNOSC_FRCPLL           // FRC w/ divider and PLL
        );
//........................................................................
//                        FILES INCLUDED IN THE PROJECT
//........................................................................
#include "p24FJ64GA008.h"
#include "system\lcd\lcd.h"          // XLCD1 LCD header
#include "system\lcd\delay.h"
//........................................................................
//                             CONFIGURATION  BITS
//........................................................................
// Config1 Code Protect off, Write protect off,
_CONFIG1(JTAGEN_OFF &                // JTAG port is disabled
         GCP_OFF &
         GWRP_OFF &
         FWDTEN_OFF                  // Watchdog Timer is disabled
        );

// Config1 Fast RC Oscillator w/ divider and PLL
_CONFIG2(   FNOSC_FRCPLL           // FRC w/ divider and PLL
        );

//........................................................................
//                              GLOBAL VARIABLES
//........................................................................
// LCD1602     1234567890123456"
 char *msg1 = "BugWorkshop v1.0"               ;  // BugWorkshop
 char *msg2 = "PIC24 LCD1602 8B"               ;  // PIC24FJ64GA008

void init(){
OSCCONbits.NOSC = 0b001;  // Set Fast RC OSC
CLKDIVbits.RCDIV = 0b000; // Set Clock Div 1:1
TRISD = 0x0000;
TRISE = 0x0000;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                             MAIN FUNCTION
//.........................................................................
int main(void){                     // Test LCD 1602 8-bit and LED Flash

init();                               // init OSC & PORT
ini_lcd();                           // init the LCD module 8bits, 2lines,
                              // Shift right cursor MODE, Display=ON
clr_lcd();                           // clear LCD
wrcmnd_lcd(DISPLAY+D_ON);         // shut down CURSOR & BLINK (keep the display) 
wrstr_lcd(msg1);                   // write in LCD Message 1
wrcmnd_lcd(SET_DDRAM+N2_ROW2);  // set cursor on the second row
wrstr_lcd(msg2);                   // write in LCD Message 2

// LED Flash and Stop
   TRISB = 0x00;

while( 1 ) {
        // LED Flash
        PORTB = 0xFF;
        delay15ms();

        // LED Stop
        PORTB = 0x00;
        delay15ms();
}

return 0; 
}   // END OF MAIN

要修改 lcd.h 文件並配置到你的設定和配置,記得讀取是用 PORTXInput = PORTX)、寫出是用 LATxOutput = LATx)。

Microchip PIC24FJ64GA008 LCD1602 lcd.h 文件:
      #define  RW        LATEbits.LATE2     // LCD R/W signal
      #define  RS        LATEbits.LATE1    // LCD RS signal
      #define  E         LATEbits.LATE3     // LCD E signal

      #define  RW_TRIS  TRISEbits.TRISE2
      #define  RS_TRIS  TRISEbits.TRISE1
      #define  E_TRIS    TRISEbits.TRISE3

      #define  DATA      LATD                 // Port for LCD data

//*********************    LCD' FUNCTIONS PROTOYPES ***********************************/
void ini_lcd(void);                          // initializes the LCD       
void wrcmnd_lcd(unsigned char cmd);     // writes a command to the LCD
void wrdata_lcd(unsigned char data);   // writes a data byte to the LCD
void clr_lcd(void);       // clears the LCD   
void back_lcd(unsigned char pos);   // turns back the cursor with 'pos' positions
void del_lcd(unsigned char pos);    // deletes back 'pos' characters
void wrstr_lcd(char *str_lcd);      // writes a string to the LCD
void wrnr_lcd(unsigned long k);     // writes a number to the LCD
void test_lcd(unsigned char data);
//.....................................................................................

Microchip PIC24FJ64GA008 LCD1602 lcd.c 程式
#include "p24FJ64GA008.h"
#include "delay.h"        
#include "lcd.h"          // XLCD1 LCD header
//........................................................................................
//                          LCD LOW LEVEL FUNCTIONS
//........................................................................................
// The below function initializes the LCD: 8 BITS, 2 LINES,
// DISPLAY ON, CURSOR/BLINK OFF, INCREMENT MODE, SHIFT RIGHT 
//........................................................................................  
void ini_lcd(void){             // initialization of the LCD
delay15ms(); delay15ms()      ; // wait Vdd reaches nnVdc before proceeding with LCD initialization        
// set initial states for the data & control pins
DATA &= 0xFF00                ; // clear RE0-RE7 (data pins)
RW = 0                        ; // R/W = low
RS = 0                        ; // RS  = low
E  = 0                        ; // E   = low
// set data & control pins to outputs
TRISE &= 0xFF00               ; // RE0 - RE7 = output (data pins)
RW_TRIS = 0                   ; // RW  = output
RS_TRIS = 0                   ; // RS  = output
E_TRIS = 0                    ; // E   = output
wrcmnd_lcd(FUNCTION+DL8+LINES); // function set (data length = 8 bits, 2 lines)
delay15ms(); delay15ms()      ;
wrcmnd_lcd(DISPLAY+D_ON)      ; // Display = ON, cursor & blink = OFF
delay15ms(); delay15ms()      ;
wrcmnd_lcd(MODE+SHR)      ;
delay15ms(); delay15ms()      ;     }  // entry mode set (inc mode, shift right)
//....................................................................................
//                 Writes a command into the LCD
//....................................................................................
void wrcmnd_lcd(unsigned char cmd){ // writting a command into the LCD
DATA &= 0xFF00                ; // clear RE0-RE7 (data pins)
DATA |= cmd                   ; // send command byte to LCD
RW = 0                        ; // ensure RW is 0
RS = 0                        ;   // and RS is 0
E = 1                         ; // toggle E signal
Nop()  ;   Nop()  ;   Nop()   ; // short delay        
E = 0                         ;    // clear E signal
delay5ms()        ;           } // 5ms delay
//....................................................................................
//                     Writes a data byte into the LCD
//....................................................................................
void wrdata_lcd(unsigned char data){ // writting a data byte into the LCD
RW = 0                        ;  // ensure RW is 0
RS = 1                        ;  // assert register select to 1
DATA &= 0xFF00                ;  // clear RE0-RE7 (data pins)
DATA |= data                  ;  // send data byte to lcd
E  = 1                        ;  // set E signal              
Nop()   ;   Nop()   ;   Nop() ;  // short delay
E  = 0                        ;  // toggle E signal
RS = 0                        ;  // negate register select to 0
delay200us() ; delay200us() ; }  // 400usec delay  #define  TRIS_RS  TRISEbits.TRISE1
//.......................................................................................

Microchip PIC24FJ64GA008 LCD1602 液晶模組
LCD1602 液晶模組顯示
20164 7日 天氣報告
氣溫:25.2@ 19:30
相對濕度:百分之 92%
天氣:霧

沒有留言:

張貼留言