Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC 16F877A Push Button and LCD

Status
Not open for further replies.
Joined
Apr 8, 2012
Messages
201
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Activity points
0
Hello!
My Circuit Consists of a PIC16F877A. Pins RB0, RB1, RB2, RB3, RB4 is configured as active high inputs. Port D0 is configured as output and is connected to a led. Port C (pins C0 to C6) is connected to lcd. I need mikroBasic or mikroC code which uses polling to handle the button clicks. The program should display some text (on lcd) depending upon the buttons (connected to port B) clicked.
 

You should connect 8 Data lines and 3 control lines(used as output Port) to interface the LCD(assuming it is an Alphanumeric Character type LCD). So I am using Port C as data lines and Port E as control lines.

I have attached a sample code in MPLAB sim. Probably should work with mikroBasic or mikroC also.
 

Attachments

  • sample.txt
    2.8 KB · Views: 207

You should connect 8 Data lines and 3 control lines(used as output Port) to interface the LCD(assuming it is an Alphanumeric Character type LCD). So I am using Port C as data lines and Port E as control lines.

I have attached a sample code in MPLAB sim. Probably should work with mikroBasic or mikroC also.

I am using 4-bit data transfer mode for lcd.

---------- Post added at 10:22 ---------- Previous post was at 10:14 ----------

The following code is not working in mikroBasic. The PB1 turns ON the LED Connected to PORTD.0 but the LED turns OFF after 2-3 secs. The 4-bit LCD simulated in proteus is working fine.

Pressing PB1 should turn on the led connected to port D.0 and Pressing PB2 Should turn OFF the LED.


Code Basic4GL - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
main:
 
     ADCON1 = 7              ' All pins digital.
     CMCON = 7               ' All comparator Off.
     TRISB = %11111111       ' PINS RB0-RB7 Configured as Inputs.
     TRISC = %00000000       ' PORTC Configured as Output.
     TRISD = %00000000       ' PORTD Configured as Outputs.
     PORTB = 0               ' PORTB initialized to zero.
     PORTC = 0               ' PORTC initialized to zero.
     PORTD = 0               ' PORTD initialized to zero.
 
     Lcd_Config(PORTC,6,5,4,3,PORTC,0,1,2)
     Lcd_Cmd(LCD_CURSOR_OFF)
 
     while(1)
 
        if(PORTB = %00000001) then
            delay_ms(10)
            if(PORTB = %00000001) then
                  PORTD = %00000001
                  Lcd_Cmd(LCD_CLEAR)
                  Lcd_Out(1,1, "LED ON")
            end if
        end if
 
        if(PORTB = %00000010) then
            delay_ms(10)
            if(PORTB = %00000010) then
               PORTD = %00000000
                  Lcd_Cmd(LCD_CLEAR)
                  Lcd_Out(1,1, "LED OFF")
            end if
        end if



wend

end.
 
Last edited by a moderator:

At what voltage are you operating your controller? What is the current consumption of your LED?

It looks like the code is fine. Give the above details. Try using the reverse logic to glow the LED. Connect the +ve terminal of LED to +Vcc and connect the -ve terminal to D.0 through a current limiting resistor. Send 1 to turn off LED and 0 to turn on.
 

You should connect 8 Data lines and 3 control lines(used as output Port) to interface the LCD(assuming it is an Alphanumeric Character type LCD). So I am using Port C as data lines and Port E as control lines.

I have attached a sample code in MPLAB sim. Probably should work with mikroBasic or mikroC also.

The led is working properly, the problem with the below code is that after converting to 8-bit data and 3-bit control type lcd code the 2nd and 4th line of the display is not displaying

Code:
[syntax = basic]

