In this example we connect an LCD to our PIC16F873 microcontroller
Schematic
Code
Mplab X using the XC8 compiler requires lcd.h
[codesyntax lang=”c”]
#define _XTAL_FREQ 4000000 #define RS RC0 #define EN RC1 #define D4 RC2 #define D5 RC3 #define D6 RC4 #define D7 RC5 // BEGIN CONFIG #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator) #pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled) #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming) #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off) #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control) #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off) //END CONFIG #include <stdio.h> #include <stdlib.h> #include <xc.h> #include "lcd.h"; /* * */ int main() { unsigned int a; TRISC = 0x00; Lcd_Init(); while(1) { Lcd_Clear(); Lcd_Set_Cursor(1,1); Lcd_Write_String("Line 1"); Lcd_Set_Cursor(2,1); Lcd_Write_String("Line 2"); } return 0; }
[/codesyntax]
Links