类似单总线,但是对时钟要求比较高,需要能精细到0.5微秒的延时,记得传输数据前需要拉低至少50微秒.
#include <stdio.h>
#include <stdbool.h>
#include "datasheet.h"
#include "gpio.h"
#define RGB_PORT GPIO_PORT_0
#define RGB_PIN GPIO_PIN_3
void set_colori(uint32_t color) {
//author:yoyo(http://yoyo.play175.com/p/da14580-ws2813.html)
int i;
for(i = 0; i < 24; ++i) {
if(color & 1) {
SetWord16(GPIO_BASE + (RGB_PORT << 5) + 2, 1 << RGB_PIN);
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
SetWord16(GPIO_BASE + (RGB_PORT << 5) + 4, 1 << RGB_PIN);
__nop();
__nop();
} else {
SetWord16(GPIO_BASE + (RGB_PORT << 5) + 2, 1 << RGB_PIN);
__nop();
__nop();
SetWord16(GPIO_BASE + (RGB_PORT << 5) + 4, 1 << RGB_PIN);
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
}
color >>= 1;
}
}
void set_color3(uint8_t r, uint8_t g, uint8_t b) {
set_colori((b << 16) | (r << 8) | g);
}
void delay(int ms) {
unsigned int i = 2400 * ms;
while(i--) {}
}
void system_init(void) {
SetWord16(CLK_AMBA_REG, 0x00); // set clocks (hclk and pclk ) 16MHz
SetWord16(SET_FREEZE_REG, FRZ_WDOG); // stop watch dog
SetBits16(SYS_CTRL_REG, PAD_LATCH_EN, 1); // open pads
SetBits16(SYS_CTRL_REG, DEBUGGER_ENABLE, 1); // open debugger
SetBits16(PMU_CTRL_REG, PERIPH_SLEEP, 0); // exit peripheral power down
}
int main (void) {
system_init();
GPIO_ConfigurePin(RGB_PORT, RGB_PIN, OUTPUT, PID_GPIO, false);
SetWord16(GPIO_BASE + (RGB_PORT << 5) + 4, 1 << RGB_PIN);
delay(1);
set_color3(0xff,0,0);
while(1);
}