Hallo,
Al enige dagen (en nachten) ben ik aan het prutsen geweest om een 18F4550 microcontroller (met usb bootloader) te programmeren om een standaard LCD paneeltje aan te kunnen sturen. Dit doe ik in MPLAB met het taaltje C18 van Microchip. Tot nog toe is dit niet gelukt, na een hele dag debuggen heb ik het 'probleem' kunnen herleiden op het volgende stukje C code:
busyxlcd.c (standaard bij MCC18)
..
#else // Lower nibble interface
if(DATA_PORT&0x08)
#endif
{
E_PIN = 0; // Reset clock line
DelayFor18TCY();
E_PIN = 1; // Clock out other nibble
DelayFor18TCY();
E_PIN = 0;
#ifdef RWENABLED
RW_PIN = 0; // Reset control line
#endif
PORTDbits.RD3 = 1; //mijn check met extra LEDje waar hij vast loopt, HIER DUS
return 1; // Return TRUE
}
else // Busy bit is low
{
..Er moet een LEDje (RD2) gaan knipperen, en er wordt op het moment nog niets naar het LCD geschreven, wel wordt hij geinitialiseerd. Het LEDje gaat dus niet knipperen.. want hij is vastgelopen bij het vorige stuk code.
mijn main.c:
#include <p18f4550.h> //Laden van de benodigde bibliotheken
#include <delays.h> //Bib. met standard delay-functies
#include <stdio.h>
#include <timers.h>
#include "lcd.h"
//Code die altijd ingevoegd moet worden i.v.m. bootloader
extern void _startup (void);
#pragma code _RESET_INTERRUPT_VECTOR = 0x000800
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code
#pragma code _HIGH_INTERRUPT_VECTOR = 0x000808
void _high_ISR (void)
{
;
}
#pragma code
#pragma code _LOW_INTERRUPT_VECTOR = 0x000818
void _low_ISR (void)
{
;
}
#pragma code
// ****** Dummy interrupt jumps ******
// You have to include this code if you want to use MPLAB SIM to debug your program (Debbugger --> select tool --> MPLAB SIM).
// The simulator only uses the default vector addresses and not the addresses specified above.
// Therefore interrupts will not work in simulation. Here you route the default adresses to the new adresses to solve this problem.
// This code will not be programmed on your microcontroller because the location of this code is protected by the linker file.
#pragma code _DUMMY_HIGH_INTERRUPT_VECTOR = 0x000008
void dummy_high_isr (void)
{
_asm goto 0x000808 _endasm;
}
#pragma code
#pragma code _DUMMY_LOW_INTERRUPT_VECTOR = 0x000018
void dummy_low_isr (void)
{
_asm goto 0x000818 _endasm;
}
#pragma code
// *** END dummy interrupt jumps ***
void main(void) //Start programma
{
TRISD=0x00; //Maak alle pinnen van poort D output
ADCON0 = 0x00; // Disable the AD converter
ADCON1 = 0x0F; // Set all ports to digital
// Initialise LCD
LcdOpen(); // See lcd.c
while(1) //Draai continu het volgende stuk code
{
PORTDbits.RD2 = 1; //Maak pin A0 hoog
Delay10KTCYx(200); //Wacht 200x10000 clockcycles
PORTDbits.RD2 = 0; //Maak pin A0 laag
Delay10KTCYx(200); //Wacht 200x10000 clockcycles
// LcdClear(); // Clear display
// LcdSetLine1(); // Put cursor on start of line 1
// printf("testje"); // Display text
}
}Op het moment ben ik er net achter gekomen dat hij dus vast zit in die while(BusyXLCD)); waarin wordt gekeken of van de poort DATA_PORT (in dit geval PORTB) een hexadecimale waarde heeft van 0x08 (binair: 00001000) dus of PORTBbits.RB3 hoog is. Zolang dat zo is blijft hij in die loop.
Heeft iemand hier ervaring mee of enig idee hoe dit opgelost zou kunnen worden?
dit is een deel van het schema wat ik gebruikt heb:
http://eegeek.net/images/stories/PIC/18f4550_usb_proto_basic.png
(van http://eegeek.net/content/view/13/32/ , daar heb ik tevens de bootloader weg). Ook zijn de nodige aanpassingen voor het LCD in xlcd.h gemaakt.
LCD connections:
/*-------------------------------------------------------
OpenLCD() redirects stdout (standard out) to the LCD.
Therefore, printf() can be used to write tot the LCD
Connections:
LCD PIC
----------------
1 GND
2 VDD
3 GND
4 B5 (RS)
5 GND (RW) (was eerst B6, maar de GND werkte ook altijd met de goede oude 16f628A)
6 B7 (EN)
7 Not connected
8 Not connected
9 Not connected
10 Not connected
11 B0 (D4)
12 B1 (D5)
13 B2 (D6)
14 B3 (D7)
(15) LCD backlight, not necessary
(16) LCD backlight, not necessary
-------------------------------------------------------*/ps. hij wordt netjes herkent en geinstalleerd door vista (AMD64!!) en ik kan mijn eigen code met het bootloader demo programma van microchip er keurig in zetten, en zolang ik het LCD maar niet initialiseer/gebruik knippert het ledje RD2 keurig. een eigen bootloader maken is nog niet gelukt, althans windows geeft dan aan dat het apparaat niet kan worden gestart(code 10) in combinatie met een slow-motion hardware gedetecteerd geluidje.
Het lukte mij ook niet met de lib van c18 maar ik heb zelf een deel van het protocol opnieuw geschreven. Dan weet je zeker dat het werkt
Hoi,
@Toca: Weet je ook nog welk deel van het protocol opnieuw geschreven moest worden? Dat zou me zeker kunnen helpen dan!
Ik ben net terug van de winkel, nieuwe soldeerpunt (de oude had een gat in zichzelf gebrand?? op verjaardag gekregen) even de datakabeltjes opnieuw solderen aan de 18f4550 (heb de 16f628a er nog aangehangen met picbasic en het lcd deed het toen uiteraard wel) en dan even met een compact protocol te vinden op onderstaande link proberen.
De resultaten post ik straks (vanacht?) wel.
http://www.piclist.com/techref/microchip/language/c/io/lcd/hitachilcd-…
Met die andere lcd code kwam ik er helemaal niet uit, op het moment heb ik de C18 library maar weer gebruikt, en heb de busyxlcd aangepast zodat deze altijd de waarde 0 retouneerd, maar nu als ik printf("testje"); probeer te compilen geeft hij aan:
main.c:79:Warning [2066] type qualifier mismatch in assignmenten er komt een knipperende cursor op mijn lcd'tje.
Op naar de volgende stap..
Eindelijk..
Na dagen speuren kwam ik dit topic tegen:
http://forum.microchip.com/tm.aspx?m=333519&mpage=1&key=&#…
ik heb daar de laatste codes van de eerste pagina van gebruikt, de VDD pins er uit gehaald, en mijn schema aangepast zodat hij aan de D port zou komen te zitten (anders meer kans op fouten als ik de hele code zou moeten aanpassen..)
en..
hij doet het
ik post hier onder de C18 code in de hoop dat deze in de toekomst nog vele anderen uitkomst mag bieden!
lcd.c:
/* $Id: openxlcd.c,v 1.1 2003/12/09 22:52:09 GrosbaJ Exp $ */
#include <p18cxxx.h>
#include "lcd.h"
#include <delays.h>
/*****/
/********************************************************************
* For the XLCD, from the xlcd.h file, we must provide:
* - DelayFor18TCY() provides a 18 Tcy delay
* - DelayPORXLCD() provides at least 15ms delay
* - DelayXLCD() provides at least 5ms delay
********************************************************************/
// My clock running at 4mhz - then devided by 4? to be ~ 1us/cycle ?
/*****/
void DelayFor18TCY(void)
{
Delay10TCYx(0x2); //delays 20 cycles
return;
}
/*****/
void DelayPORXLCD(void) // minimum 15ms
{
Delay100TCYx(0xA0); // 100TCY * 160
return;
}
/*****/
void DelayXLCD(void) // minimum 5ms
{
Delay100TCYx(0x36); // 100TCY * 54
return;
}
/*****/
/********************************************************************
* Function Name: OpenXLCD *
* Return Value: void *
* Parameters: lcdtype: sets the type of LCD (lines) *
* Description: This routine configures the LCD. Based on *
* the Hitachi HD44780 LCD controller. The *
* routine will configure the I/O pins of the *
* microcontroller, setup the LCD for 4- or *
* 8-bit mode and clear the display. The user *
* must provide three delay routines: *
* DelayFor18TCY() provides a 18 Tcy delay *
* DelayPORXLCD() provides at least 15ms delay *
* DelayXLCD() provides at least 5ms delay *
* *
* NOTE: I think there may be some problems with the BIT8 mode *
********************************************************************/
void OpenXLCD(unsigned char lcdtype)
{
// For PICDEM 2 Plus, setup the A/D bits so they
// don't interfere with the XLCD control bits.
ADCON1 = 0b00001110;
DelayPORXLCD(); // delay 15ms
//TRIS_VDD = 0; // LCD VCC pin output
//VDD_PIN = 1;
/* intialize VCC of LCD */
DDRDbits.RD7 = 0; // LCD VCC pin output
PORTDbits.RD7 = 1;
/*********Violin*********/
/*****/
// The data bits must be either a 8-bit port or the upper or
// lower 4-bits of a port. These pins are made into inputs
#ifdef BIT8 // 8-bit mode, use whole port
DATA_PORT = 0;
TRIS_DATA_PORT = 0xff;
#else // 4-bit mode
#ifdef UPPER // Upper 4-bits of the port
DATA_PORT &= 0x0f;
TRIS_DATA_PORT |= 0xf0;
#else // Lower 4-bits of the port
DATA_PORT &= 0xf0;
TRIS_DATA_PORT |= 0x0f;
#endif
#endif
PORTDbits.RD7 = 1;
/*****/
TRIS_RW = 0; // All control signals made outputs
TRIS_RS = 0;
TRIS_E = 0;
RW_PIN = 0; // R/W pin made low
RS_PIN = 0; // Register select pin made low
E_PIN = 0; // Clock pin made low
/*****/
// Delay for 15ms to allow for LCD Power on reset
DelayPORXLCD();
/*****/
// Setup interface to LCD - First Init Nibble
#ifdef BIT8 // 8-bit mode interface
TRIS_DATA_PORT = 0; // Data port output
DATA_PORT = 0b00110000; // Function set cmd(8-bit interface)
#else // 4-bit mode interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;
DATA_PORT &= 0x0f;
DATA_PORT |= 0b00110000; // Function set cmd(4-bit interface)
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0;
DATA_PORT &= 0xf0;
DATA_PORT |= 0b00000011; // Function set cmd(4-bit interface)
#endif
#endif
E_PIN = 1; // Clock the cmd in
DelayFor18TCY();
E_PIN = 0;
/*****/
// Delay for at least 4.1ms
DelayXLCD();
/*****/
// Second Init Nibble
#ifdef BIT8 // 8-bit mode interface
#else // 4-bit mode interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;
DATA_PORT &= 0x0f;
DATA_PORT |= 0b00110000; // Function set cmd(4-bit interface)
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0;
DATA_PORT &= 0xf0;
DATA_PORT |= 0b00000011; // Function set cmd(4-bit interface)
#endif
#endif
E_PIN = 1; // Clock the cmd in
DelayFor18TCY();
E_PIN = 0;
/*****/
// Delay for at least 100us
DelayXLCD();
/*****/
// Third Init Nibble
#ifdef BIT8 // 8-bit interface
DATA_PORT = 0b00110000; // Function set cmd(8-bit interface)
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Function set cmd(4-bit interface)
DATA_PORT |= 0b00110000;
#else // Lower nibble interface
DATA_PORT &= 0xf0; // Function set cmd(4-bit interface)
DATA_PORT |= 0b00000011;
#endif
#endif
E_PIN = 1; // Clock the cmd in
DelayFor18TCY();
E_PIN = 0;
// Delay for at least 4.1ms
DelayXLCD();
/*****/
// Fourth Init Nibble (Word for 8 bit)
#ifdef BIT8 // 8-bit interface
DATA_PORT = 0b00111100; // Function set cmd(8-bit interface)
// Check lower Nibble for correct bits
// This Init sequence has some issues!
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Function set cmd(4-bit interface)
DATA_PORT |= 0b00100000;
#else // Lower nibble interface
DATA_PORT &= 0xf0; // Function set cmd(4-bit interface)
DATA_PORT |= 0b00000010;
#endif
#endif
E_PIN = 1; // Clock cmd in
DelayFor18TCY();
E_PIN = 0;
/*****/
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0xff; // Make data port input
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0; // Make data nibble input
#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f; // Make data nibble input
#endif
#endif
/*****/
// From now on, send bytes - (2) nibbles for 4bit
// Set data interface width, # lines, font
while(BusyXLCD()); // Wait if LCD busy
// WriteCmdXLCD(lcdtype); // Function set cmd
WriteCmdXLCD(0x28); // Function set cmd
/*****/
// Turn the display on then off
while(BusyXLCD()); // Wait if LCD busy
// WriteCmdXLCD(DOFF&CURSOR_OFF&BLINK_OFF); // Display OFF/Blink OFF
WriteCmdXLCD(0x0e);
/********change from D *****blink off***/
/***************************************/
/*****/
while(BusyXLCD()); // Wait if LCD busy
// WriteCmdXLCD(DON&CURSOR_ON&BLINK_ON); // Display ON/Blink ON
/*****/
// Clear display
while(BusyXLCD()); // Wait if LCD busy
WriteCmdXLCD(0x01); // Clear display
/*****/
// Set entry mode inc, no shift
while(BusyXLCD()); // Wait if LCD busy
// WriteCmdXLCD(SHIFT_CUR_LEFT); // Entry Mode
WriteCmdXLCD(0x06);
/*****/
// Set DD Ram address to 0
while(BusyXLCD()); // Wait if LCD busy
// SetDDRamAddr(0); // Set Display data ram address to 0
WriteCmdXLCD(0x80); // Set Display data ram address to 0
/*****/
// while(1){
// led();
//}
/*****/
return;
}
/*****/
/********************************************************************
* Function Name: BusyXLCD *
* Return Value: char: busy status of LCD controller *
* Parameters: void *
* Description: This routine reads the busy status of the *
* Hitachi HD44780 LCD controller. *
********************************************************************/
unsigned char BusyXLCD(void)
{
RW_PIN = 1; // Set the control bits for read
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock in the command
DelayFor18TCY();
#ifdef BIT8 // 8-bit interface
if(DATA_PORT&0x80) // Read bit 7 (busy bit)
{ // If high
E_PIN = 0; // Reset clock line
RW_PIN = 0; // Reset control line
return 1; // Return TRUE
}
else // Bit 7 low
{
E_PIN = 0; // Reset clock line
RW_PIN = 0; // Reset control line
return 0; // Return FALSE
}
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
if(DATA_PORT&0x80)
#else // Lower nibble interface
if(DATA_PORT&0x08)
#endif
{
E_PIN = 0; // Reset clock line
DelayFor18TCY();
E_PIN = 1; // Clock out other nibble
DelayFor18TCY();
E_PIN = 0;
RW_PIN = 0; // Reset control line
return 1; // Return TRUE
}
else // Busy bit is low
{
E_PIN = 0; // Reset clock line
DelayFor18TCY();
E_PIN = 1; // Clock out other nibble
DelayFor18TCY();
E_PIN = 0;
RW_PIN = 0; // Reset control line
return 0; // Return FALSE
}
#endif
}
/*****/
/********************************************************************
* Function Name: putrsXLCD
* Return Value: void
* Parameters: buffer: pointer to string
* Description: This routine writes a string of bytes to the
* Hitachi HD44780 LCD controller. The user
* must check to see if the LCD controller is
* busy before calling this routine. The data
* is written to the character generator RAM or
* the display data RAM depending on what the
* previous SetxxRamAddr routine was called.
********************************************************************/
/*
void putrsXLCD(const rom char *buffer)
{
while(*buffer) // Write data to LCD up to null
{
while(BusyXLCD()); // Wait while LCD is busy
WriteDataXLCD(*buffer); // Write character to LCD
buffer++; // Increment buffer
}
return;
}
*/
/********************************************************************
* Function Name: putsXLCD
* Return Value: void
* Parameters: buffer: pointer to string
* Description: This routine writes a string of bytes to the
* Hitachi HD44780 LCD controller. The user
* must check to see if the LCD controller is
* busy before calling this routine. The data
* is written to the character generator RAM or
* the display data RAM depending on what the
* previous SetxxRamAddr routine was called.
********************************************************************/
void putsXLCD(char *buffer)
{
while(*buffer) // Write data to LCD up to null
{
while(BusyXLCD()); // Wait while LCD is busy
WriteDataXLCD(*buffer); // Write character to LCD
buffer++; // Increment buffer
}
return;
}
/*****/
/*********************************************************************
* Function Name: ReadAddrXLCD *
* Return Value: char: address from LCD controller *
* Parameters: void *
* Description: This routine reads an address byte from the *
* Hitachi HD44780 LCD controller. The user *
* must check to see if the LCD controller is *
* busy before calling this routine. The address*
* is read from the character generator RAM or *
* the display data RAM depending on what the *
* previous SetxxRamAddr routine was called. *
*********************************************************************/
unsigned char ReadAddrXLCD(void)
{
char data; // Holds the data retrieved from the LCD
/*****/
#ifdef BIT8 // 8-bit interface
RW_PIN = 1; // Set control bits for the read
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock data out of the LCD controller
DelayFor18TCY();
data = DATA_PORT; // Save the data in the register
E_PIN = 0;
RW_PIN = 0; // Reset the control bits
#else // 4-bit interface
RW_PIN = 1; // Set control bits for the read
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock data out of the LCD controller
DelayFor18TCY();
#ifdef UPPER // Upper nibble interface
data = DATA_PORT&0xf0; // Read the nibble into the upper nibble of data
#else // Lower nibble interface
data = (DATA_PORT<<4)&0xf0; // Read the nibble into the upper nibble of data
#endif
E_PIN = 0; // Reset the clock
DelayFor18TCY();
E_PIN = 1; // Clock out the lower nibble
DelayFor18TCY();
#ifdef UPPER // Upper nibble interface
data |= (DATA_PORT>>4)&0x0f; // Read the nibble into the lower nibble of data
#else // Lower nibble interface
data |= DATA_PORT&0x0f; // Read the nibble into the lower nibble of data
#endif
E_PIN = 0;
RW_PIN = 0; // Reset the control lines
#endif
return (data&0x7f); // Return the address, Mask off the busy bit
}
/*****/
/********************************************************************
* Function Name: ReadDataXLCD *
* Return Value: char: data byte from LCD controller *
* Parameters: void *
* Description: This routine reads a data byte from the *
* Hitachi HD44780 LCD controller. The user *
* must check to see if the LCD controller is *
* busy before calling this routine. The data *
* is read from the character generator RAM or *
* the display data RAM depending on what the *
* previous SetxxRamAddr routine was called. *
********************************************************************/
char ReadDataXLCD(void)
{
char data;
/*****/
#ifdef BIT8 // 8-bit interface
RS_PIN = 1; // Set the control bits
RW_PIN = 1;
DelayFor18TCY();
E_PIN = 1; // Clock the data out of the LCD
DelayFor18TCY();
data = DATA_PORT; // Read the data
E_PIN = 0;
RS_PIN = 0; // Reset the control bits
RW_PIN = 0;
#else // 4-bit interface
RW_PIN = 1;
RS_PIN = 1;
DelayFor18TCY();
E_PIN = 1; // Clock the data out of the LCD
DelayFor18TCY();
#ifdef UPPER // Upper nibble interface
data = DATA_PORT&0xf0; // Read the upper nibble of data
#else // Lower nibble interface
data = (DATA_PORT<<4)&0xf0; // read the upper nibble of data
#endif
E_PIN = 0; // Reset the clock line
DelayFor18TCY();
E_PIN = 1; // Clock the next nibble out of the LCD
DelayFor18TCY();
#ifdef UPPER // Upper nibble interface
data |= (DATA_PORT>>4)&0x0f; // Read the lower nibble of data
#else // Lower nibble interface
data |= DATA_PORT&0x0f; // Read the lower nibble of data
#endif
E_PIN = 0;
RS_PIN = 0; // Reset the control bits
RW_PIN = 0;
#endif
return(data); // Return the data byte
}
/*****/
/********************************************************************
* Function Name: SetCGRamAddr *
* Return Value: void *
* Parameters: CGaddr: character generator ram address *
* Description: This routine sets the character generator *
* address of the Hitachi HD44780 LCD *
* controller. The user must check to see if *
* the LCD controller is busy before calling *
* this routine. *
********************************************************************/
void SetCGRamAddr(unsigned char CGaddr)
{
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make data port ouput
DATA_PORT = CGaddr | 0b01000000; // Write cmd and address to port
RW_PIN = 0; // Set control signals
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock cmd and address in
DelayFor18TCY();
E_PIN = 0;
DelayFor18TCY();
TRIS_DATA_PORT = 0xff; // Make data port inputs
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f; // Make nibble input
DATA_PORT &= 0x0f; // and write upper nibble
DATA_PORT |= ((CGaddr | 0b01000000) & 0xf0);
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0; // Make nibble input
DATA_PORT &= 0xf0; // and write upper nibble
DATA_PORT |= (((CGaddr |0b01000000)>>4) & 0x0f);
#endif
RW_PIN = 0; // Set control signals
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock cmd and address in
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Write lower nibble
DATA_PORT |= ((CGaddr<<4)&0xf0);
#else // Lower nibble interface
DATA_PORT &= 0xf0; // Write lower nibble
DATA_PORT |= (CGaddr&0x0f);
#endif
DelayFor18TCY();
E_PIN = 1; // Clock cmd and address in
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0; // Make inputs
#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f; // Make inputs
#endif
#endif
return;
}
/*****/
/********************************************************************
* Function Name: SetDDRamAddr *
* Return Value: void *
* Parameters: CGaddr: display data address *
* Description: This routine sets the display data address *
* of the Hitachi HD44780 LCD controller. The *
* user must check to see if the LCD controller*
* is busy before calling this routine. *
********************************************************************/
void SetDDRamAddr(unsigned char DDaddr)
{
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make port output
DATA_PORT = DDaddr | 0b10000000; // Write cmd and address to port
RW_PIN = 0; // Set the control bits
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock the cmd and address in
DelayFor18TCY();
E_PIN = 0;
DelayFor18TCY();
TRIS_DATA_PORT = 0xff; // Make port input
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f; // Make port output
DATA_PORT &= 0x0f; // and write upper nibble
DATA_PORT |= ((DDaddr | 0b10000000) & 0xf0);
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0; // Make port output
DATA_PORT &= 0xf0; // and write upper nibble
DATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f);
#endif
RW_PIN = 0; // Set control bits
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock the cmd and address in
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Write lower nibble
DATA_PORT |= ((DDaddr<<4)&0xf0);
#else // Lower nibble interface
DATA_PORT &= 0xf0; // Write lower nibble
DATA_PORT |= (DDaddr&0x0f);
#endif
DelayFor18TCY();
E_PIN = 1; // Clock the cmd and address in
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0; // Make port input
#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f; // Make port input
#endif
#endif
return;
}
/*****/
/********************************************************************
* Function Name: WriteCmdXLCD *
* Return Value: void *
* Parameters: cmd: command to send to LCD *
* Description: This routine writes a command to the Hitachi*
* HD44780 LCD controller. The user must check *
* to see if the LCD controller is busy before *
* calling this routine. *
********************************************************************/
void WriteCmdXLCD(unsigned char cmd)
{
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Data port output
DATA_PORT = cmd; // Write command to data port
RW_PIN = 0; // Set the control signals
RS_PIN = 0; // for sending a command
DelayFor18TCY();
E_PIN = 1; // Clock the command in
DelayFor18TCY();
E_PIN = 0;
DelayFor18TCY();
TRIS_DATA_PORT = 0xff; // Data port input
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;
DATA_PORT &= 0x0f;
DATA_PORT |= cmd&0xf0;
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0;
DATA_PORT &= 0xf0;
DATA_PORT |= (cmd>>4)&0x0f;
#endif
RW_PIN = 0; // Set control signals for command
RS_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock command in
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f;
DATA_PORT |= (cmd<<4)&0xf0;
#else // Lower nibble interface
DATA_PORT &= 0xf0;
DATA_PORT |= cmd&0x0f;
#endif
DelayFor18TCY();
E_PIN = 1; // Clock command in
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Make data nibble input
TRIS_DATA_PORT |= 0xf0;
#else
TRIS_DATA_PORT |= 0x0f;
#endif
#endif
return;
}
/*****/
/********************************************************************
* Function Name: WriteDataXLCD *
* Return Value: void *
* Parameters: data: data byte to be written to LCD *
* Description: This routine writes a data byte to the *
* Hitachi HD44780 LCD controller. The user *
* must check to see if the LCD controller is *
* busy before calling this routine. The data *
* is written to the character generator RAM or*
* the display data RAM depending on what the *
* previous SetxxRamAddr routine was called. *
********************************************************************/
void WriteDataXLCD(char data)
{
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make port output
DATA_PORT = data; // Write data to port
PORTDbits.RD7 = 1;
RS_PIN = 1; // Set control bits
RW_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock data into LCD
DelayFor18TCY();
E_PIN = 0;
RS_PIN = 0; // Reset control bits
TRIS_DATA_PORT = 0xff; // Make port input
#else // 4-bit interface
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;
DATA_PORT &= 0x0f;
DATA_PORT |= data&0xf0;
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0;
DATA_PORT &= 0xf0;
DATA_PORT |= ((data>>4)&0x0f);
#endif
RS_PIN = 1; // Set control bits
RW_PIN = 0;
DelayFor18TCY();
E_PIN = 1; // Clock nibble into LCD
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f;
DATA_PORT |= ((data<<4)&0xf0);
#else // Lower nibble interface
DATA_PORT &= 0xf0;
DATA_PORT |= (data&0x0f);
#endif
DelayFor18TCY();
E_PIN = 1; // Clock nibble into LCD
DelayFor18TCY();
E_PIN = 0;
#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0;
#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f;
#endif
#endif
return;
}
/*
#include <p18f4550.h>
#include <delays.h>
#include "xlcd.h"
#include <stdio.h>
/*-------------------------------------------------------
OpenLCD() redirects stdout (standard out) to the LCD.
Therefore, printf() can be used to write tot the LCD
Connections:
LCD PIC
----------------
1 GND
2 VDD
3 GND
4 D4 (RS)
5 D5 (RW) kan ook met GND
6 D6 (EN)
7 Not connected
8 Not connected
9 Not connected
10 Not connected
11 D0 (D4)
12 D1 (D5)
13 D2 (D6)
14 D3 (D7)
(15) LCD backlight, not necessary
(16) LCD backlight, not necessary
-------------------------------------------------------*/
/*
// Set cursor at start of line 1
void LcdSetLine1(void){
while(BusyXLCD());
SetDDRamAddr(0); // set address
}
// Set cursor at start of line 2
void LcdSetLine2(void){
while(BusyXLCD());
SetDDRamAddr(0x40); // set address
}
// Clear LCD display
void LcdClear(void)
{
WriteCmdXLCD(0x01);
}
// Open LCD module
void LcdOpen(void)
{
OpenXLCD( FOUR_BIT & LINES_5X7 );
while(BusyXLCD());
WriteCmdXLCD(BLINK_OFF & CURSOR_OFF );
while(BusyXLCD());
LcdClear();
stdout = _H_USER; // redirect printf to LCD using _user_putc
//stdout = _H_USART; // use this redirect stdout back to usart if desired
}
// Close LCD module
void LcdClose(void)
{
LcdClear();
}
//========================PrintfRedirection===============================
// _user_putc is called by printf if stdout is set to _H_USER.
int _user_putc(char data){
while(BusyXLCD());
putcXLCD(data); // write character to LCD display
}
// =====================Delay functions for LCD===========================
// Delay for 18 clockcycles
void DelayFor18TCY( void )
{
Delay10TCYx(1);
Delay1TCY();
Delay1TCY();
}
// Delay 1 x * millisecond
void Delay1mSLCD(int x)
{
int i;
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (1ms * 48MHz) / 4
// Cycles = 12,000
for (i=0; i<x;i++) Delay1KTCYx(12);
}
// Delay for 15 milliseconds
void DelayPORXLCD (void)
{
Delay1mSLCD(15); // Delay of 15ms
return;
}
// Delay for 5 milliseconds
void DelayXLCD (void)
{
Delay1mSLCD(5); // Delay of 5ms
return;
}
*/lcd.h:
#ifndef __XLCD_H
#define __XLCD_H
/* PIC18 XLCD peripheral routines.
*
* >>>>>>>>>>> MODIFIED BY DWD FOR THE PICDEM 2 PLUS LCD SETUP
*
* Notes:
* - These libraries routines are written to support the
* Hitachi HD44780 LCD controller.
* - The user must define the following items:
* - The LCD interface type (4- or 8-bits)
* - If 4-bit mode
* - whether using the upper or lower nibble
* - The data port
* - The tris register for data port
* - The control signal ports and pins
* - The control signal port tris and pins
* - The user must provide three delay routines:
* - DelayFor18TCY() provides a 18 Tcy delay
* - DelayPORXLCD() provides at least 15ms delay
* - DelayXLCD() provides at least 5ms delay
*/
/* Interface type 8-bit or 4-bit
* For 8-bit operation uncomment the #define BIT8
*/
/* #define BIT8 */
/* When in 4-bit interface define if the data is in the upper
* or lower nibble. For lower nibble, comment the #define UPPER
*/
/* #define UPPER */
/* DATA_PORT defines the port to which the LCD data lines are connected */
/* FOR PICDEM 2 PLUS - USE PORTD */
#define DATA_PORT PORTD
#define TRIS_DATA_PORT TRISD
/* CTRL_PORT defines the port where the control lines are connected.
* These are just samples, change to match your application.
*/
/* FOR PICDEM 2 PLUS - USE PORTD */
#define RW_PIN PORTDbits.RD5 /* PORT for RW */
#define TRIS_RW DDRDbits.RD5 /* TRIS for RW */
#define RS_PIN PORTDbits.RD4 /* PORT for RS */
#define TRIS_RS DDRDbits.RD4 /* TRIS for RS */
#define E_PIN PORTDbits.RD6 /* PORT for E */
#define TRIS_E DDRDbits.RD6 /* TRIS for E */
//#define VDD_PIN PORTDbits.RD7 /* PORT for VDD */
//#define TRIS_VDD DDRDbits.RD7 /* TRIS for VDD */
/* Adjustment of XLCD Violin*/
/* Display ON/OFF Control defines */
#define DON 0b00001111 /* Display on */
#define DOFF 0b00001011 /* Display off */
#define CURSOR_ON 0b00001111 /* Cursor on */
#define CURSOR_OFF 0b00001101 /* Cursor off */
#define BLINK_ON 0b00001111 /* Cursor Blink */
#define BLINK_OFF 0b00001110 /* Cursor No Blink */
/* Cursor or Display Shift defines */
#define SHIFT_CUR_LEFT 0b00010011 /* Cursor shifts to the left */
#define SHIFT_CUR_RIGHT 0b00010111 /* Cursor shifts to the right */
#define SHIFT_DISP_LEFT 0b00011011 /* Display shifts to the left */
#define SHIFT_DISP_RIGHT 0b00011111 /* Display shifts to the right */
/* Function Set defines */
#define FOUR_BIT 0b00101111 /* 4-bit Interface */
#define EIGHT_BIT 0b00111111 /* 8-bit Interface */
#define LINE_5X7 0b00110011 /* 5x7 characters, single line */
#define LINE_5X10 0b00110111 /* 5x10 characters */
#define LINES_5X7 0b00111011 /* 5x7 characters, multiple line */
#define PARAM_SCLASS auto
#define MEM_MODEL far /* Change this to near for small memory model */
/* OpenXLCD
* Configures I/O pins for external LCD
*/
void OpenXLCD(PARAM_SCLASS unsigned char);
/* SetCGRamAddr
* Sets the character generator address
*/
void SetCGRamAddr(PARAM_SCLASS unsigned char);
/* SetDDRamAddr
* Sets the display data address
*/
void SetDDRamAddr(PARAM_SCLASS unsigned char);
/* BusyXLCD
* Returns the busy status of the LCD
*/
unsigned char BusyXLCD(void);
/* ReadAddrXLCD
* Reads the current address
*/
unsigned char ReadAddrXLCD(void);
/* ReadDataXLCD
* Reads a byte of data
*/
char ReadDataXLCD(void);
/* WriteCmdXLCD
* Writes a command to the LCD
*/
void WriteCmdXLCD(PARAM_SCLASS unsigned char);
/* WriteDataXLCD
* Writes a data byte to the LCD
*/
void WriteDataXLCD(PARAM_SCLASS char);
/* putcXLCD
* A putc is a write
*/
#define putcXLCD WriteDataXLCD
/* putsXLCD
* Writes a string of characters to the LCD
*/
void putsXLCD(PARAM_SCLASS char *);
/* putrsXLCD
* Writes a string of characters in ROM to the LCD
*/
void putrsXLCD(PARAM_SCLASS const MEM_MODEL rom char *); //BLANK SCREEN
/* User defines these routines according to the oscillator frequency */
extern void DelayFor18TCY(void);
extern void DelayPORXLCD(void);
extern void DelayXLCD(void);
#endif
/*
#ifndef __LCD_H
#define __LCD_H
// Open LCD module
void LcdOpen(void);
// Close LCD module
void LcdClose(void);
// Write to LCD routines
void LcdSetLine1(void);
void LcdSetLine2(void);
// Clear LCD display
void LcdClear(void);
*/main.c:
#include <p18cxxx.h> //Laden van de benodigde bibliotheken
#include <delays.h> //Bib. met standard delay-functies
#include <stdio.h>
#include <timers.h>
#include <string.h>
#include <stdlib.h>
#include <spi.h>
#include "lcd.h"
//Code die altijd ingevoegd moet worden i.v.m. bootloader
extern void _startup (void);
#pragma code _RESET_INTERRUPT_VECTOR = 0x000800
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code
#pragma code _HIGH_INTERRUPT_VECTOR = 0x000808
void _high_ISR (void)
{
;
}
#pragma code
#pragma code _LOW_INTERRUPT_VECTOR = 0x000818
void _low_ISR (void)
{
;
}
#pragma code
// ****** Dummy interrupt jumps ******
// You have to include this code if you want to use MPLAB SIM to debug your program (Debbugger --> select tool --> MPLAB SIM).
// The simulator only uses the default vector addresses and not the addresses specified above.
// Therefore interrupts will not work in simulation. Here you route the default adresses to the new adresses to solve this problem.
// This code will not be programmed on your microcontroller because the location of this code is protected by the linker file.
#pragma code _DUMMY_HIGH_INTERRUPT_VECTOR = 0x000008
void dummy_high_isr (void)
{
_asm goto 0x000808 _endasm;
}
#pragma code
#pragma code _DUMMY_LOW_INTERRUPT_VECTOR = 0x000018
void dummy_low_isr (void)
{
_asm goto 0x000818 _endasm;
}
#pragma code
// *** END dummy interrupt jumps ***
#define CLEAR_DISPLAY 0x01
/*****/
void DelayFor18TCY(void);
void DelayPORXLCD(void);
void DelayXLCD(void);
/*****/
char filename[14] ="Picdem2+ test";
char teststring[15]="End of Testing";
/********************************************************************/
void clear_lcd(int line)
{
char ln;
int x;
/*****/
if (line==2) ln = 0xC0;
else ln = 0x00;
/*****/
//while( BusyXLCD() );
Delay10KTCYx(5);
/*****/
SetDDRamAddr(ln); // set line
/*****/
for (x=0; x<16; x++)
{
// while( BusyXLCD() );
Delay10KTCYx(5);
/*****/
WriteDataXLCD(' '); // write a blank
}
/*****/
//while( BusyXLCD() );
Delay10KTCYx(5);
/*****/
SetDDRamAddr(ln); // set back to beginning
return;
}
/*****/
/*************** main starts here **************/
void main(void) //Start programma
{
TRISB=0x00; //Maak alle pinnen van poort B output
ADCON0 = 0x00; // Disable the AD converter
ADCON1 = 0x0F; // Set all ports to digital
// Initialise LCD
OpenXLCD(FOUR_BIT & LINES_5X7); // Init the LCD Display
Delay10KTCYx(250);
WriteCmdXLCD(DON&CURSOR_OFF&BLINK_OFF);
while(BusyXLCD());
/*****/
Delay10KTCYx(250);
putrsXLCD("I hope it works "); // Write a string variable
//LcdOpen(); // See lcd.c
while(1) //Draai continu het volgende stuk code
{
PORTBbits.RB1 = 1; //Maak pin A0 hoog
Delay10KTCYx(200); //Wacht 200x10000 clockcycles
PORTBbits.RB1 = 0; //Maak pin A0 laag
Delay10KTCYx(200); //Wacht 200x10000 clockcycles
// LcdSetLine1(); // Put cursor on start of line 1
// putsXLCD("Hello World");
// printf("SET TIME");
}
}