PIC18 PORTA Trouble

Discussion in 'Embedded Systems and Microcontrollers' started by Rxreed, Oct 27, 2012.

  1. Rxreed

    Thread Starter New Member

    Oct 27, 2012
    2
    0
    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

    Code ( (Unknown Language)):
    1.  
    2. #include<p18f1320.h>
    3. #include <stdio.h>
    4. #pragma config WDT = OFF
    5. #pragma config PWRT = ON
    6. #pragma config LVP = OFF
    7. #pragma config MCLRE = OFF
    8. #pragma config OSC = INTIO2
    9.  
    10. #define RS PORTAbits.RA3
    11. #define RW PORTAbits.RA4
    12. #define E PORTAbits.RA7
    13.  
    14.  
    15. void lcd_setup(void);
    16. void write(char message);
    17. void delay(int time);
    18. void bflag (void);
    19. void movlcd(int x);
    20. void lcdcmd(int x);
    21. void creatchar(void);
    22. void usechar(int x);
    23. void black_out(int x);
    24. void makechar (int a, int b, int c,int d,int e,int f,int g,int h);
    25.  
    26. void main(void)
    27. {
    28.     OSCCON = 0x70;
    29.     ADCON1 = 0x7F;
    30.     TRISA = 0;
    31.     TRISB = 0;
    32.     creatchar();
    33.     lcd_setup();
    34.     bflag();
    35.  
    36.     lcdcmd(0x80);
    37.     bflag ();
    38.     write('R');
    39.     bflag ();
    40.     write('y');
    41.     bflag ();
    42.     write('a');
    43.     bflag ();
    44.     write('n');
    45.     bflag ();
    46.  
    47.     lcdcmd(0xC0);
    48.     bflag ();
    49.     write('R');
    50.     bflag ();
    51.     write('e');
    52.     bflag ();
    53.     write('e');
    54.     bflag ();
    55.     write('d');
    56.     bflag ();
    57.  
    58.     lcdcmd(0x90);
    59.     bflag ();
    60.     write('I');
    61.     bflag ();
    62.     write('n');
    63.     bflag ();
    64.     write('d');
    65.     bflag ();
    66.     write('u');
    67.     bflag ();
    68.     write('s');
    69.     bflag ();
    70.     write('t');
    71.     bflag ();
    72.     write('r');
    73.     bflag ();
    74.     write('i');
    75.     bflag ();
    76.     write('a');
    77.     bflag ();
    78.     write('l');
    79.     bflag ();
    80.  
    81.     lcdcmd(0xD0);
    82.     bflag ();
    83.     write('C');
    84.     bflag ();
    85.     write('o');
    86.     bflag ();
    87.     write('n');
    88.     bflag ();
    89.     write('t');
    90.     bflag ();
    91.     write('r');
    92.     bflag ();
    93.     write('o');
    94.     bflag ();
    95.     write('l');
    96.     bflag ();
    97.     write('s');
    98.     bflag ();
    99.     write(' ');
    100.     bflag ();
    101.     write('4');
    102.     bflag ();
    103.     write('3');
    104.     bflag ();
    105.     write('0');
    106.     bflag ();
    107.     write('4');
    108.     bflag ();
    109.  
    110.     delay(6000);
    111.     lcdcmd(0x01);   //clears LCD
    112.     bflag ();
    113.  
    114.    black_out(85);
    115.    black_out(21);
    116.    black_out(69);
    117.    black_out(6);
    118.    black_out(71);
    119.  
    120.    lcdcmd(0x96);  //0
    121.    write(0x00);
    122.  
    123.    lcdcmd(0x97);  //1
    124.    bflag ();
    125.    write(0x01);
    126.  
    127.    lcdcmd(0xD8);  //2
    128.    bflag ();
    129.    write(0x02);
    130.  
    131.    lcdcmd(0xD7);  //3
    132.    bflag ();
    133.    write(0x03);
    134.  
    135.    lcdcmd(0xC6);  //4
    136.    bflag ();
    137.    write(0x04);
    138.  
    139.    lcdcmd(0x85);  //5
    140.    bflag ();
    141.    write(0x05);
    142.  
    143.    lcdcmd(0x87);  //6
    144.    bflag ();
    145.    write(0x06);
    146.  
    147.    lcdcmd(0xC7);  //7
    148.    bflag ();
    149.    write(0x07);
    150.  
    151.     while(1);
    152. }
    153. void black_out(int x)
    154. {
    155.     x = 128 + x;
    156.     lcdcmd(x);
    157.     bflag ();
    158.     write(0xFF);
    159.     bflag();
    160. }
    161. void lcdcmd(int x)
    162. {
    163.     RS = 0;
    164.     RW = 0;
    165.     PORTB = x;
    166.     E=1;
    167.     delay(1);
    168.     E=0;
    169. }
    170. void write(char message)
    171. {
    172.     RS=1;
    173.     RW=0;
    174.     PORTB = message;
    175.     E=1;
    176.     delay(1);
    177.     E=0;
    178.     }
    179. void delay( int time)
    180. {
    181.    unsigned int i,j;
    182.     for (i=0; i<time; i++)
    183.         for(j=0;j<165; j++)
    184.             ;
    185. }
    186. void bflag (void)
    187. {
    188.     TRISB = 0xFF;
    189.     RW = 1;
    190.     RS = 0;
    191.     do
    192.         {
    193.         E=1;
    194.         delay(1);
    195.         E=0;
    196.         }
    197.     while(PORTBbits.RB7 == 1);
    198.     TRISB = 0;
    199. }
    200. void lcd_setup(void)
    201. {
    202.     RS = 0;
    203.     RW = 0;
    204.     PORTB = 0x30;
    205.     E = 1;
    206.     delay(1);
    207.     E = 0;
    208.  
    209.     RS = 0;
    210.     RW = 0;
    211.     PORTB = 0x30;
    212.     E = 1;
    213.     delay(1);
    214.     E = 0;
    215.  
    216.     RS = 0;
    217.     RW = 0;
    218.     PORTB = 0x30;
    219.     E = 1;
    220.     delay(1);
    221.     E = 0;
    222.  
    223.     RS = 0;
    224.     RW = 0;
    225.     PORTB = 0x38;
    226.     E = 1;
    227.     delay(1);
    228.     E = 0;
    229.  
    230.     RS = 0;
    231.     RW = 0;
    232.     PORTB = 0x08;
    233.     E = 1;
    234.     delay(1);
    235.     E = 0;
    236.  
    237.     RS = 0;
    238.     RW = 0;
    239.     PORTB = 0x0C;
    240.     E = 1;
    241.     delay(1);
    242.     E = 0;
    243.  
    244.     RS = 0;
    245.     RW = 0;
    246.     PORTB = 0x06;
    247.     E = 1;
    248.     delay(1);
    249.     E = 0;
    250.  
    251.     RS = 0;
    252.     RW = 0;
    253.     PORTB = 0x01;
    254.     E = 1;
    255.     delay(1);
    256.     E = 0;
    257. }
    258. void creatchar(void)
    259. {
    260.     RS=0;
    261.     RW=0;
    262.     PORTB = 0x40;
    263.     delay(1);
    264.     E=1;
    265.     delay(1);
    266.     E=0;
    267.     delay(1);
    268.     RS = 1;
    269.     RW = 0;
    270.  
    271.     makechar(31,31,31,7,3,1,0,0); //0
    272.     delay(1);
    273.     makechar(28,24,24,24,28,30,31,31); //1
    274.     delay(1);
    275.     makechar(16,24,28,30,31,31,15,7);  //2
    276.     delay(1);
    277.     makechar(7,3,1,0,0,0,0,0);  //3
    278.     delay(1);
    279.     makechar(27,17,0,0,0,0,17,27);  //4
    280.     delay(1);
    281.     makechar(7,15,31,31,31,31,31,31);  //5
    282.     delay(1);
    283.     makechar(28,30,31,31,31,31,31,31);  //6
    284.     delay(1);
    285.     makechar(31,31,31,31,31,31,30,28);  //7
    286.     delay(1);
    287.  
    288. }
    289. void makechar (int a, int b, int c,int d,int e,int f,int g,int h)
    290. {
    291.     PORTB = a;
    292.     E=1;
    293.     delay(1);
    294.     E=0;
    295.     delay(1);
    296.     PORTB = b;
    297.     E=1;
    298.     delay(1);
    299.     E=0;
    300.     delay(1);
    301.     PORTB = c;
    302.     E=1;
    303.     delay(1);
    304.     E=0;
    305.     delay(1);
    306.     PORTB = d;
    307.     E=1;
    308.     delay(1);
    309.     E=0;
    310.     delay(1);
    311.     PORTB = e;
    312.     E=1;
    313.     delay(1);
    314.     E=0;
    315.     delay(1);
    316.     PORTB = f;
    317.     E=1;
    318.     delay(1);
    319.     E=0;
    320.     delay(1);
    321.     PORTB = g;
    322.     E=1;
    323.     delay(1);
    324.     E=0;
    325.     delay(1);
    326.     PORTB = h;
    327.     E=1;
    328.     delay(1);
    329.     E=0;
    330.     delay(1);
    331. }
    332.  
     
    Last edited by a moderator: Oct 27, 2012
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    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?
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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?
     
    Rxreed likes this.
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991

    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;
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.)
     
  7. Rxreed

    Thread Starter New Member

    Oct 27, 2012
    2
    0
    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: Oct 27, 2012
  8. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    It looks like Microchip's C18 compiler
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991

    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.
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Look at your bflag() function and ask yourself what THIS line does:

    while(PORTBbits.RB7 == 1);
     
Loading...