51单片机练习(04)

eg1:使用定时器的方式实现单片机流水灯

#include 
#include 
#define uchar unsigned char
#define uint unsigned int
uchar temp,t0;

// 初始化函数
void init(){
  temp = 0xfe;
	//第一个发光二级管点亮
	P1 = temp;
	// 初始化定时器
	TMOD = 0x11;
	TH0 = (65536-50000)/256;
	TL0 = (65536-50000)%256;
	EA = 1;
	ET0 = 1;
	// 启动定时器
	TR0 = 1;
}
// 定时器中断函数
void timer0() interrupt 1 {
    	TH0 = (65536-50000)/256;
			TL0 = (65536-50000)%256;
	    t0 ++;
	    if(t0 == 10){
			   t0 = 1;
				 temp = _crol_(temp,1);
				 P1 = temp;
			}
}

void main(){
	  // 初始化函数调用
    init();
   	while(1){
		  

		}

}

代码的截图如下所示
51单片机练习(04)_第1张图片单片机课后作业数码管和流水灯显示

#include 
#include 
#define uchar unsigned char
#define uint unsigned int
uchar temp,t0,t1,bai,shi,ge,flag,flag1;
uint shu;
sbit dula  = P2^6;
sbit wela  = P2^7;	
uchar code table[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x36,0x76,0x38,0x3f,0};

// 初始化函数
void init(){
	shu = 432;
  temp = 0xfe;
	//第一个发光二级管点亮
	P1 = temp;
	// 初始化定时器
	TMOD = 0x11;
	TH0 = (65536-50000)/256;
	TL0 = (65536-50000)%256;
	// 初始化定时器1
	TH1 = (65536-50000)/256;
	TL1 = (65536-50000)%256;
	EA = 1;
	ET0 = 1;
	// 打开定时器1
	ET1 = 1;
	// 启动定时器
	TR1 = 1;
	TR0 = 1;
}
// 定时器中断函数
void timer0() interrupt 1 {
    	TH0 = (65536-50000)/256;
			TL0 = (65536-50000)%256;
	    t0 ++;
	    if(flag != 1){
			  if(t0 == 10){
			     t0 = 1;
			  	 temp = _crol_(temp,1);
				   P1 = temp;
			  }
			  

			}else{
			   if(t0%4 == 0){
				     P1 = ~P1;
				 }
				 if(t0 == 60){
				     TR0 = 0;
					   P1 = 0xff;
					   flag1 = 1;
					   
				 }
			}

}

// 定时器1的中断函数
void timer1() interrupt 3 {
    	TH1 = (65536-50000)/256;
			TL1 = (65536-50000)%256;
	    t1 ++;
	    if(t1 == 2){
				  t1 = 0;
			    shu --;
			    // 将我们的bai shi ge 进行分离
				  bai = shu/100;
				  shi = shu%100/10;
				  ge = shu%10;
				  if(shu == 398){
		      			  
  						TR0 = 0;
						  TH0 = (65536-50000)/256;
							TL0= (65536-50000)%256;
						  TR0 = 1;
						  flag = 1;
						  t0 = 0;
						  // 关闭led灯
						  P1 = 0xff;
					    TR1 = 0;
         						

					}
			}   

}
// 延时函数
void delay(uint z){
    uint x,y;
	  for(x = z; x > 0; x --){
		   for(y = 110; y > 0; y--){
			     


        }
     }

}

// 显示子程序
void display(uchar aa , uchar bb , uchar cc ,uchar bai,uchar shi,uchar ge)
{
       

	     dula = 1;
		   P0 = table[aa];
			 dula = 0;
			 wela = 1;
			 P0 = 0xfe;
			 wela = 0;
			 delay(5);
			
			 dula = 1;
		   P0 = table[bb];
			 dula = 0;
			 wela = 1;
			 P0 = 0xfd;
			 wela = 0;
			 delay(5);
			
			 dula = 1;
		   P0 = table[cc];
			 dula = 0;
			 wela = 1;
			 P0 = 0xfb;
			 wela = 0;
			 delay(5);
	    
	     // -------------||-----------------||----------------||---------------||-------------||---------------------||--------------
	
	     dula = 1;
		   P0 = table[bai];
			 dula = 0;


			 wela = 1;
			 P0 = 0xf7;
			 wela = 0;
			 delay(5);
			
			 dula = 1;
		   P0 = table[shi];
			 dula = 0;
			
			 wela = 1;
			 P0 = 0xef;
			 wela = 0;
			 delay(5);
			
			 dula = 1;
		   P0 = table[ge];
			 dula = 0;
			
			 wela = 1;
			 P0 = 0xdf;
			 wela = 0;
			 delay(5);

}



void main(){
	  // 初始化函数调用
    init();
   	while(1){
			   // 调用函数并赋值形参
			   if(flag != 1){
				      display(7,6,5,bai,shi,ge);
				 }else{
				      display(16,17,18,18,19,20);
				 }
		   

		}

}

51单片机练习(04)_第2张图片

你可能感兴趣的:(51单片机,嵌入式硬件,单片机,c语言)