main:
     ADCON1 = 7              ' All pins digital.
     CMCON = 7               ' All comparator Off.
     TRISB = %11111111
     TRISC = %00000000
     TRISD = %00000000
     PORTB = 0
     PORTC = 0
     PORTD = 0
     
     Lcd8_Config(PORTE, PORTC, 0,1,2, 7,6,5,4,3,2,1,0)
     Lcd8_Cmd(LCD_CURSOR_OFF)
     Lcd8_Cmd(LCD_CLEAR)

     while(1)

        if (PORTB = %00000001) then
            delay_ms(10)
            if (PORTB = %00000001) then
                  PORTD =  %00000001
                  Lcd8_Cmd(LCD_CLEAR)
                  Lcd8_Out(1,1, "LED ON")
                  Lcd8_Out(2,1, "LED ON")
                  Lcd8_Out(3,1, "LED ON")
                  Lcd8_Out(4,1, "LED ON")
            end if
        end if

        if (PORTB = %00000010) then
            delay_ms(10)
            if (PORTB = %00000010) then
                  PORTD = %00000000
                  Lcd8_Cmd(LCD_CLEAR)
                  Lcd8_Out(1,1, "LED OFF")
                  Lcd8_Out(2,1, "LED OFF")
                  Lcd8_Out(3,1, "LED OFF")
                  Lcd8_Out(4,1, "LED OFF")


            end if
        end if
        
                      
    wend

end.

[/syntax]
 

You should check your Initialization code of your display. Or may be the address you are sending for 2nd and 4th line may be wrong. Try different address and check for the correct one.
 

You should check your Initialization code of your display. Or may be the address you are sending for 2nd and 4th line may be wrong. Try different address and check for the correct one.

I am using mikroBasic but know both mikroC and mikroBasic. Tell me how to use a different address in mikroC or mikroBasic.

---------- Post added at 13:24 ---------- Previous post was at 13:23 ----------

At what voltage are you operating your controller? What is the current consumption of your LED?

It looks like the code is fine. Give the above details. Try using the reverse logic to glow the LED. Connect the +ve terminal of LED to +Vcc and connect the -ve terminal to D.0 through a current limiting resistor. Send 1 to turn off LED and 0 to turn on.

I am operating th controller @ 5V. The led current is 10mA.
 

I have attached a sample code of the LCD interface with 4 data lines. It uses PORTD and PORTE for interface. Change the IO to your requirement.

For other two lines, try using the following as the starting address of the lines. You should first send the address of the location you want to display.
0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0.

Any two of the above should work for the other two lines.
 

Attachments

  • lcd_disp.txt
    2.6 KB · Views: 96

I have attached a sample code of the LCD interface with 4 data lines. It uses PORTD and PORTE for interface. Change the IO to your requirement.

For other two lines, try using the following as the starting address of the lines. You should first send the address of the location you want to display.
0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0.

Any two of the above should work for the other two lines.

Can you please give the mikroBasic or mikroC code for the above.
 

I have never worked in mikroBasic or mikroC. I have only worked in MPLAB Sim. So I cant help it.

Where did you get Lcd8_Cmd() and Lcd8_Out() ? From the code that you have attached, I believe the first parameter decides the Line of the Display. Can you give the original code?

Actually you should send the address before you send any data to LCD. Then you should send the ASCII Value of the data to be displayed. It will be in the below way:

If you want display "ABCDEF" from the 5th Position of the first line, your code should be (from the sample code that I have previously attached):

lcdcmd (0x84); // Send the Address
for (i=0x41; i<0x47; i++)
lcddat (i); // Send the ASCII Value of the data to be displayed

If only 2nd and 4th lines are not displaying, there are only two possibilities. One is that you are sending wrong address. Other is that the display itself is faulty. Debug your code properly with the above data. You should be able solve the problem.
 

Where did you get Lcd8_Cmd() and Lcd8_Out() ? ..... Can you give the original code?

internetuser2k11 is using the MikroC Library, both of the above routines you mentioned belong to this library. Unfortunately, MikroE does not make available the source code for any of its library routines.

BigDog
 

I have never worked in mikroBasic or mikroC. I have only worked in MPLAB Sim. So I cant help it.

Where did you get Lcd8_Cmd() and Lcd8_Out() ? From the code that you have attached, I believe the first parameter decides the Line of the Display. Can you give the original code?

