其他分享
首页 > 其他分享> > 用PROTEUS仿真单片机与七段数码管

用PROTEUS仿真单片机与七段数码管

作者:互联网

设计要求:

用七段数码管显示数字0~9

(代码来源于指导书)

#include<reg51.h>

void cct_init(void);
void delay(int);
void DisplayOn7Segment(char);

int main(void)
{
   char ch = '0';              // Character to be displayed on 7seg

   cct_init();                 // Make all ports zero    

   while(1)           
   {
    DisplayOn7Segment(ch);      // Display ch on 7seg
    delay(30000);              // About 1 sec delay

    switch(ch)                  // Update ch with new value to be displayed
    {
        case '0':    ch = '1';  break;
        case '1':    ch = '2';  break;
        case '2':    ch = '3';  break;
        case '3':    ch = '4';  break;
        case '4':    ch = '5';  break;
        case '5':    ch = '6';  break;
        case '6':    ch = '7';  break;
        case '7':    ch = '8';  break;
        case '8':    ch = '9';  break;
        case '9':    ch = '0';  break;
    
    
        default: ch = '0';  break;
    }
   }
}

void cct_init(void)
{
    P0 = 0x00;   
    P1 = 0x00;   
    P2 = 0x00;   
    P3 = 0x00;  
}

void delay(int a)
{
   int i;
   for(i=0;i<a;i++);   //null statement
}

void DisplayOn7Segment(char ch)   // ch can have a value from '0' to 'F' only
{
    switch(ch)
    {
        case '0':    P2 = 0x3F;  break;
        case '1':    P2 = 0x06;  break;
        case '2':    P2 = 0x5B;  break;
        case '3':    P2 = 0x4F;  break;
        case '4':    P2 = 0x66;  break;
        case '5':    P2 = 0x6D;  break;
        case '6':    P2 = 0x7D;  break;
        case '7':    P2 = 0x07;  break;
        case '8':    P2 = 0x7F;  break;
        case '9':    P2 = 0x6F;  break;
    

        default: P2 = 0x3F;  break;
    }    
}

 以上为共阴极

然后共阳极的不确定对不对,但是能运行,仅供参考吧

 

放一下代码,只是更改了一些数值,生成的hex文件还是可以用的

#include<reg51.h>
void cct_init(void);
void delay(int);
void DisplayOn7Segment(char);
int main(void)
{
   char ch = '0';              // Character to be displayed on 7seg
   cct_init();                 // Make all ports zero    
   while(1)           
   {
    DisplayOn7Segment(ch);      // Display ch on 7seg
    delay(30000);              // About 1 sec delay
    switch(ch)                  // Update ch with new value to be displayed
    {
        case '0':    ch = '1';  break;
        case '1':    ch = '2';  break;
        case '2':    ch = '3';  break;
        case '3':    ch = '4';  break;
        case '4':    ch = '5';  break;
        case '5':    ch = '6';  break;
        case '6':    ch = '7';  break;
        case '7':    ch = '8';  break;
        case '8':    ch = '9';  break;
        case '9':    ch = '0';  break;
        default: ch = '0';  break;
    }1
   }
}
void cct_init(void)
{
    P0 = 0x00;   
    P1 = 0x00;   
    P2 = 0x00;   
    P3 = 0x00;  
}
void delay(int a)
{
   int i;
   for(i=0;i<a;i++);   //null statement
}
void DisplayOn7Segment(char ch)   // ch can have a value from '0' to 'F' only
{
    switch(ch)
    {
        case '0':    P2 = 0xC0;  break;
        case '1':    P2 = 0xF9;  break;
        case '2':    P2 = 0xA4;  break;
        case '3':    P2 = 0xB0;  break;
        case '4':    P2 = 0x99;  break;
        case '5':    P2 = 0x92;  break;
        case '6':    P2 = 0x82;  break;
        case '7':    P2 = 0xF8;  break;
        case '8':    P2 = 0x80;  break;
        case '9':    P2 = 0x90;  break;
        default: P2 = 0xC0;  break;
    }    
}

 

 大概就是这个样子吧

标签:case,ch,int,void,0x00,break,单片机,PROTEUS,七段
来源: https://www.cnblogs.com/lbc518-1005/p/16387728.html