找回密码
 立即注册

QQ登录

只需一步,快速开始

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

matlab生成51单片机的pwm源代码 附仿真图等资料

[复制链接]
跳转到指定楼层
楼主
用matlab自动生成PWM源代码,keil4编译后灌入8051芯片内,在proteus上仿真实现pwm.很好地显示了自动代码生成的过程。pwm.mdl是模型文件,pwmsil_ert_rtw在环测试。pwmsil8051_ert_rtw是keil4文件夹。pwm.dsn是proteus仿真电路图。pwmsil8051_ert_rtw是keil4文件夹。pwm.dsn是proteus电路图。



仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源程序如下:
  1. /*
  2. * File: pwmSIL.c
  3. *
  4. * Real-Time Workshop code generated for Simulink model pwmSIL.
  5. *
  6. * Model version                        : 1.11
  7. * Real-Time Workshop file version      : 7.6  (R2010b)  03-Aug-2010
  8. * Real-Time Workshop file generated on : Sun May 26 11:24:29 2013
  9. * TLC version                          : 7.6 (Jul 13 2010)
  10. * C/C++ source code generated on       : Sun May 26 11:24:30 2013
  11. *
  12. * Target selection: ert.tlc
  13. * Embedded hardware selection: Intel->8051 Compatible
  14. * Code generation objectives: Unspecified
  15. * Validation result: Not run
  16. */

  17. #include "pwmSIL.h"
  18. #include "pwmSIL_private.h"

  19. /* Named constants for Stateflow: '<Root>/Chart' */
  20. #define pwmSIL_IN_a                    (1U)
  21. #define pwmSIL_IN_b                    (2U)

  22. /* Block signals (auto storage) */
  23. BlockIO_pwmSIL pwmSIL_B;

  24. /* Block states (auto storage) */
  25. D_Work_pwmSIL pwmSIL_DWork;

  26. /* External inputs (root inport signals with auto storage) */
  27. ExternalInputs_pwmSIL pwmSIL_U;

  28. /* External outputs (root outports fed by signals with auto storage) */
  29. ExternalOutputs_pwmSIL pwmSIL_Y;

  30. /* Real-time model */
  31. RT_MODEL_pwmSIL pwmSIL_M_;
  32. RT_MODEL_pwmSIL *pwmSIL_M = &pwmSIL_M_;

  33. /* Model step function */
  34. void pwmSIL_step(void)
  35. {
  36.   uint16_T tmp;
  37.   uint8_T tmp_0;
  38.   uint8_T tmp_1;

  39.   /* Stateflow: '<Root>/Chart' incorporates:
  40.    *  Inport: '<Root>/In1'
  41.    *  Inport: '<Root>/In2'
  42.    *  Inport: '<Root>/In3'
  43.    *  Inport: '<Root>/In4'
  44.    *  Inport: '<Root>/In5'
  45.    *  Inport: '<Root>/In6'
  46.    *  Inport: '<Root>/In7'
  47.    *  Inport: '<Root>/In8'
  48.    */
  49.   /* Gateway: Chart */
  50.   /* During: Chart */
  51.   if (pwmSIL_DWork.temporalCounter_i1 < MAX_uint32_T) {
  52.     pwmSIL_DWork.temporalCounter_i1 = pwmSIL_DWork.temporalCounter_i1 + 1UL;
  53.   }

  54.   if (pwmSIL_DWork.is_active_c1_pwmSIL == 0) {
  55.     /* Entry: Chart */
  56.     pwmSIL_DWork.is_active_c1_pwmSIL = 1U;

  57.     /* Transition: '<S1>:13' */
  58.     /* Entry 'a': '<S1>:1' */
  59.     pwmSIL_DWork.is_c1_pwmSIL = pwmSIL_IN_a;
  60.     pwmSIL_DWork.temporalCounter_i1 = 0UL;

  61.     /* Embedded MATLAB Function 'bin2dec': '<S1>:16' */
  62.     /* '<S1>:16:2' */
  63.     if (pwmSIL_U.In8 > 1) {
  64.       tmp_0 = MAX_uint8_T;
  65.     } else {
  66.       tmp_0 = (uint8_T)(pwmSIL_U.In8 << 7);
  67.     }

  68.     if (pwmSIL_U.In7 > 3) {
  69.       tmp_1 = MAX_uint8_T;
  70.     } else {
  71.       tmp_1 = (uint8_T)(pwmSIL_U.In7 << 6);
  72.     }

  73.     tmp = (uint16_T)tmp_0 + (uint16_T)tmp_1;
  74.     if (tmp > 255U) {
  75.       tmp = 255U;
  76.     }

  77.     if (pwmSIL_U.In6 > 7) {
  78.       tmp_0 = MAX_uint8_T;
  79.     } else {
  80.       tmp_0 = (uint8_T)(pwmSIL_U.In6 << 5);
  81.     }

  82.     tmp += (uint16_T)tmp_0;
  83.     if (tmp > 255U) {
  84.       tmp = 255U;
  85.     }

  86.     if (pwmSIL_U.In5 > 15) {
  87.       tmp_0 = MAX_uint8_T;
  88.     } else {
  89.       tmp_0 = (uint8_T)(pwmSIL_U.In5 << 4);
  90.     }

  91.     tmp += (uint16_T)tmp_0;
  92.     if (tmp > 255U) {
  93.       tmp = 255U;
  94.     }

  95.     if (pwmSIL_U.In4 > 31) {
  96.       tmp_0 = MAX_uint8_T;
  97.     } else {
  98.       tmp_0 = (uint8_T)(pwmSIL_U.In4 << 3);
  99.     }

  100.     tmp += (uint16_T)tmp_0;
  101.     if (tmp > 255U) {
  102.       tmp = 255U;
  103.     }

  104.     if (pwmSIL_U.In3 > 63) {
  105.       tmp_0 = MAX_uint8_T;
  106.     } else {
  107.       tmp_0 = (uint8_T)(pwmSIL_U.In3 << 2);
  108.     }

  109.     tmp += (uint16_T)tmp_0;
  110.     if (tmp > 255U) {
  111.       tmp = 255U;
  112.     }

  113.     if (pwmSIL_U.In2 > 127) {
  114.       tmp_0 = MAX_uint8_T;
  115.     } else {
  116.       tmp_0 = (uint8_T)(pwmSIL_U.In2 << 1);
  117.     }

  118.     tmp += (uint16_T)tmp_0;
  119.     if (tmp > 255U) {
  120.       tmp = 255U;
  121.     }

  122.     tmp += (uint16_T)pwmSIL_U.In1;
  123.     if (tmp > 255U) {
  124.       tmp = 255U;
  125.     }

  126.     pwmSIL_DWork.v = (uint8_T)tmp;
  127.     pwmSIL_B.pwm = 1U;
  128.   } else {
  129.     switch (pwmSIL_DWork.is_c1_pwmSIL) {
  130.      case pwmSIL_IN_a:
  131.       /* During 'a': '<S1>:1' */
  132.       if (pwmSIL_DWork.temporalCounter_i1 >= (uint32_T)(pwmSIL_DWork.v / 10)) {
  133.         /* Transition: '<S1>:14' */
  134.         /* Exit 'a': '<S1>:1' */
  135.         /* Entry 'b': '<S1>:12' */
  136.         pwmSIL_DWork.is_c1_pwmSIL = pwmSIL_IN_b;
  137.         pwmSIL_DWork.temporalCounter_i1 = 0UL;

  138.         /* Embedded MATLAB Function 'bin2dec': '<S1>:16' */
  139.         /* '<S1>:16:2' */
  140.         if (pwmSIL_U.In8 > 1) {
  141.           tmp_0 = MAX_uint8_T;
  142.         } else {
  143.           tmp_0 = (uint8_T)(pwmSIL_U.In8 << 7);
  144.         }

  145.         if (pwmSIL_U.In7 > 3) {
  146.           tmp_1 = MAX_uint8_T;
  147.         } else {
  148.           tmp_1 = (uint8_T)(pwmSIL_U.In7 << 6);
  149.         }

  150.         tmp = (uint16_T)tmp_0 + (uint16_T)tmp_1;
  151.         if (tmp > 255U) {
  152.           tmp = 255U;
  153.         }

  154.         if (pwmSIL_U.In6 > 7) {
  155.           tmp_0 = MAX_uint8_T;
  156.         } else {
  157.           tmp_0 = (uint8_T)(pwmSIL_U.In6 << 5);
  158.         }

  159.         tmp += (uint16_T)tmp_0;
  160.         if (tmp > 255U) {
  161.           tmp = 255U;
  162.         }

  163.         if (pwmSIL_U.In5 > 15) {
  164.           tmp_0 = MAX_uint8_T;
  165.         } else {
  166.           tmp_0 = (uint8_T)(pwmSIL_U.In5 << 4);
  167.         }

  168.         tmp += (uint16_T)tmp_0;
  169.         if (tmp > 255U) {
  170.           tmp = 255U;
  171.         }

  172.         if (pwmSIL_U.In4 > 31) {
  173.           tmp_0 = MAX_uint8_T;
  174.         } else {
  175.           tmp_0 = (uint8_T)(pwmSIL_U.In4 << 3);
  176.         }

  177.         tmp += (uint16_T)tmp_0;
  178.         if (tmp > 255U) {
  179.           tmp = 255U;
  180.         }

  181.         if (pwmSIL_U.In3 > 63) {
  182.           tmp_0 = MAX_uint8_T;
  183.         } else {
  184.           tmp_0 = (uint8_T)(pwmSIL_U.In3 << 2);
  185.         }

  186.         tmp += (uint16_T)tmp_0;
  187.         if (tmp > 255U) {
  188.           tmp = 255U;
  189.         }

  190.         if (pwmSIL_U.In2 > 127) {
  191.           tmp_0 = MAX_uint8_T;
  192.         } else {
  193.           tmp_0 = (uint8_T)(pwmSIL_U.In2 << 1);
  194.         }

  195.         tmp += (uint16_T)tmp_0;
  196.         if (tmp > 255U) {
  197.           tmp = 255U;
  198.         }

  199.         tmp += (uint16_T)pwmSIL_U.In1;
  200.         if (tmp > 255U) {
  201.           tmp = 255U;
  202.         }

  203.         pwmSIL_DWork.v = (uint8_T)tmp;
  204.         pwmSIL_B.pwm = 0U;
  205.       }
  206.       break;

  207.      case pwmSIL_IN_b:
  208.       /* During 'b': '<S1>:12' */
  209.       if (pwmSIL_DWork.temporalCounter_i1 >= (uint32_T)((255 - pwmSIL_DWork.v) /
  210.            10)) {
  211.         /* Transition: '<S1>:15' */
  212.         /* Exit 'b': '<S1>:12' */
  213.         /* Entry 'a': '<S1>:1' */
  214.         pwmSIL_DWork.is_c1_pwmSIL = pwmSIL_IN_a;
  215.         pwmSIL_DWork.temporalCounter_i1 = 0UL;

  216.         /* Embedded MATLAB Function 'bin2dec': '<S1>:16' */
  217.         /* '<S1>:16:2' */
  218.         if (pwmSIL_U.In8 > 1) {
  219.           tmp_0 = MAX_uint8_T;
  220.         } else {
  221.           tmp_0 = (uint8_T)(pwmSIL_U.In8 << 7);
  222.         }

  223.         if (pwmSIL_U.In7 > 3) {
  224.           tmp_1 = MAX_uint8_T;
  225.         } else {
  226.           tmp_1 = (uint8_T)(pwmSIL_U.In7 << 6);
  227.         }

  228.         tmp = (uint16_T)tmp_0 + (uint16_T)tmp_1;
  229.         if (tmp > 255U) {
  230.           tmp = 255U;
  231.         }

  232.         if (pwmSIL_U.In6 > 7) {
  233.           tmp_0 = MAX_uint8_T;
  234.         } else {
  235.           tmp_0 = (uint8_T)(pwmSIL_U.In6 << 5);
  236.         }

  237.         tmp += (uint16_T)tmp_0;
  238.         if (tmp > 255U) {
  239.           tmp = 255U;
  240.         }

  241.         if (pwmSIL_U.In5 > 15) {
  242.           tmp_0 = MAX_uint8_T;
  243.         } else {
  244.           tmp_0 = (uint8_T)(pwmSIL_U.In5 << 4);
  245.         }

  246.         tmp += (uint16_T)tmp_0;
  247.         if (tmp > 255U) {
  248.           tmp = 255U;
  249.         }

  250.         if (pwmSIL_U.In4 > 31) {
  251.           tmp_0 = MAX_uint8_T;
  252.         } else {
  253.           tmp_0 = (uint8_T)(pwmSIL_U.In4 << 3);
  254.         }

  255.         tmp += (uint16_T)tmp_0;
  256.         if (tmp > 255U) {
  257.           tmp = 255U;
  258.         }

  259.         if (pwmSIL_U.In3 > 63) {
  260.           tmp_0 = MAX_uint8_T;
  261.         } else {
  262.           tmp_0 = (uint8_T)(pwmSIL_U.In3 << 2);
  263.         }

  264.         tmp += (uint16_T)tmp_0;
  265.         if (tmp > 255U) {
  266.           tmp = 255U;
  267.         }

  268.         if (pwmSIL_U.In2 > 127) {
  269.           tmp_0 = MAX_uint8_T;
  270.         } else {
  271.           tmp_0 = (uint8_T)(pwmSIL_U.In2 << 1);
  272.         }

  273.         tmp += (uint16_T)tmp_0;
  274.         if (tmp > 255U) {
  275.           tmp = 255U;
  276.         }

  277.         tmp += (uint16_T)pwmSIL_U.In1;
  278.         if (tmp > 255U) {
  279.           tmp = 255U;
  280.         }

  281.         pwmSIL_DWork.v = (uint8_T)tmp;
  282.         pwmSIL_B.pwm = 1U;
  283.       }
  284.       break;

  285.      default:
  286.       /* Transition: '<S1>:13' */
  287.       /* Entry 'a': '<S1>:1' */
  288.       pwmSIL_DWork.is_c1_pwmSIL = pwmSIL_IN_a;
  289.       pwmSIL_DWork.temporalCounter_i1 = 0UL;

  290.       /* Embedded MATLAB Function 'bin2dec': '<S1>:16' */
  291.       /* '<S1>:16:2' */
  292.       if (pwmSIL_U.In8 > 1) {
  293.         tmp_0 = MAX_uint8_T;
  294.       } else {
  295.         tmp_0 = (uint8_T)(pwmSIL_U.In8 << 7);
  296.       }

  297.       if (pwmSIL_U.In7 > 3) {
  298.         tmp_1 = MAX_uint8_T;
  299.       } else {
  300.         tmp_1 = (uint8_T)(pwmSIL_U.In7 << 6);
  301.       }

  302.       tmp = (uint16_T)tmp_0 + (uint16_T)tmp_1;
  303.       if (tmp > 255U) {
  304.         tmp = 255U;
  305.       }

  306.       if (pwmSIL_U.In6 > 7) {
  307.         tmp_0 = MAX_uint8_T;
  308.       } else {
  309.         tmp_0 = (uint8_T)(pwmSIL_U.In6 << 5);
  310.       }

  311.       tmp += (uint16_T)tmp_0;
  312.       if (tmp > 255U) {
  313.         tmp = 255U;
  314.       }

  315.       if (pwmSIL_U.In5 > 15) {
  316.         tmp_0 = MAX_uint8_T;
  317.       } else {
  318.         tmp_0 = (uint8_T)(pwmSIL_U.In5 << 4);
  319.       }

  320.       tmp += (uint16_T)tmp_0;
  321.       if (tmp > 255U) {
  322.         tmp = 255U;
  323.       }

  324.       if (pwmSIL_U.In4 > 31) {
  325.         tmp_0 = MAX_uint8_T;
  326.       } else {
  327.         tmp_0 = (uint8_T)(pwmSIL_U.In4 << 3);
  328.       }

  329.       tmp += (uint16_T)tmp_0;
  330.       if (tmp > 255U) {
  331.         tmp = 255U;
  332.       }

  333.       if (pwmSIL_U.In3 > 63) {
  334.         tmp_0 = MAX_uint8_T;
  335.       } else {
  336.         tmp_0 = (uint8_T)(pwmSIL_U.In3 << 2);
  337.       }

  338.       tmp += (uint16_T)tmp_0;
  339.       if (tmp > 255U) {
  340.         tmp = 255U;
  341.       }

  342.       if (pwmSIL_U.In2 > 127) {
  343.         tmp_0 = MAX_uint8_T;
  344.       } else {
  345.         tmp_0 = (uint8_T)(pwmSIL_U.In2 << 1);
  346.       }

  347.       tmp += (uint16_T)tmp_0;
  348.       if (tmp > 255U) {
  349.         tmp = 255U;
  350.       }

  351.       tmp += (uint16_T)pwmSIL_U.In1;
  352.       if (tmp > 255U) {
  353.         tmp = 255U;
  354.       }

  355.       pwmSIL_DWork.v = (uint8_T)tmp;
  356. ……………………

  357. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
pwm-simulink.zip (472.35 KB, 下载次数: 41)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

沙发
ID:190374 发表于 2018-12-4 17:45 | 只看该作者
我也在做这个例子,但是我在生成hex文件后,在proteus里面仿真的时候,程序死在了startup.a51里面。请问这个你遇到过吗?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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