本帖最后由 mathison 于 2015-10-10 14:38 编辑
参考
http://www.amobbs.com/thread-5276839-1-1.html
http://www.amobbs.com/thread-5512126-1-1.html
思路来自
http://www.amobbs.com/thread-4192967-1-1.html
中“cowboy” 回复中提到的
“频率固定,亮度Y取值为0~255, {MOD}彩用RBG的比例表示,R:B:G 比值不同就有不同的 {MOD}彩。设定 {MOD}彩时使R+B+G=256,然后,R、B、G分别与Y相乘,再除以256,分别作为三个PWM的点空比。”
8组 RGB LED灯 3x8 = 24个 普通 GPIO口 控制 LED灯,
废话不说 直接上代码:
#if 1 //12中颜 {MOD}的RGB定义ok
u8_t code RGB[13][3] =
{
{0xFF, 0x00, 0x00},// 0 ok
{0xFF, 0x0C, 0x00 },// 1 ok
{0xFF, 0x33, 0x00},// 2 ok
{0xFF, 0x66, 0x00}, // 3 ok
{0xFF, 0xFF, 0x00 }, // 4 //ok
{0x00, 0x99, 0x00},// 5 ok g
{0x00, 0x99, 0x33},// 6 ok 99
{0x00, 0x50, 0x99},// 6 ok
{0x00, 0x00, 0x99},// 7 ok blue
//{0x33, 0x00, 0x99}, // 8 ok
{0x99, 0x00, 0x99}, // 9 ok
{0xCC, 0x00, 0x66}, // 10 ok
{0xCC, 0x00, 0x33}, // 11 ok
//{0xFF, 0xFF, 0xFF}, // 12
{0x00, 0x00, 0x00}
};
#endif
//80级的对数 LED 亮度的定义
#define CONST_RESPIRATION_LAMP_SERIES 80
code unsigned char RESPIRATION_LAMP_TABLE[] =
{
1,
1,
1,
1,
1,
2,
2,
2,
2,
2,
2,
2,
2,
3,
3,
3,
3,
3,
4,
4,
4,
5,
5,
5,
6,
6,
6,
7,
7,
8,
9,
9,
10,
11,
11,
12,
13,
14,
15,
16,
17,
18,
20,
21,
23,
24,
26,
28,
30,
32,
34,
37,
39,
42,
45,
48,
52,
56,
60,
64,
68,
73,
79,
84,
90,
97,
104,
111,
119,
128,
137,
147,
157,
168,
180,
193,
207,
222,
238,
255,
255
};
//3x8 = 24个 普通 GPIO口
// 1
#define LED1_RED_OUT GPIO7_CFG_OUTPUT
#define LED1_RED_ON GPIO7(1)//_nop_()//
#define LED1_RED_OFF GPIO7(0)
#define LED1_GREEN_OUT GPIO8_CFG_OUTPUT
#define LED1_GREEN_ON GPIO8(1)
#define LED1_GREEN_OFF GPIO8(0)
#define LED1_BLUE_OUT GPIO12_CFG_OUTPUT
#define LED1_BLUE_ON GPIO12(1)
#define LED1_BLUE_OFF GPIO12(0)
// 2
#define LED2_RED_OUT P02_CFG_OUTPUT//P00_CFG_OUTPUT
#define LED2_RED_ON P02 = 1
#define LED2_RED_OFF P02 = 0
#define LED2_GREEN_OUT P01_CFG_OUTPUT
#define LED2_GREEN_ON P01 = 1
#define LED2_GREEN_OFF P01 = 0
#define LED2_BLUE_OUT P00_CFG_OUTPUT//P02_CFG_OUTPUT
#define LED2_BLUE_ON P00 = 1
#define LED2_BLUE_OFF P00 = 0
// 3
#define LED3_RED_OUT P20_CFG_OUTPUT
#define LED3_RED_ON P20 = 1
#define LED3_RED_OFF P20 = 0
#define LED3_GREEN_OUT P04_CFG_OUTPUT
#define LED3_GREEN_ON P04 = 1
#define LED3_GREEN_OFF P04 = 0
#define LED3_BLUE_OUT P03_CFG_OUTPUT
#define LED3_BLUE_ON P03 = 1
#define LED3_BLUE_OFF P03 = 0
// 4
#define LED4_RED_OUT P23_CFG_OUTPUT
#define LED4_RED_ON P23 = 1
#define LED4_RED_OFF P23 = 0
#define LED4_GREEN_OUT P24_CFG_OUTPUT
#define LED4_GREEN_ON P24 =1
#define LED4_GREEN_OFF P24 =0
#define LED4_BLUE_OUT P25_CFG_OUTPUT
#define LED4_BLUE_ON P25 = 1
#define LED4_BLUE_OFF P25 = 0
// 5
#define LED5_RED_OUT P26_CFG_OUTPUT
#define LED5_RED_ON P26=1
#define LED5_RED_OFF P26 = 0
#define LED5_GREEN_OUT P27_CFG_OUTPUT
#define LED5_GREEN_ON P27 =1
#define LED5_GREEN_OFF P27 = 0
#define LED5_BLUE_OUT P17_CFG_OUTPUT
#define LED5_BLUE_ON P17 = 1
#define LED5_BLUE_OFF P17 = 0
// 6
#define LED6_RED_OUT GPIO17_CFG_OUTPUT
#define LED6_RED_ON GPIO17(1)
#define LED6_RED_OFF GPIO17(0)
#define LED6_GREEN_OUT GPIO18_CFG_OUTPUT
#define LED6_GREEN_ON GPIO18(1)
#define LED6_GREEN_OFF GPIO18(0)
#define LED6_BLUE_OUT P10_CFG_OUTPUT
#define LED6_BLUE_ON P10 = 1
#define LED6_BLUE_OFF P10 = 0
// 7
#define LED7_RED_OUT GPIO14_CFG_OUTPUT
#define LED7_RED_ON GPIO14(1)
#define LED7_RED_OFF GPIO14(0)
#define LED7_GREEN_OUT GPIO15_CFG_OUTPUT
#define LED7_GREEN_ON GPIO15(1)
#define LED7_GREEN_OFF GPIO15(0)
#define LED7_BLUE_OUT GPIO16_CFG_OUTPUT
#define LED7_BLUE_ON GPIO16(1)
#define LED7_BLUE_OFF GPIO16(0)
// 8
#define LED8_RED_OUT GPIO13_CFG_OUTPUT
#define LED8_RED_ON GPIO13(1)
#define LED8_RED_OFF GPIO13(0)
#define LED8_GREEN_OUT P34_CFG_OUTPUT
#define LED8_GREEN_ON P34 = 1
#define LED8_GREEN_OFF P34 = 0
#define LED8_BLUE_OUT P35_CFG_OUTPUT
#define LED8_BLUE_ON P35 = 1
#define LED8_BLUE_OFF P35 = 0
//Timer0 系统时间片
//Timer1:用来做呼吸灯 大约80us产生一次中断 4MHz的 系统时钟
//TMOD[5:4]=10来选择模式 2
//定时器是由清除 TMOD[6]来选择的
//作为一个 8位重载定时器/计数器工作的。
void timer_init(void) //定时器初始化
{
//TMOD = 0x21;
TMOD = TMOD & 0xF0;
TMOD = TMOD | 0x01;
TL0 = T0_LOW;
TH0 = T0_HIGH;
TR0 = 1;
ET0 = 1;
#if 1
TMOD = TMOD & 0x0F;
TMOD = TMOD | 0x20;
PT1 = 1;//优先级
//TH1 = 0xC3;//200us
//TL1 = 0xC3;//
TH1 = 0xE8;//80us--E8
TL1 = 0xE8;//
TR1 = 1; //允许T1跑起来
ET1 = 1;
#endif
}
实现的代码如下:
void TIMER1_ISR (void) interrupt 3 //using 1
{
#if 1
u16_t tmp_respiration_lamp_threshold;
//GPIO0 ^= BIT7; 50us //TH1 = 0xF0;//50us //TL1 = 0xF0;//
//GPIO0 ^= BIT7; //80us 79us
//if(bFlag_respiration_lamp_enable)
if(led_display_mode == BREATHE)
{
//
if(!(++respiration_lamp_timer))
{
TL0 = T0_LOW;
TH0 = T0_HIGH;
//TR0 = 0;
//ET0 = 0;
//256*100us=25600us
//respiration_lamp_timer = 96; //100-->96 200us * 100 =20000us =2ms 2ms*256=512ms *80
//GPIO0 ^= BIT7;//13.5ms 50us*256 = 12800us = 12.8ms
//GPIO0 ^= BIT7
if(led_inc_or_dec_flag)
{
//lamp_series_down
if(!(--respiration_lamp_series))
{
//TURN_ON_RED;
led_inc_or_dec_flag = 0;
//GPIO0 ^= BIT7;// 一呼一吸2.160s 单呼单吸1.080s 13.5ms X 80 = 1080ms //TH1 = 0xF0;//50us //TL1 = 0xF0;//
//++respiration_lamp_num;
//GPIO0 ^= BIT7;//
if(++respiration_lamp_num > 11)respiration_lamp_num = 0;
}
}
else
{
//lamp_series_up
if(++respiration_lamp_series >= CONST_RESPIRATION_LAMP_SERIES)
{
led_inc_or_dec_flag = 1;//TURN_OFF_RED;
//GPIO0 ^= BIT7;// 一呼一吸2.160s 单呼单吸1.080s //TH1 = 0xF0;//50us //TL1 = 0xF0;//
//GPIO0 ^= BIT7;//// 一呼一吸3.260s 单呼单吸1.630s
}
}
//mov here
respiration_lamp_threshold = RESPIRATION_LAMP_TABLE[
respiration_lamp_series];
#if 1 //“频率固定,亮度Y取值为0~255, {MOD}彩用RBG的比例表示,R:B:G 比值不同就有不同的 {MOD}彩。设定 {MOD}彩时使R+B+G=256,然后,R、B、G分别与Y相乘,再除以256,分别作为三个PWM的点空比。”
//这里没有严格按照 cowboy 的回复去做 而是根据 RGB的数组来做,RGB的颜 {MOD}是客户指定的,我用photoshop 试管取的。
red_lamp = RGB[respiration_lamp_num][0];
tmp_respiration_lamp_threshold = respiration_lamp_threshold * red_lamp;
red_lamp = (u8_t)(tmp_respiration_lamp_threshold >> 8);
green_lamp = RGB[respiration_lamp_num][1]; //
tmp_respiration_lamp_threshold = respiration_lamp_threshold * green_lamp;
green_lamp = (u8_t)(tmp_respiration_lamp_threshold >> 8);
blue_lamp = RGB[respiration_lamp_num][2];
tmp_respiration_lamp_threshold = respiration_lamp_threshold * blue_lamp;
blue_lamp = (u8_t)(tmp_respiration_lamp_threshold >> 8);
#endif
}
{
#if 1
if(respiration_lamp_timer >= red_lamp)
{
if(red_lamp_on)
{
red_lamp_on = 0;
LED1_RED_OFF;
LED2_RED_OFF;
LED3_RED_OFF;
LED4_RED_OFF;
LED5_RED_OFF;
LED6_RED_OFF;
LED7_RED_OFF;
LED8_RED_OFF;
}
}
else
{
if(!red_lamp_on)
{
red_lamp_on = 1;
LED1_RED_ON;
LED2_RED_ON;
LED3_RED_ON;
LED4_RED_ON;
LED5_RED_ON;
LED6_RED_ON;
LED7_RED_ON;
LED8_RED_ON;
}
}
#endif
#if 1
if(respiration_lamp_timer >= green_lamp)
{
if(green_lamp_on)
{
green_lamp_on = 0;
LED1_GREEN_OFF;
LED2_GREEN_OFF;
LED3_GREEN_OFF;
LED4_GREEN_OFF;
LED5_GREEN_OFF;
LED6_GREEN_OFF;
LED7_GREEN_OFF;
LED8_GREEN_OFF;
}
}
else
{
if(!green_lamp_on)
{
green_lamp_on = 1;
LED1_GREEN_ON;
LED2_GREEN_ON;
LED3_GREEN_ON;
LED4_GREEN_ON;
LED5_GREEN_ON;
LED6_GREEN_ON;
LED7_GREEN_ON;
LED8_GREEN_ON;
}
}
#endif
#if 1
if(respiration_lamp_timer >= blue_lamp)
{
if(blue_lamp_on)
{
blue_lamp_on = 0;
LED1_BLUE_OFF;
LED2_BLUE_OFF;
LED3_BLUE_OFF;
LED4_BLUE_OFF;
LED5_BLUE_OFF;
LED6_BLUE_OFF;
LED7_BLUE_OFF;
LED8_BLUE_OFF;
}
}
else
{
if(!blue_lamp_on)
{
blue_lamp_on = 1;
LED1_BLUE_ON;
LED2_BLUE_ON;
LED3_BLUE_ON;
LED4_BLUE_ON;
LED5_BLUE_ON;
LED6_BLUE_ON;
LED7_BLUE_ON;
LED8_BLUE_ON;
}
}
#endif
}
}
}
一周热门 更多>