今天刚收到这个板子,研究了一下,这个板子上有个RGB的LED灯,那我们不从简单的开始让RGB交替显示。
1、电路图
2、实现思路
对GPIO端口 P0口的29,30,31引脚分别置高低电平,实现交替闪烁。
3、实现代码
- #include <LPC54xxx.h> // Device header
- #include <stdint.h>
-
- #define LED_NUM (3)
- #define LED_RED 29
- #define LED_GREEN 30
- #define LED_BLUE 31
-
- const uint32_t led_mask[] = { 1UL << LED_RED, 1UL << LED_GREEN, 1UL << LED_BLUE };
- //打开一个LED
- void LED_On (uint32_t num)
- {
- num = num - 29;
- LPC_GPIO->CLR0 = led_mask[num]; /* LED On */
- }
- //关闭一个LED
- void LED_Off (uint32_t num)
- {
- num = num - 29;
- LPC_GPIO->SET0 = led_mask[num]; /* LED Off */
- }
- //打开或关闭全部的LED
- int32_t LED_SetOut(uint32_t value)
- {
- int i;
-
- for (i = 0; i < LED_NUM; i++)
- {
- if (value & (1<<i))
- {
- LED_On (i);
- } else
- {
- LED_Off(i);
- }
- }
- return 0;
- }
- //初始化LED
- void LED_Initialize (void)
- {
-
- LPC_SYSCON->AHBCLKCTRLSET0 = (1UL << 14); /* enable GPIO0 clock */
-
- LPC_GPIO->DIR0 |= (led_mask[0] | /* configure GPIO as output */
- led_mask[1] |
- led_mask[2] );
-
- LED_SetOut (0); /* switch LEDs off */
- }
- //延时
- void delay(uint32_t num)
- {
- uint32_t i,j;
- for(i=0;i<num;i++)
- for(j=0;j<num;j++)
- {}
- }
-
- int main()
- {
- int flashHz = 1000;
- LED_Initialize();
- while(1)
- {
- LED_On(LED_RED);
- delay(flashHz);
- LED_Off(LED_RED);
- delay(flashHz);
- LED_On(LED_GREEN);
- delay(flashHz);
- LED_Off(LED_GREEN);
- delay(flashHz);
- LED_On(LED_BLUE);
- delay(flashHz);
- LED_Off(LED_BLUE);
- delay(flashHz);
- }
- }
4、实现效果
视频发布中,稍后补上。
本帖最后由 ketose 于 2015-2-16 10:42 编辑