IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    da14580驱动全彩RGB LED(内嵌芯片ws2813/ws2812)

    yoyo发表于 2016-03-01 21:41:00
    love 0

    类似单总线,但是对时钟要求比较高,需要能精细到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);
    }
    


沪ICP备19023445号-2号
友情链接