蓝桥杯单片机开发板-定时器中断实现流水灯

本程序主要是通过定时器实现中断进行流水灯灯的实现

#include "reg52.h"
#include "intrins.h"

sfr AUXR=0X8E;    //NOTES:sbit是位操作,用于将某个sfr中具体位赋值给一个变量

unsigned int count  = 0;
unsigned int flag   = 0;

void selectHC38(unsigned char n);
void InitTimer0(void);
void init();

void Timer0Interrupt(void) interrupt 1
{
	count++;	
	if(count == 100)
	{
		flag=flag+1;
		switch(flag)
		{
			case 1: selectHC38(4);P0=~(0x01); break;
			case 2: selectHC38(4);P0=~(0x02); break;
			case 3: selectHC38(4);P0=~(0x04); break;
			case 4: selectHC38(4);P0=~(0x08); break;
			case 5: selectHC38(4);P0=~(0x10); break;
			case 6: selectHC38(4);P0=~(0x20); break;
			case 7: selectHC38(4);P0=~(0x40); break;
			case 8: selectHC38(4);P0=~(0x80); break;
			default:selectHC38(4);P0=~(0x01);flag=0;break;
    }
		count=0;
	}
}

void main()
{
	init();
	while(1)
	{
		
	}

}


void selectHC38(unsigned char n)
{
	switch(n)
	{
		case 4 :P2 = 0x80;break;
		case 5 :P2 = 0xA0;break;
		case 6 :P2 = 0xC0;break;
		case 7 :P2 = 0xE0;break;	
	}
}

void init()
{
	InitTimer0();
	selectHC38(5);P0 = 0X00;P2=0x00;
	selectHC38(4);P0 = 0XFF;P2=0x00;
}
void InitTimer0(void)
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x00;		//设置定时初值
	TH0 = 0x28;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
  EA = 1;
  ET0 = 1;
  TR0 = 1;
}

你可能感兴趣的:(蓝桥杯,单片机,单片机,蓝桥杯,嵌入式硬件)