在CSR8670中,PIO口被映射到一个寄存器中,寄存器中的每一位代表着一个PIO口,比如:我们想让PIO1口为led1,PIO2口为led2,则:
#define LED1 0x01 /* bit 1 */ #define LED2 0x02 /* bit 2 */
然后使用PioSetDir32函数将PIO口设置为output,在通过PioSet32函数来设置led的点亮/熄灭。
下面程序可以实现简单的led1和led2的交替闪烁:
#include <message.h> #include <pio.h> /* Peripheral Input/Output */ #define LED1 0x01 /* bit 1 */ #define DELAY1 200 /* ms */ #define LED2 0x02 /* bit 2 */ #define DELAY2 400 /* ms */ static void led_controller1( Task t, MessageId id, Message payload ) { PioSet32( LED1, (PioGet32() ^ LED1) ); MessageSendLater( t, 0, 0, DELAY1 ); } static void led_controller2( Task t, MessageId id, Message payload ) { PioSet32( LED2, (PioGet32() ^ LED2) ); MessageSendLater( t, 0, 0, DELAY2 ); } static TaskData led_controller1_task = { led_controller1 }; static TaskData led_controller2_task = { led_controller2 }; int main(void) { PioSetDir32(0xFF, 0xFF); /* Set all PIO to be output */ PioSet32(0xFF, 0); /* Set all PIO off (0) */ MessageSend( &led_controller1_task, 0 , 0 ); MessageSend( &led_controller2_task, 0 , 0 ); MessageLoop(); return 0; }
当我们需要led表现出复杂的动作时,上述编写代码就显得臃肿和繁琐,为此,CSR提供了ledparse.exe工具来解析一个可以用来自定义led动作的.led文件,我们可以将我们想要led表现的动作写在.led文件中,比如:我们想实现led1 ~ led4的跑马灯效果,则我们只需编写这样一个example.led文件即可,
// An example LED file // ‘led‘ is used to define the dedicated output LED that are available on certain // BlueCore Variants. // ‘pio‘ is used for controlling standard pio lines. #ifdef BC5_MODULE led 0 LED2 led 1 LED1 pio 0 LED3 pio 1 LED4 #else pio 0 LED1 pio 1 LED2 pio 2 LED3 pio 3 LED4 #endif // Flash each LED, one after the other. pattern PATTERN1 RPT LED1 ON 200 LED1 OFF 0 LED2 ON 200 LED2 OFF 0 LED3 ON 200 LED3 OFF 0 LED4 ON 200 LED4 OFF 0 // Flash alternate pairs pattern PATTERN2 RPT LED1 LED3 ON 200 LED1 LED3 OFF 0 LED2 LED4 ON 200 LED2 LED4 OFF 0
然后通过ledparse.exe工具对其进行解析:
ledparse example.led example
即可生成example.c和example.h代码文件,我们在主文件中引用example.h文件即可:
#include <message.h> #include <charger.h> #include <pio.h> /* Peripheral Input/Output */ #include <print.h> /* debug PRINT */ #include "example.h" #define DELAY 5000 #define NO_OF_PATTERNS (2) uint8 patterns[] = { PATTERN1, PATTERN2}; uint8 count; static void led_controller1( Task t, MessageId id, Message payload ) { PRINT(("Pattern No. %d\n", count )); ledsPlay( patterns[count] ); count++; count %= NO_OF_PATTERNS; MessageSendLater( t, 0, 0, DELAY ); } static TaskData led_controller1_task = { led_controller1 }; int main(void) { #if BC5_MODULE /* Prevent the LED0 flashing during charging */ ChargerConfigure(CHARGER_SUPPRESS_LED0, 1); #endif MessageSend( &led_controller1_task, 0 , 0 ); MessageLoop(); return 0; }
至此一个led的跑马灯效果就出来了,同时led文件还定义了另一个led动作。
时间: 2024-11-04 09:21:41