新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 51單片機的七彩LED燈漸變調(diào)光調(diào)色程序

51單片機的七彩LED燈漸變調(diào)光調(diào)色程序

作者: 時間:2016-12-01 來源:網(wǎng)絡(luò) 收藏
//------------------------------------------------------------------------------------
// Main.c
//------------------------------------------------------------------------------------
// Copyright (C) 2013 Shenyang JYXD.
// WEN BO RUI
// Tool chain: KEIL Full c
//
//#pragma CD OE DB SB // Compilation directives
#include
#include
#define uchar unsigned char
#define uint unsigned int
uint Period; //Period刷新周期
uint PWM;
uint Pulse; //脈沖個數(shù)
uchar Speed; //速度變量
uchar count;
uint excount;
bit Gra; //漸變/跳變總控制位
bit Gra7; //七彩漸亮漸滅控制位
bit Gra3; //紅綠藍漸亮漸滅控制位
bit BP; //亮到暗,暗到亮轉(zhuǎn)換位
bit LEDdelay ; //LED定時到標(biāo)志位
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;
//***********************************************************
//以下定義函數(shù)
// 1毫秒延時函數(shù)delayms();花樣及速度掃描函數(shù)keyscan();
//***********************************************************
/*void delayms(unsigned int ms)
{
unsigned char i ;
while(ms--)
{
for(i = 0 ; i < 120 ; i++) ;
}
}*/
void Pattern1(); //花樣1函數(shù)--------->跳變無滅燈過程
void Pattern2(); //花樣2函數(shù)--------->跳變有滅燈過程
void Pattern34();
void keyscan()
{
if((key4==1)&&(key3==1)) {Speed = 20;Period = 25;} //預(yù)設(shè)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();} //預(yù)設(shè)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; //脈沖個數(shù)
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; //下降沿觸發(fā)
while(1)
{

keyscan();
}
}
//*************************************************************************
//Pattern1() 花樣函數(shù)1-------------跳變無滅燈過程
//*************************************************************************
void Pattern1()
{
//keyscan();
Gra = 0; //跳變位=0
if(LEDdelay)
{
LEDdelay = 0;
P1 = led1[ledcount];
ledcount++;
if(ledcount>11)
{
ledcount = 0;
}
}
}
//***********************************************************************
//Pattern2() 花樣函數(shù)2-------------跳變有滅燈過程
//***********************************************************************
void Pattern2()
{
//keyscan();
Gra = 0;
if(LEDdelay)
{
LEDdelay = 0;
P1 = led2[ledcount];
ledcount++;
if(ledcount>23)
{
ledcount = 0;
}
}
}
//***********************************************************************
//Pattern3() 花樣函數(shù)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;
}*/

上一頁 1 2 下一頁

評論


技術(shù)專區(qū)

關(guān)閉