标题:
matlab生成51单片机的pwm源代码 附仿真图等资料
[打印本页]
作者:
maojingkui
时间:
2018-4-26 21:29
标题:
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电路图。
0.png
(47.67 KB, 下载次数: 28)
下载附件
2018-4-26 23:53 上传
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
0.png
(23.58 KB, 下载次数: 35)
下载附件
2018-4-26 23:53 上传
单片机源程序如下:
/*
* File: pwmSIL.c
*
* Real-Time Workshop code generated for Simulink model pwmSIL.
*
* Model version : 1.11
* Real-Time Workshop file version : 7.6 (R2010b) 03-Aug-2010
* Real-Time Workshop file generated on : Sun May 26 11:24:29 2013
* TLC version : 7.6 (Jul 13 2010)
* C/C++ source code generated on : Sun May 26 11:24:30 2013
*
* Target selection: ert.tlc
* Embedded hardware selection: Intel->8051 Compatible
* Code generation objectives: Unspecified
* Validation result: Not run
*/
#include "pwmSIL.h"
#include "pwmSIL_private.h"
/* Named constants for Stateflow: '<Root>/Chart' */
#define pwmSIL_IN_a (1U)
#define pwmSIL_IN_b (2U)
/* Block signals (auto storage) */
BlockIO_pwmSIL pwmSIL_B;
/* Block states (auto storage) */
D_Work_pwmSIL pwmSIL_DWork;
/* External inputs (root inport signals with auto storage) */
ExternalInputs_pwmSIL pwmSIL_U;
/* External outputs (root outports fed by signals with auto storage) */
ExternalOutputs_pwmSIL pwmSIL_Y;
/* Real-time model */
RT_MODEL_pwmSIL pwmSIL_M_;
RT_MODEL_pwmSIL *pwmSIL_M = &pwmSIL_M_;
/* Model step function */
void pwmSIL_step(void)
{
uint16_T tmp;
uint8_T tmp_0;
uint8_T tmp_1;
/* Stateflow: '<Root>/Chart' incorporates:
* Inport: '<Root>/In1'
* Inport: '<Root>/In2'
* Inport: '<Root>/In3'
* Inport: '<Root>/In4'
* Inport: '<Root>/In5'
* Inport: '<Root>/In6'
* Inport: '<Root>/In7'
* Inport: '<Root>/In8'
*/
/* Gateway: Chart */
/* During: Chart */
if (pwmSIL_DWork.temporalCounter_i1 < MAX_uint32_T) {
pwmSIL_DWork.temporalCounter_i1 = pwmSIL_DWork.temporalCounter_i1 + 1UL;
}
if (pwmSIL_DWork.is_active_c1_pwmSIL == 0) {
/* Entry: Chart */
pwmSIL_DWork.is_active_c1_pwmSIL = 1U;
/* Transition: '<S1>:13' */
/* Entry 'a': '<S1>:1' */
pwmSIL_DWork.is_c1_pwmSIL = pwmSIL_IN_a;
pwmSIL_DWork.temporalCounter_i1 = 0UL;
/* Embedded MATLAB Function 'bin2dec': '<S1>:16' */
/* '<S1>:16:2' */
if (pwmSIL_U.In8 > 1) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In8 << 7);
}
if (pwmSIL_U.In7 > 3) {
tmp_1 = MAX_uint8_T;
} else {
tmp_1 = (uint8_T)(pwmSIL_U.In7 << 6);
}
tmp = (uint16_T)tmp_0 + (uint16_T)tmp_1;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In6 > 7) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In6 << 5);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In5 > 15) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In5 << 4);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In4 > 31) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In4 << 3);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In3 > 63) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In3 << 2);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In2 > 127) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In2 << 1);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
tmp += (uint16_T)pwmSIL_U.In1;
if (tmp > 255U) {
tmp = 255U;
}
pwmSIL_DWork.v = (uint8_T)tmp;
pwmSIL_B.pwm = 1U;
} else {
switch (pwmSIL_DWork.is_c1_pwmSIL) {
case pwmSIL_IN_a:
/* During 'a': '<S1>:1' */
if (pwmSIL_DWork.temporalCounter_i1 >= (uint32_T)(pwmSIL_DWork.v / 10)) {
/* Transition: '<S1>:14' */
/* Exit 'a': '<S1>:1' */
/* Entry 'b': '<S1>:12' */
pwmSIL_DWork.is_c1_pwmSIL = pwmSIL_IN_b;
pwmSIL_DWork.temporalCounter_i1 = 0UL;
/* Embedded MATLAB Function 'bin2dec': '<S1>:16' */
/* '<S1>:16:2' */
if (pwmSIL_U.In8 > 1) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In8 << 7);
}
if (pwmSIL_U.In7 > 3) {
tmp_1 = MAX_uint8_T;
} else {
tmp_1 = (uint8_T)(pwmSIL_U.In7 << 6);
}
tmp = (uint16_T)tmp_0 + (uint16_T)tmp_1;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In6 > 7) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In6 << 5);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In5 > 15) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In5 << 4);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In4 > 31) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In4 << 3);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In3 > 63) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In3 << 2);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In2 > 127) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In2 << 1);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
tmp += (uint16_T)pwmSIL_U.In1;
if (tmp > 255U) {
tmp = 255U;
}
pwmSIL_DWork.v = (uint8_T)tmp;
pwmSIL_B.pwm = 0U;
}
break;
case pwmSIL_IN_b:
/* During 'b': '<S1>:12' */
if (pwmSIL_DWork.temporalCounter_i1 >= (uint32_T)((255 - pwmSIL_DWork.v) /
10)) {
/* Transition: '<S1>:15' */
/* Exit 'b': '<S1>:12' */
/* Entry 'a': '<S1>:1' */
pwmSIL_DWork.is_c1_pwmSIL = pwmSIL_IN_a;
pwmSIL_DWork.temporalCounter_i1 = 0UL;
/* Embedded MATLAB Function 'bin2dec': '<S1>:16' */
/* '<S1>:16:2' */
if (pwmSIL_U.In8 > 1) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In8 << 7);
}
if (pwmSIL_U.In7 > 3) {
tmp_1 = MAX_uint8_T;
} else {
tmp_1 = (uint8_T)(pwmSIL_U.In7 << 6);
}
tmp = (uint16_T)tmp_0 + (uint16_T)tmp_1;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In6 > 7) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In6 << 5);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In5 > 15) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In5 << 4);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In4 > 31) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In4 << 3);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In3 > 63) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In3 << 2);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In2 > 127) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In2 << 1);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
tmp += (uint16_T)pwmSIL_U.In1;
if (tmp > 255U) {
tmp = 255U;
}
pwmSIL_DWork.v = (uint8_T)tmp;
pwmSIL_B.pwm = 1U;
}
break;
default:
/* Transition: '<S1>:13' */
/* Entry 'a': '<S1>:1' */
pwmSIL_DWork.is_c1_pwmSIL = pwmSIL_IN_a;
pwmSIL_DWork.temporalCounter_i1 = 0UL;
/* Embedded MATLAB Function 'bin2dec': '<S1>:16' */
/* '<S1>:16:2' */
if (pwmSIL_U.In8 > 1) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In8 << 7);
}
if (pwmSIL_U.In7 > 3) {
tmp_1 = MAX_uint8_T;
} else {
tmp_1 = (uint8_T)(pwmSIL_U.In7 << 6);
}
tmp = (uint16_T)tmp_0 + (uint16_T)tmp_1;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In6 > 7) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In6 << 5);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In5 > 15) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In5 << 4);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In4 > 31) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In4 << 3);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In3 > 63) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In3 << 2);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
if (pwmSIL_U.In2 > 127) {
tmp_0 = MAX_uint8_T;
} else {
tmp_0 = (uint8_T)(pwmSIL_U.In2 << 1);
}
tmp += (uint16_T)tmp_0;
if (tmp > 255U) {
tmp = 255U;
}
tmp += (uint16_T)pwmSIL_U.In1;
if (tmp > 255U) {
tmp = 255U;
}
pwmSIL_DWork.v = (uint8_T)tmp;
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
pwm-simulink.zip
(472.35 KB, 下载次数: 41)
2018-4-26 21:28 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
qwertylg
时间:
2018-12-4 17:45
我也在做这个例子,但是我在生成hex文件后,在proteus里面仿真的时候,程序死在了startup.a51里面。请问这个你遇到过吗?
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1