Actually you should send the address before you send any data to LCD. Then you should send the ASCII Value of the data to be displayed. It will be in the below way:

If you want display "ABCDEF" from the 5th Position of the first line, your code should be (from the sample code that I have previously attached):

lcdcmd (0x84); // Send the Address
for (i=0x41; i<0x47; i++)
lcddat (i); // Send the ASCII Value of the data to be displayed

If only 2nd and 4th lines are not displaying, there are only two possibilities. One is that you are sending wrong address. Other is that the display itself is faulty. Debug your code properly with the above data. You should be able solve the problem.

I am getting 1st line and 3rd line display

---------- Post added at 08:53 ---------- Previous post was at 08:44 ----------

I have never worked in mikroBasic or mikroC. I have only worked in MPLAB Sim. So I cant help it.

Where did you get Lcd8_Cmd() and Lcd8_Out() ? From the code that you have attached, I believe the first parameter decides the Line of the Display. Can you give the original code?

Actually you should send the address before you send any data to LCD. Then you should send the ASCII Value of the data to be displayed. It will be in the below way:

If you want display "ABCDEF" from the 5th Position of the first line, your code should be (from the sample code that I have previously attached):

lcdcmd (0x84); // Send the Address
for (i=0x41; i<0x47; i++)
lcddat (i); // Send the ASCII Value of the data to be displayed

If only 2nd and 4th lines are not displaying, there are only two possibilities. One is that you are sending wrong address. Other is that the display itself is faulty. Debug your code properly with the above data. You should be able solve the problem.

I am getting the 1st and 3rd line of display. and if RESET button is pressed, the display displays all the 4 lines.(20x4 LCD) Lcd8_Cmd() AND Lcd8_Out() are mikroBasic routines for 8-bit LCD. and Lcd_Cmd() and Lcd_Out() are for 4-bit LCD.

somebody here has the same problem https://www.picbasic.co.uk/forum/showthread.php?t=10635
but I don't no how to Initialize flags in mikroBasic.
Here is the code.

Code:
[SYNTAX = MIKROBASIC]

'Microcontroller:            PIC16F877A
'Clock Frequency:            8Mhz

main:
     ADCON1 = 7              ' All pins digital.
     CMCON = 7               ' All comparator Off.
     TRISB = %00011111
     TRISC = %00000000
     TRISD = %00000000
     TRISE = %000
     PORTB = 0
     PORTC = 0
     PORTD = 0
     PORTE = 0
     
     Lcd8_Config(PORTE, PORTC, 0,1,2, 7,6,5,4,3,2,1,0)
     Lcd8_Cmd(LCD_CURSOR_OFF)
     Lcd8_Cmd(LCD_CLEAR)
     Lcd8_Out(1,1, "LCD TEST")
     Lcd8_Out(2,1, "LCD TEST")
     Lcd8_Out(3,1, "LCD TEST")
     Lcd8_Out(4,1, "LCD TEST")
                   
     while(1)

        select case PORTB
           
           case %00000001
                delay_ms(10)
                if (PORTB = %00000001) and (PORTD.0 = 0) then
                   PORTD.0 =  1
                   Lcd8_Cmd(LCD_CLEAR)
                   Lcd8_Out(1,1, "RELAY ON")
                   Lcd8_Out(2,1, "RELAY ON")
                   Lcd8_Out(3,1, "RELAY ON")
                   Lcd8_Out(4,1, "RELAY ON")
                end if
                
           case %00000010
                delay_ms(10)
                if (PORTB = %00000010) and (PORTD.0 = 1) then
                   PORTD.0 =  0
                   Lcd8_Cmd(LCD_CLEAR)
                   Lcd8_Out(1,1, "RELAY OFF")
                   Lcd8_Out(2,1, "RELAY OFF")
                   Lcd8_Out(3,1, "RELAY OFF")
                   Lcd8_Out(3,1, "RELAY OFF")
                end if

        end select

    wend

end.

[/SYNTAX]
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top