找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4937|回复: 7
打印 上一主题 下一主题
收起左侧

基于STM32的六路互补SPWM源码

  [复制链接]
跳转到指定楼层
楼主
ID:278764 发表于 2018-1-22 22:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
下载到的一个产生六路互补的SPWM波形的源码程序,感觉写的很不错

单片机源程序如下:
  1. #include "led.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. #include "pwm.h"
  5. #include "timer.h"
  6. #include "dac.h"
  7. //ALIENTEK Mini STM32开发板范例代码8
  8. //PWM输出实验   
  9. const u16 sindata[1024]={
  10. 450,452,454,456,459,461,463,465,467,469,471,474,476,478,480,482,484,486,488,
  11. 491,493,495,497,499,501,503,505,508,510,512,514,516,518,520,522,524,526,529,531,
  12. 533,535,537,539,541,543,545,547,549,551,553,555,557,559,562,564,566,568,570,572,
  13. 574,576,578,580,582,584,586,588,589,591,593,595,597,599,601,603,605,607,609,611,
  14. 613,615,616,618,620,622,624,626,628,629,631,633,635,637,639,640,642,644,646,647,
  15. 649,651,653,654,656,658,660,661,663,665,666,668,670,671,673,675,676,678,680,681,
  16. 683,684,686,688,689,691,692,694,695,697,698,700,701,703,704,706,707,709,710,711,
  17. 713,714,716,717,718,720,721,722,724,725,726,728,729,730,732,733,734,735,737,738,
  18. 739,740,741,743,744,745,746,747,748,749,750,752,753,754,755,756,757,758,759,760,
  19. 761,762,763,764,765,765,766,767,768,769,770,771,772,772,773,774,775,776,776,777,
  20. 778,779,779,780,781,781,782,783,783,784,785,785,786,786,787,787,788,789,789,790,
  21. 790,791,791,791,792,792,793,793,793,794,794,795,795,795,796,796,796,796,797,797,
  22. 797,797,798,798,798,798,798,798,798,799,799,799,799,799,799,799,799,799,799,799,
  23. 799,799,799,799,799,799,798,798,798,798,798,798,798,797,797,797,797,796,796,796,
  24. 796,795,795,795,794,794,793,793,793,792,792,791,791,791,790,790,789,789,788,787,
  25. 787,786,786,785,785,784,783,783,782,781,781,780,779,779,778,777,776,776,775,774,
  26. 773,772,772,771,770,769,768,767,766,765,765,764,763,762,761,760,759,758,757,756,
  27. 755,754,753,752,750,749,748,747,746,745,744,743,741,740,739,738,737,735,734,733,
  28. 732,730,729,728,726,725,724,722,721,720,718,717,716,714,713,711,710,709,707,706,
  29. 704,703,701,700,698,697,695,694,692,691,689,688,686,684,683,681,680,678,676,675,
  30. 673,671,670,668,666,665,663,661,660,658,656,654,653,651,649,647,646,644,642,640,
  31. 639,637,635,633,631,629,628,626,624,622,620,618,616,615,613,611,609,607,605,603,
  32. 601,599,597,595,593,591,589,588,586,584,582,580,578,576,574,572,570,568,566,564,
  33. 562,559,557,555,553,551,549,547,545,543,541,539,537,535,533,531,529,526,524,522,
  34. 520,518,516,514,512,510,508,505,503,501,499,497,495,493,491,488,486,484,482,480,
  35. 478,476,474,471,469,467,465,463,461,459,456,454,452,450,448,446,444,441,439,437,
  36. 435,433,431,429,426,424,422,420,418,416,414,412,409,407,405,403,401,399,397,395,
  37. 392,390,388,386,384,382,380,378,376,374,371,369,367,365,363,361,359,357,355,353,
  38. 351,349,347,345,343,341,338,336,334,332,330,328,326,324,322,320,318,316,314,312,
  39. 311,309,307,305,303,301,299,297,295,293,291,289,287,285,284,282,280,278,276,274,
  40. 272,271,269,267,265,263,261,260,258,256,254,253,251,249,247,246,244,242,240,239,
  41. 237,235,234,232,230,229,227,225,224,222,220,219,217,216,214,212,211,209,208,206,
  42. 205,203,202,200,199,197,196,194,193,191,190,189,187,186,184,183,182,180,179,178,
  43. 176,175,174,172,171,170,168,167,166,165,163,162,161,160,159,157,156,155,154,153,
  44. 152,151,150,148,147,146,145,144,143,142,141,140,139,138,137,136,135,135,134,133,
  45. 132,131,130,129,128,128,127,126,125,124,124,123,122,121,121,120,119,119,118,117,
  46. 117,116,115,115,114,114,113,113,112,111,111,110,110,109,109,109,108,108,107,107,
  47. 107,106,106,105,105,105,104,104,104,104,103,103,103,103,102,102,102,102,102,102,
  48. 102,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,102,102,
  49. 102,102,102,102,102,103,103,103,103,104,104,104,104,105,105,105,106,106,107,107,
  50. 107,108,108,109,109,109,110,110,111,111,112,113,113,114,114,115,115,116,117,117,
  51. 118,119,119,120,121,121,122,123,124,124,125,126,127,128,128,129,130,131,132,133,
  52. 134,135,135,136,137,138,139,140,141,142,143,144,145,146,147,148,150,151,152,153,
  53. 154,155,156,157,159,160,161,162,163,165,166,167,168,170,171,172,174,175,176,178,
  54. 179,180,182,183,184,186,187,189,190,191,193,194,196,197,199,200,202,203,205,206,
  55. 208,209,211,212,214,216,217,219,220,222,224,225,227,229,230,232,234,235,237,239,
  56. 240,242,244,246,247,249,251,253,254,256,258,260,261,263,265,267,269,271,272,274,
  57. 276,278,280,282,284,285,287,289,291,293,295,297,299,301,303,305,307,309,311,312,
  58. 314,316,318,320,322,324,326,328,330,332,334,336,338,341,343,345,347,349,351,353,
  59. 355,357,359,361,363,365,367,369,371,374,376,378,380,382,384,386,388,390,392,395,
  60. 397,399,401,403,405,407,409,412,414,416,418,420,422,424,426,429,431,433,435,437,
  61. 439,441,444,446,448};
  62. extern u16 add_1_n;;
  63. int main(void)
  64. {        
  65.         //u16 led0pwmval=0;   
  66.         //u8 dir=1;        
  67.         delay_init();                     //延时函数初始化         
  68.         LED_Init();                          //初始化与LED连接的硬件接口
  69.         Dac1_Init();
  70.         Dac2_Init();
  71.         TIM1_PWM_Init(899,39);//PWM频率=72000/(899+1)=80Khz //2k
  72.         TIM2_Int_Init(99,71);//1Mhz的计数频率,计数到100为10k
  73.         //TIM3_Int_Init(899,39);//2K hz  
  74.            while(1)
  75.         {
  76. //                 delay_ms(10);         
  77. //                if(dir)led0pwmval++;
  78. //                else led0pwmval--;         
  79. //                 if(led0pwmval>300)dir=0;
  80. //                if(led0pwmval==0)dir=1;                                                   
  81.                 TIM_SetCompare1(TIM1,sindata[add_1_n]);
  82.                 if(add_1_n+341>1023)
  83.                 {
  84.                         TIM_SetCompare2(TIM1,sindata[add_1_n+341-1023]);        
  85.                 }
  86.                 else
  87.                 TIM_SetCompare2(TIM1,sindata[add_1_n+341]);        
  88.                 if(add_1_n+341+341>1023)
  89.                 {
  90.                         TIM_SetCompare3(TIM1,sindata[add_1_n+341+341-1023]);        
  91.                 }
  92.                 else
  93.                 TIM_SetCompare3(TIM1,sindata[add_1_n+341+341]);        
  94.                
  95.         }
  96. }

复制代码

程序临时编写的,没有经过验证,如果有错误,麻烦大家帮我指出错误在哪里,我好去修改:
6路spwm输出有死区控制.rar (301.87 KB, 下载次数: 142)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏4 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:76408 发表于 2018-2-25 17:19 | 只看该作者
楼主里面有图吗?我下载不了金币不够。
回复

使用道具 举报

板凳
ID:99570 发表于 2019-9-20 15:27 | 只看该作者
好东西哦,学习中,谢谢分享!
回复

使用道具 举报

地板
ID:578547 发表于 2019-11-20 13:57 | 只看该作者
好东西 谢谢分享
回复

使用道具 举报

5#
ID:669062 发表于 2020-12-30 15:01 | 只看该作者
验证了,波形不对
回复

使用道具 举报

6#
ID:434018 发表于 2021-1-10 18:29 | 只看该作者
SPWM 無需sindata!
回复

使用道具 举报

7#
ID:254226 发表于 2022-3-27 22:56 | 只看该作者
调不出来的原因应该是在main()函数中while()里面最后几句有矛盾。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表