00001
00002
00003
00004
00005
00006 #include "piccolo_all.h"
00007 #include <stdio.h>
00008 #include <delays.h>
00009
00023
00024 #if defined(__18F14K50)
00025 #define BUSY_FLAG PORTCbits.RC7; // foglaltság figyelése
00026 #elif defined(__18F4550)
00027 #define BUSY_FLAG PORTDbits.RD7;
00028 #endif
00029
00030 #define RSHIGH() LATBbits.LATB5=1; // RS vezérlése
00031 #define RSLOW() LATBbits.LATB5=0;
00032 #define RS_init() TRISBbits.TRISB5=0;
00033 #define RWHIGH() LATBbits.LATB6=1; // R/W vezérlése
00034 #define RWLOW() LATBbits.LATB6=0;
00035 #define RW_init() TRISBbits.TRISB6=0;
00036 #define EHIGH() LATBbits.LATB7=1; // E vezérlése
00037 #define ELOW() LATBbits.LATB7=0;
00038 #define E_init() TRISBbits.TRISB7=0;
00039
00040 #define DATA_DIR_RD() LEDtris = 0xFF; // adatvonalak beállítása olvasásra
00041 #define DATA_DIR_WR() LEDtris = 0x00; // adatvonalak beállítása írásra
00042 #define OUTPUT_DATA(x) {LEDport = x;} //adat kiküldésa
00043
00048 void delay_ms(unsigned int d) {
00049 unsigned int i;
00050 for(i=0; i<d; i++) {
00051 Delay1KTCYx(12);
00052 }
00053 }
00054
00055
00056 void epulse(void){
00057 Delay10TCYx(2);
00058 EHIGH(); Delay10TCYx(2);
00059 ELOW(); Delay10TCYx(1);
00060 }
00061
00062
00063 void lcd8_write( unsigned char cmd,
00064 unsigned char data_flag,
00065 unsigned char chk_busy)
00066 {
00067 char bflag;
00068 if (chk_busy) {
00069 RSLOW();
00070 DATA_DIR_RD();
00071 RWHIGH();
00072 do {
00073 EHIGH();
00074 Delay10TCYx(2);
00075 bflag = BUSY_FLAG;
00076 ELOW(); Delay10TCYx(2);
00077 } while(bflag);
00078 } else {
00079 Delay1KTCYx(12);
00080 }
00081 DATA_DIR_WR();
00082 if (data_flag) { RSHIGH()}
00083 else RSLOW();
00084 RWLOW();
00085 OUTPUT_DATA(cmd);
00086 epulse();
00087 }
00088
00089
00090 void lcd8_init(void) {
00091 ELOW();
00092 RSLOW();
00093 RWLOW();
00094 RW_init();
00095 RS_init();
00096 E_init();
00097 DATA_DIR_WR();
00098
00099 Delay10KTCYx(60);
00100 lcd8_write(0x30,0,0);
00101 Delay10KTCYx(6);
00102 lcd8_write(0x30,0,0);
00103 Delay1KTCYx(2);
00104 lcd8_write(0x30,0,0);
00105
00106 lcd8_write(0x38,0,0);
00107
00108 lcd8_write(0x08,0,1);
00109 lcd8_write(0x01,0,1);
00110 lcd8_write(0x0C,0,1);
00111 stdout = _H_USER;
00112
00113 }
00114
00115
00116
00117 void _user_putc (auto char c) {
00118 lcd8_write(c,1,1);
00119 }
00120
00121 const rom char line1[] = "<== 2x16 LCD ==>";
00122 const rom char line2[] = "Now in 8bit mode";
00123
00124 void main(void){
00125 unsigned char i,k;
00126 DISABLE_ALL_ANALOG();
00127 lcd8_init();
00128 while(1) {
00129 lcd8_write(0x01,0,1);
00130 delay_ms(500);
00131 printf(line1);
00132 delay_ms(500);
00133 for(i=0; i<16; i++) {
00134 k = (i*7) & 0x0F;
00135 lcd8_write(0xC0+k,0,1);
00136 putc(line2[k],stdout);
00137 delay_ms(120);
00138 }
00139 delay_ms(2000);
00140 }
00141 }