u_pwm_drive.c 7.4 KB

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