// INCLUDES #include #include #include #include // CONFIG #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config MCLRE = OFF // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD) #pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled) #pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming) #pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off) #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off) // DEFINITIONS #define _XTAL_FREQ 16000000 // refenrence freq for the compiler #define Rs PORTAbits.RA0 // Rs is connected to RA0 #define En PORTAbits.RA1 // En is connected to RA1 #define lcd_port PORTB // lcd port b // FUNCTION PROTOTYPE int lcd_strobe(void); void lcd_write(unsigned char dat); void lcd_cmd(unsigned char cmd); void lcd_data(unsigned char dat); void lcd_init(void); void lcd_writestr(const unsigned char *c); void lcd_setcursor(unsigned char loc); void lcd_line1(void); void lcd_line2(void); // function to pulse the En pin high int lcd_strobe(void) { En = 1; __delay_us(1); En = 0; } // function to write to the lcd port void lcd_write(unsigned char dat) { lcd_port &= 0x0f; // get current port and clear upper bits lcd_port |= (dat & 0xf0); // combine upper and lower, leave lower lcd_strobe(); lcd_port &= 0x0f; lcd_port |= ((dat << 4) & (0xf0)); // combine upper and lower lcd_strobe(); __delay_ms(2); // little delay for lcd to process } // function to set lcd in command mode void lcd_cmd(unsigned char cmd) { Rs = 0; // rs low for cmd mode lcd_write(cmd); } // functionto set lcd in data mode void lcd_data (unsigned char dat) { Rs = 1; // rs high for data mode lcd_write(dat); } // function to initialize the lcd void lcd_init(void) { lcd_cmd(0x28); __delay_ms(50); lcd_cmd(0x28); // 4-bit, 2-line, 5x7 font lcd_cmd(0x0e); // display on, cursor on, blink off lcd_cmd(0x06); // auto increment, no shift lcd_cmd(0x80); // address ddram with 0 offset 80h } // function to write a strng to the lcd void lcd_writestr(const unsigned char *c) { while (*c != '\0') { lcd_data(*c); c++; } } // function to place the cursor on the lcd void lcd_setcursor(unsigned char loc) { lcd_cmd(loc | 0x80); } // function to move cursor to 1st place on line 1 void lcd_line1(void) { lcd_setcursor(0); } // function to move cursor to 1st place on line 2 void lcd_line2(void) { lcd_setcursor(40); } void main(void) { CMCON = 0x07; TRISA = 0b00000000; TRISB = 0b00000110; PORTA = 0b00000000; PORTB = 0b00000000; lcd_strobe(); lcd_init(); lcd_writestr(" HackerBoxes "); lcd_line2(); lcd_writestr(" HackLife "); while (1); }