超炫幻彩跑马灯
視頻:http://v.eepw.com.cn/video/play/id/1935
用RL78/G13 开发板做的超炫幻彩跑马灯,可以随意调整模式,速度,颜色
用了八個RGB三色共陽LED
R LED連接到P2
G LED連接到P1
B LED連接到P7
三個接鍵連接到P00/P01/P02 用于選擇模式/顏色/速度/亮度
視頻正在上傳中,稍候登場
程序如下:
定義以下文件頭:
#define KeyPort P0
#define NoKeyData 0x07
#define Key1Data 0x06
#define Key2Data 0x05
#define Key3Data 0x03
#define Key12Data 0x04
#define MODE_STEP 300
#define RedLedPort P2
#define GreenLedPort P1
#define BlueLedPort P7
#define MAX_LED_MODE 10
#define LED_OFF 0xff
#define MAX_COLOUR_MODE 8
#define MAX_SPEED 500
#define MIN_SPEED 5000
#define FLASH_SPEED_STEP 300
#define MAX_BRIGHTNESS_RATE 5
定議以下寄存器:
unsigned char LedTimerCount;
unsigned char LedOnCount;
unsigned int LedStepCount;
unsigned char Direction=0;
unsigned char LedMode;
unsigned char ColourMode;
unsigned char RedLedOnCount;
unsigned char GreenLedOnCount;
unsigned char BlueLedOnCount;
unsigned char RedPortData;
unsigned char GreenPortData;
unsigned char BluePortData;
unsigned short FLashSpeed;
unsigned char BrightnessRate;
unsigned char KeySave;
unsigned char KeyBuffer;
/***************config RGB colour*************/
const unsigned char RGBData[MAX_COLOUR_MODE][3]=
{
100,0,0,
80,50,10,
90,80,0,
10,100,0,
0,0,100,
10,100,20,
100,40,70,
100,100,100
};
LED上電初始化程序:
/******setup default mode***********/
void SetupLed(void)
{
ADPC=0x01;
PU7=0xff;
PM7=0x00;
PU1=0xff;
PM1=0x00;
PM2=0x00;
RedLedPort=LED_OFF; //set red off
GreenLedPort=LED_OFF; //set green off
BlueLedPort=LED_OFF; //set blueoff
LedMode=0; //set default led mode
ColourMode=1; //set default led colour
RedPortData=0x80; //set default red led status
GreenPortData=0x80; //set default green led status
BluePortData=0x80; //set default blue led status
FLashSpeed=2000; //set default flash speed
BrightnessRate=2; //set default led brightness rate
}
定時器中斷程序如下:
#pragma vector = INTTM00_vect
__interrupt void MD_INTTM00(void)
{
/* Start user code. Do not edit comment generated here */
//unsigned char Temp;
TMIF00 = 0U;
LedStepCount++;
if(LedStepCount>FLashSpeed)
{
LedStepCount=0;
switch (LedMode)
{
case 0:
{
RedPortData >>=1;
if(RedPortData==0x00)RedPortData=0x80;
GreenPortData >>=1;
if(GreenPortData==0x00)GreenPortData=0x80;
BluePortData >>=1;
if(BluePortData==0x00)BluePortData=0x80;
break;
}
case 1:
{
RedPortData <<=1;
if(RedPortData==0x00)RedPortData=0x01;
GreenPortData <<=1;
if(GreenPortData==0x00)GreenPortData=0x01;
BluePortData <<=1;
if(BluePortData==0x00)BluePortData=0x01;
break;
}
case 2:
{
RedPortData ^=0xff;
BluePortData ^=0xff;
GreenPortData ^=0xff;
break;
}
case 3:
{
ColourMode++;
if(ColourMode>(MAX_COLOUR_MODE-1))
{
ColourMode=0;
}
break;
}
case 4:
{
if(Direction==0)
{
RedPortData >>=1;
if(RedPortData==0x00)
{
RedPortData=0x01;
Direction=1;
}
GreenPortData >>=1;
if(GreenPortData==0x00)GreenPortData=0x01;
BluePortData >>=1;
if(BluePortData==0x00)BluePortData=0x01;
}
else
{
RedPortData <<=1;
if(RedPortData==0x00)
{
RedPortData=0x80;
Direction=0;
}
GreenPortData <<=1;
if(GreenPortData==0x00)GreenPortData=0x80;
BluePortData <<=1;
if(BluePortData==0x00)BluePortData=0x80;
}
break;
}
case 5:
{
RedPortData ^=0xff;
RedPortData >>=1;
if(RedPortData==0x00)RedPortData=0x80;
RedPortData ^=0xff;
GreenPortData ^=0xff;
GreenPortData >>=1;
if(GreenPortData==0x00)GreenPortData=0x80;
GreenPortData ^=0xff;
BluePortData ^=0xff;
BluePortData >>=1;
if(BluePortData==0x00)BluePortData=0x80;
BluePortData ^=0xff;
break;
}
case 6:
{
if(Direction==0)
{
if(RedPortData==0xff)
{
Direction=1;
RedPortData=0xfe;
}
else
{
RedPortData >>=1;
RedPortData |= 0x80;
}
if(GreenPortData==0xff)
{
Direction=1;
GreenPortData=0xfe;
}
else
{
GreenPortData >>=1;
GreenPortData |= 0x80;
}
if(BluePortData==0xff)
{
Direction=1;
BluePortData=0xfe;
}
else
{
BluePortData >>=1;
BluePortData |= 0x80;
}
}
else
{
if(RedPortData==0x00)
{
Direction=0;
RedPortData=0x80;
}
else
{
RedPortData <<=1;
}
if(GreenPortData==0x00)
{
Direction=0;
GreenPortData=0x80;
}
else
{
GreenPortData <<=1;
}
if(BluePortData==0x00)
{
Direction=0;
BluePortData=0x80;
}
else
{
BluePortData <<=1;
}
}
break;
}
default:
{
RedPortData >>=1;
if(RedPortData==0x00)RedPortData=0x80;
GreenPortData >>=1;
if(GreenPortData==0x00)GreenPortData=0x80;
BluePortData >>=1;
if(BluePortData==0x00)BluePortData=0x80;
break;
}
}
}
RedLedOnCount=RGBData[ColourMode][0];
GreenLedOnCount=RGBData[ColourMode][1];
BlueLedOnCount=RGBData[ColourMode][2];
RedLedOnCount /= BrightnessRate;
GreenLedOnCount /= BrightnessRate;
BlueLedOnCount /= BrightnessRate;
/**********PWM Timer Control*********/
LedTimerCount++;
if(LedTimerCount>100)
{
LedTimerCount=0;
}
/*****Red LED control*********/
if(LedTimerCount>=RedLedOnCount)
{
RedLedPort=LED_OFF; //off
}
else
{
RedLedPort=RedPortData;
}
/*****Green LED control*********/
if(LedTimerCount>=GreenLedOnCount)
{
GreenLedPort=LED_OFF; //off
}
else
{
GreenLedPort=GreenPortData;
}
/*****Blue LED control*********/
if(LedTimerCount>=BlueLedOnCount)
{
BlueLedPort=LED_OFF; //off
}
else
{
BlueLedPort=BluePortData;
}
}
按鍵處理程序:
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function initializes Key.
**
** Parameters:
** None
**
** Returns:
** None
**
**-----------------------------------------------------------------------------
*/
void ConfigKey(void)
{
PMC0=0x00;
PU0=0xff; //set pull high
PM0=0xff; //set input
}
void DelayMs(unsigned short MS)
{
unsigned short i,j;
for(j=0;j<MS;j++)
{
for(i=0;i<200;i++)
{
;
}
}
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function Scan key.
**
** Parameters:
** None
**
** Returns:
** None
**
**-----------------------------------------------------------------------------
*/
unsigned char ScanKey(void)
{
unsigned char TempData,TempData1,KeyCode;
TempData=KeyPort;
TempData &=NoKeyData;
KeyCode=NoKeyData;
if(TempData !=NoKeyData )
{
DelayMs(50);
TempData1=KeyPort;
TempData1 &=NoKeyData;
if(TempData==TempData1)
{
KeyCode=TempData;
}
}
return(KeyCode);
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function handle key function.
**
** Parameters:
** None
**
** Returns:
** None
**
**-----------------------------------------------------------------------------
*/
void HandleKey(void)
{
KeySave=ScanKey();
if(KeySave!=KeyBuffer && KeySave==Key1Data)
{
LedMode++; //change LED mode
if(LedMode>MAX_LED_MODE)
{
LedMode=0;
}
switch (LedMode)
{
case 0: //set mode 0 default status
{
RedPortData=0x80; //set default red led status
GreenPortData=0x80; //set default green led status
BluePortData=0x80; //set default blue led status
break;
}
case 1: //set mode 1 default status
{
RedPortData=0x01; //set default red led status
GreenPortData=0x01; //set default green led status
BluePortData=0x01; //set default blue led status
break;
}
case 2: //set mode 2 default status
{
RedPortData=0x00; //set default red led status
GreenPortData=0x00; //set default green led status
BluePortData=0x00; //set default blue led status
break;
}
case 3: //set mode 3 default status
{
RedPortData=0x00; //set default red led status
GreenPortData=0x00; //set default green led status
BluePortData=0x00; //set default blue led status
break;
}
case 4: //set mode 4 default status
{
RedPortData=0x01; //set default red led status
GreenPortData=0x01; //set default green led status
BluePortData=0x01; //set default blue led status
break;
}
case 5: //set mode 5 default status
{
RedPortData=0x80; //set default red led status
GreenPortData=0x80; //set default green led status
BluePortData=0x80; //set default blue led status
break;
}
case 6: //set mode 6 default status
{
RedPortData=0x80; //set default red led status
GreenPortData=0x80; //set default green led status
BluePortData=0x80; //set default blue led status
Direction=0;
break;
}
default:
{
RedPortData=0x80; //set default red led status
GreenPortData=0x80; //set default green led status
BluePortData=0x80; //set default blue led status
break;
}
}
}
else if(KeySave!=KeyBuffer && KeySave==Key2Data) //change led colour
{
ColourMode++;
if(ColourMode>(MAX_COLOUR_MODE-1))
{
ColourMode=0;
}
}
else if(KeySave!=KeyBuffer && KeySave==Key3Data) //change flash speed
{
FLashSpeed +=FLASH_SPEED_STEP;
if(FLashSpeed > MIN_SPEED)
{
FLashSpeed=MAX_SPEED;
}
}
else if(KeySave!=KeyBuffer && KeySave==Key12Data) //change brightness
{
BrightnessRate++;
if(BrightnessRate > MAX_BRIGHTNESS_RATE)
{
BrightnessRate=1;
}
}
KeyBuffer=KeySave;
}
GPIO INTP/KeyBoard INT 測試
先用Applilet生基礎代碼:
在中斷程序中實現RL78/G13開發板上的LED翻轉,中斷代碼如下:
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function is INTP0 interrupt service routine.
**
** Parameters:
** None
**
** Returns:
** None
**
**-----------------------------------------------------------------------------
*/
__interrupt void MD_INTP0(void)
{
/* Start user code. Do not edit comment generated here */
P7 ^=0x80;
/* End user code. Do not edit comment generated here */
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function is INTKR interrupt service routine.
**
** Parameters:
** None
**
** Returns:
** None
**
**-----------------------------------------------------------------------------
*/
__interrupt void MD_INTKR(void)
{
/* Start user code. Do not edit comment generated here */
P7 ^=0x80;
/* End user code. Do not edit comment generated here */
}
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
【换取逻辑分析仪】-基于ADI单片机MAX78000的简易MP3音乐播放器被打赏48分 | |
我想要一部加热台+树莓派PICO驱动AHT10被打赏38分 | |
【换取逻辑分析仪】-硬件SPI驱动OLED屏幕被打赏36分 | |
换逻辑分析仪+上下拉与多路选择器被打赏29分 | |
Let'sdo第3期任务合集被打赏50分 | |
换逻辑分析仪+Verilog三态门被打赏27分 | |
换逻辑分析仪+Verilog多输出门被打赏24分 | |
【分享评测,赢取加热台】使用8051单片机驱动WS2812被打赏40分 | |
【换取逻辑分析仪】rtthread添加RRH62000传感器驱动-基于野火启明6M5被打赏48分 | |
换逻辑分析仪+Verilog多输入门被打赏27分 |