|
|
View previous topic :: View next topic |
Author |
Message |
monkeytennis
Joined: 18 Feb 2004 Posts: 16 Location: UK
|
LCD driver problem |
Posted: Wed Jul 21, 2004 8:40 am |
|
|
Hi.
I have a circuit built (as in http://ww1.microchip.com/downloads/en/AppNotes/00582b.pdf) and I need it now to do something else. Quite simply it is used to set a day counter and when the countdown is finished, an output pin goes high. I however, have attempted to write a suitable program and have (hopefully) adjusted the LCD.C driver so that it should work, but it doesn't. Can anyone see if there is something obvious that I have done wrong?
(Oh yeah timer1 is driven from an external 32.768KHz watch crystal)
Cheers,
Simon
Code: | // Main C program
#include <16C64A.h>
#include <LCD2.C>
#use delay(clock=4000000)
#fuses HS, NOPUT, NOWDT, NOPROTECT, NOBROWNOUT
//Defitions below make it easy to change the settings without playing with the
//program. Change COUNTDOWN to 'days' or 'hours'.
#define COUNTDOWN hours
#define FIRE_PIN1 PIN_C6
#define FIRE_PIN2 PIN_C7
#define UP_PIN PIN_B7
#define SET_PIN PIN_B6
void update_lcddays(void);
int secs=0,hours=0,days=0,dayset=0;
int1 debounce=0;
#int_TIMER1
TIMER1_isr()
{
set_timer1(0xF000);
secs++;
if (secs==3600)
{
secs=0;
hours++;
}
if (hours==24)
{
hours=0;
days++;
}
if (COUNTDOWN==dayset)
{
disable_interrupts(global);
output_high(FIRE_PIN1);
for (secs=0;secs<60;secs++)
delay_ms(100);
output_high(FIRE_PIN2);
}
}
void main()
{
int1 set=0,update=1;
int a;
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
port_b_pullups(TRUE);
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_8);
setup_timer_2(T2_DISABLED,0,1);
lcd_init();
enable_interrupts(INT_TIMER1);
enable_interrupts(global);
disable_interrupts(global);
output_float(UP_PIN);
output_float(SET_PIN);
printf(lcd_putc,"\fChange days setting: %3d",dayset);
do
{
if (input(UP_PIN)==0)
{
update=1;
for (a=0;a<10;a++)
if (input(UP_PIN))
update=0;
else
delay_ms(100);
if (update)
{
dayset++;
if (dayset==1000)
dayset=0;
printf(lcd_putc,"\fChange days setting: %3d",dayset);
}
}
if (input(SET_PIN)==0)
{
set=1;
for (a=0;a<100;a++)
if (input(SET_PIN))
set=0;
else
delay_ms(100);
}
}while(set==0);
printf(lcd_putc,"\fSet for %d days",dayset);
enable_interrupts(global);
while(true);
}
///////////////////////////////////////////////////////////////////////////
//// LCDD.C ////
//// Driver for common LCD modules ////
//// ////
//// lcd_init() Must be called before any other function. ////
//// ////
//// lcd_putc(c) Will display c on the next position of the LCD. ////
//// The following have special meaning: ////
//// \f Clear display ////
//// \n Go to start of second line ////
//// \b Move back one position ////
//// ////
//// lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1) ////
//// ////
//// lcd_getc(x,y) Returns character at position x,y on LCD ////
//// ////
///////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2003 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS C ////
//// compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, reproduction ////
//// or distribution is permitted without written permission. ////
//// Derivative programs created using this software in object code ////
//// form are not restricted in any way. ////
///////////////////////////////////////////////////////////////////////////
// As defined in the following structure the pin connection is as follows:
#define enable PIN_A0
#define rs PIN_A2
#define rw PIN_A1
#define D4 PIN_B0
#define D5 PIN_B1
#define D6 PIN_B2
#define D7 PIN_B3
#define TEST PIN_A3
//
// LCD pins D0-D3 are not used and PIC D3 is not used.
// Un-comment the following define to use port B
// #define use_portb_lcd TRUE
#byte PORTB = 6
#define IN 0xFF
#define OUT 0
/*
struct lcd_pin_map { // This structure is overlayed
BOOLEAN enable; // on to an I/O port to gain
BOOLEAN rs; // access to the LCD pins.
BOOLEAN rw; // The bits are allocated from
BOOLEAN unused; // low order up. ENABLE will
int data : 4; // be pin B0.
} lcd;
#if defined(__PCH__)
#if defined use_portb_lcd
#byte lcd = 0xF81 // This puts the entire structure
#else
#byte lcd = 0xF83 // This puts the entire structure
#endif
#else
#if defined use_portb_lcd
#byte lcd = 6 // on to port B (at address 6)
#else
#byte lcd = 8 // on to port D (at address 8)
#endif
#endif
#if defined use_portb_lcd
#define set_tris_lcd(x) set_tris_b(x)
#else
#define set_tris_lcd(x) set_tris_d(x)
#endif
*/
#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines
#define lcd_line_two 0x40 // LCD RAM address for the second line
BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
// These bytes need to be sent to the LCD
// to start it up.
// The following are used for setting
// the I/O port direction register.
//struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0}; // For write mode all pins are out
//struct lcd_pin_map const LCD_READ = {0,0,0,0,15}; // For read mode data pins are in
BYTE lcd_read_byte() {
BYTE low,high;
output_high(TEST);
output_float(D4);
output_float(D5);
output_float(D6);
output_float(D7);
output_high(rw);
delay_cycles(1);
output_high(enable);
delay_cycles(1);
set_tris_b(IN);
high = PORTB & 0x0F;
output_low(enable);
delay_cycles(1);
output_high(enable);
delay_us(1);
low = PORTB & 0x0F;
output_low(enable);
set_tris_b(OUT);
output_low(TEST);
return( (high<<4) | low);
}
void lcd_send_nibble( BYTE n ) {
set_tris_b(OUT);
PORTB = n & 0x0F;
delay_cycles(1);
output_high(enable);
delay_us(2);
output_low(enable);
}
void lcd_send_byte( BYTE address, BYTE n ) {
output_high(TEST);
output_low(rs);
while (input(D7)) ;
if (address==0)
output_low(rs);
else
output_high(rs);
delay_cycles(1);
output_low(rw);
delay_cycles(1);
output_low(enable);
lcd_send_nibble(n >> 4);
lcd_send_nibble(n & 0xf);
output_low(TEST);
}
void lcd_init() {
BYTE i;
set_tris_b(OUT);
output_low(rs);
output_low(rw);
output_low(enable);
delay_ms(15);
for(i=1;i<=3;++i) {
lcd_send_nibble(3);
delay_ms(5);
}
lcd_send_nibble(2);
for(i=0;i<=3;++i)
lcd_send_byte(0,LCD_INIT_STRING[i]);
}
void lcd_gotoxy( BYTE x, BYTE y) {
BYTE address;
if(y!=1)
address=lcd_line_two;
else
address=0;
address+=x-1;
lcd_send_byte(0,0x80|address);
}
void lcd_putc( char c) {
switch (c) {
case '\f' : lcd_send_byte(0,1);
delay_ms(2);
break;
case '\n' : lcd_gotoxy(1,2); break;
case '\b' : lcd_send_byte(0,0x10); break;
default : lcd_send_byte(1,c); break;
}
}
char lcd_getc( BYTE x, BYTE y) {
char value;
lcd_gotoxy(x,y);
while (input(D7)); // wait until busy flag is low
output_high(rs);
value = lcd_read_byte();
output_low(rs);
return(value);
}
|
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Jul 21, 2004 11:12 am |
|
|
Here is a version of LCDD.C that I did sometime back allowing the control signals to be on different ports than the data. I changed it per App582. It is untested so try it if you want.
Code: |
////////////////////////////////////////////////////////////////////////////
//// LCDD.C ////
//// Driver for common LCD modules ////
//// ////
//// lcd_init() Must be called before any other function. ////
//// ////
//// lcd_putc(c) Will display c on the next position of the LCD. ////
//// The following have special meaning: ////
//// \f Clear display ////
//// \n Go to start of second line ////
//// \b Move back one position ////
//// ////
//// lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1) ////
//// ////
//// lcd_getc(x,y) Returns character at position x,y on LCD ////
//// ////
////////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,1997 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS C ////
//// compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, reproduction ////
//// or distribution is permitted without written permission. ////
//// Derivative programs created using this software in object code ////
//// form are not restricted in any way. ////
////////////////////////////////////////////////////////////////////////////
// This structure is overlayed onto the data ports so that you may use
// whatever ports you desire
struct lcd_pin_map
{
BOOLEAN enable; // PinA0
BOOLEAN rw; // PinA1
BOOLEAN rs; // PinA2
BOOLEAN dummy; // PinA3 is not used
int unusedA : 4; // The rest of portA
int data : 4; // lower nibble of portB is used for data lines
int unusedB : 4; // The rest of portB
int unusedC; // portC is not used
int unusedD; // portD is not used
} lcd;
#if defined(__PCH__)
#locate lcd = 0xF80
#else
#locate lcd = 5
#endif
struct lcd_tris_map
{
int control : 3;
BOOLEAN dummy; // PinA3 is not used
int unusedA : 4; // The rest of portA
int data : 4; // lower nibble of portB is used for data lines
int unusedB : 4; // The rest of portB
int unusedC; // portC is not used
int unusedD; // portD is not used
} lcdtris;
#if defined(__PCH__)
#locate lcdtris = 0xF92
#else
#locate lcdtris = 0x85
#endif
#define set_tris_lcd(x) lcdtris.data = (x); lcdtris.control = 0;
#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines
#define lcd_line_two 0x40 // LCD RAM address for the second line
BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
// These bytes need to be sent to the LCD
// to start it up.
// The following are used for setting
// the I/O port direction register.
#define LCD_WRITE 0 // For write mode all pins are out
#define LCD_READ 15 // For read mode data pins are in
BYTE lcd_read_byte() {
BYTE low,high;
set_tris_lcd(LCD_READ);
lcd.rw = 1;
delay_cycles(1);
lcd.enable = 1;
delay_cycles(1);
high = lcd.data;
lcd.enable = 0;
delay_cycles(1);
lcd.enable = 1;
delay_us(1);
low = lcd.data;
lcd.enable = 0;
set_tris_lcd(LCD_WRITE);
return( (high<<4) | low);
}
void lcd_send_nibble( BYTE n ) {
lcd.data = n;
delay_cycles(1);
lcd.enable = 1;
delay_us(2);
lcd.enable = 0;
}
void lcd_send_byte( BYTE address, BYTE n ) {
lcd.rs = 0;
while ( bit_test(lcd_read_byte(),7) ) ;
lcd.rs = address;
delay_cycles(1);
lcd.rw = 0;
delay_cycles(1);
lcd.enable = 0;
lcd_send_nibble(n >> 4);
lcd_send_nibble(n & 0xf);
}
void lcd_init() {
BYTE i;
set_tris_lcd(LCD_WRITE);
lcd.rs = 0;
lcd.rw = 0;
lcd.enable = 0;
delay_ms(15);
for(i=1;i<=3;++i) {
lcd_send_nibble(3);
delay_ms(5);
}
lcd_send_nibble(2);
for(i=0;i<=3;++i)
lcd_send_byte(0,LCD_INIT_STRING[i]);
}
void lcd_gotoxy( BYTE x, BYTE y) {
BYTE address;
if(y!=1)
address=lcd_line_two;
else
address=0;
address+=x-1;
lcd_send_byte(0,0x80|address);
}
void lcd_putc( char c) {
switch (c) {
case '\f' : lcd_send_byte(0,1);
delay_ms(2);
break;
case '\n' : lcd_gotoxy(1,2); break;
case '\b' : lcd_send_byte(0,0x10); break;
default : lcd_send_byte(1,c); break;
}
}
char lcd_getc( BYTE x, BYTE y) {
char value;
lcd_gotoxy(x,y);
lcd.rs=1;
value = lcd_read_byte();
lcd.rs=0;
return(value);
}
|
|
|
|
monkeytennis
Joined: 18 Feb 2004 Posts: 16 Location: UK
|
|
Posted: Thu Jul 22, 2004 3:13 am |
|
|
Cheers, I'll give that a bash!
Simon |
|
|
monkeytennis
Joined: 18 Feb 2004 Posts: 16 Location: UK
|
|
Posted: Thu Jul 22, 2004 7:09 am |
|
|
Cheers for that, it worked spot-on first time. Now I've just gotta work out why the rest of it doesn't work.
Simon |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|