u_pwm_drive.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. UserSet_OutPWM(PWM_CHANCE_G,PWM_CHANNEL_0,20000,pwm_chan_0);// G
  72. UserSet_OutPWM(PWM_CHANCE_R,PWM_CHANNEL_1,20000,pwm_chan_2);//R
  73. //UserSet_OutPWM(PWM_CHANCE_B,PWM_CHANNEL_2,20000,pwm_chan_1);//B
  74. // PWM_SetupIOL(BIT(PWM_CHANCE_R));
  75. // PWM_OPEN_CLOCK(PWM_CHANCE_R, PWM_CLKSEL_DPLL);
  76. // PWM_Setup(PWM_CHANCE_R, 1, PWM_PERIOD, chan_2);
  77. // PWM_Open(PWM_CHANCE_R);
  78. //
  79. // PWM_SetupIOL(BIT(PWM_CHANCE_G));
  80. // PWM_OPEN_CLOCK(PWM_CHANCE_G, PWM_CLKSEL_DPLL);
  81. // PWM_Setup(PWM_CHANCE_G, 1, PWM_PERIOD, chan_0);
  82. // PWM_Open(PWM_CHANCE_G);
  83. //
  84. // PWM_SetupIOL(BIT(PWM_CHANCE_B));
  85. // PWM_OPEN_CLOCK(PWM_CHANCE_B, PWM_CLKSEL_DPLL);
  86. // PWM_Setup(PWM_CHANCE_B, 1, PWM_PERIOD, chan_1);
  87. // PWM_Open(PWM_CHANCE_B);
  88. #if PWM_DEBUG_EN
  89. U_UART_PRINTF("chan_1 = %d\r\n", (int)(chan_1*10));
  90. U_UART_PRINTF("chan_0 = %d\r\n", (int)(chan_0*10));
  91. U_UART_PRINTF("chan_2 = %d\r\n", (int)(chan_2*10));
  92. 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]);
  93. #endif
  94. }
  95. //bk3431
  96. //void light_set_color(uint32_t color_rgb)
  97. //{
  98. // uint32_t clr_temp[3] = {0x00}, chan_0 = 0, chan_1 = 0, chan_2 = 0, rgb_num = 0;
  99. // //U_UART_PRINTF("RGB = %06X\n", color_rgb);
  100. // //U_UART_PRINTF("RGB = %d, %d, %d\n", clr_temp[1], clr_temp[0], clr_temp[2]);
  101. //
  102. // clr_temp[1] = ((color_rgb & 0x0ff) * 3125 + 500) / 1000;
  103. // clr_temp[0] = (((color_rgb >> 8) & 0x0ff) * 3125 + 500) / 1000;
  104. // clr_temp[2] = (((color_rgb >> 16) & 0x0ff) * 3125 + 500) / 1000;
  105. //
  106. // rgb_num = clr_temp[1] + clr_temp[0] + clr_temp[2];
  107. // if (rgb_num <= 1594)
  108. // {
  109. //
  110. // }
  111. // else if (rgb_num >= 2300) //797*3
  112. // {
  113. // clr_temp[1] = 75 * clr_temp[1] / 100;
  114. // clr_temp[0] = 75 * clr_temp[0] / 100;
  115. // clr_temp[2] = 75 * clr_temp[2] / 100;
  116. // }
  117. // else if (rgb_num > 1594)
  118. // {
  119. // float rt_k = 1594.0 / (clr_temp[1] + clr_temp[0] + clr_temp[2]);
  120. //
  121. // clr_temp[1] = rt_k * clr_temp[1];
  122. // clr_temp[0] = rt_k * clr_temp[0];
  123. // clr_temp[2] = rt_k * clr_temp[2];
  124. // }
  125. //
  126. // chan_1 = clr_temp[g_light_para.rgb_order[1]];
  127. // chan_0 = clr_temp[g_light_para.rgb_order[0]];
  128. // chan_2 = clr_temp[g_light_para.rgb_order[2]];
  129. //
  130. // PWM_SetupIOL(BIT(PWM_CHANCE_R));
  131. // PWM_OPEN_CLOCK(PWM_CHANCE_R, PWM_CLKSEL_DPLL);
  132. // PWM_Setup(PWM_CHANCE_R, 1, PWM_PERIOD, chan_2);
  133. // PWM_Open(PWM_CHANCE_R);
  134. //
  135. // PWM_SetupIOL(BIT(PWM_CHANCE_G));
  136. // PWM_OPEN_CLOCK(PWM_CHANCE_G, PWM_CLKSEL_DPLL);
  137. // PWM_Setup(PWM_CHANCE_G, 1, PWM_PERIOD, chan_0);
  138. // PWM_Open(PWM_CHANCE_G);
  139. //
  140. // PWM_SetupIOL(BIT(PWM_CHANCE_B));
  141. // PWM_OPEN_CLOCK(PWM_CHANCE_B, PWM_CLKSEL_DPLL);
  142. // PWM_Setup(PWM_CHANCE_B, 1, PWM_PERIOD, chan_1);
  143. // PWM_Open(PWM_CHANCE_B);
  144. //
  145. // //U_UART_PRINTF("period = %d, rgb = %d, %d %d\n", period, clr_temp[1], clr_temp[0], clr_temp[2]);
  146. //}
  147. #else
  148. #define PWM_CHANCE_R (8)
  149. #define PWM_CHANCE_G (9)
  150. #define PWM_CHANCE_B (10)
  151. #define PWM_RT_PERIOD (3400)
  152. #define PWM_PERIOD (2550)
  153. void light_set_color(uint32_t color_rgb)
  154. {
  155. uint16_t color_r = 0, color_g = 0, color_b = 0, rgb_num = 0, period = 0;
  156. color_r = ((color_rgb >> 8) & 0x0ff) * 10;
  157. color_g = (color_rgb & 0x0ff) * 10;
  158. color_b = ((color_rgb >> 16) & 0x0ff) * 10;
  159. rgb_num = color_r + color_g + color_b;
  160. //U_UART_PRINTF("RGB = %d, %d, %d\n", color_r, color_g, color_b);
  161. if (rgb_num >= 7650)
  162. {
  163. period = 3400;
  164. }
  165. else if (rgb_num > 5100)
  166. {
  167. float rt_k = 5100.0 / (color_r + color_g + color_b);
  168. period = 2550;
  169. color_r = rt_k * color_r;
  170. color_g = rt_k * color_g;
  171. color_b = rt_k * color_b;
  172. }
  173. else
  174. {
  175. period = 2550;
  176. }
  177. PWM_SetupIOL(BIT(PWM_CHANCE_R));
  178. PWM_OPEN_CLOCK(PWM_CHANCE_R, PWM_CLKSEL_DPLL);
  179. PWM_Setup(PWM_CHANCE_R, 1, period, color_r);
  180. PWM_Open(PWM_CHANCE_R);
  181. PWM_SetupIOL(BIT(PWM_CHANCE_G));
  182. PWM_OPEN_CLOCK(PWM_CHANCE_G, PWM_CLKSEL_DPLL);
  183. PWM_Setup(PWM_CHANCE_G, 1, period, color_g);
  184. PWM_Open(PWM_CHANCE_G);
  185. PWM_SetupIOL(BIT(PWM_CHANCE_B));
  186. PWM_OPEN_CLOCK(PWM_CHANCE_B, PWM_CLKSEL_DPLL);
  187. PWM_Setup(PWM_CHANCE_B, 1, period, color_b);
  188. PWM_Open(PWM_CHANCE_B);
  189. //U_UART_PRINTF("period = %d, rgb = %d, %d %d\n", period, color_r, color_g, color_b);
  190. }
  191. #endif