# PIC18 PORTA Trouble

#### Rxreed

Joined Oct 27, 2012
2
Hi all,
I have been reading articles from this form for a while now and have learned much. This is my first post with a problem I have not been able to figure out. I was given an assignment to write to an LCD screen with some basic information followed by a capital letter made of special characters. This assignment has been completed and is working correctly. The problem was found when I was playing with the circuit. I found if I used PORTAbits.7 or PORTAbits.6 the circuit would not display characters. Can anyone tell me why this is so?
Thank you for any commits

Rich (BB code):
#include<p18f1320.h>
#include <stdio.h>
#pragma config WDT = OFF
#pragma config PWRT = ON
#pragma config LVP = OFF
#pragma config MCLRE = OFF
#pragma config OSC = INTIO2

#define RS PORTAbits.RA3
#define RW PORTAbits.RA4
#define E PORTAbits.RA7

void lcd_setup(void);
void write(char message);
void delay(int time);
void bflag (void);
void movlcd(int x);
void lcdcmd(int x);
void creatchar(void);
void usechar(int x);
void black_out(int x);
void makechar (int a, int b, int c,int d,int e,int f,int g,int h);

void main(void)
{
OSCCON = 0x70;
TRISA = 0;
TRISB = 0;
creatchar();
lcd_setup();
bflag();

lcdcmd(0x80);
bflag ();
write('R');
bflag ();
write('y');
bflag ();
write('a');
bflag ();
write('n');
bflag ();

lcdcmd(0xC0);
bflag ();
write('R');
bflag ();
write('e');
bflag ();
write('e');
bflag ();
write('d');
bflag ();

lcdcmd(0x90);
bflag ();
write('I');
bflag ();
write('n');
bflag ();
write('d');
bflag ();
write('u');
bflag ();
write('s');
bflag ();
write('t');
bflag ();
write('r');
bflag ();
write('i');
bflag ();
write('a');
bflag ();
write('l');
bflag ();

lcdcmd(0xD0);
bflag ();
write('C');
bflag ();
write('o');
bflag ();
write('n');
bflag ();
write('t');
bflag ();
write('r');
bflag ();
write('o');
bflag ();
write('l');
bflag ();
write('s');
bflag ();
write(' ');
bflag ();
write('4');
bflag ();
write('3');
bflag ();
write('0');
bflag ();
write('4');
bflag ();

delay(6000);
lcdcmd(0x01);   //clears LCD
bflag ();

black_out(85);
black_out(21);
black_out(69);
black_out(6);
black_out(71);

lcdcmd(0x96);  //0
write(0x00);

lcdcmd(0x97);  //1
bflag ();
write(0x01);

lcdcmd(0xD8);  //2
bflag ();
write(0x02);

lcdcmd(0xD7);  //3
bflag ();
write(0x03);

lcdcmd(0xC6);  //4
bflag ();
write(0x04);

lcdcmd(0x85);  //5
bflag ();
write(0x05);

lcdcmd(0x87);  //6
bflag ();
write(0x06);

lcdcmd(0xC7);  //7
bflag ();
write(0x07);

while(1);
}
void black_out(int x)
{
x = 128 + x;
lcdcmd(x);
bflag ();
write(0xFF);
bflag();
}
void lcdcmd(int x)
{
RS = 0;
RW = 0;
PORTB = x;
E=1;
delay(1);
E=0;
}
void write(char message)
{
RS=1;
RW=0;
PORTB = message;
E=1;
delay(1);
E=0;
}
void delay( int time)
{
unsigned int i,j;
for (i=0; i<time; i++)
for(j=0;j<165; j++)
;
}
void bflag (void)
{
TRISB = 0xFF;
RW = 1;
RS = 0;
do
{
E=1;
delay(1);
E=0;
}
while(PORTBbits.RB7 == 1);
TRISB = 0;
}
void lcd_setup(void)
{
RS = 0;
RW = 0;
PORTB = 0x30;
E = 1;
delay(1);
E = 0;

RS = 0;
RW = 0;
PORTB = 0x30;
E = 1;
delay(1);
E = 0;

RS = 0;
RW = 0;
PORTB = 0x30;
E = 1;
delay(1);
E = 0;

RS = 0;
RW = 0;
PORTB = 0x38;
E = 1;
delay(1);
E = 0;

RS = 0;
RW = 0;
PORTB = 0x08;
E = 1;
delay(1);
E = 0;

RS = 0;
RW = 0;
PORTB = 0x0C;
E = 1;
delay(1);
E = 0;

RS = 0;
RW = 0;
PORTB = 0x06;
E = 1;
delay(1);
E = 0;

RS = 0;
RW = 0;
PORTB = 0x01;
E = 1;
delay(1);
E = 0;
}
void creatchar(void)
{
RS=0;
RW=0;
PORTB = 0x40;
delay(1);
E=1;
delay(1);
E=0;
delay(1);
RS = 1;
RW = 0;

makechar(31,31,31,7,3,1,0,0); //0
delay(1);
makechar(28,24,24,24,28,30,31,31); //1
delay(1);
makechar(16,24,28,30,31,31,15,7);  //2
delay(1);
makechar(7,3,1,0,0,0,0,0);  //3
delay(1);
makechar(27,17,0,0,0,0,17,27);  //4
delay(1);
makechar(7,15,31,31,31,31,31,31);  //5
delay(1);
makechar(28,30,31,31,31,31,31,31);  //6
delay(1);
makechar(31,31,31,31,31,31,30,28);  //7
delay(1);

}
void makechar (int a, int b, int c,int d,int e,int f,int g,int h)
{
PORTB = a;
E=1;
delay(1);
E=0;
delay(1);
PORTB = b;
E=1;
delay(1);
E=0;
delay(1);
PORTB = c;
E=1;
delay(1);
E=0;
delay(1);
PORTB = d;
E=1;
delay(1);
E=0;
delay(1);
PORTB = e;
E=1;
delay(1);
E=0;
delay(1);
PORTB = f;
E=1;
delay(1);
E=0;
delay(1);
PORTB = g;
E=1;
delay(1);
E=0;
delay(1);
PORTB = h;
E=1;
delay(1);
E=0;
delay(1);
}

