additions for lcd code

Discussion in 'Embedded Systems and Microcontrollers' started by foxbrain, Jul 6, 2011.

  1. foxbrain

    Thread Starter New Member

    Jun 27, 2011
    1
    0
    i have this code that is used to program lcd 16X2 using atmega16A:

    Code ( (Unknown Language)):
    1.  
    2.  
    3.  
    4.  
    5. /*----------------------------------------------------------------  
    6. -----------------HEADER FILES-------------------------------------  
    7. -----------------------------------------------------------------*/  
    8. #include<avr/io.h>  
    9. #include <util/delay.h>
    10.  
    11.  
    12. #define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
    13. #define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
    14. #define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
    15.  
    16. /*----------------------------------------------------------------  
    17. -------------CONNECTION BETWEEN LCD AND ATMEGA32-----------------  
    18. -----------------------------------------------------------------*/  
    19. #define DATA_DDR    DDRB  
    20. #define DATA_PORT     PORTB  
    21.  
    22.  
    23. #define CONTROL_DDR        DDRA
    24. #define CONTROL_PORT     PORTA
    25. #define Enable_Pin        2
    26. #define RegSelect_Pin     0  
    27. #define ReadWrite_Pin     1
    28. #define CONTROL_MASK     0X07  
    29.  
    30.  
    31. /*----------------------------------------------------------------  
    32. -------------CONTROL BITS OF LCD --------------------------------  
    33. -----------------------------------------------------------------*/  
    34.  
    35. #define Set_Enable              CONTROL_PORT|=_BV(Enable_Pin)  
    36. #define Clear_Enable             CONTROL_PORT&=~_BV(Enable_Pin)  
    37. #define Write_Lcd                CONTROL_PORT&=~_BV(ReadWrite_Pin)  
    38. #define Read_Lcd                  CONTROL_PORT|=_BV(ReadWrite_Pin)  
    39. #define Select_InstructionRegister    CONTROL_PORT&=~_BV(RegSelect_Pin)  
    40. #define Select_DataRegister          CONTROL_PORT|=_BV(RegSelect_Pin)  
    41. #define Data_Lcd(a)               DATA_PORT=a  
    42. #define delay(a)               _delay_ms(a)  
    43.  
    44.  
    45.  
    46.  
    47. /*----------------------------------------------------------------  
    48. -----------------SEND A CHARACTER TO LCD-------------------------  
    49. -----------------------------------------------------------------*/  
    50. void Lcd_Send(unsigned char a)  
    51. {  
    52. Select_DataRegister;  
    53. Write_Lcd;  
    54. Data_Lcd(a);  
    55. Set_Enable;  
    56. delay(30);  
    57. Clear_Enable;  
    58. delay(30);  
    59. }  
    60.  
    61.      
    62.      
    63.  
    64.  
    65.  
    66. /*----------------------------------------------------------------  
    67. -----------------FUNCTIONS TO INITIALIZE PORTS--------------------  
    68. -----------------------------------------------------------------*/  
    69. void Init_Ports(void)  
    70. {  
    71. DATA_DDR=0XFF; //Setting data port for output  
    72. CONTROL_DDR=CONTROL_MASK;//setting selected pins of control port for output  
    73. CONTROL_PORT&=~(_BV(Enable_Pin)|_BV(RegSelect_Pin )|_BV(ReadWrite_Pin)); //setting values to 0 at starting  
    74. }  
    75.  
    76.  
    77.  
    78.  
    79. /*----------------------------------------------------------------  
    80. ------------FUNCTION TO INITIATE LCD -----------------------------  
    81. -----------------------------------------------------------------*/  
    82. void Init_Lcd(void)  
    83. {  
    84. char init[10];  
    85.  int i;  
    86.  init[0] = 0x01;  
    87.  init[1] = 0x38;  
    88.  init[2] = 0x0c;  
    89.  init[3] = 0x06;  
    90.  init[4] = 0x80;  
    91.  
    92. Select_InstructionRegister;  
    93. Write_Lcd;  
    94.    for(i=0;i<5;i++)  
    95.    {  
    96.    Data_Lcd(init[i]);  
    97.    Set_Enable;  
    98.    delay(30);  
    99.    Clear_Enable;  
    100.    delay(30);  
    101.    }  
    102. }  
    103.  
    104. ///////////////////
    105. void gotos(char x,char y)
    106. {
    107.    char val;
    108.    switch (y)
    109.    {
    110.    case 0:
    111.       val=0x80+0x00+x;
    112.       break;
    113.    case 1:
    114.       val=0x80+0x40+x;
    115.       break;
    116.    default:break;
    117.    }
    118.    Select_InstructionRegister;      
    119.    Write_Lcd;  
    120.     delay(30);  
    121.    Data_Lcd(val);  
    122.    Set_Enable;  
    123.    delay(30);  
    124.    Clear_Enable;  
    125.    delay(30);  
    126. }
    127.  
    128.  
    129. /*----------------------------------------------------------------  
    130. ----------------MAIN FUNCTION--------------------------------------  
    131. -----------------------------------------------------------------*/  
    132. int main(void)  
    133. {  
    134. int len,i;  
    135. char string[] = {'H','E','L','L','O',' ','J','O','J','O'};  
    136. Init_Ports();  
    137. Init_Lcd();  
    138. len=10;
    139. gotos(0x04,1);
    140.    for (i=0; i< len;i++)  
    141.      Lcd_Send(string[i]);  
    142.      
    143. }    
    144.  
    145.   [/i][/i]


    what additions can i add on it?
    like: to check if the lcd is busy or not , clear only the first line...
    thanks
     
Loading...