[DemoCode下载] ML51驱动WS2812

[复制链接]
1001|1
 楼主| xuanhuanzi 发表于 2023-11-28 19:13 | 显示全部楼层 |阅读模式
ML51_RGBLED-main.zip (1.33 MB, 下载次数: 4)
  1. /*_____ I N C L U D E S ____________________________________________________*/
  2. #include "ML51.h"

  3. #include "project_config.h"
  4. #include "LED_stripe.h"

  5. /*_____ D E C L A R A T I O N S ____________________________________________*/
  6. // xdata volatile uint8_t u8TH0_Tmp = 0;
  7. // xdata volatile uint8_t u8TL0_Tmp = 0;

  8. #define TH0_INIT                                                        (HIBYTE(TIMER_DIV12_VALUE_1ms_FOSC_240000))
  9. #define TL0_INIT                                                        (LOBYTE(TIMER_DIV12_VALUE_1ms_FOSC_240000))

  10. //UART 0
  11. bit BIT_UART;
  12. bit uart0_receive_flag=0;
  13. unsigned char uart0_receive_data;


  14. uint16_t counter = 0;
  15. /*_____ D E F I N I T I O N S ______________________________________________*/
  16. volatile uint32_t BitFlag = 0;
  17. volatile uint32_t counter_tick = 0;

  18. /*_____ M A C R O S ________________________________________________________*/
  19. #define SYS_CLOCK                                                                 (24000000ul)


  20. /*_____ F U N C T I O N S __________________________________________________*/


  21. void tick_counter(void)
  22. {
  23.         counter_tick++;
  24. }

  25. uint32_t get_tick(void)
  26. {
  27.         return (counter_tick);
  28. }

  29. void set_tick(uint32_t t)
  30. {
  31.         counter_tick = t;
  32. }

  33. //void compare_buffer(uint8_t *src, uint8_t *des, int nBytes)
  34. //{
  35. //    uint16_t i = 0;       
  36. //       
  37. //    for (i = 0; i < nBytes; i++)
  38. //    {
  39. //        if (src[i] != des[i])
  40. //        {
  41. //            printf("error idx : %4d : 0x%2X , 0x%2X\r\n", i , src[i],des[i]);
  42. //                        set_flag(flag_error , Enable);
  43. //        }
  44. //    }

  45. //        if (!is_flag_set(flag_error))
  46. //        {
  47. //            printf("compare_buffer finish \r\n");       
  48. //                set_flag(flag_error , Disable);
  49. //        }

  50. //}

  51. //void reset_buffer(void *dest, unsigned int val, unsigned int size)
  52. //{
  53. //    uint8_t *pu8Dest;
  54. ////    unsigned int i;
  55. //   
  56. //    pu8Dest = (uint8_t *)dest;

  57. //        #if 1
  58. //        while (size-- > 0)
  59. //                *pu8Dest++ = val;
  60. //        #else
  61. //        memset(pu8Dest, val, size * (sizeof(pu8Dest[0]) ));
  62. //        #endif
  63. //       
  64. //}

  65. //void copy_buffer(void *dest, void *src, unsigned int size)
  66. //{
  67. //    uint8_t *pu8Src, *pu8Dest;
  68. //    unsigned int i;
  69. //   
  70. //    pu8Dest = (uint8_t *)dest;
  71. //    pu8Src  = (uint8_t *)src;


  72. //        #if 0
  73. //          while (size--)
  74. //            *pu8Dest++ = *pu8Src++;
  75. //        #else
  76. //    for (i = 0; i < size; i++)
  77. //        pu8Dest[i] = pu8Src[i];
  78. //        #endif
  79. //}

  80. //void dump_buffer(uint8_t *pucBuff, int nBytes)
  81. //{
  82. //    uint16_t i = 0;
  83. //   
  84. //    printf("dump_buffer : %2d\r\n" , nBytes);   
  85. //    for (i = 0 ; i < nBytes ; i++)
  86. //    {
  87. //        printf("0x%2X," , pucBuff[i]);
  88. //        if ((i+1)%8 ==0)
  89. //        {
  90. //            printf("\r\n");
  91. //        }            
  92. //    }
  93. //    printf("\r\n\r\n");
  94. //}

  95. //void  dump_buffer_hex(uint8_t *pucBuff, int nBytes)
  96. //{
  97. //    int     nIdx, i;

  98. //    nIdx = 0;
  99. //    while (nBytes > 0)
  100. //    {
  101. //        printf("0x%04X  ", nIdx);
  102. //        for (i = 0; i < 16; i++)
  103. //            printf("%02X ", pucBuff[nIdx + i]);
  104. //        printf("  ");
  105. //        for (i = 0; i < 16; i++)
  106. //        {
  107. //            if ((pucBuff[nIdx + i] >= 0x20) && (pucBuff[nIdx + i] < 127))
  108. //                printf("%c", pucBuff[nIdx + i]);
  109. //            else
  110. //                printf(".");
  111. //            nBytes--;
  112. //        }
  113. //        nIdx += 16;
  114. //        printf("\n");
  115. //    }
  116. //    printf("\n");
  117. //}

  118. void delay(uint16_t dly)
  119. {
  120. /*
  121.         delay(100) : 14.84 us
  122.         delay(200) : 29.37 us
  123.         delay(300) : 43.97 us
  124.         delay(400) : 58.5 us       
  125.         delay(500) : 73.13 us       
  126.        
  127.         delay(1500) : 0.218 ms (218 us)
  128.         delay(2000) : 0.291 ms (291 us)       
  129. */

  130.         while( dly--);
  131. }


  132. //void send_UARTString(uint8_t* Data)
  133. //{
  134. //        #if 1
  135. //        uint16_t i = 0;

  136. //        while (Data[i] != '\0')
  137. //        {
  138. //                #if 1
  139. //                SBUF = Data[i++];
  140. //                #else
  141. //                UART_Send_Data(UART0,Data[i++]);               
  142. //                #endif
  143. //        }

  144. //        #endif

  145. //        #if 0
  146. //        uint16_t i = 0;
  147. //       
  148. //        for(i = 0;i< (strlen(Data)) ;i++ )
  149. //        {
  150. //                UART_Send_Data(UART0,Data[i]);
  151. //        }
  152. //        #endif

  153. //        #if 0
  154. //    while(*Data)  
  155. //    {  
  156. //        UART_Send_Data(UART0, (unsigned char) *Data++);  
  157. //    }
  158. //        #endif
  159. //}

  160. //void send_UARTASCII(uint16_t Temp)
  161. //{
  162. //    uint8_t print_buf[16];
  163. //    uint16_t i = 15, j;

  164. //    *(print_buf + i) = '\0';
  165. //    j = (uint16_t)Temp >> 31;
  166. //    if(j)
  167. //        (uint16_t) Temp = ~(uint16_t)Temp + 1;
  168. //    do
  169. //    {
  170. //        i--;
  171. //        *(print_buf + i) = '0' + (uint16_t)Temp % 10;
  172. //        (uint16_t)Temp = (uint16_t)Temp / 10;
  173. //    }
  174. //    while((uint16_t)Temp != 0);
  175. //    if(j)
  176. //    {
  177. //        i--;
  178. //        *(print_buf + i) = '-';
  179. //    }
  180. //    send_UARTString(print_buf + i);
  181. //}

  182. void WS2812C_Init(void)
  183. {
  184.         #if defined (ENABLE_SPI)

  185.         #if 1
  186.     /* Set LED output */
  187.     P00 = 1;
  188.     P00_INPUT_MODE;
  189.     ENABLE_P00_PULLDOWN;
  190.         #endif


  191.     MFP_P00_SPI0_MOSI;
  192.     // P00_QUASI_MODE;

  193.     MFP_P02_SPI0_CLK;       /*define  SPI0 CLK pin */
  194.     P02_QUASI_MODE;       
  195.         #if 0
  196.     MFP_P01_SPI0_MISO;      /*define  SPI0 MISO pin */
  197.     P01_QUASI_MODE;

  198.     MFP_P03_GPIO;           /*define  SPI0 SS pin as normal GPIO*/
  199.     P03_PUSHPULL_MODE;
  200.         #endif

  201.     set_SPI0SR_DISMODF;                                        // SS General purpose I/O ( No Mode Fault )
  202.     clr_SPI0CR0_SSOE;

  203.     clr_SPI0CR0_LSBFE;                                  // MSB first

  204.     clr_SPI0CR0_CPOL;                                   // The SPI clock is low in idle mode
  205.     clr_SPI0CR0_CPHA;                                   // The data is sample on the second edge of SPI clock

  206.     set_SPI0CR0_MSTR;                                   // SPI in Master mode
  207.     SPI0_CLOCK_DIV_3;                                        // Select SPI clock
  208.     set_SPI0CR0_SPIEN;                                  // Enable SPI function
  209.     clr_SPI0SR_SPIF;

  210.         #elif defined (ENABLE_GPIO_EMULATE)
  211.         P00_PUSHPULL_MODE;
  212.         P02_PUSHPULL_MODE;        // for debug
  213.         #endif
  214. }


  215. void loop(void)
  216. {
  217.         static uint8_t flag_first_pwr_on = 1;

  218.         if (is_flag_set(flag_1000ms))
  219.         {
  220.                 set_flag(flag_1000ms , FALSE);
  221.                 P32 ^= 1;               
  222.                 SFRS = 0;printf("log :%d\r\n" , counter++);               
  223.         }

  224.     if (RI)
  225.     {   
  226.                 clr_SCON_RI;                                         // Clear RI (Receive Interrupt).

  227.                 if (flag_first_pwr_on)
  228.                 {
  229.                         DemoState = state_Rainbow;
  230.                         flag_first_pwr_on = 0;
  231.                 }
  232.                 else
  233.                 {
  234.                         DemoState = SBUF - 0x30;
  235.                 }

  236.                 SFRS = 0;printf("DemoState = %bu,%bc\r\n" , DemoState, SBUF);
  237.     }

  238.         if (!trans_finish_flag)
  239.         {
  240.                 #if defined (ENABLE_DEBUG_WS2812_PROTOCOL)
  241.                 setLED_ColorWipe(0xFF, 0x00, 0x00);
  242.                 setLED_delayms(1);
  243.                 #else

  244.                 StateMachine();
  245.                 #endif
  246.         }       
  247. }


  248. void GPIO_Init(void)
  249. {
  250.     MFP_P32_GPIO;
  251.     P32_PUSHPULL_MODE;       
  252.                

  253. //        P30_PUSHPULL_MODE;       
  254. }

  255. void Timer0_IRQHandler(void)
  256. {
  257.         tick_counter();

  258.         if ((get_tick() % 1000) == 0)
  259.         {
  260.                 set_flag(flag_1000ms , TRUE);
  261.         }

  262.         if ((get_tick() % 50) == 0)
  263.         {

  264.         }               

  265. }

  266. void Timer0_ISR(void) interrupt 1        // Vector [url=home.php?mod=space&uid=72445]@[/url]  0x0B
  267. {
  268.         _push_(SFRS);       
  269.         SFRS = 0;

  270.         clr_TCON_TF0;               
  271.         TH0 = TH0_INIT;
  272.         TL0 = TL0_INIT;

  273.         Timer0_IRQHandler();
  274.         _pop_(SFRS);       
  275. }

  276. void Timer0_Init(void)
  277. {
  278.         ENABLE_TIMER0_MODE1;
  279.         TIMER0_FSYS_DIV12;

  280.         TH0 = TH0_INIT;
  281.         TL0 = TL0_INIT;
  282.         clr_TCON_TF0;

  283.         set_TCON_TR0;                                  //Timer0 run
  284.        
  285.         ENABLE_TIMER0_INTERRUPT;                       //enable Timer0 interrupt
  286.         ENABLE_GLOBAL_INTERRUPT;                       //enable interrupts       
  287. }


  288. // void Serial_ISR (void) interrupt 4
  289. // {
  290. //     _push_(SFRS);

  291. //     if (RI)
  292. //     {   
  293. //       uart0_receive_flag = 1;
  294. //       uart0_receive_data = SBUF;
  295. //       clr_SCON_RI;                                         // Clear RI (Receive Interrupt).
  296. //     }
  297. //     if  (TI)
  298. //     {
  299. //                 clr_SCON_TI;
  300. //                 //      if(!BIT_UART)
  301. //                 //      {
  302. //                 //          TI = 0;
  303. //                 //      }
  304. //     }

  305. //     _pop_(SFRS);       
  306. // }


  307. void UART0_Init(void)
  308. {
  309.         MFP_P31_UART0_TXD;                              
  310.         P31_QUASI_MODE;                                 
  311.         MFP_P30_UART0_RXD;
  312.         P30_QUASI_MODE;       

  313.         SFRS = 0x00;
  314.         SCON = 0x50;                    /*UART0 Mode1,REN=1,TI=1*/
  315.         set_PCON_SMOD;                  /*UART0 Double Rate Enable*/
  316.         T3CON &= 0xF8;                   /*T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1)*/
  317.         set_T3CON_BRCK;                  /*UART0 baud rate clock source = Timer3*/
  318.         RH3    = 0xFF;                                           /* HIBYTE(65536 - 13)*/
  319.         RL3    = 0xF3;                                           /* LOBYTE(65536 - 13); */
  320.         set_T3CON_TR3;                  /*Trigger Timer3*/

  321.         set_SCON_RI;
  322.         set_SCON_TI;
  323.         BIT_UART = 1;

  324.         // ENABLE_UART0_INTERRUPT;                                   /* Enable UART0 interrupt */
  325.         // ENABLE_GLOBAL_INTERRUPT;                                  /* Global interrupt enable */

  326.         SFRS = 0;printf("\r\n\r\n\r\n\r\nUART0_Init\r\n");
  327. }

  328. void SYS_Init(void)
  329. {
  330. //        FsysSelect(FSYS_HIRC);

  331.     ALL_GPIO_QUASI_MODE;
  332. //    ENABLE_GLOBAL_INTERRUPT;                // global enable bit       
  333. }

  334. void main (void)
  335. {
  336.         SYS_Init();

  337.         UART0_Init();
  338.         GPIO_Init();

  339.     WS2812C_Init();
  340.         // setLED_ColorWipe(0, 0, 0);

  341.         Timer0_Init();

  342.         while(1)
  343.         {
  344.                 loop();

  345.         }
  346. }





gejigeji521 发表于 2023-11-28 22:15 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

183

主题

2331

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部