Last edited by a moderator:

#### spinnaker

Joined Oct 29, 2009
7,835
If it worked before then it should work now. I assume you changed from PortB to PorA (you did not mention that)?

And it worked on PortA before?

The first thing I see is that you are not defining your databits anywhere.

You define control

#define RS PORTAbits.RA3 #define RW PORTAbits.RA4 #define E PORTAbits.RA7

But not databits.

Where is that or what does write and lcdcmd do? Where is the code?

#### t06afre

Joined May 11, 2009
5,934
Here is something dodgy. You use a lot of the data type int. Then writing to the LCD. But the data type int is 16 bit wide. You should have used the data type char that is 8 byte wide. I would not have approved your assignment due to this. This may have worked now. But this way of coding will sooner or later get you in trouble. How you print your message to the LCD is also quite awkward. I would have stored this as a constant in in the ROM. Which compiler are you using?

#### ErnieM

Joined Apr 24, 2011
8,168
The lcdcmd(int x) function sends the data to PortB, and the port is hard coded. So no matter if an int or a char gets sent there the correct data should make it to the pins.

What I do not see is how the TRIS registers are set in lcd_setup(). Pins default to being inputs, and ain't gonna output nuthin' till told to do so.

Also the port pins of the device should be checked for default analog functions: most every device I have ever used needs the have the ADCON settings adjusted to turn off analog things at start-up.

#### spinnaker

Joined Oct 29, 2009
7,835
The lcdcmd(int x) function sends the data to PortB, and the port is hard coded. So no matter if an int or a char gets sent there the correct data should make it to the pins.

What I do not see is how the TRIS registers are set in lcd_setup(). Pins default to being inputs, and ain't gonna output nuthin' till told to do so.

Also the port pins of the device should be checked for default analog functions: most every device I have ever used needs the have the ADCON settings adjusted to turn off analog things at start-up.

Oh I see I did not scroll down enough, Too early in the morning for me.

TRIS is set here in main.

TRISA = 0; TRISB = 0;

#### ErnieM

Joined Apr 24, 2011
8,168
Oh I see I did not scroll down enough, Too early in the morning for me.

TRIS is set here in main.

TRISA = 0; TRISB = 0;
Ah, and I had MATCH CASE checked when I searched for "tris" using Programmer's Notepad.

(With Programmer's Notepad you can CnP the code then select C/C++ and get keyword highlighting.)

#### Rxreed

Joined Oct 27, 2012
2
Morning all,
I am using a PIC18f1320 coding with MPLAB and using a PICKIT3 to program. This is the only thing that I have changed from my original code to my new code(below). I was looking at the data sheet and found that RA5(not used) is only an input but RA6 and RA7 are I/O. So I’m wondering why this will not work.

OLD
#define RS PORTAbits.RA0
#define RW PORTAbits.RA1
#define E PORTAbits.RA2
NEW
#define RS PORTAbits.RA3
#define RW PORTAbits.RA4
#define E PORTAbits.RA7

(Thanks for the tips I will make changes to char from int.)

Last edited:

#### tshuck

Joined Oct 18, 2012
3,534
Here is something dodgy. You use a lot of the data type int. Then writing to the LCD. But the data type int is 16 bit wide. You should have used the data type char that is 8 byte wide. I would not have approved your assignment due to this. This may have worked now. But this way of coding will sooner or later get you in trouble. How you print your message to the LCD is also quite awkward. I would have stored this as a constant in in the ROM. Which compiler are you using?
It looks like Microchip's C18 compiler

#### spinnaker

Joined Oct 29, 2009
7,835
Morning all,
I am using a PIC18f1320 coding with MPLAB and using a PICKIT3 to program. This is the only thing that I have changed from my original code to my new code(below). I was looking at the data sheet and found that RA5(not used) is only an input but RA6 and RA7 are I/O. So Im wondering why this will not work.

OLD
#define RS PORTAbits.RA0
#define RW PORTAbits.RA1
#define E PORTAbits.RA2
NEW
#define RS PORTAbits.RA3
#define RW PORTAbits.RA4
#define E PORTAbits.RA7

(Thanks for the tips I will make changes to char from int.)

Do you have a scope, logic probe or a multi meter?

Step through you code line by line and make sure the ports that you think are being changed, really are being changed at the LCD.

#### ErnieM

Joined Apr 24, 2011
8,168
Look at your bflag() function and ask yourself what THIS line does:

while(PORTBbits.RB7 == 1);