|
|
View previous topic :: View next topic |
Author |
Message |
Pecl Guest
|
PIC16F877 and Optrex LCD problem |
Posted: Sun Apr 21, 2002 6:07 pm |
|
|
I'm trying to get the LCD working but having no luck. I'm adding an LCD as an after thought to a project so I can't use
the standard LCD commands in the compiler. That means port b was already in use so I used any available pins. Changing it now is not an option. In my program I'm trying to initialize the LCD then write a character to it. Maybe somebody can point out the errors in my code.
#define enable PIN_E0
#define rs PIN_C4
#define rw PIN_C5
#define db7 PIN_E2
#define db6 PIN_E1
#define db5 PIN_A5
#define db4 PIN_A4
#define db3 PIN_A3
#define db2 PIN_A2
#define db1 PIN_A1
#define db0 PIN_A0
void lcd_init()
{
int x;
printf("LCD initialization...\n");
delay_ms(50); //step 2 delay after power on
//step 3 send function set command
printf("Sending first function set command...\n");
output_low(rs);
output_low(rw);
output_low(enable);
//db0 - db3 are don't cares
output_low(db7);
output_low(db6);
output_high(db5);
output_high(db4);
// output_high(enable);
// delay_cycles(2);
// output_low(enable);
//step 4 wait/delay
delay_ms(8);
//step 5 send function set command same as step 3 ---- 2nd time
//rs, rw, and enable should still be low
printf("Sending second function set command...\n");
output_low(rs);
output_low(rw);
output_low(enable); //just in case
//db0 - db3 are don't cares
output_low(db7);
output_low(db6);
output_high(db5);
output_high(db4);
// output_high(enable);
// delay_cycles(2);
// output_low(enable);
//step 6 wait/delay more than 100uS
delay_ms(1); //100us delay
//step 7 send function set command again - same as step 3 --- //3rd time
printf("Sending third function set command...\n");
output_low(rs);
output_low(rw);
output_low(enable); //just in case
//db0 - db3 are don't cares
output_low(db7);
output_low(db6);
output_high(db5);
output_high(db4);
// output_high(enable);
// delay_cycles(2);
// output_low(enable);
//step 8 Function set
// 0 0 0 0 1 1 N F x x
printf("Setting up LCD # of lines and size...\n");
output_low(rw);
output_low(rs);
output_low(enable);
//db0 and db1 are don't cares
output_low(db7);
output_low(db6);
output_high(db5);
output_high(db4);
output_high(db3); //set # of lines
output_low(db2); //set 5x10
// output_high(enable);
// delay_cycles(2);
// output_low(enable);
//step 9 display off
printf("Turning off LCD display...\n");
output_low(rs);
output_low(rw);
output_low(enable);
output_low(db7);
output_low(db6);
output_low(db5);
output_low(db4);
output_high(db3);
output_low(db2);
output_low(db1);
output_low(db0);
// output_high(enable);
// delay_cycles(2);
// output_low(enable);
//step 9.5 is it still busy?
printf("Checking busy flag...\n");
output_high(rw);
x = input(db7);
while (x==1)
{
;
}
//step 10 clear display
printf("Clear LCD display...\n");
output_low(rs);
output_low(rw);
output_low(enable);
output_low(db7);
output_low(db6);
output_low(db5);
output_low(db4);
output_low(db3);
output_low(db2);
output_low(db1);
output_high(db0);
// output_high(enable);
// delay_cycles(2);
// output_low(enable);
//step 10.5 is it still busy?
printf("Checking busy flag...\n");
output_high(rw);
x = input(db7);
while (x==1)
{
;
}
//step 11 entry mode set
printf("Setting entry mode...\n");
output_low(rs);
output_low(rw);
output_low(enable);
output_low(db7);
output_low(db6);
output_low(db5);
output_low(db4);
output_low(db3);
output_high(db2);
output_high(db1); // I/D
output_low(db0); // S
// output_high(enable);
// delay_cycles(2);
// output_low(enable);
//step 11.5 is it still busy?
printf("Checking busy flag...\n");
output_high(rw);
x = input(db7);
while (x==1)
{
;
}
//step 12 display on
printf("Turning display on...\n");
output_high(rs);
output_low(rw);
output_low(enable);
output_low(db7);
output_low(db6);
output_low(db5);
output_low(db4);
output_high(db3);
output_high(db2);
output_high(db1); // C
output_high(db0); // B
// output_high(enable);
// delay_cycles(2);
// output_low(enable);
return;
}
void main()
{
int x;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_spi(FALSE);
setup_psp(PSP_DISABLED);
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
lcd_init();
output_low(enable);
delay_cycles(2);
output_low(rs);
delay_cycles(1);
output_high(rw);
x = input(db7); //check busy flag
while (x==1)
{
printf("\%d\n", x);
}
output_high(rs);
delay_cycles(1);
output_low(rw);
delay_cycles(1);
output_high(enable);
output_high(db0);
output_low(db1);
output_low(db2);
output_low(db3);
output_low(db4);
output_low(db5);
output_high(db6);
output_low(db7);
delay_cycles(2);
output_low(enable);
// delay_cycles(1);
// output_high(rw);
// delay_cycles(1);
// output_low(rs);
// delay_cycles(1);
// output_high(enable);
// delay_us(2);
// output_low(enable);
while(TRUE)
{
//alive led
OUTPUT_low(PIN_C0);
delay_ms(500);
output_high(PIN_C0);
delay_ms(500);
}
}
The printf statements are for debugging. The data sheet (DMC16433N-SEWB)didn't say a lot about the enable pin so you can see where I've commented the code out to toggle the enable line. You get nothing on the display when you run this code. Am I missing something or is my code just bad?
Thanks and Best Regards
___________________________
This message was ported from CCS's old forum
Original Post ID: 3922 |
|
|
JB Guest
|
Re: PIC16F877 and Optrex LCD problem |
Posted: Mon Apr 22, 2002 7:56 am |
|
|
make sure you have a pull-up resistor on pin A4, because it is an open drain output.
:=I'm trying to get the LCD working but having no luck. I'm adding an LCD as an after thought to a project so I can't use
:=the standard LCD commands in the compiler. That means port b was already in use so I used any available pins. Changing it now is not an option. In my program I'm trying to initialize the LCD then write a character to it. Maybe somebody can point out the errors in my code.
:=
:=
:=#define enable PIN_E0
:=#define rs PIN_C4
:=#define rw PIN_C5
:=#define db7 PIN_E2
:=#define db6 PIN_E1
:=#define db5 PIN_A5
:=#define db4 PIN_A4
:=#define db3 PIN_A3
:=#define db2 PIN_A2
:=#define db1 PIN_A1
:=#define db0 PIN_A0
:=
:=void lcd_init()
:={
:= int x;
:=
:= printf("LCD initialization...\n");
:= delay_ms(50); //step 2 delay after power on
:=
:= //step 3 send function set command
:= printf("Sending first function set command...\n");
:= output_low(rs);
:= output_low(rw);
:= output_low(enable);
:=
:= //db0 - db3 are don't cares
:= output_low(db7);
:= output_low(db6);
:= output_high(db5);
:= output_high(db4);
:=
:= // output_high(enable);
:= // delay_cycles(2);
:= // output_low(enable);
:=
:= //step 4 wait/delay
:= delay_ms(8);
:=
:= //step 5 send function set command same as step 3 ---- 2nd time
:= //rs, rw, and enable should still be low
:= printf("Sending second function set command...\n");
:= output_low(rs);
:= output_low(rw);
:= output_low(enable); //just in case
:=
:= //db0 - db3 are don't cares
:= output_low(db7);
:= output_low(db6);
:= output_high(db5);
:= output_high(db4);
:=
:=// output_high(enable);
:=// delay_cycles(2);
:=// output_low(enable);
:=
:= //step 6 wait/delay more than 100uS
:= delay_ms(1); //100us delay
:=
:=//step 7 send function set command again - same as step 3 --- //3rd time
:= printf("Sending third function set command...\n");
:= output_low(rs);
:= output_low(rw);
:= output_low(enable); //just in case
:=
:= //db0 - db3 are don't cares
:= output_low(db7);
:= output_low(db6);
:= output_high(db5);
:= output_high(db4);
:=
:=// output_high(enable);
:=// delay_cycles(2);
:=// output_low(enable);
:=
:= //step 8 Function set
:= // 0 0 0 0 1 1 N F x x
:= printf("Setting up LCD # of lines and size...\n");
:= output_low(rw);
:= output_low(rs);
:= output_low(enable);
:=
:= //db0 and db1 are don't cares
:= output_low(db7);
:= output_low(db6);
:= output_high(db5);
:= output_high(db4);
:= output_high(db3); //set # of lines
:= output_low(db2); //set 5x10
:=
:=// output_high(enable);
:=// delay_cycles(2);
:=// output_low(enable);
:=
:= //step 9 display off
:= printf("Turning off LCD display...\n");
:= output_low(rs);
:= output_low(rw);
:= output_low(enable);
:= output_low(db7);
:= output_low(db6);
:= output_low(db5);
:= output_low(db4);
:= output_high(db3);
:= output_low(db2);
:= output_low(db1);
:= output_low(db0);
:=
:=// output_high(enable);
:=// delay_cycles(2);
:=// output_low(enable);
:=
:= //step 9.5 is it still busy?
:= printf("Checking busy flag...\n");
:= output_high(rw);
:= x = input(db7);
:= while (x==1)
:= {
:= ;
:= }
:=
:= //step 10 clear display
:= printf("Clear LCD display...\n");
:= output_low(rs);
:= output_low(rw);
:= output_low(enable);
:= output_low(db7);
:= output_low(db6);
:= output_low(db5);
:= output_low(db4);
:= output_low(db3);
:= output_low(db2);
:= output_low(db1);
:= output_high(db0);
:=
:=// output_high(enable);
:=// delay_cycles(2);
:=// output_low(enable);
:=
:= //step 10.5 is it still busy?
:= printf("Checking busy flag...\n");
:= output_high(rw);
:= x = input(db7);
:= while (x==1)
:= {
:= ;
:= }
:=
:= //step 11 entry mode set
:= printf("Setting entry mode...\n");
:= output_low(rs);
:= output_low(rw);
:= output_low(enable);
:= output_low(db7);
:= output_low(db6);
:= output_low(db5);
:= output_low(db4);
:= output_low(db3);
:= output_high(db2);
:= output_high(db1); // I/D
:= output_low(db0); // S
:=
:=// output_high(enable);
:=// delay_cycles(2);
:=// output_low(enable);
:=
:= //step 11.5 is it still busy?
:= printf("Checking busy flag...\n");
:= output_high(rw);
:= x = input(db7);
:= while (x==1)
:= {
:= ;
:= }
:=
:= //step 12 display on
:= printf("Turning display on...\n");
:= output_high(rs);
:= output_low(rw);
:= output_low(enable);
:=
:= output_low(db7);
:= output_low(db6);
:= output_low(db5);
:= output_low(db4);
:= output_high(db3);
:= output_high(db2);
:= output_high(db1); // C
:= output_high(db0); // B
:=
:= // output_high(enable);
:= // delay_cycles(2);
:= // output_low(enable);
:=
:= return;
:=}
:=
:=void main()
:={
:= int x;
:= setup_adc_ports(NO_ANALOGS);
:= setup_adc(ADC_CLOCK_DIV_2);
:= setup_spi(FALSE);
:= setup_psp(PSP_DISABLED);
:= setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
:= setup_timer_1(T1_DISABLED);
:= setup_timer_2(T2_DISABLED,0,1);
:= setup_ccp1(CCP_OFF);
:= setup_ccp2(CCP_OFF);
:=
:= lcd_init();
:=
:= output_low(enable);
:= delay_cycles(2);
:= output_low(rs);
:= delay_cycles(1);
:= output_high(rw);
:=
:= x = input(db7); //check busy flag
:= while (x==1)
:= {
:= printf("\%d\n", x);
:= }
:= output_high(rs);
:= delay_cycles(1);
:= output_low(rw);
:= delay_cycles(1);
:= output_high(enable);
:= output_high(db0);
:= output_low(db1);
:= output_low(db2);
:= output_low(db3);
:= output_low(db4);
:= output_low(db5);
:= output_high(db6);
:= output_low(db7);
:= delay_cycles(2);
:= output_low(enable);
:=
:=// delay_cycles(1);
:=// output_high(rw);
:=// delay_cycles(1);
:=// output_low(rs);
:=// delay_cycles(1);
:=// output_high(enable);
:=// delay_us(2);
:= // output_low(enable);
:= while(TRUE)
:= {
:= //alive led
:= OUTPUT_low(PIN_C0);
:= delay_ms(500);
:= output_high(PIN_C0);
:= delay_ms(500);
:= }
:=}
:=
:=The printf statements are for debugging. The data sheet (DMC16433N-SEWB)didn't say a lot about the enable pin so you can see where I've commented the code out to toggle the enable line. You get nothing on the display when you run this code. Am I missing something or is my code just bad?
:=
:=Thanks and Best Regards
___________________________
This message was ported from CCS's old forum
Original Post ID: 3931 |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
Re: PIC16F877 and Optrex LCD problem |
Posted: Mon Apr 22, 2002 8:18 am |
|
|
Are you using a 4X20 display. If so, I currently use that in a product. I am in the process of changing from a 16C76 to a F876 today. I can send you my routines if you like. I recall some timing issues with this display. I probably did those in assembly for a 4MHz device. I will check and let you know.
Mark
:=I'm trying to get the LCD working but having no luck. I'm adding an LCD as an after thought to a project so I can't use
:=the standard LCD commands in the compiler. That means port b was already in use so I used any available pins. Changing it now is not an option. In my program I'm trying to initialize the LCD then write a character to it. Maybe somebody can point out the errors in my code.
:=
:=
:=#define enable PIN_E0
:=#define rs PIN_C4
:=#define rw PIN_C5
:=#define db7 PIN_E2
:=#define db6 PIN_E1
:=#define db5 PIN_A5
:=#define db4 PIN_A4
:=#define db3 PIN_A3
:=#define db2 PIN_A2
:=#define db1 PIN_A1
:=#define db0 PIN_A0
:=
:=void lcd_init()
:={
:= int x;
:=
:= printf("LCD initialization...\n");
:= delay_ms(50); //step 2 delay after power on
:=
:= //step 3 send function set command
:= printf("Sending first function set command...\n");
:= output_low(rs);
:= output_low(rw);
:= output_low(enable);
:=
:= //db0 - db3 are don't cares
:= output_low(db7);
:= output_low(db6);
:= output_high(db5);
:= output_high(db4);
:=
:= // output_high(enable);
:= // delay_cycles(2);
:= // output_low(enable);
:=
:= //step 4 wait/delay
:= delay_ms(8);
:=
:= //step 5 send function set command same as step 3 ---- 2nd time
:= //rs, rw, and enable should still be low
:= printf("Sending second function set command...\n");
:= output_low(rs);
:= output_low(rw);
:= output_low(enable); //just in case
:=
:= //db0 - db3 are don't cares
:= output_low(db7);
:= output_low(db6);
:= output_high(db5);
:= output_high(db4);
:=
:=// output_high(enable);
:=// delay_cycles(2);
:=// output_low(enable);
:=
:= //step 6 wait/delay more than 100uS
:= delay_ms(1); //100us delay
:=
:=//step 7 send function set command again - same as step 3 --- //3rd time
:= printf("Sending third function set command...\n");
:= output_low(rs);
:= output_low(rw);
:= output_low(enable); //just in case
:=
:= //db0 - db3 are don't cares
:= output_low(db7);
:= output_low(db6);
:= output_high(db5);
:= output_high(db4);
:=
:=// output_high(enable);
:=// delay_cycles(2);
:=// output_low(enable);
:=
:= //step 8 Function set
:= // 0 0 0 0 1 1 N F x x
:= printf("Setting up LCD # of lines and size...\n");
:= output_low(rw);
:= output_low(rs);
:= output_low(enable);
:=
:= //db0 and db1 are don't cares
:= output_low(db7);
:= output_low(db6);
:= output_high(db5);
:= output_high(db4);
:= output_high(db3); //set # of lines
:= output_low(db2); //set 5x10
:=
:=// output_high(enable);
:=// delay_cycles(2);
:=// output_low(enable);
:=
:= //step 9 display off
:= printf("Turning off LCD display...\n");
:= output_low(rs);
:= output_low(rw);
:= output_low(enable);
:= output_low(db7);
:= output_low(db6);
:= output_low(db5);
:= output_low(db4);
:= output_high(db3);
:= output_low(db2);
:= output_low(db1);
:= output_low(db0);
:=
:=// output_high(enable);
:=// delay_cycles(2);
:=// output_low(enable);
:=
:= //step 9.5 is it still busy?
:= printf("Checking busy flag...\n");
:= output_high(rw);
:= x = input(db7);
:= while (x==1)
:= {
:= ;
:= }
:=
:= //step 10 clear display
:= printf("Clear LCD display...\n");
:= output_low(rs);
:= output_low(rw);
:= output_low(enable);
:= output_low(db7);
:= output_low(db6);
:= output_low(db5);
:= output_low(db4);
:= output_low(db3);
:= output_low(db2);
:= output_low(db1);
:= output_high(db0);
:=
:=// output_high(enable);
:=// delay_cycles(2);
:=// output_low(enable);
:=
:= //step 10.5 is it still busy?
:= printf("Checking busy flag...\n");
:= output_high(rw);
:= x = input(db7);
:= while (x==1)
:= {
:= ;
:= }
:=
:= //step 11 entry mode set
:= printf("Setting entry mode...\n");
:= output_low(rs);
:= output_low(rw);
:= output_low(enable);
:= output_low(db7);
:= output_low(db6);
:= output_low(db5);
:= output_low(db4);
:= output_low(db3);
:= output_high(db2);
:= output_high(db1); // I/D
:= output_low(db0); // S
:=
:=// output_high(enable);
:=// delay_cycles(2);
:=// output_low(enable);
:=
:= //step 11.5 is it still busy?
:= printf("Checking busy flag...\n");
:= output_high(rw);
:= x = input(db7);
:= while (x==1)
:= {
:= ;
:= }
:=
:= //step 12 display on
:= printf("Turning display on...\n");
:= output_high(rs);
:= output_low(rw);
:= output_low(enable);
:=
:= output_low(db7);
:= output_low(db6);
:= output_low(db5);
:= output_low(db4);
:= output_high(db3);
:= output_high(db2);
:= output_high(db1); // C
:= output_high(db0); // B
:=
:= // output_high(enable);
:= // delay_cycles(2);
:= // output_low(enable);
:=
:= return;
:=}
:=
:=void main()
:={
:= int x;
:= setup_adc_ports(NO_ANALOGS);
:= setup_adc(ADC_CLOCK_DIV_2);
:= setup_spi(FALSE);
:= setup_psp(PSP_DISABLED);
:= setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
:= setup_timer_1(T1_DISABLED);
:= setup_timer_2(T2_DISABLED,0,1);
:= setup_ccp1(CCP_OFF);
:= setup_ccp2(CCP_OFF);
:=
:= lcd_init();
:=
:= output_low(enable);
:= delay_cycles(2);
:= output_low(rs);
:= delay_cycles(1);
:= output_high(rw);
:=
:= x = input(db7); //check busy flag
:= while (x==1)
:= {
:= printf("\%d\n", x);
:= }
:= output_high(rs);
:= delay_cycles(1);
:= output_low(rw);
:= delay_cycles(1);
:= output_high(enable);
:= output_high(db0);
:= output_low(db1);
:= output_low(db2);
:= output_low(db3);
:= output_low(db4);
:= output_low(db5);
:= output_high(db6);
:= output_low(db7);
:= delay_cycles(2);
:= output_low(enable);
:=
:=// delay_cycles(1);
:=// output_high(rw);
:=// delay_cycles(1);
:=// output_low(rs);
:=// delay_cycles(1);
:=// output_high(enable);
:=// delay_us(2);
:= // output_low(enable);
:= while(TRUE)
:= {
:= //alive led
:= OUTPUT_low(PIN_C0);
:= delay_ms(500);
:= output_high(PIN_C0);
:= delay_ms(500);
:= }
:=}
:=
:=The printf statements are for debugging. The data sheet (DMC16433N-SEWB)didn't say a lot about the enable pin so you can see where I've commented the code out to toggle the enable line. You get nothing on the display when you run this code. Am I missing something or is my code just bad?
:=
:=Thanks and Best Regards
___________________________
This message was ported from CCS's old forum
Original Post ID: 3933 |
|
|
johnpcunningham Guest
|
Re: PIC16F877 and Optrex LCD problem |
Posted: Mon Apr 22, 2002 10:22 am |
|
|
On start up these signals should be:
RS = 1
RW = 0
E = 1
I have found problems if it is not that way on startup. Also, I dont think your command and data writes have enough delay in between them, from my experience. Here are some examples of command and data writes using PORT D as the data port:
void write_data(char text)
{
OUTPUT_HIGH(LCD_RS); /* RS pin set high for data */
delay_us(1); /* delay 1us*/
PORTD=text; /* Put data on 8 bit port*/
delay_us(1); /* delay 1us*/
OUTPUT_LOW(LCD_E); /* E pin brought low*/
delay_us(100); /* delay 100us*/
OUTPUT_HIGH(LCD_E); /* E pin brought high*/
delay_us(1); /* delay 1us*/
}
/******************************************/
/******************************************/
void write_command(char command)
{
OUTPUT_LOW(LCD_RS); /* RS pin set low for command*/
delay_us(1); /* delay 1us*/
PORTD=command; /* Put data on 8 bit port*/
delay_us(1); /* delay 1us*/
OUTPUT_LOW(LCD_E); /* E pin brought low*/
delay_us(100); /* delay 100us*/
OUTPUT_HIGH(LCD_E); /* E pin brought high*/
delay_us(1); /* delay 1us*/
}
/******************************************/
___________________________
This message was ported from CCS's old forum
Original Post ID: 3941 |
|
|
Pecl Guest
|
Re: PIC16F877 and Optrex LCD problem |
Posted: Tue Apr 23, 2002 4:00 pm |
|
|
Thanks to all who responded. I'm using a 4x16 display. The micro is running at 20MHz so I may not have enough delay in my program. I did install a pull-up resistor on A4. I'll keep playing with the delays and see if that turns anything up.
Thanks.
___________________________
This message was ported from CCS's old forum
Original Post ID: 3972 |
|
|
|
|
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
|