Please help me with checking the busy flag of a lcd in 4 bit mode

Discussion in 'Embedded Systems and Microcontrollers' started by aakashjsr, Apr 16, 2014.

  1. aakashjsr

    Thread Starter New Member

    Apr 16, 2014
    1
    0
    I am able to execute commands on my lcd when I use delay instead of checking the busy flag but when I check the busy flag,it appears as if an infinite loop has started.Here's the connections for my 4 bit Lcd with the AVR Atmega 16.Thanks a lot in advance :)
    Code ( (Unknown Language)):
    1.  
    2. ************************************************** ***************************
    3. PB0-> RS ; PB1->ENABLE ; PB2->R/W ; PB3->NOT CONNECTED
    4. PB4-PB7----->DB4-DB7
    5.  
    6. ************************************************** *************************** Code:
    7. ************************************************** ***************************
    8.  
    9. #define data PORTB
    10. #define e PB1
    11. #define rs PB0
    12. #define rw PB2
    13.  
    14. void lcd_ready() //checks for busy flag
    15. {
    16.  
    17. DDRB&=0b00001111; //data lines as read
    18. int flag=0;
    19.  
    20. cbi(PORTB,rs);
    21. sbi(PORTB,rw);
    22.  
    23. do
    24. {
    25. pos_pulse();
    26. _delay_us(10);
    27. flag=PINB;
    28. flag=(flag&0x80); //to store the value of busy flag
    29. pos_pulse(); //discard lower nibble
    30. _delay_us(10);
    31. }while(flag);
    32. DDRB=0xff; //resetting data lines as output
    33. }
    34.  
    35. void pos_pulse()
    36. {  
    37. cbi(PORTB,e); // eable=0
    38. _delay_us(2);
    39. sbi(PORTB,e) ; // eable=1
    40. _delay_us(2);
    41. }
    42.  
    43. void neg_pulse()
    44. {
    45. sbi(PORTB,e); // eable=1
    46. _delay_us(2);
    47. cbi(PORTB,e) ; // eable=0
    48. _delay_us(2);
    49. }
    50.  
    51. void command(int a) // to receive and send command to LCD
    52. {
    53.  
    54. //higher nibble
    55.  
    56. lcd_ready();
    57.  
    58. data=(a&0xf0);
    59. cbi(PORTB,rs);
    60. cbi(PORTB,rw);
    61. neg_pulse();
    62.  
    63. //Lower Nibble
    64.  
    65. lcd_ready();
    66.  
    67. data=((a<<4)&0xf0);
    68. cbi(PORTB,rs);
    69. cbi(PORTB,rw);
    70. neg_pulse();
    71. }
    72.  
    73.  
    74.  
    75. void value(int a) //To send Data to the Lcd
    76. {
    77.  
    78. //higher nibble
    79.  
    80. lcd_ready();
    81.  
    82. data=(a&0xf0);
    83. sbi(PORTB,rs);
    84. cbi(PORTB,rw);
    85. neg_pulse();
    86.  
    87. //Lower Nibble
    88.  
    89. lcd_ready();
    90.  
    91. data=((a<<4)&0xf0);
    92. sbi(PORTB,rs);
    93. cbi(PORTB,rw);
    94. neg_pulse();
    95.  
    96.  
    97. }
    98.  
     
    Last edited by a moderator: Apr 16, 2014
  2. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,362
    When posting code please use code tags:

    [code=rich]
    Your code goes here
    [/code]
     
    djsfantasi likes this.
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,605
    The statement:

    while(flag);

    has no ability to refresh the value of flag once set in a pervious statement.
     
  4. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    I think you missed the
    do {
    statement before it Ernie.
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,605
    Ah, so I did.

    Never mind.
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,605
    Does the Atmega require the direction of port pins to be either in or out?

    And what is that PINB for?
     
Loading...