PIC16f877a 2x16 LCD + KEypad

Discussion in 'Embedded Systems and Microcontrollers' started by freigo, Jan 9, 2011.

  1. freigo

    Thread Starter New Member

    Jan 9, 2011
    2
    0
    PLEASE HELP ME!

    I doing a simple calculator for my project using a 2x16 Lcd keypad and button, i got my circuit from combination of circuits in http://www.mikroe.com tutorials

    and im using proteus to simulate it, Good thing is it works perfectly fine in simulation, BUT it Doesn't work on actual..it only displays BLOCK

    here my code and diagram, please check it out for some problem

    View attachment HOPE.zip

    i check all my connection and it seems right,
    i can't find the problem Please please help me

    Tnx
     
    Last edited: Jan 9, 2011
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,005

    Don't post a zip file. Just post the code in line with the code block.

    Be certain of your connection. Step through your code and verify eeach input on the LCD is being set as expected.

    Check your timing on sending data to the LCD. Especially after power up. I put a 50-75 ms delay after power up and before trying to do anything with the LCD.

    Check you "contrast" (v0 input) setting.
     
  3. freigo

    Thread Starter New Member

    Jan 9, 2011
    2
    0
    thanks for the reply. we've checked the connections and the code.. everything seems right because its doing fine with proteus

    anyway here's the diagram and the code.

    [​IMG]


    Code ( (Unknown Language)):
    1. program HOPE
    2.  
    3. dim text as char[20]
    4. dim kp, cnt, operator_sign as byte
    5. dim txt as string[5]
    6. dim number, number_temp, number_temp2, save_number, save_number2  as word
    7.  
    8.  
    9. main:
    10.   TRISB = 0
    11.   Keypad_Init(PORTD)                   ' PORTB is output
    12.   Lcd_Init(PORTB)             ' Initialize LCD on PORTB
    13.   Lcd_Cmd(Lcd_CURSOR_OFF)     ' Turn off cursor
    14.   Lcd_Cmd(LCD_CLEAR)
    15.   text = "-----COE 5A------"
    16.   Lcd_Out(1, 1, text)         ' Print text at LCD
    17.   delay_ms(5000)
    18.   Lcd_Cmd(LCD_CLEAR)
    19.  
    20.  
    21. while TRUE
    22.     kp = 0
    23.  
    24.     '--- Wait for key to be pressed
    25.     while kp = 0
    26.    '--- un-comment one of the keypad reading functions
    27.       kp = Keypad_Released
    28.  
    29.       if (Button(PORTC,7,1,1)) then ' If PORTB.7 is pressed Plus
    30.       kp = 16
    31.       end if
    32.  
    33.       if (Button(PORTC,6,1,1)) then ' If PORTB.7 is pressed Minus
    34.       kp = 17
    35.       end if
    36.  
    37.       if (Button(PORTC,5,1,1)) then ' If PORTB.7 is pressed multiply
    38.       kp = 18
    39.       end if
    40.  
    41.       if (Button(PORTC,4,1,1)) then ' If PORTB.7 is pressed divide
    42.       kp = 19
    43.       end if
    44.  
    45.       if (Button(PORTC,3,1,1)) then ' If PORTB.7 is pressed equals
    46.       kp = 20
    47.       end if
    48.  
    49.       if (Button(PORTC,2,1,1)) then ' If PORTB.7 is pressed equals
    50.       kp = 21
    51.       end if
    52.  
    53.       if (Button(PORTC,1,1,1)) then ' If PORTB.7 is pressed equals
    54.       kp = 22
    55.       end if
    56.  
    57.       if (Button(PORTC,0,1,1)) then ' If PORTB.7 is pressed equals
    58.       kp = 23
    59.       end if
    60.     wend
    61.  
    62.  
    63.  
    64.   select case kp     ' Prepare value for output
    65.     case 1 number = 1
    66.     case 2 number = 2
    67.     case 3 number = 3
    68.     case 5 number = 4
    69.     case 6 number = 5
    70.     case 7 number = 6
    71.     case 9 number = 7
    72.     case 10 number = 8
    73.     case 11 number = 9
    74.     case 13 save_number = number_temp
    75.             Lcd_Cmd(LCD_CLEAR)
    76.             cnt = 4
    77.     case 14 number = 0
    78.     case 15 WordToStr(save_number, txt)
    79.             number_temp = save_number
    80.             Lcd_Out(1, 12, txt)
    81.             cnt = 4
    82.     case 16 Lcd_Cmd(LCD_CLEAR)
    83.  
    84.             operator_sign = 1 '--------------sign operator
    85.  
    86.             Lcd_Out(2, 1, "+")
    87.             number_temp2 = number_temp
    88.             WordToStr(number_temp2, txt)
    89.             Lcd_Out(2, 12, txt)
    90.  
    91.  
    92.             cnt = 4
    93.     case 17 Lcd_Cmd(LCD_CLEAR)
    94.  
    95.             operator_sign = 2 '--------------sign operator
    96.  
    97.             Lcd_Out(2, 1, "-")
    98.             number_temp2 = number_temp
    99.             WordToStr(number_temp2, txt)
    100.             Lcd_Out(2, 12, txt)
    101.             cnt = 4
    102.     case 18 Lcd_Cmd(LCD_CLEAR)
    103.  
    104.             operator_sign = 3 '--------------sign operator
    105.  
    106.             Lcd_Out(2, 1, "*")
    107.             number_temp2 = number_temp
    108.             WordToStr(number_temp2, txt)
    109.             Lcd_Out(2, 12, txt)
    110.             cnt = 4
    111.     case 19 Lcd_Cmd(LCD_CLEAR)
    112.  
    113.             operator_sign = 4 '--------------sign operator
    114.  
    115.             Lcd_Out(2, 1, "/")
    116.             number_temp2 = number_temp
    117.             WordToStr(number_temp2, txt)
    118.             Lcd_Out(2, 12, txt)
    119.             cnt = 4
    120.     case 20
    121.             if operator_sign = 1 then
    122.                number_temp = number_temp2 + number_temp
    123.                number_temp2 = 0
    124.             end if
    125.  
    126.             if operator_sign = 2 then
    127.                number_temp = number_temp2 - number_temp
    128.                number_temp2 = 0
    129.             end if
    130.  
    131.             if operator_sign = 3 then
    132.                number_temp = number_temp2 * number_temp
    133.                number_temp2 = 0
    134.             end if
    135.  
    136.             if operator_sign = 4 then
    137.                number_temp = number_temp2 / number_temp
    138.                number_temp2 = 0
    139.             end if
    140.  
    141.             Lcd_Cmd(LCD_CLEAR)
    142.             WordToStr(number_temp, txt)
    143.             Lcd_Out(1, 12, txt)
    144.             operator_sign = 0
    145.             cnt = 4
    146.     case 21 save_number2 = number_temp
    147.             Lcd_Cmd(LCD_CLEAR)
    148.             cnt = 4
    149.  
    150.     case 22 WordToStr(save_number2, txt)
    151.             number_temp = save_number2
    152.             Lcd_Out(1, 12, txt)
    153.             cnt = 4
    154.     case 23 number_temp = 0
    155.             number_temp2 = 0
    156.             save_number = 0
    157.             save_number2 = 0
    158.             Lcd_Cmd(LCD_CLEAR)
    159.             cnt = 4
    160.  
    161.     end select
    162.  
    163.    inc(cnt)
    164.  
    165. select case cnt
    166.   case 1 number_temp = number
    167.          WordToStr(number_temp, txt)
    168.          Lcd_Out(1, 12, txt)
    169.  
    170.   case 2 number_temp = (number_temp * 10) + number
    171.          WordToStr(number_temp, txt)
    172.          Lcd_Out(1, 12, txt)
    173.  
    174.   case 3 number_temp =  (number_temp * 10) + number
    175.          WordToStr(number_temp, txt)
    176.          Lcd_Out(1, 12, txt)
    177.  
    178.   case 4 Lcd_Cmd(Lcd_Clear)
    179.          number_temp = number
    180.          WordToStr(number_temp, txt)
    181.          Lcd_Out(1, 12, txt)
    182.          cnt = 1
    183.   case 5 cnt = 0
    184.  
    185. end select
    186.  
    187.  
    188.   wend
    189.  
    190.  
    191.  
    192.  
    193. end.
     
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,005
    How did you check the connections? Did you just look at them? Or did you single step through your code and put a meter on each pin of the LCD to be sure it is setting what it should be setting.

    How do you know anything is even running?


    Did you put in a startup delay?

    Proteus is not the real world.



    Keep your code simple at first. Forget about all that other code that processes the key press. Just write some simple code to display "Hello World" to the LCD.

    I do not see an pot for your contrast. The voltage on VEE might be too high.


    What are you expecting that 10K to do near the LCD ?

    R2 is wired wrong. It should be just like the other switches.
     
  5. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Have you posted your questions on the mikroe forums?

    It may be an issue with the way the compiler interprets some lines.

    Otherwise, be sure you are using real models for all elements in Proteus, rather than "Ideal" switches, capacitors, resistors, etc.
     
Loading...