Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18F45K20 with barcode reader

Status
Not open for further replies.
I seem to remember bit 0 to 3 of port B default to analogue inputs. Have you set them as digital?
 

I've set it to digital but still the same thing, the LCD still had not initialized properly.
Here's my code

Code:
//LCD Testing
#pragma config OSC = INTIO2
#include "p18f2220.h"
#include "delays.h"

// LCD display ports etc
#define LCDdata LATA		// data port
#define LCDstatus LATBbits    	// control/status port
#define RS LATB0				// read/write bit in LCDstatus
#define E  LATB1 				// enable bit in LCDstatus

void lcd_delay() { Delay1KTCYx(40); }  // if LCD does not work make this longer

// Write a nibble to the LCD
void lcdNibble(int n)
{
  	LCDdata = ((n & 0x0f));			// send out lower Nibble
    LCDstatus.E=1;					// take clock E high 
	lcd_delay();
    LCDstatus.E=0;
	lcd_delay();
 }

// Write a Control Command to the LCD
// This is written as two nibbles
void lcdCmd(int c)
{
 	LCDstatus.RS=0;			        // Take RS pin low for command
	lcdNibble(c >>4);		        // Makeup Upper Nibble
	lcdNibble(c);			        // Makeup Lower Nibble
}

// write a data byte to LCD
int lcdPutchar(int d)
{
	LCDstatus.RS=1; 				// Take RS pin high for data
	lcdNibble(d >>4);     		    // Makeup Upper Nibble
	lcdNibble(d);		            // Makeup Lower Nibble
    return 1;
}

// Initialise the LCD in 4bit Mode
void lcdInit()
{
	LATA = 0;
	LATB = 0;
	ADCON1 = 0x0F;
    TRISBbits.TRISB0=0;  	// set RS and E bits output
    TRISBbits.TRISB1=0;  	// set RS and E bits output
    TRISA &= 0xf0;          // set bits 0-3 output for data
 	LCDstatus.RS=0;			// Take RS pin low for command
	lcdNibble(0x3);		// This put the LCD into Soft Reset 
	lcdNibble(0x3);
	lcdNibble(0x3);
	lcdNibble(0x2);
	lcdCmd(0x28);			// 2 line, 4 bit mode 
    lcdCmd(0x6);			// increment cursor after each write
    lcdCmd(0x1);			// clear display
    lcdCmd(0x2);			// home
    lcdCmd(0xF);			// turn disply on
}

void main(void)
{
	OSCCON = 0x72;			//8MHz Internal Clock
	Delay1KTCYx(50);
	lcdInit();
	lcdPutchar('a');
}
 

The only doubtful thing I can see is
Code:
    TRISA &= 0xf0;          // set bits 0-3 output for data
I have found accessing the same register too fast can cause problems, i.e. read TRISA, AND with 0xf0 and write it back. try
Code:
    TRISA = 0xf0;          // set bits 0-3 output for data
writing to LCDs can be time critial - it may be worth put in some more delays, e.g. after setting RS in lcdCmd() and lcdPutchar()
Code:
void lcdCmd(int c)
{
 	LCDstatus.RS=0;			        // Take RS pin low for command
	lcd_delay();                        // delay ?
	lcdNibble(c >>4);		        // Makeup Upper Nibble
	lcdNibble(c);			        // Makeup Lower Nibble
}
what exactly happens on the LCD now?
I don't use PIC18's these days unless working on legacy projects so I am not too sure about the code (my original was for a dsPIC).
Perhaps some of our PIC18 experts can comment? what compiler are you using?
 

Only one line of the LCD is visible, but not displaying anything. Using C18, I'm working on it right now. Is the initializing sequence correct? I'll feedback to you asap.
 

the initialization code worked OK for the PC1602-J LCD I was using
check the data sheet on the LCD you are using - it will specify the bit patterns of commands to drive the device.
try writeing a number of characters - see if anything appears
 

Hey thanks for the help, I've managed to make the LCD to function with the coding from **broken link removed**
I just merely changed the codes from hi-tech to mcc18, i still dunno what's the different between the codes you provided and from the link.
Thanks alot anyways, you've been a big help to me :)

By the way, it's the power supply from pickit3 enough to power up the LCD?
 

hi, i'm now trying to interface the PIC with a PS/2 barcode scanner.
I've read through this link
**broken link removed**
but i have something unsure.
Do I treat the barcode scanner as a keyboard?
Do I need to do any initialization?
I've tried to trace the data and clock lines with oscilloscope, but the two lines were kept high all the while even with some barcode scanned.
Any advice?

Thank you very much!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top