Home Programmering PIC 16F628 LCD 2 x 20
PIC 16F628 LCD 2 x 20 PDF Skriv ut E-post
Skrevet av Jens Christoffersen   
mandag 07. november 2011 19:15

PIC16F628 og LCD display.

 

Kladd og worklog. Dette er pretty much copy and paste. Bedre kode finnes og vil komme.

 

/*
* LCD interface example
* Uses routines from delay.c
* This code will interface to a standard LCD controller
* like the Hitachi HD44780. It uses it in 4 bit mode, with
* the hardware connected as follows (the standard 14 pin
* LCD connector is used):
*
* PORTB bits 4-7 are connected to the LCD data bits 4-7 (high nibble)
* PORTA bit 3 is connected to the LCD RS input (register select)
* PORTB bit 1 is connected to the LCD EN bit (enable)
*
* To use these routines, set up the port I/O (TRISA, TRISB) then
* call lcd_init(), then other routines as required.
*
*/
#ifndef _XTAL_FREQ
// Unless specified elsewhere, 4MHz system frequency is assumed
#define _XTAL_FREQ 4000000
#endif




#include "" // Remove the "
#include "lcd.h"
#define LCD_RS RA2 //endret
#define LCD_RW RA3 //endret
#define LCD_EN RB3 //endret
#define LCD_DATA PORTB //endret

#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
/* write a byte to the LCD in 4 bit mode */

// Configuration
__CONFIG (WDTEN & PWRTEN & MCLRDIS & BOREN & LVPDIS & UNPROTECT & INTIO);


void
lcd_write(unsigned char c)
{
__delay_us(40);
LCD_DATA = ( c & 0xF0 );//skriv mest signifikant nibble (4 bit)
LCD_STROBE();
LCD_DATA = ((c >> 4) | (c << 4)& 0xF0);//Skriv minst signifikant nibble
//LCD_DATA = ( ( c >> 4 ) & 0x0F );
LCD_STROBE();
}
/*
* Clear and home the LCD
*/
void
lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x10);
__delay_ms(2);
}
/* write a string of chars to the LCD */
void
lcd_puts(const char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}
/* write one character to the LCD */
void
lcd_putch(char c)
{
LCD_RS = 1; // write characters
lcd_write( c );
}

/*
* Go to the specified position
*/
void
lcd_goto(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0x80+pos);
}

/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
char init_value;
// ADCON1 = 0x06; // Disable analog pins on PORTA
init_value = 0x30;//endret fra 0x3 til 0x30
TRISA=0;
TRISB=0;
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;

__delay_ms(15); // wait 15mSec after power applied,
LCD_DATA = init_value;
LCD_STROBE();
__delay_ms(5);
LCD_STROBE();
__delay_us(200);
LCD_STROBE();
__delay_us(200);
LCD_DATA = 0x20;//endret fra 2 til 0x20
// Four bit mode
LCD_STROBE();
lcd_write(0x28); // Set interface length
lcd_write(0xF); // Display On, Cursor On, Cursor Blink
lcd_clear(); // Clear screen
lcd_write(0x6); // Set entry Mode
} 


void main()
{
 lcd_init();
 lcd_goto(0);    // select first line
 lcd_puts("WWW.NERDEGUTTA.ORG");
 lcd_goto(0x40); // Selects second line
 lcd_puts("Open source matters");
for(;;);

}

 

IDE er MPLAB 8.63, med Hi Tech C v.9.80 kompilator.

Et mer ryddig oppsett vil komme med tiden. Der er noe av koden fjernet og laget i en egen *.c-fil.

Sist oppdatert tirsdag 08. november 2011 05:27
 
Kopirett © 2012 nerdegutta.org. Alle rettigheter reservert.
Joomla! er fri programvare utgitt under GNU/GPL License.