2016年4月12日 星期二

DIY - PIC24:PIC24FJ64GA008 LCD1602 4BIT 液晶模組電路(十四)

DIY - PIC24PIC24FJ64GA008 LCD1602 4BIT 液晶模組電路十四): 

Microchip PIC24FJ64GA008 LCD1602 8-bit 液晶模組電路會改變成 4-bit 電路盡量減少 I/O 選用 4-bit 模式資料傳送LCD 4條資料引腳 D4D7 MSBMost Significant Bit直接接到 MCU PortE (RE4 – D4RE5 – D5RE62 – D6RE7 – D7)LSBLeast Significant Bit)的 4條資料引腳 D0D3 會空接而控 LCD 制引腳接到 MCU PortE (RE0 - RSRE1 - RWRE2 - E)

Microchip PIC24FJ64GA008 LCD1602 4BIT 液晶模組
電路選用 4BIT 資料傳送操作與 8BIT 相同只是資料分兩次操作先發送高 4BITMSBMost Significant Bit),然後低 4BITLSBLeast Significant Bit),介面只用 D7D4D3D0 不用其他的與 8BIT 方式相同。

Microchip PIC24FJ64GA008 LCD1602 4BIT 液晶模組
Microchip PIC24FJ64GA008 LCD1602 xlcd.h 文件
//*************************** LCD SIGNALS ***********************************************/
//* When in 4-bit interface define if the data is in the upper
//* or lower nibble.  For lower nibble, comment the #define UPPER

#define UPPER       // Upper 4bit Mode D4-D7 Connected to LCD D4-D7

      #define  DATA_PORT        LATE                 // Port for LCD data
       #define  TRIS_DATA_PORT  TRISE

      #define  RS_PIN            LATEbits.LATE0    // LCD RS signal
      #define  RW_PIN           LATEbits.LATE1    // LCD R/W signal
      #define  E_PIN             LATEbits.LATE2    // LCD E signal

      #define  TRIS_RS        TRISEbits.TRISE0
      #define  TRIS_RW       TRISEbits.TRISE1
      #define  TRIS_E         TRISEbits.TRISE2

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Microchip PIC24FJ64GA008 LCD1602 4BIT 液晶模組的的正面
Microchip PIC24FJ64GA008 LCD1602 4BIT 液晶模組的的背面
LCD1602 4BIT 液晶模組顯示
Microchip PIC24FJ64GA008 LCD1602 4BIT 液晶模組電路圖 (Schematic)
20164 12日 天氣報告
氣溫:21.5@ 20:10
相對濕度:百分之 99%
天氣:微雨

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%
天氣:霧

2016年4月4日 星期一

DIY - PIC24:PIC24FJ64GA008 LCD1602 8BIT 液晶模組電路(十二)

DIY - PIC24PIC24FJ64GA008 LCD1602 8BIT 液晶模組電路(十二): 

確定了 Microchip PIC24FJ64GA008 電路硬件(Hardware)和軟件(Software)都能夠順利運行後,下一步就要加入新的輸出裝置,這是方便顯示狀態,當然 LED 燈是可以,但能夠顯示出更多資料數據,對於編程和試驗會容易知道錯誤在那裡,所以裝置 LCD(Liquid-Crystal Display液晶模組顯示是最直接簡單。

Microchip PIC24FJ64GA008 LCD1602 液晶模組
Microchip PIC24FJ64GA008 電路上加入了新的 LCD 液晶模組,LCD1602 是有 216字符(LCD 6x2)的 LCD (液晶) 顯示屏,共有 16條引腳,2條電源引腳、8條資料引腳、2條背光燈(BackLite)引腳及4條控制引腳。LCD1602 引腳連接到 MCU PORT D,為了減低程式的複雜性,首先會選用 8BIT 模式資料傳送,LCD 8條資料引腳 D0D7 直接接到 MCU PortD (RD0 – D0RD1 – D1RD2 – D2RD3 – D3RD4 – D4RD5 – D5RD6 – D6RD7 – D7),而控 LCD 制引腳接到 MCU PortE (RE0 - RSRE1 - RWRE2 - E)LCD1602 液晶模組的詳細資料,可參考網址:http://bugworkshop.blogspot.hk/2011/02/diy-piclcd1602.html

LCD1602 液晶模組電路圖 (Schematic)
LCD1602 液晶模組腳定義 (Pin Assignment)
準備 LCD1602 液晶模組電路的電子零件
完成 LCD1602 液晶模組電路的組裝
測試 LCD1602 液晶模組電路
20164 4日 天氣報告
氣溫:20.4@ 20:40
相對濕度:百分之 93%
天氣:雨