[DemoCode下载] M258 GPIO 实现 ARGB2 LED 灯效

[复制链接]
824|1
 楼主| 643757107 发表于 2022-6-13 21:23 | 显示全部楼层 |阅读模式
GB, IO, ar, gp, pi
EC_M258_GPIO_ARGB2_LED_Control_V1.00.zip (5.87 MB, 下载次数: 2)
码使用M251、M252、M254、M256、M258系列的GPIO来模拟第二代灯条(ARGB2) 的时序,并演示ARGB2 LED 彩虹灯、跑马灯效果。此范例代码使用一组Timer来定时更新ARGB2 LED彩虹灯、跑马灯的颜色,一组UART用来选择执行模式。
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show how to use GPIO input/output to control ARGB2 strip.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2022 Nuvoton Technology Corp. All rights reserved.
  8. ******************************************************************************/
  9. #include "stdio.h"
  10. #include "NuMicro.h"
  11. #include "Nuvoton_ARGB2_Strip.h"

  12. /*---------------------------------------------------------------------------------------------------------*/
  13. /* Global variables                                                                                        */
  14. /*---------------------------------------------------------------------------------------------------------*/
  15. volatile int32_t g_i32Wait = TRUE;
  16. int FeedbackMode_LEDcnt = 0;
  17. int ch;

  18. char        MODE = '0';                                                                                                                        //Which MODE to run,        0: Strip_GRBLoop()
  19.                                                                                                                                                                                         //                                                                                1: Strip_rainbowCycle()
  20.                                                                                                                                                                                         //                                                                                2: Strip_theaterChaseRainbow()
  21.                                                                                                                                                                                         //                                                                                3: Strip_feedbackMode()
  22.                                                                                                                                                                                         //                                                                                4: Strip_Th20SetMode()
  23.                                                                                                                                                                                         //                                                                                 5: Y cable mode()


  24. //Y Cable Mode
  25. char Command = '2';                                                                                                                //Set_ID = 1, Clr_ID = 2, Chk_ID = 3, Specify_ID = 4
  26. char ID        = '0';                                                                                                                                 //range: 0x0~0xf

  27. char ID_set_status[16] = {0, 0, 0, 0, 0, 0,
  28.                                                                                                         0, 0, 0, 0, 0,
  29.                                                                                                         0, 0, 0, 0, 0};
  30. char ID_set_status_chg = FALSE;

  31. char ID_chk_status[16] = {0, 0, 0, 0, 0, 0,
  32.                                                                                                         0, 0, 0, 0, 0,
  33.                                                                                                         0, 0, 0, 0, 0};
  34. char ID_chk_status_chg = FALSE;

  35. extern int YCableMode_SetID_cnt;
  36. extern int YCableMode_ChkID_cnt;

  37. /*---------------------------------------------------------------------------------------------------------*/
  38. /* Define functions prototype                                                                              */
  39. /*---------------------------------------------------------------------------------------------------------*/
  40. void TerminalShow_Init(void);
  41. void FeedbackMode_waitFeedback(void);
  42. void YCableMode_SetCommandID(void);

  43. void SYS_Init(void)
  44. {

  45.     /*---------------------------------------------------------------------------------------------------------*/
  46.     /* Init System Clock                                                                                       */
  47.     /*---------------------------------------------------------------------------------------------------------*/
  48.                
  49.     /* Enable HIRC clock (Internal RC 48 MHz) */
  50.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  51.     /* Wait for HIRC clock ready */
  52.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  53.     /* Select HCLK clock source as HIRC and and HCLK clock divider as 1 */
  54.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
  55.                
  56.                 /* Set both PCLK0 and PCLK1 as HCLK */
  57.     CLK->PCLKDIV = CLK_PCLKDIV_APB0DIV_DIV1 | CLK_PCLKDIV_APB1DIV_DIV1;

  58.     /* Select IP clock source */
  59.     /* Select UART0 clock source is HIRC */
  60.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
  61.                
  62.                 /* Enable IP clock */
  63.     CLK_EnableModuleClock(GPC_MODULE);
  64.                 CLK_EnableModuleClock(UART0_MODULE);
  65.     CLK_EnableModuleClock(TMR0_MODULE);
  66.                
  67.                 /* Select IP clock source */
  68.                 CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK0, 0);
  69.                
  70.                 /* Update System Core Clock */
  71.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  72.     SystemCoreClockUpdate();
  73.                
  74.                 /*----------------------------------------------------------------------*/
  75.     /* Init I/O Multi-function                                              */
  76.     /*----------------------------------------------------------------------*/
  77.     /* Set PB multi-function pins for UART0 RXD and TXD */
  78.     Uart0DefaultMPF();
  79.                
  80. }

  81. /*---------------------------------------------------------------------------------------------------------*/
  82. /* ISR to handle GPIO(PC) interrupt event                                                                                              */
  83. /*---------------------------------------------------------------------------------------------------------*/
  84. void GPC_IRQHandler(void)
  85. {
  86.                 /* To check if PC.2 interrupt occurred */
  87.     if (GPIO_GET_INT_FLAG(Dout_Port, Dout_PinMask))
  88.                 {
  89.                                 GPIO_CLR_INT_FLAG(Dout_Port, Dout_PinMask);
  90.                                
  91.                                 if(MODE == '3')
  92.                                 {
  93.                                                 FeedbackMode_LEDcnt++;
  94.                                 }
  95.                                 else if((MODE == '5') && (Command == 1))
  96.                                 {
  97.                                                 ID_set_status[YCableMode_SetID_cnt] = 1;
  98.                                                 ID_set_status_chg = TRUE;
  99.                                 }
  100.                                 else if((MODE == '5') && ((Command == 3) || (Command == 4)))
  101.                                 {
  102.                                                 ID_chk_status[YCableMode_ChkID_cnt] = 1;
  103.                                                 ID_chk_status_chg = TRUE;
  104.                                 }
  105.                 }
  106.                 else
  107.                 {
  108.                                 uint32_t u32Status;
  109.         u32Status = PC->INTSRC;
  110.         /* Un-expected interrupt. Just clear all PC interrupts */
  111.         PC->INTSRC = u32Status;
  112.                 }
  113. }

  114. /*---------------------------------------------------------------------------------------------------------*/
  115. /* Init UART0                                                                                             */
  116. /*---------------------------------------------------------------------------------------------------------*/
  117. void UART0_Init(void)
  118. {
  119.                 /* Configure UART0 and set UART0 baud rate */
  120.     UART_Open(UART0, 115200);
  121. }

  122. /*---------------------------------------------------------------------------------------------------------*/
  123. /* ISR to handle UART0 interrupt event                                                            */
  124. /*---------------------------------------------------------------------------------------------------------*/
  125. void UART0_IRQHandler(void)
  126. {
  127.        
  128.                 uint32_t u32IntSts = UART0->INTSTS;
  129.                
  130.                 g_i32Wait = FALSE;
  131.                
  132.                 if ((u32IntSts & UART_INTSTS_RDAINT_Msk) || (u32IntSts & UART_INTSTS_RXTOINT_Msk))
  133.                 {
  134.                                 /* Get all the input characters */
  135.                                 while (UART_GET_RX_EMPTY(UART0) == 0)
  136.                                 {
  137.                                                 /* Get the character from UART Buffer */
  138.                                                 uint8_t u8InChar = UART_READ(UART0);
  139.                                                 MODE = u8InChar;
  140.                                                 printf("%c\n\n", u8InChar);       
  141.                                                
  142.                                                 if(MODE == '5')
  143.                                                 {
  144.                                                                 UART_DisableInt(UART0, UART_INTEN_RDAIEN_Msk);                                               
  145.                                                                
  146.                                                                 YCableMode_SetCommandID();
  147.                                                                
  148.                                                                 UART_EnableInt(UART0, UART_INTEN_RDAIEN_Msk);
  149.                                                 }
  150.                                                 else
  151.                                                 {
  152.                                                                 if((MODE == '0') || (MODE == '1') || (MODE == '2'))
  153.                                                                 {
  154.                                                                                 GPIO_SetMode(Dout_Port, Dout_PinMask, GPIO_MODE_OUTPUT);
  155.                                                                 }
  156.                                                                                        
  157.                                                                 printf("+---------------------------------------+\n");
  158.                                                                 printf("  Press the key to start test ARGB2\n");
  159.                                                                 printf("+---------------------------------------+\n");
  160.                                                                 printf("    0: Run GRBLoop\n");
  161.                                                                 printf("    1: Run rainbowCycle\n");
  162.                                                                 printf("    2: Run theaterChaseRainbow\n");
  163.                                                                 printf("    3: Run Feedback mode\n");
  164.                                                                 printf("    4: Run TH20 SET mode\n");
  165.                                                                 printf("    5: Run Y cable mode\n");
  166.                                                                 printf("    Others: mode pause\n");
  167.                                                                 printf("+---------------------------------------+\n");
  168.                                                                
  169.                                                                 if((MODE == '0') || (MODE == '1') || (MODE == '2') || (MODE == '3') || (MODE == '4') || (MODE == '5'))
  170.                                                                 {
  171.                                                                                 printf("Start to run MODE: %c!\n\n", MODE);
  172.                                                                                
  173.                                                                                 if(MODE == '3')
  174.                                                                                 {
  175.                                                                                                 printf("Please check only one strip to test.\n");
  176.                                                                                 }
  177.                                                                                 else
  178.                                                                                 {
  179.                                                                                                 printf("Press the key: ");
  180.                                                                                 }
  181.                                                                 }
  182.                                                                 else
  183.                                                                 {
  184.                                                                                 printf("Please press key range from 0 to 5\n");
  185.                                                                                 printf("Press the key: ");
  186.                                                                 }                                                                               
  187.                                                 }                       
  188.                                 }
  189.                                
  190.                                 GPIO_SetMode(Dout_Port, Dout_PinMask, GPIO_MODE_OUTPUT);
  191.                                
  192.                                 /*Forces a write of all user-space buffered data for the given output*/
  193.         fflush(stdout);               
  194.                 }
  195.                
  196.                 if (UART0->FIFOSTS & (UART_FIFOSTS_BIF_Msk | UART_FIFOSTS_FEF_Msk | UART_FIFOSTS_PEF_Msk | UART_FIFOSTS_RXOVIF_Msk))
  197.     {
  198.         UART0->FIFOSTS = (UART_FIFOSTS_BIF_Msk | UART_FIFOSTS_FEF_Msk | UART_FIFOSTS_PEF_Msk | UART_FIFOSTS_RXOVIF_Msk);
  199.     }
  200. }

  201. /*---------------------------------------------------------------------------------------------------------*/
  202. /* Init TIMER0                                                                                             */
  203. /*---------------------------------------------------------------------------------------------------------*/
  204. void TIMER0_Init(void)
  205. {
  206.     /* Configure and set TIMER0 */
  207.     // Give a dummy target frequency here. Will over write prescale and compare value with macro
  208.     TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 48000000);

  209.     // Update prescale and compare value to what we need in event counter mode.
  210.     TIMER_SET_PRESCALE_VALUE(TIMER0, 0);
  211.     TIMER_SET_CMP_VALUE(TIMER0, 160);
  212.        
  213.     // Counter increase on rising edge
  214.     TIMER_EnableEventCounter(TIMER0, TIMER_COUNTER_EVENT_RISING);
  215. }

  216. /*---------------------------------------------------------------------------------------------------------*/
  217. /*  Main Function                                                                                          */
  218. /*---------------------------------------------------------------------------------------------------------*/
  219. int main(void)
  220. {

  221.     /* Unlock protected registers */
  222.     SYS_UnlockReg();

  223.     /* Init System, peripheral clock and multi-function I/O */
  224.     SYS_Init();
  225.                
  226.                 /* Init UART0 for printf and test */
  227.     UART0_Init();

  228.     /* Lock protected registers */
  229.     SYS_LockReg();

  230.     /* Init ARGB2 Strip */
  231.     Strip_Init();
  232.                
  233.                 /* Init display of first show in terminal*/
  234.     TerminalShow_Init();
  235.                
  236.           while(1)
  237.                 {
  238.                                 switch(MODE)
  239.                                 {
  240.                                         case '0':                                                                                                                //Run GRBLoop
  241.                                                 Strip_GRBLoop();
  242.                                                 break;
  243.                                         case '1':                                                                                                                //Run rainbowCycle
  244.                                                 Strip_rainbowCycle();
  245.                                                 break;
  246.                                         case '2':                                                                                                                //Run theaterChaseRainbow
  247.                                                 Strip_theaterChaseRainbow();
  248.                                                 break;
  249.                                         case '3':                                                                                                                //feedback mode                                               
  250.                                                 Strip_FeedbackMode();
  251.                                                 FeedbackMode_waitFeedback();
  252.                                                 break;
  253.                                         case '4':                                                                                                                //TH20 SET mode                                       
  254.                                                 Strip_Th20SetMode();
  255.                                                 break;
  256.                                         case '5':                                                                                                                //Y cable mode                                       
  257.                                                 Strip_YCableMode();
  258.                                                 break;
  259.                                         default:
  260.                                                 break;
  261.                                 }
  262.                 }
  263.                
  264. }

  265. /**
  266.   * @brief  Setting of first show in terminal after power on
  267.   * @param  None
  268.   * @retval None
  269.   */
  270. void TerminalShow_Init(void)
  271. {
  272.                 printf("+---------------------------------------+\n");
  273.                 printf("  Press the key to start test ARGB2\n");
  274.                 printf("+---------------------------------------+\n");
  275.                 printf("    0: Run GRBLoop\n");
  276.                 printf("    1: Run rainbowCycle\n");
  277.                 printf("    2: Run theaterChaseRainbow\n");
  278.                 printf("    3: Run Feedback mode\n");
  279.                 printf("    4: Run TH20 SET mode\n");
  280.                 printf("    5: Run Y cable mode\n");
  281.                 printf("    Others: mode pause\n");
  282.                 printf("+---------------------------------------+\n");
  283.                 printf("  Press the key: ");
  284.                
  285.                 UART_SetTimeoutCnt(UART0, 0x10);

  286.     UART0->FIFO &= ~ UART_FIFO_RFITL_Msk;
  287.     UART0->FIFO |= UART_FIFO_RFITL_1BYTE;
  288.                
  289.                 /* Enable UART RDA/THRE/Time-out interrupt */
  290.     NVIC_EnableIRQ(UART0_IRQn);
  291.                 UART_EnableInt(UART0, UART_INTEN_RDAIEN_Msk);
  292.                
  293.                 while (g_i32Wait);
  294.                
  295.                 g_i32Wait = FALSE;
  296. }

  297. /**
  298.   * @brief  Wait pulse from ARGB2 that know LED's number in a strip
  299.   * @param  None
  300.   * @retval None
  301.   */
  302. void FeedbackMode_waitFeedback(void)
  303. {
  304.                 TIMER_Delay(TIMER0, 1000000);
  305.                                        
  306.                 printf("There are %d pcs LED in the strip\n", FeedbackMode_LEDcnt);
  307.        
  308.                 GPIO_DisableInt(Dout_Port, Dout_IRQn_Pin);
  309.                 FeedbackMode_LEDcnt = 0;
  310.                 printf("Press ENTER key to Continue...\n");  
  311.                 scanf("%d",&ch);
  312. }

  313. /**
  314.   * @brief  Use to show information and set Command and ID about Y Cable mode
  315.   * @param  None
  316.   * @retval None
  317.   */
  318. void YCableMode_SetCommandID(void)
  319. {
  320.                 printf("Start the setting of Y Cable mode\n");
  321.                
  322.                 printf("Please set Command, then press the ENTER.: (Set_ID = 1, Clr_ID = 2, Chk_ID = 3, Specify_ID = 4)\n");
  323.                 printf("Command:");
  324.                 scanf("%d",&ch);
  325.                 Command = ch;
  326.                 printf("%d\n\n", Command);
  327.                                                                
  328.                 printf("Please set ID, then press the ENTER.: (Range: 0x0~0xf)\n");
  329.                 printf("ID:");
  330.                 scanf("%d",&ch);
  331.                 ID = ch;       
  332.                 printf("%d\n\n", ID);
  333.                 printf("You set the Command = %d, ID = %d\n\n", Command, ID);
  334.                
  335.                 if(ID > 0xf)
  336.                 {
  337.                                 printf("Setting ID is over range. (0x0~0xf)\n");
  338.                                 printf("Leave Y Cable mode.\n\n");
  339.                                
  340.                                 printf("Press any key to start test.\n");
  341.                        
  342.                                 return;
  343.                 }
  344.                
  345.                 if(Command == 1)
  346.                 {
  347.                                 printf("Run Set_ID function. Setting number is all strip in parallel. ID start from %d to last .\n", ID);
  348.                                 printf("You can check waveform from an oscilloscope now.\n");
  349.                 }
  350.                 else if(Command == 2)
  351.                 {
  352.                                 if(ID == 0)
  353.                                 {
  354.                                                 printf("Run Clr_ID function. Clear all strip's ID to 0.\n");
  355.                                 }
  356.                                 else if((ID > 0) && (ID <= 0xf))
  357.                                 {
  358.                                                 printf("Run Clr_ID. Clear strip's ID to 0.\n");
  359.                                 }               
  360.                                
  361.                                 printf("You can check waveform from an oscilloscope now.\n");
  362.                 }
  363.                 else if(Command == 3)
  364.                 {
  365.                                 printf("Run Chk_ID function. Checking number is all strip in parallel. ID start from %d to last .\n", ID);
  366.                                 printf("You can check waveform from an oscilloscope now.\n");
  367.                 }
  368.                 else if(Command == 4)
  369.                 {
  370.                                 printf("Run Specify_ID function. Specifying number is all strip in parallel. ID start from %d to last one.\n", ID);
  371.                                 printf("You can see all strip show the different light effect.\n");
  372.                 }
  373.                 else
  374.                 {
  375.                                 printf("Set Command error!\n");
  376.                                 printf("Leave Y Cable mode.\n\n");
  377.                 }
  378.                
  379.                
  380.                 printf("Press any key to run other mode.\n");
  381. }



kiwis66 发表于 2022-6-19 14:42 | 显示全部楼层
ARGB2是灯带的型号,还是什么
您需要登录后才可以回帖 登录 | 注册

本版积分规则

223

主题

3955

帖子

11

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