单片机c语言任务1——数码显示管0~9

3/8/2017来源:ASP.NET技巧人气:2150

仿真电路图

元件清单

程序代码

#include<reg52.h>
//#define u16 unsigned int
//#define u8 unsigned char
typedef unsigned int u16;
typedef unsigned char u8; 
sbit led1=P1^0;
sbit led2=P1^1;
sbit led3=P1^2;
sbit led4=P1^3;
sbit led5=P1^4;
sbit led6=P1^5;
sbit led7=P1^6;
sbit led8=P1^7;
void delay(u16 num);
void main()
{
  P1=0XFF;//初始化p1八位,设置p1^0~p^7为输出
  while(1)
{  
    led1=0;//灯亮
    delay(500);
    led1=1;
    delay(500);

	led2=0;//灯亮
    delay(500);
    led2=1;
    delay(500);

	led3=0;//灯亮
    delay(500);
    led3=1;
    delay(500);

	led4=0;//灯亮
    delay(500);
    led4=1;
    delay(500);

	led5=0;//灯亮
    delay(500);
    led5=1;
    delay(500);

	led6=0;//灯亮
    delay(500);
    led6=1;
    delay(500);

	led7=0;//灯亮
    delay(500);
    led7=1;
    delay(500);

	led8=0;//灯亮
    delay(500);
    led8=1;
    delay(500);
}
}  
void delay(u16  num)//num=100
{
   unsigned int i,j;
   for(i=100;i>0;i--)
   for(j=110;j>0;j--);//延迟1ms

程序代码:

#include<reg52.h>
//#define u16 unsigned int
//#define u8 unsigned char
typedef unsigned int u16;
typedef unsigned char u8; 
sbit led1=P1^0;
sbit led2=P1^1;
sbit led3=P1^2;
sbit led4=P1^3;
sbit led5=P1^4;
sbit led6=P1^5;
sbit led7=P1^6;
sbit led8=P1^7;
void delay(u16 num);
void main()
{
  P1=0XFF;//初始化p1八位,设置p1^0~p^7为输出
  while(1)
{  
  P1=0XFE;
  delay(1000);//延时1s

    P1=0XFD;
  delay(1000);//延时1s

    P1=0XFB;
  delay(1000);//延时1s

    P1=0XF7;
  delay(1000);//延时1s

    P1=0XEF;
  delay(1000);//延时1s

    P1=0XDF;
  delay(1000);//延时1s

    P1=0XBF;
  delay(1000);//延时1s

    P1=0X7F;
  delay(1000);//延时1s
}
}  
void delay(u16  num)//num=100
{
   unsigned int i,j;
   for(i=100;i>0;i--)
   for(j=110;j>0;j--);//延迟1ms

程序代码3:

#include<reg51.h>
#define uint unsigned int	  
delay(uint t)
{
  uint i,j;
  for(j=t;j>0;j--)
  for(i=110;i>0;i--);
}
main()
 {
   uint i, abs[8]={0XFE,0XFD,0XFb,0XF7,0Xef,0XDF,0Xbf,0X7f};
   while(1)
      {
        for(i=0;i<8;i++)
          {
            P1=abs[i];
            delay(1000);
          }
      }							 
}

程序代码

	 #include"reg51.h"
	 #include"intrins.h"
	 #define uchar unsigned char
	 #define uint unsigned int
	 delay(uint t)
	 {
	  uint i,j;
	  for(j=t;j>0;j--)
	  for(i=110;i>0;i--);
	 }
	 main()
	 {
	 uint n=0Xfe;
	 while(1)
	 {
	 P1=n;
	 delay(1000);
	 n=_crol_(n,1);
	 }
	 }