//------------------------------------------------------------------------------------
// Main.c
//------------------------------------------------------------------------------------
// Copyright (C) 2013 Shenyang JYXD.
// WEN BO RUI
// Tool chain: KEIL Full 'c'
//
//#pragma CD OE DB SB // Compilation directives
#include<reg2051.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint Period; //Period刷新周期
uint PWM;
uint Pulse; //脉冲个数
uchar Speed; //速度变量
uchar count;
uint excount;
bit Gra; //渐变/跳变总控制位
bit Gra7; //七彩渐亮渐灭控制位
bit Gra3; //红绿蓝渐亮渐灭控制位
bit BP; //亮到暗,暗到亮转换位
bit LEDdelay ; //LED定时到标志位
sbit key1 = P3^1; //花样组合键1
sbit key2 = P3^0; //花样组合键2
sbit key3 = P3^5; //速度组合键1
sbit key4 = P3^4; //速度组合键2
uchar led1[12]={0x10,0x030,0x20,0x60,0x40,0x50,0x70,0x50,0x40,0x60,0x20,0x30};//红、黄、绿、青、蓝、粉、白、粉、蓝、青、绿、黄
uchar led2[24]={0x10,0x00,0x30,0x00,0x20,0x00,0x60,0x00,0x40,0x00,0x50,0x00,
0x70,0x00,0x50,0x00,0x40,0x00,0x60,0x00,0x20,0x00,0x30,0x00};
uchar led3[3]={0x10,0x20,0x40}; //红、绿、蓝
uchar ledcount,ledcount1;
//***********************************************************
//以下定义函数
// 1毫秒延时函数delayms();花样及速度扫描函数keyscan();
//***********************************************************
/*void delayms(unsigned int ms)
{
unsigned char i ;
while(ms--)
{
for(i = 0 ; i < 120 ; i++) ;
}
}*/
void Pattern1(); //花样1函数--------->跳变无灭灯过程
void Pattern2(); //花样2函数--------->跳变有灭灯过程
void Pattern34();
void keyscan()
{
if((key4==1)&&(key3==1)) {Speed = 20;Period = 25;} //预设4种速度200MS
if((key4==0)&&(key3==1)) {Speed = 50;Period = 50;} //500MS
if((key4==1)&&(key3==0)) {Speed = 100;Period = 100;} //1S
if((key4==0)&&(key3==0)) {Speed = 150;Period = 200;} //1.5S
if((key2==1)&&(key1==1)) {ledcount1 = 0;Pattern1();} //预设4种花样
if((key2==0)&&(key1==1)) {ledcount1 = 0;Pattern2();}
if((key2==1)&&(key1==0)) {ledcount = 0;Gra3 = 1;Pattern34();}
if((key2==0)&&(key1==0)) {ledcount = 0;Gra7 = 1;Pattern34();}
}
void value()
{
if(Speed == 20) PWM = Pulse*8;
if(Speed == 50) PWM = Pulse*4; //脉冲个数
if(Speed == 100)PWM = Pulse*2;
if(Speed == 150)PWM = Pulse;
}
void Reset(void)
{
unsigned char code rst[ ]={0xe4,0xc0,0xe0,0xc0,0xe0,0x32};
(*((void (*)(void))(rst)))();
}
void main()
{
_nop_ ();
_nop_ ();
_nop_ ();
P1 = 0;
TMOD = 0x11; //11.0592M晶振的10ms定时初值(100分之一秒)
TH0 = 0x0db;
TL0 = 0x0ff;
TH1 = 0x0ff; //0.05ms(20000分之一秒)
TL1 = 0x0D1;
EA = 1; //开启总中断允许
ET0 = 1; //允许T0定时器中断
ET1 = 1; //允许T1定时器中断
TR0 = 1; //开启T0定时器
EX0=1; //开外部中断0
IT0=1; //下降沿触发
while(1)
{
keyscan();
}
}
//*************************************************************************
//Pattern1() 花样函数1-------------跳变无灭灯过程
//*************************************************************************
void Pattern1()
{
//keyscan();
Gra = 0; //跳变位=0
if(LEDdelay)
{
LEDdelay = 0;
P1 = led1[ledcount];
ledcount++;
if(ledcount>11)
{
ledcount = 0;
}
}
}
//***********************************************************************
//Pattern2() 花样函数2-------------跳变有灭灯过程
//***********************************************************************
void Pattern2()
{
//keyscan();
Gra = 0;
if(LEDdelay)
{
LEDdelay = 0;
P1 = led2[ledcount];
ledcount++;
if(ledcount>23)
{
ledcount = 0;
}
}
}
//***********************************************************************
//Pattern3() 花样函数3-------------3彩渐变
//***********************************************************************
/*void Pattern3()
{
keyscan();
if(Speed == 20) Period = 25;
if(Speed == 50) Period = 50;
if(Speed == 100)Period = 100;
if(Speed == 150)Period = 200;
Gra = 1;
Gra3 = 1;
}*/
//***********************************************************************
//Pattern4() 花样函数4-------------7彩渐变
//***********************************************************************
void Pattern34()
{
Gra = 1; //渐变位 = 1
}
/*********************************************************/
// 定时器0中断服务程序
/*********************************************************/
void time0(void) interrupt 1
{
TR1 =0;
TF0 =0;
TH0=0xdb; //重新写入初值
TL0=0xff;
if(!Gra) //执行跳变程序
{
count ++;
if(count > Speed)
{
LEDdelay = 1;
count = 0;
}
}
if(Gra)
{
TR1 = 1; //开启TR1;
if(!BP) //
{
if(Gra7)
{
P1 = led1[ledcount1]; //10ms到,亮相应的LED
}
if(Gra3)
{
P1 = led3[ledcount1]; //10ms到,亮相应的LED
}
Pulse++;
value();
if(Pulse >= Period)
{
Pulse = Period;
BP = 1; //一个周期完成转换模式
}
}
if(BP)
{
if(Gra7)
{
P1 = led1[ledcount1]; //10ms到,亮相应的LED
}
if(Gra3)
{
P1 = led3[ledcount1]; //10ms到,亮相应的LED
}
value();
Pulse--; //脉冲个数
if(Pulse == 0)
{
Pulse = 0;
BP = 0; //一个周期完成转换模式
ledcount1++;
if(Gra7)
{
if(ledcount1 > 11)
{
ledcount1 = 0;
}
Gra7 = 0;
}
if(Gra3)
{
if(ledcount1 > 2)
{
ledcount1 = 0;
}
Gra3 = 0;
}
}
}
}
}
/*********************************************************/
// 定时器1中断服务程序
/*********************************************************/
void timer1() interrupt 3
{
PWM --;
TH1 = 0xff; //0.05ms(20000分之一秒)
TL1 = 0xD1;
if(PWM == 0)
{
P1 = 0;
TR1 = 0;
}
}
/*********************************************************/
// 外部中断0中断服务程序 下降沿触发
/*********************************************************/
void extern0(void) interrupt 0
{
IE0 = 0; // 清标志位
excount++;
if(excount>50) // 50大约3秒
{
EA = 0;
//P1_5 = 1;
excount = 0;
Reset();
}
}