//****************************************************************************************** // PIC18F14K50 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H24.02.08 // Replace date : // History : // Laungage : MPLAB C18 //****************************************************************************************** // Description // (1)OSC 48MHz (12MHz Crystal * PLL4) // (2)I/O PORT // PORTC(0) : SD1602HU0B-XA(E) // PORTC(1) : SD1602HU0B-XA(RS) // PORTC(7-4) : SD1602HU0B-XA(DB7-4) // // PORTB(4) : No connection // PORTB(6) : No connection // PORTC(2)/AN6 : No connection // //****************************************************************************************** #include #include #include #include #include #include #include #include // //Define configration // #pragma config CPUDIV=NOCLKDIV //CPU System Clock Selection bit(No CPU System Clock divide) #pragma config USBDIV = OFF //USB Clock Selection bit(USB Clock comes directly from the OSC1/OSC2 oscillator block; no divide) #pragma config FOSC = HS //Oscillator Selection bits(HS oscillator) #pragma config PLLEN = ON //X PLL Enable bit(Oscillator multiplied by 4) #pragma config PCLKEN = OFF //Primary Clock Enable Bit(Primary clock is under software control) #pragma config FCMEN = OFF #pragma config IESO = OFF //Internal/External Oscillator Switchover bit(Oscillator Switchover mode disabled) #pragma config PWRTEN = OFF,BOREN = OFF,BORV = 19 #pragma config WDTEN = OFF,WDTPS = 1 #pragma config MCLRE = OFF //MCLR Pin Enable bit(RE3 input pin enabled; MCLR disabled) #pragma config HFOFST = OFF,STVREN = OFF,LVP = OFF,BBSIZ = OFF,XINST = OFF #pragma config CP0 = OFF,CP1 = OFF,CPB = OFF,CPD = OFF,WRT0 = OFF,WRT1 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF,EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF // //Define function // #define LCD_E 0 #define LCD_RS 1 #define LCD_DB4 4 #define LCD_DB5 5 #define LCD_DB6 6 #define LCD_DB7 7 #define TERM_0 0 #define TERM_1 1 #define TERM_2 2 #define PI_NORMAL 0 #define PI_FAILURE 1 #define PI_ENBL_N 10 #define PI_DSPLINE_1 0x00 #define PI_DSPLINE_2 0x40 // //Define function // void Init_LCD(void); void LCD_Disp_str(unsigned char *, unsigned char); void LCD_Disp(unsigned char); void LCD_CLR(void); void LCD_set_addr(unsigned char); void LCD_SETH(unsigned char); void LCD_SETL(unsigned char); void BCF(unsigned int); void BSF(unsigned int); void mnop(unsigned int); void Timer3(void); void Wait_1mS(unsigned int); void main(void) { unsigned char msg[17]; // ************************************* // 1.Initialize Segment // ************************************* // (1)Define I/O port PORTA = 0b00000000; PORTB = 0b00000000; PORTC = 0b00000000; TRISA = 0b11111111; TRISB = 0b11111111; TRISC = 0b00001100; // (2)Select A/D Converter ANSEL = 0b00000000; //ALL Degital PORT ANSELH = 0b00000000; //ALL Degital PORT // (3)Initialized LCD Init_LCD(); // (4)Clear LCD LCD_CLR(); // ************************************* // 2.Program Main // ************************************* while(1){ // (1)Output message 1 strcpypgm2ram(msg," Start 18F14K50 "); LCD_Disp_str(msg,PI_DSPLINE_1); //LCDの1行目に表示する strcpypgm2ram(msg," OK>> "); LCD_Disp_str(msg,PI_DSPLINE_2); //LCDの2行目に表示する // (2)Wait Wait_1mS(2000); // (3)Clear display LCD_CLR(); // (4)Output message 1 strcpypgm2ram(msg," DEMO PROGRAM "); LCD_Disp_str(msg,PI_DSPLINE_1); //LCDの1行目に表示する strcpypgm2ram(msg," NO1 OK>> "); LCD_Disp_str(msg,PI_DSPLINE_2); //LCDの2行目に表示する // (5)Wait Wait_1mS(2000); // (6)Clear display LCD_CLR(); } } //****************************************************************************************** // Function Name : Init_LCD // Title : インストラクションによるLCDの初期化 // Input : // Output : // Description : 4ビットインターフェース用 //****************************************************************************************** void Init_LCD(void) { // ****************************** // 1.ファンクションセット // ****************************** // (1)15mS以上待つ Wait_1mS(100); // (2)ファンクションセット(1); LCD_SETH(0b00110000); //PORTB = 0b0011**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal // (3)4.1mS以上待つ Wait_1mS(10); // (4)ファンクションセット(2) LCD_SETH(0b00110000); //PORTB = 0b0011**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal // (5)100μS以上待つ Wait_1mS(10); // (6)ファンクションセット(3) LCD_SETH(0b00110000); //PORTB = 0b0011**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // ****************************** // 2.表示設定 // ****************************** // (1)ファンクションセット LCD_SETH(0b00100000); //PORTB = 0b0010**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 上位4ビット LCD_SETH(0b00100000); //PORTB = 0b0010**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETH(0b10000000); //PORTB = 0bnf****** n=1:2 Line, f=0:5*8 dots  BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // // (2)表示OFF // 上位4ビット LCD_SETH(0b00000000); //PORTB = 0b0000**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETH(0b10000000); //PORTB = 0b1000**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // // (3)表示ON // 上位4ビット LCD_SETH(0b00000000); //PORTB = 0b0000**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETH(0b00010000); //PORTB = 0b0001**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // // (4)エントリーモードセット // 上位4ビット LCD_SETH(0b00000000); //PORTB = 0b0000**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETH(0b01100000); //PORTB = 0b01I/DS**** I/D=1:Incriment, S=0 BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // // (5)表示ON // // 上位4ビット LCD_SETH(0b00000000); //PORTB = 0b00000000; BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETH(0b11000000); //PORTB = 0b11000000; BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // // (6)DD RAM アドレスセット(先頭表示位置を設定する) // 上位4ビット LCD_SETH(0b10000000); //PORTB = 0b1000**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETH(0b00000000); //PORTB = 0b0000**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); } //****************************************************************************************** // Function name : LCD_Disp_str // Title : LCDに文字列を表示する // Input : // Output : //****************************************************************************************** void LCD_Disp_str(unsigned char *in_msg, unsigned char line_no) { // 1.LCDの表示開始位置を設定する LCD_set_addr(line_no); // 2.データを表示する while(1){ if(*in_msg==0x00)break; LCD_Disp(*in_msg); in_msg++; } } //****************************************************************************************** // Function Name : LCD_Disp // Title : LCDに文字を表示する // Input : // Output : // Description : //****************************************************************************************** void LCD_Disp(unsigned char in_char) { // 上位4ビット LCD_SETH(in_char); BSF(LCD_RS); //RS mnop(PI_ENBL_N); //48MHz mode Only BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal mnop(PI_ENBL_N); //48MHz mode Only BCF(LCD_RS); //RS mnop(PI_ENBL_N); // 下位4ビット LCD_SETL(in_char); BSF(LCD_RS); //RS mnop(PI_ENBL_N); //48MHz mode Only BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal mnop(PI_ENBL_N); //48MHz mode Only BCF(LCD_RS); //RS mnop(PI_ENBL_N); Wait_1mS(10); } //****************************************************************************************** // Function Name : LCD_CLR // Title : LCDの画面クリア // Input : // Output : // Description : //****************************************************************************************** void LCD_CLR(void) { // 上位4ビット LCD_SETH(0b00000000); //PORTB = 0b0000**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETH(0b00010000); //PORTB = 0b0001**** BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); } //****************************************************************************************** // Function name : LCD_set_addr // Title : LCDのDD−RAM−ADRESSを設定する // Input : // Output : //****************************************************************************************** void LCD_set_addr(unsigned char addr) { unsigned char tmp_char; // 上位4ビット tmp_char = 0b10000000 | addr ; LCD_SETH(tmp_char); //PORTB = 0b1***---- BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETL(addr); //PORTB = 0b----**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); } //****************************************************************************************** // Function Name : LCD_SETH // Title : LCDのDB(7-4)にデータ設定 // Input : // Output : //****************************************************************************************** void LCD_SETH(unsigned char tmp_data) { PORTC = PORTC & 0b00001111; PORTC = PORTC | (tmp_data & 0b11110000); } //****************************************************************************************** // Function Name : LCD_SETL // Title : LCDのDB(3-0)にデータ設定 // Input : // Output : //****************************************************************************************** void LCD_SETL(unsigned char tmp_data) { PORTC = PORTC & 0b00001111; PORTC = PORTC | ((tmp_data & 0b00001111) << 4); } //****************************************************************************************** // Function Name : BCF // Title : // Input : // Output : //****************************************************************************************** void BCF(unsigned int pos) { PORTC = PORTC & ~(1 << pos); } //****************************************************************************************** // Function Name : BSF // Title : // Input : // Output : //****************************************************************************************** void BSF(unsigned int pos) { PORTC = PORTC | (1 << pos); } //****************************************************************************************** // Function Name : mnop // Title : wait function // Input : // Output : //****************************************************************************************** void mnop(unsigned int no) { unsigned int lp; for(lp=0;lp 0d12,000(Count) ==> 0x2EE0(Count) // TMR1 = 0xFFFF - 0x2EE0 ==> 0xD11F // TMR3H = 0xD1; TMR3L = 0x1F; // ******************************************************* // 3.Timer // ******************************************************* PIR2bits.TMR3IF = 0; while(1){ if(PIR2bits.TMR3IF != 0 )break; } }