|
|
View previous topic :: View next topic |
Author |
Message |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 13, 2005 5:10 pm |
|
|
1. Before, you were using an 18F1320. Now your code shows a 1220.
2. Your code shows a 8 MHz crystal. Is that what you're using ?
3. What is the version of your compiler ?
4. I don't remember if you told us what demo board you are using ? |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1900
|
|
Posted: Fri May 13, 2005 5:16 pm |
|
|
This code will work - it uses the same LCD driver I use for every project I do. Just connect the LCD as detailed in the code, or if you change things around, just be sure to change the structure that corresponds to the LCD pinout.
BTW, this is for an 18F452, @ 4 MHz.
Code: | #include <18F452.h>
#device adc=8
#fuses WDT,WDT1, XT, NOPROTECT, NOOSCSEN, BROWNOUT, BORV20, PUT, STVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB
#use delay(clock=4000000,RESTART_WDT)
#use fast_io(B)
// Connect the LCD as below:
// PIC pin B0 - LCD RS
// PIC pin B1 - LCD E
// PIC pin B2 - LCD R/W
// PIC pin B3 - Not Connected
// PIC pin B4 - LCD D4
// PIC pin B5 - LCD D5
// PIC pin B6 - LCD D6
// PIC pin B7 - LCD D7
struct lcd_pin_map {
boolean rs;
boolean enable;
boolean rw;
boolean unused;
int data : 4;
} lcd;
#byte lcd = 0xf81 // 18f452
#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
int8 lcd_read_byte() {
int8 low,high;
set_tris_b(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_b(LCD_WRITE);
return( (high<<4) | low);
}
void lcd_send_nibble( int8 n ) {
lcd.data = n;
delay_cycles(1);
lcd.enable = 1;
delay_us(2);
lcd.enable = 0;
}
void lcd_send_byte( int8 address, int8 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_b(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( int8 x, int8 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( int8 x, int8 y) {
char value;
lcd_gotoxy(x,y);
lcd.rs=1;
value = lcd_read_byte();
lcd.rs=0;
return(value);
}
void main() {
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_ON);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
set_tris_b(0x00);
lcd_init();
while (TRUE) {
restart_wdt();
lcd_putc("\fFirst message");
delay_ms(1000);
lcd_putc("\fSecond message");
delay_ms(1000);
}
} |
|
|
|
Bryan
Joined: 23 Apr 2005 Posts: 73
|
|
Posted: Fri May 13, 2005 5:16 pm |
|
|
Ah, sorry about not mentioning this before. Somehow my programmer messed up the PIC18F1320 I was using and I had some spare 1220's which I ended up using because they are so similar to 1320's. I am using an 8MHz crystal, but just in case that was the problem I changed it to 4MHz and unfortunately got the same result.
I am using PCWH version 3.221 and I am not using a demo board from CCS. I put the PIC in a breadboard and wire the LCD directly to it while using a dual power supply I built as a power source (It works ) |
|
|
Bryan
Joined: 23 Apr 2005 Posts: 73
|
|
Posted: Fri May 13, 2005 5:40 pm |
|
|
Same thing with using your driver newguy First row boxes all filled in. I am so confused because I have tried 4 different drivers now and a few different LCD types and they all give the same exact thing (I made sure to change the pins when using your driver newguy, since E and RS are swapped from the CCS one). Thank you for posting it though! I'm not sure what to try next...
I am using the first four data pins on the hantronix LCD (pins 7-10 as per the spec sheet) and connecting them to RB4-RB7 because the spec sheet doesn't specify which pins to use for 4-bit operation (as far as I have been able to see), is this correct? I reversed the connections for pins RS and E (connected to RB0 and RB1 and vice versa) to see what would happen and I was alarmed to find that it made no difference - Still a single line of boxes... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 13, 2005 5:54 pm |
|
|
You've changed the PIC on your board. Do you know for sure that
the PIC works at all ? Can you write a program to blink a LED
and have it work ?
Do you guarantee that the clock speed is really what you think it is ?
I know I asked you this before, but could you do it again, and use
the delay_ms() statement, instead of using a timer ? Like this:
Code: | #include <18F1220.h>
#fuses HS, NOWDT,NOPROTECT, PUT, BROWNOUT, NOLVP
#use delay(clock = 8000000)
//========================================
void main()
{
while(1)
{
output_high(PIN_B0);
delay_ms(500);
output_low(PIN_B0);
delay_ms(500);
}
} |
|
|
|
newguy
Joined: 24 Jun 2004 Posts: 1900
|
|
Posted: Fri May 13, 2005 6:04 pm |
|
|
Bryan wrote: | I am using the first four data pins on the hantronix LCD (pins 7-10 as per the spec sheet) and connecting them to RB4-RB7 because the spec sheet doesn't specify which pins to use for 4-bit operation (as far as I have been able to see), is this correct? I reversed the connections for pins RS and E (connected to RB0 and RB1 and vice versa) to see what would happen and I was alarmed to find that it made no difference - Still a single line of boxes... |
Bingo - there's the problem. In 4 bit mode, you only connect the D4 - D7 of the LCD's data bits (pins 11 - 14). D0 - D3 aren't connected, just let them float. Try it again with this change. |
|
|
Bryan
Joined: 23 Apr 2005 Posts: 73
|
|
Posted: Fri May 13, 2005 6:30 pm |
|
|
PCM programmer - The code you gave does not work correctly, but I modified it to use fuse INTRC_IO and after removing fuse HS and keeping the rest the same it flashes an LED every 0.5 seconds as per the delay. I have always wondered why this happens (I have encountered this before). As for the LCD, I need to solder leads to pins 11-14 later on, and I will report what I get when I do that. Thanks for the help!
Code for flashing LED:
Code: |
#include <18F1220.h>
#fuses NOWDT,NOPROTECT, PUT, BROWNOUT, NOLVP, INTRC_IO
#use delay(clock = 8000000)
//========================================
void main()
{
while(1)
{
output_high(PIN_B0);
delay_ms(500);
output_low(PIN_B0);
delay_ms(500);
}
}
|
|
|
|
Bryan
Joined: 23 Apr 2005 Posts: 73
|
|
Posted: Sat May 14, 2005 11:20 pm |
|
|
A big thank you to all those that helped me. I finally got the LCD to work and it works perfectly now. Unfortunately, I never got the "off brand" LCDs that I bought to work even after testing them with the exact same code that worked on the hantronix displays; go figure. I needed to set the INTRC_IO fuse to get the hantronix to work, but it looks like some of you do not always use it and I'm curious about how you are doing it without setting that fuse. Maybe it is the PIC? Thanks again for the help and bearing with me through some of the learning curve . |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1900
|
|
Posted: Sat May 14, 2005 11:57 pm |
|
|
Bryan wrote: | A big thank you to all those that helped me. I finally got the LCD to work and it works perfectly now. Unfortunately, I never got the "off brand" LCDs that I bought to work even after testing them with the exact same code that worked on the hantronix displays; go figure. I needed to set the INTRC_IO fuse to get the hantronix to work, but it looks like some of you do not always use it and I'm curious about how you are doing it without setting that fuse. Maybe it is the PIC? Thanks again for the help and bearing with me through some of the learning curve . |
I'll admit it: the curiosity is killing me. What did the trick for you?
As for the INTRC_IO fuse, no PIC I've ever used has had an internal oscillator. I've only ever used crystals (XT and HS). |
|
|
Bryan
Joined: 23 Apr 2005 Posts: 73
|
|
Posted: Sun May 15, 2005 12:02 am |
|
|
Buckling down and shelling out some extra cash to buy the hantronix did the trick for me . As soon as I switched the data lines (as you suggested) from pins 7-10 to 11-14 and set the INTRC_IO fuse when the new hantronix LCD came in it worked fine! Needless to say, I had a big celebration when I got that one working . Now to attempt to make a new driver to use control lines on port A and data lines on port B - luckily Mark has put a sample out there so it shouldn't be too tough to figure out. Thanks again for the help! |
|
|
Charles Linquist
Joined: 07 May 2005 Posts: 28 Location: Campbell, CA
|
|
Posted: Sun May 15, 2005 7:32 am |
|
|
If your supply is backlit, it probably does NOT have a dropping resistor on-board. Many displays assume that you will control backlight brightness by varying a resistor on the driving circuit. The backlights generally need about 4.2V (see the datasheet for your unit) and you need to put a 5 ohm 1/2 W resistor in series with it when driven from 5V.
The backlights are LEDs, not lamps, and they clamp hard at Vforward. |
|
|
Charles Linquist
Joined: 07 May 2005 Posts: 28 Location: Campbell, CA
|
|
Posted: Sun May 15, 2005 7:33 am |
|
|
Er, I mean if your LCD is backlit.... |
|
|
|
|
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
|