u_pwm_drive.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "user_driver.h"
  2. //#include "t_pwm.h"
  3. #include "u_main.h"
  4. //#define PWM_DEBUG_EN
  5. #if 1
  6. //#define PWM_CHANCE_R (8)
  7. //#define PWM_CHANCE_G (9)
  8. //#define PWM_CHANCE_B (10) //bk
  9. #define PWM_CHANCE_R (24)
  10. #define PWM_CHANCE_G (0)
  11. #define PWM_CHANCE_B (25)
  12. #define PWM_RT_PERIOD (3400)
  13. #define PWM_PERIOD (797)
  14. void light_set_color(uint32_t color_rgb)
  15. {
  16. float clr_temp[3] = {0x00}, chan_0 = 0, chan_1 = 0, chan_2 = 0, rgb_num = 0;
  17. uint32_t pwm_chan_0 = 0, pwm_chan_1 = 0, pwm_chan_2 = 0;
  18. //以下为计算占空比相关设置
  19. clr_temp[1] = ((color_rgb & 0x0ff) * 3125 + 500) / 1000;
  20. clr_temp[0] = (((color_rgb >> 8) & 0x0ff) * 3125 + 500) / 1000;
  21. clr_temp[2] = (((color_rgb >> 16) & 0x0ff) * 3125 + 500) / 1000;
  22. rgb_num = clr_temp[1] + clr_temp[0] + clr_temp[2];
  23. #if PWM_DEBUG_EN
  24. U_UART_PRINTF("RGB = 0x%06X %d \r\n", color_rgb,color_rgb);
  25. U_UART_PRINTF("RGB = %d, %d, %d \r\n", clr_temp[1], clr_temp[0], clr_temp[2]);
  26. U_UART_PRINTF("rgb_num = %d\r\n", rgb_num);
  27. #endif
  28. if (rgb_num <= 1594)
  29. {
  30. }
  31. else if (rgb_num >= 2300) //797*3
  32. {
  33. clr_temp[1] = 75 * clr_temp[1] / 100;
  34. clr_temp[0] = 75 * clr_temp[0] / 100;
  35. clr_temp[2] = 75 * clr_temp[2] / 100;
  36. }
  37. else if (rgb_num > 1594)
  38. {
  39. float rt_k = 1594.0 / (clr_temp[1] + clr_temp[0] + clr_temp[2]);
  40. clr_temp[1] = rt_k * clr_temp[1];
  41. clr_temp[0] = rt_k * clr_temp[0];
  42. clr_temp[2] = rt_k * clr_temp[2];
  43. }
  44. // chan_1 = clr_temp[g_light_para.rgb_order[1]];//bk
  45. // chan_0 = clr_temp[g_light_para.rgb_order[0]];
  46. // chan_2 = clr_temp[g_light_para.rgb_order[2]];
  47. // chan_1 = (uint32_t)((float)(clr_temp[g_light_para.rgb_order[1]]*101/797.0));
  48. // chan_0 = (uint32_t)((float)(clr_temp[g_light_para.rgb_order[0]]*101/797.0));
  49. // chan_2 = (uint32_t)((float)(clr_temp[g_light_para.rgb_order[2]]*101/797.0));
  50. // chan_1 = ((float)(clr_temp[g_light_para.rgb_order[1]]*101/797.0));
  51. // chan_0 = ((float)(clr_temp[g_light_para.rgb_order[0]]*101/797.0));
  52. // chan_2 = ((float)(clr_temp[g_light_para.rgb_order[2]]*101/797.0));
  53. chan_1 = ((float)(clr_temp[g_light_para.rgb_order[1]]*100/797.0));
  54. chan_0 = ((float)(clr_temp[g_light_para.rgb_order[0]]*100/797.0));
  55. chan_2 = ((float)(clr_temp[g_light_para.rgb_order[2]]*100/797.0));
  56. pwm_chan_0 = (uint32_t)(chan_0+0.5);//小数取整
  57. pwm_chan_1 = (uint32_t)(chan_1+0.5);
  58. pwm_chan_2 = (uint32_t)(chan_2+0.5);
  59. // clr_temp[0] = (uint8_t)((float)((color_rgb >> 8) & 0xff)/255.0)*99;
  60. // clr_temp[1] = (uint8_t)((float)(color_rgb & 0xff)/255.0)*99;
  61. // clr_temp[2] = (uint8_t)((float)((color_rgb >> 16) & 0xff)/255.0)*99;
  62. // chan_1 = clr_temp[g_light_para.rgb_order[1]];
  63. // chan_0 = clr_temp[g_light_para.rgb_order[0]];
  64. // chan_2 = clr_temp[g_light_para.rgb_order[2]];
  65. // UserSet_OutPWM(PWM_CHANCE_G,PWM_CHANNEL_0,20000,chan_0);// G
  66. // UserSet_OutPWM(PWM_CHANCE_R,PWM_CHANNEL_1,20000,chan_2);//R
  67. // UserSet_OutPWM(PWM_CHANCE_B,PWM_CHANNEL_2,20000,chan_1);//B
  68. UserSet_OutPWM(PWM_CHANCE_G,PWM_CHANNEL_0,20000,pwm_chan_0);// G
  69. UserSet_OutPWM(PWM_CHANCE_R,PWM_CHANNEL_1,20000,pwm_chan_2);//R
  70. UserSet_OutPWM(PWM_CHANCE_B,PWM_CHANNEL_2,20000,pwm_chan_1);//B
  71. // PWM_SetupIOL(BIT(PWM_CHANCE_R));
  72. // PWM_OPEN_CLOCK(PWM_CHANCE_R, PWM_CLKSEL_DPLL);
  73. // PWM_Setup(PWM_CHANCE_R, 1, PWM_PERIOD, chan_2);
  74. // PWM_Open(PWM_CHANCE_R);
  75. //
  76. // PWM_SetupIOL(BIT(PWM_CHANCE_G));
  77. // PWM_OPEN_CLOCK(PWM_CHANCE_G, PWM_CLKSEL_DPLL);
  78. // PWM_Setup(PWM_CHANCE_G, 1, PWM_PERIOD, chan_0);
  79. // PWM_Open(PWM_CHANCE_G);
  80. //
  81. // PWM_SetupIOL(BIT(PWM_CHANCE_B));
  82. // PWM_OPEN_CLOCK(PWM_CHANCE_B, PWM_CLKSEL_DPLL);
  83. // PWM_Setup(PWM_CHANCE_B, 1, PWM_PERIOD, chan_1);
  84. // PWM_Open(PWM_CHANCE_B);
  85. #if PWM_DEBUG_EN
  86. U_UART_PRINTF("chan_1 = %d\r\n", (int)(chan_1*10));
  87. U_UART_PRINTF("chan_0 = %d\r\n", (int)(chan_0*10));
  88. U_UART_PRINTF("chan_2 = %d\r\n", (int)(chan_2*10));
  89. U_UART_PRINTF("\r\n rgb_order[0] = %d, rgb_order[1]=%d rgb_order[2]=%d\r\n", g_light_para.rgb_order[0], g_light_para.rgb_order[1], g_light_para.rgb_order[2]);
  90. #endif
  91. }
  92. //bk3431
  93. //void light_set_color(uint32_t color_rgb)
  94. //{
  95. // uint32_t clr_temp[3] = {0x00}, chan_0 = 0, chan_1 = 0, chan_2 = 0, rgb_num = 0;
  96. // //U_UART_PRINTF("RGB = %06X\n", color_rgb);
  97. // //U_UART_PRINTF("RGB = %d, %d, %d\n", clr_temp[1], clr_temp[0], clr_temp[2]);
  98. //
  99. // clr_temp[1] = ((color_rgb & 0x0ff) * 3125 + 500) / 1000;
  100. // clr_temp[0] = (((color_rgb >> 8) & 0x0ff) * 3125 + 500) / 1000;
  101. // clr_temp[2] = (((color_rgb >> 16) & 0x0ff) * 3125 + 500) / 1000;
  102. //
  103. // rgb_num = clr_temp[1] + clr_temp[0] + clr_temp[2];
  104. // if (rgb_num <= 1594)
  105. // {
  106. //
  107. // }
  108. // else if (rgb_num >= 2300) //797*3
  109. // {
  110. // clr_temp[1] = 75 * clr_temp[1] / 100;
  111. // clr_temp[0] = 75 * clr_temp[0] / 100;
  112. // clr_temp[2] = 75 * clr_temp[2] / 100;
  113. // }
  114. // else if (rgb_num > 1594)
  115. // {
  116. // float rt_k = 1594.0 / (clr_temp[1] + clr_temp[0] + clr_temp[2]);
  117. //
  118. // clr_temp[1] = rt_k * clr_temp[1];
  119. // clr_temp[0] = rt_k * clr_temp[0];
  120. // clr_temp[2] = rt_k * clr_temp[2];
  121. // }
  122. //
  123. // chan_1 = clr_temp[g_light_para.rgb_order[1]];
  124. // chan_0 = clr_temp[g_light_para.rgb_order[0]];
  125. // chan_2 = clr_temp[g_light_para.rgb_order[2]];
  126. //
  127. // PWM_SetupIOL(BIT(PWM_CHANCE_R));
  128. // PWM_OPEN_CLOCK(PWM_CHANCE_R, PWM_CLKSEL_DPLL);
  129. // PWM_Setup(PWM_CHANCE_R, 1, PWM_PERIOD, chan_2);
  130. // PWM_Open(PWM_CHANCE_R);
  131. //
  132. // PWM_SetupIOL(BIT(PWM_CHANCE_G));
  133. // PWM_OPEN_CLOCK(PWM_CHANCE_G, PWM_CLKSEL_DPLL);
  134. // PWM_Setup(PWM_CHANCE_G, 1, PWM_PERIOD, chan_0);
  135. // PWM_Open(PWM_CHANCE_G);
  136. //
  137. // PWM_SetupIOL(BIT(PWM_CHANCE_B));
  138. // PWM_OPEN_CLOCK(PWM_CHANCE_B, PWM_CLKSEL_DPLL);
  139. // PWM_Setup(PWM_CHANCE_B, 1, PWM_PERIOD, chan_1);
  140. // PWM_Open(PWM_CHANCE_B);
  141. //
  142. // //U_UART_PRINTF("period = %d, rgb = %d, %d %d\n", period, clr_temp[1], clr_temp[0], clr_temp[2]);
  143. //}
  144. #else
  145. #define PWM_CHANCE_R (8)
  146. #define PWM_CHANCE_G (9)
  147. #define PWM_CHANCE_B (10)
  148. #define PWM_RT_PERIOD (3400)
  149. #define PWM_PERIOD (2550)
  150. void light_set_color(uint32_t color_rgb)
  151. {
  152. uint16_t color_r = 0, color_g = 0, color_b = 0, rgb_num = 0, period = 0;
  153. color_r = ((color_rgb >> 8) & 0x0ff) * 10;
  154. color_g = (color_rgb & 0x0ff) * 10;
  155. color_b = ((color_rgb >> 16) & 0x0ff) * 10;
  156. rgb_num = color_r + color_g + color_b;
  157. //U_UART_PRINTF("RGB = %d, %d, %d\n", color_r, color_g, color_b);
  158. if (rgb_num >= 7650)
  159. {
  160. period = 3400;
  161. }
  162. else if (rgb_num > 5100)
  163. {
  164. float rt_k = 5100.0 / (color_r + color_g + color_b);
  165. period = 2550;
  166. color_r = rt_k * color_r;
  167. color_g = rt_k * color_g;
  168. color_b = rt_k * color_b;
  169. }
  170. else
  171. {
  172. period = 2550;
  173. }
  174. PWM_SetupIOL(BIT(PWM_CHANCE_R));
  175. PWM_OPEN_CLOCK(PWM_CHANCE_R, PWM_CLKSEL_DPLL);
  176. PWM_Setup(PWM_CHANCE_R, 1, period, color_r);
  177. PWM_Open(PWM_CHANCE_R);
  178. PWM_SetupIOL(BIT(PWM_CHANCE_G));
  179. PWM_OPEN_CLOCK(PWM_CHANCE_G, PWM_CLKSEL_DPLL);
  180. PWM_Setup(PWM_CHANCE_G, 1, period, color_g);
  181. PWM_Open(PWM_CHANCE_G);
  182. PWM_SetupIOL(BIT(PWM_CHANCE_B));
  183. PWM_OPEN_CLOCK(PWM_CHANCE_B, PWM_CLKSEL_DPLL);
  184. PWM_Setup(PWM_CHANCE_B, 1, period, color_b);
  185. PWM_Open(PWM_CHANCE_B);
  186. //U_UART_PRINTF("period = %d, rgb = %d, %d %d\n", period, color_r, color_g, color_b);
  187. }
  188. #endif