Menu

all about electronic and microcontrollers

Tuesday, November 30, 2010

Lesson nr.17-Alphanumeric LCD and Push Button

Hardware setup:
In this experiment we will work with alphanumeric LCD and push button.
Communication with LCD will be performed through 4-bits and connections is made as follows: D4 with RB0, D5 with RB1, D6 with RB2, D7 with RB3, RS with RB4 and EN with RB5. The Push button is connected to PORT RA4 (for increment) and PORT RA6 (for decrement). Of course both button have pull-up resistors (4k7).
To make this project more interesting , you can reach from 0000 to 9999 by pressing the button.



Circuit Diagram:
For those who want to build it on their own breadboard or other platform, here is the electronic scheme built in Eagle Cad, free version:


Software:
Here is the C program written for MikroC PRO for PIC 2010 (version v4.15).
/*
'*******************************************************************************
'  Lesson nr.17:
'          Alphanumeric LCD and Push Button.
'  Done by:
'          Aureliu Raducu Macovei, 2010.
'  Description:
'          In this experiment we will work with alphanumeric LCD and push button.
'          Communication with LCD will be performed through 4-bits and connections
'          is made as follows: D4 with RB0, D5 with RB1, D6 with RB2, D7 with RB3;
'                              RS with RB4 and EN with RB5.
'          The Push button is connected to PORT RA4 (for increment) and PORT RA6
'          (for decrement). Of course both button have pull-up resistors (4k7).
'          To make this project more interesting , you can reach from 0000 to 9999 
'          by pressing the button.
'  Test configuration:
'    MCU:                        PIC16F628A
'    Test.Board:                 WB-106 Breadboard 2420 dots
'    SW:                         MikroC PRO for PIC 2010 (version v4.15)
'  Configuration Word
'    Oscillator:                 INTOSC:I/O on RA.6, I/O on RA.7
'    Watchdog Timer:             OFF
'    Power up Timer:             Disabled
'    Master Clear Enable:        Enabled
'    Browun Out Detect:          Enabled
'    Low Voltage Program:        Disabled
'    Data EE Read Protect:       Disabled
'    Code Protect:               OFF
'*******************************************************************************
*/
// LCD module connections
sbit LCD_RS at RB4_bit;                 // LCD_RS assigned to PORT RB4;
sbit LCD_EN at RB5_bit;                 // LCD_EN assigned to PORT RB5;
sbit LCD_D4 at RB0_bit;                 // LCD_D4 assigned to PORT RB0;
sbit LCD_D5 at RB1_bit;                 // LCD_D5 assigned to PORT RB1;
sbit LCD_D6 at RB2_bit;                 // LCD_D6 assigned to PORT RB2;
sbit LCD_D7 at RB3_bit;                 // LCD_D7 assigned to PORT RB3;

sbit LCD_RS_Direction at TRISB4_bit;    // LCD_RS assigned to TRIS B4;
sbit LCD_EN_Direction at TRISB5_bit;    // LCD_EN assigned to TRIS B5;
sbit LCD_D4_Direction at TRISB0_bit;    // LCD_D4 assigned to TRIS B0;
sbit LCD_D5_Direction at TRISB1_bit;    // LCD_D5 assigned to TRIS B1;
sbit LCD_D6_Direction at TRISB2_bit;    // LCD_D6 assigned to TRIS B2;
sbit LCD_D7_Direction at TRISB3_bit;    // LCD_D7 assigned to TRIS B3;
// End LCD module connections

char Message1[]="LCD and Button";       // Message for line1;
unsigned int number = 0;

char *digit = "0000";

void Display_init()   // define display_init;
{                    
 digit[0] = number/1000 + 48;           // thousands digit;
 digit[1] = (number/100)%10 +48;        // hundreds digit;
 digit[2] = (number/10)%10 + 48;        // tens digit;
 digit[3] = number%10 +48;              // unit digit;
 Lcd_Out(2,7,digit);                    // display on LCD from column 2, character 7;
}

void main()                             // main;
{
 CMCON |= 7;                            // turn off analogue comparator and make PORTA to digital I/O;
 TRISA6_bit = 1;                        // make PORT RA6 as input for button;
 TRISA7_bit = 1;                        // make PORT RA7 as input for button;
 PORTA = 0;                             // Turn ON PORTA;
 TRISB = 0;                             // Set PORTB direction to be output;
 PORTB = 0;                             // Turn ON PORTB;

 Lcd_init();                            // LCD Initialization;
 Lcd_cmd(_LCD_CLEAR);                   // Clear LCD;
 Lcd_cmd(_LCD_CURSOR_OFF);              // Cursor mode, off;
 Lcd_out(1,2,Message1);                 // display message1 from column 1, character 3;

 do{
    if(Button(&PORTA,4,1,0)){
                             Delay_ms(200);    // If button is pressed, delay 0,2s and increment "number" with 1;
                             number = number +1;
                             }
    if(Button(&PORTA,6,1,0)){
                             Delay_ms(200);    // If button is pressed, delay 0,2s and decrement "number" with 1;
                             number = number -1;
                             }
    if (number > 9999u)                 // if it's more than 9999 go to 0;
    number = 0;
    display_init();                     // call display_init();
    } while(1);                         // infinite loop;
 }                                      // end.

