|
include "reg52.h" //此文件中定义了单片机的一些特殊功能寄存器
#include <intrins.h> //51基本运算(包括_nop_空函数)
typedef unsigned int u16; //对数据类型进行声明定义
typedef unsigned char u8;
sbit k3=P3^2; //定义按键K3
sbit led=P2^0;
u8 t=1;
u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//显示0~F的值
sbit RR=P0^0;//red红灯
sbit GG=P0^1;//green绿灯
sbit BB=P0^2;//blue蓝灯
u8 r,g,b,min,max;
void ledout(unsigned char r,g,b)
{ unsigned char n;
for (n=0;n<255;n++)
{
if(g>n)
GG=0;
else
GG=1;
if(b>n)
BB=0;
else
BB=1;
if(r>n)
RR=0;
else
RR=1;
_nop_();
_nop_();
_nop_();
}
}
void Colors(u16 c)
{
if(c==1)
{
ledout(255,0,0);
}
if(c==2)
{
ledout(250,250,0);
}
if(c==3)
{
ledout(0,250,250);
}
if(c==4)
{
ledout(250,0,250);
}
if(c==5)
{
ledout(0,250,0);
}
if(c==6)
{
ledout(0,0,250);
}
if(c==7)
{
u8 i;
min=5;
max=250;
r=min;
g=min;
for (i=min;i<=max;i++)
{
b=max+min-i-1;
ledout(r,g,b);
if(t!=7)
{
return ;
}
}
for (i=min;i<=max;i++)
{
g=i;
ledout(r,g,b);
if(t!=7)
{
return ;
}
}
for (i=min;i<=max;i++)
{
r=max+min-i-1;
ledout(r,g,b);
if(t!=7)
{
return ;
}
}
for (i=min;i<=max;i++)
{
b=i;
ledout(r,g,b);
if(t!=7)
{
return ;
}
}
for (i=min;i<=max;i++)
{
g=max+min-i-1;
ledout(r,g,b);
if(t!=7)
{
return ;
}
}
for (i=min;i<=max;i++)
{
r=i;
ledout(r,g,b);
if(t!=7)
{
return ;
}
}
}
}
/*******************************************************************************
* 函 数 名 : delay
* 函数功能 : 延时函数,i=1时,大约延时10us
*******************************************************************************/
void delay(u16 i)
{
while(i--);
}
/*******************************************************************************
* 函 数 名 : Int1Init()
* 函数功能 : 设置外部中断1
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void Int0Init()
{
//设置INT0
IT0=1;//跳变沿出发方式(下降沿)
EX0=1;//打开INT0的中断允许。
EA=1;//打开总中断
}
/*******************************************************************************
* 函 数 名 : main
* 函数功能 : 主函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void main()
{
Int0Init(); // 设置外部中断0
while(1)
{
P1=~smgduan[t];
Colors(t);
EA=1;
}
}
/*******************************************************************************
* 函 数 名 : Int0() interrupt 0
* 函数功能 : 外部中断0的中断函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void Int0() interrupt 0 //外部中断0的中断函数
{
delay(1000); //延时消抖
if(k3==0)
{
led=~led;
if(t<7)
{
t=t+1;
}
else
{
t=1;
}
}
while(!k3) ;
}
|
评分
-
查看全部评分
|