This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/MCU/MCU_3 _TIM_Interrupt/main.c

72 lines
1006 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <reg52.h>
#include <intrins.h>
#include "delay.h"
unsigned long int count = 0;
unsigned char var = 0xFF;
#define RELOAD (65535-46080)
int TH, TL;
unsigned char enable = 0;
void setConfig()
{
TH = RELOAD / 256;
TL = RELOAD - (RELOAD / 256) * 256;
TH0 = TH;
TL0 = TL;
TMOD = 0x01; //使用定时器T0高4位全为0,低4位GATE=0C/T=0采用工作方式1M1=0,M1=1
TR0 = 1; //启动定时器0
EA = 1; //打开中断总开关
ET0 = 1; //打开定时器0中断开关
P1 = 0X00;
}
void pulse() interrupt 1
{
TH0 = TH;
TL0 = TL0 + TL;
TL0 = TL0 + 23;
enable = 1;
count++;
}
void ifLight()
{
if(enable)
{
int time;
for(time = 1; time < 9; time++) //时间匹配
{
if(count % (20 * time) == 0) //是否整数倍
{
int i = 1;
i = i << (time - 1);
var = var ^ i;
P1 = var;
}
}
enable = 0;
}
}
int main()
{
//P1 灯
setConfig();
while(1)
{
ifLight();
}
}