16 comments:

  1. Excellent!
    Now how do u do to get generate alphabetical?

    ReplyDelete
  2. Simple, if you follow ascii chart, you can see how the number 48 in ascii, signify the number "0", change it to use letters with "65" and will starts from A, B. .. and so on. Ascii chart, here.

    More specifically change in code, these line:
    digit[0] = number/1000 + 48;
    digit[1] = (number/100)%10 +48;
    digit[2] = (number/10)%10 + 48;
    digit[3] = number%10 +48;

    with these:

    digit[0] = number/1000 + 65;
    digit[1] = (number/100)%10 +65;
    digit[2] = (number/10)%10 + 65;
    digit[3] = number%10 +65;

    Now, you must test these changes and see what it's hapening.

    ReplyDelete
  3. Many thanks again. Just to let u know that i used same circuit as u did on Lesson nr.19-Using Internal EEPROM Memory. So i changed this code sbit LCD_RS at RB4_bit to sbit LCD_RS at RA0_bit. The problem is my pic16F628A-i/p-20Mhz. Can i used 20Mhz osc? If i used 4Mhz osc, when the counter reached to 65, it slowlu, stopped and freezing and then i waited for 2 or more minutes to pressed button again and the counter counted slowly and then rapidly faster and so on.

    AFAIK, i was looking at hd44780 cgrom. I was looking at 4-bit lower and 4-bit higher. I was dumbed fool I didn't convert from binary to hex.

    But right now, I'm using 20*4 lcd all times. I am learning into mikroC langauges as i din with mikroBasic languages.

    Keep up good work.

    ReplyDelete
  4. My mistaken. I wired my owned circuit. Like this...

    sbit LCD_RS at RA0_bit;
    sbit LCD_EN at RA1_bit;
    sbit LCD_D4 at RB4_bit;
    sbit LCD_D5 at RB5_bit;
    sbit LCD_D6 at RB6_bit;
    sbit LCD_D7 at RB7_bit;

    ReplyDelete
  5. As ur commented stating that The Push button is connected to PORT RA4 (for increment) and PORT RA6
    (for decrement). and
    TRISA6_bit = 1;
    TRISA7_bit = 1;

    It should be:
    TRISA4_bit = 1;
    TRISA6_bit = 1;

    Am I right?

    ReplyDelete
  6. U seem to know your way around this very well. Perhaps I could get you to help me with a little problem regarding making a pic16f628a run a alphanumeric display. I have to show a message along with a value of an encoder - well actually two, if possible. Please send me an e-mail, if you would like to give along a little of your knowledge. mailto:pic16f628a@vw-madsen.dk

    Best regards
    Torben Madsen

    ReplyDelete
  7. Good observations "supra", I am glad to see that my work helps someone else.

    I'll try to get in touch with you, Torben Madsen, depending on how I can find some free time.

    Kind Regards,
    ducu

    ReplyDelete
  8. hello. can i have an idea of counter timer?
    for example: i want to display how many push button were pushed in 3 seconds.
    arr!

    ReplyDelete
  9. Hey, could you post the HEX file?

    ReplyDelete
  10. Thanks a lot. it is work fine. can you help me how possible save count value on eeprom? please

    ReplyDelete
    Replies
    1. Hi Nurul, plese study this: http://electronicexperiments.blogspot.it/2010/12/lesson-nr19-using-internal-eeprom.html
      regards.

      Delete
  11. sir would it be possible if there's a relay to trigger when the button is press and also
    stop at predefined value, let say a 100.

    ReplyDelete
  12. Nurul and MECS both asked the question what i wanted to ask. But as i see the admin stopped responding to this post. I'd have to give a try to find out another blog explain something like this.

    ReplyDelete
    Replies
    1. Hi Rajpal, i'm not stopping answering. Now i'm stuck with other tasks and i have limited access to my resources. About my e-mail...: aureliu.macovei@yahoo.com.
      I will try to give you all my support.
      Regards.

      Delete

If you do not understand something, or if you make some aplication helped by this blog, let me know.