1. You can now buy finished microcontroller project from us, Check out the Store for the complete list of projects.
  2. Need a custom project, Send us some details about your project. So that we can quote the price for it.

Interfacing 7-segment Display

C code for Interfacing 7-segment Display with 8051 microcontroller.

  1. Binu
    Click here to Know about the seven segment display

    Now, we want to run the display with the AT89C51 microcontroller. We will use Port 0 to run the display. Connect the AT89C51 to the 7 segment display as follows.
    [​IMG]
    Code:
    Code (Text):
    1. #include <at89x52.h>
    2. void display_digit(unsigned char);
    3. void delay();
    4.  
    5. #define dis_a P0_1        /*a segment*/
    6. #define dis_b P0_0        /*b segment*/
    7. #define dis_c P0_6        /*c segment*/
    8. #define dis_d P0_5        /*d segment*/
    9. #define dis_e P0_4        /*e segment*/
    10. #define dis_f P0_2        /*f segment*/
    11. #define dis_g P0_3        /*g segment*/
    12.  
    13. void main(void)
    14. {
    15. unsigned char i;
    16. while(1){                  /*Repeat fowever*/
    17. for(i=0;i<10;i++)
    18. {
    19. display_digit(i);        /*Display 0-9*/
    20. delay();                        /*delay*/
    21. }
    22. }
    23. }
    24.  
    25. /*---------------------------------------------------
    26. Display the data passed to this function
    27. ----------------------------------------------------*/
    28. void display_digit(unsigned char digg)
    29. {
    30. if (digg==0x00)                /*Display 0*/
    31. {
    32. dis_a=0;
    33. dis_b=0;
    34. dis_c=0;
    35. dis_d=0;
    36. dis_e=0;
    37. dis_f=0;
    38. dis_g=1;
    39. }
    40. else if (digg==1)
    41. {
    42. dis_a=1;
    43. dis_b=0;
    44. dis_c=0;
    45. dis_d=1;
    46. dis_e=1;
    47. dis_f=1;
    48. dis_g=1;
    49. }
    50. else if (digg==2)
    51. {
    52. dis_a=0;
    53. dis_b=0;
    54. dis_c=1;
    55. dis_d=0;
    56. dis_e=0;
    57. dis_f=1;
    58. dis_g=0;
    59. }
    60. else if (digg==3)
    61. {
    62. dis_a=0;
    63. dis_b=0;
    64. dis_c=0;
    65. dis_d=0;
    66. dis_e=1;
    67. dis_f=1;
    68. dis_g=0;
    69. }
    70. else if (digg==4)
    71. {
    72. dis_a=1;
    73. dis_b=0;
    74. dis_c=0;
    75. dis_d=1;
    76. dis_e=1;
    77. dis_f=0;
    78. dis_g=0;
    79. }
    80. else if (digg==5)
    81. {
    82. dis_a=0;
    83. dis_b=1;
    84. dis_c=0;
    85. dis_d=0;
    86. dis_e=1;
    87. dis_f=0;
    88. dis_g=0;
    89. }
    90. else if (digg==6)
    91. {
    92. dis_a=0;
    93. dis_b=1;
    94. dis_c=0;
    95. dis_d=0;
    96. dis_e=0;
    97. dis_f=0;
    98. dis_g=0;
    99. }
    100. else if (digg==7)
    101. {
    102. dis_a=0;
    103. dis_b=0;
    104. dis_c=0;
    105. dis_d=1;
    106. dis_e=1;
    107. dis_f=1;
    108. dis_g=1;
    109. }
    110. else if (digg==8)
    111. {
    112. dis_a=0;
    113. dis_b=0;
    114. dis_c=0;
    115. dis_d=0;
    116. dis_e=0;
    117. dis_f=0;
    118. dis_g=0;
    119. }
    120. else if (digg==9)
    121. {
    122. dis_a=0;
    123. dis_b=0;
    124. dis_c=0;
    125. dis_d=0;
    126. dis_e=1;
    127. dis_f=0;
    128. dis_g=0;
    129. }
    130. }
    131. /*---------------------------------------------------
    132. Delay Program
    133. ----------------------------------------------------*/
    134. void delay()
    135. {
    136. TMOD=0x01;                /*Timer 0, mode 1*/
    137. TL0=0xFE;
    138. TH0=0xFE;
    139. TR0=1;
    140. while(TF0==0);
    141. TR0=0;
    142. TF0=0;
    143. }
    coemplong likes this.