MATLAB-DSP官方例程------ADC触发PWM

MATLAB C2000----- 例程(二)------ADC触发PWM


官方例程链接如下:MATLAB

一.模型

在这里插入图片描述
总系统由两哥子系统组成一个中断触发 一个ADC-PWM

ADC-PWM系统

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3IptgCAh-1628340766621)(C:\My_Project_Markdown\Study_YellowNew\image\ADCPWM1.png)]

ADC触发pwm并且adc的输出为pwm的duty的输入,且为五倍关系。

二.烧入过程

与上篇文章相似

三.代码分析

我们先看一下所生成的文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rfSXnio6-1628340766624)(C:\My_Project_Markdown\Study_YellowNew\image\ADCPWM3.png)]

如红框所示 这些为所生成的文件

  • c2807x_2837xx_adcpwmasynctest_ert_data.c

    主要定义了数据的大小

  • c2807x_2837xx_adcpwmasynctest_ert.c

    初始化PWM和ADC 以及模型所使用的函数以及结构体

  • c2837xDBoard_Realtime_Support.c

    一些常用型的库函数

  • c2837xDSchedulerTimer0.c

    就是跟求解器步长相关的函数,如果加入积分模块很有可能就在这里运算。

  • ert_main.c

    主函数

    初始化外设

  • MW_c28xGlobalInterrupts.c

  • MW_c28xGPIO.c

  • MW_c28xx_adc.c

  • MW_c28xx_board.c

  • MW_c28xx_csl.c

  • MW_c28xx_pwm.c

  • MW_c28xx_xbar.c

  • profiler_Support.c // 可能为调试文件暂未尝试 (看大佬这样说的)

    ert_main.c

    /*
     * File: ert_main.c
     *
     * Code generated for Simulink model 'c2807x_2837xx_adcpwmasynctest_ert'.
     *
     * Model version                  : 9.1
     * Simulink Coder version         : 9.5 (R2021a) 14-Nov-2020
     * C/C++ source code generated on : Fri Aug  6 09:56:42 2021
     *
     * Target selection: ert.tlc
     * Embedded hardware selection: Texas Instruments->C2000
     * Code generation objectives: Unspecified
     * Validation result: Not run
     */
    
    #include "c2807x_2837xx_adcpwmasynctest_ert.h"
    #include "rtwtypes.h"
    
    volatile int IsrOverrun = 0;
    static boolean_T OverrunFlag = 0;
    void rt_OneStep(void)
    {
         
    }
    
    volatile boolean_T stopRequested;
    volatile boolean_T runModel;
    int main(void)
    {
         
      float modelBaseRate = 0.2; //进入中断时间 0.2秒进入一次中断 
      float systemClock = 200;  //CPU晶振频率,与Clocking中的参数相互对应
    
      /* Initialize variables */
      stopRequested = false;
      runModel = false;
      c2000_flash_init();
      init_board();
    
    #ifdef MW_EXEC_PROFILER_ON
    
      config_profilerTimer();
    
    #endif
    
      ;
      rtmSetErrorStatus(c2807x_2837xx_adcpwmasynctes_M, 0); //_M结构体的错误标志为0
      c2807x_2837xx_adcpwmasynctest_ert_initialize(); //初始化
      globalInterruptDisable();
      configureTimer0(modelBaseRate, systemClock); // 初始化定时器计数时间
      runModel =
        rtmGetErrorStatus(c2807x_2837xx_adcpwmasynctes_M) == (NULL);
      enableTimer0Interrupt();
      enable_interrupts();
      globalInterruptEnable();
      while (runModel) {
         
        stopRequested = !(
                          rtmGetErrorStatus(c2807x_2837xx_adcpwmasynctes_M) == (NULL));
      } //判断 stopRequested 不等于_M结构体的错误标志指向空指针
    
      /* Disable rt_OneStep() here */
    
      /* Terminate model */
      c2807x_2837xx_adcpwmasynctest_ert_terminate();
      globalInterruptDisable();
      return 0;
    }
    
    /*
     * File trailer for generated code.
     *
     * [EOF]
     */
    
    

c2807x_2837xx_adcpwmasynctest_ert.c

/*
 * File: c2807x_2837xx_adcpwmasynctest_ert.c
 *
 * Code generated for Simulink model 'c2807x_2837xx_adcpwmasynctest_ert'.
 *
 * Model version                  : 9.1
 * Simulink Coder version         : 9.5 (R2021a) 14-Nov-2020
 * C/C++ source code generated on : Fri Aug  6 09:56:42 2021
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Texas Instruments->C2000
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "c2807x_2837xx_adcpwmasynctest_ert.h"
#include "c2807x_2837xx_adcpwmasynctest_ert_private.h"

/* Block signals (default storage) */
B_c2807x_2837xx_adcpwmasyncte_T c2807x_2837xx_adcpwmasynctest_B;

/* Real-time model */
static RT_MODEL_c2807x_2837xx_adcpwm_T c2807x_2837xx_adcpwmasynctes_M_;
RT_MODEL_c2807x_2837xx_adcpwm_T *const c2807x_2837xx_adcpwmasynctes_M =
  &c2807x_2837xx_adcpwmasynctes_M_;
uint16_T MW_adcAInitFlag = 0;

/* Hardware Interrupt Block: '<Root>/C28x Hardware Interrupt' */
//中断函数
void isr_int1pie1_task_fcn(void)
{
   
  if (1 == runModel) {
   
    /* Call the system: <Root>/ADC-PWM Subsystem */
    {
   
      /* S-Function (c28xisr_c2000): '<Root>/C28x Hardware Interrupt' */

      /* Output and update for function-call system: '<Root>/ADC-PWM Subsystem' */
      {
   
        uint16_T rtb_Gain;

        /* S-Function (c2802xadc): '<S1>/ADC' */
        {
   
          /*  Internal Reference Voltage : Fixed scale 0 to 3.3 V range.  */
          /*  External Reference Voltage : Allowable ranges of VREFHI(ADCINA0) = 3.3 and VREFLO(tied to ground) = 0  */
          c2807x_2837xx_adcpwmasynctest_B.ADC = (AdcaResultRegs.ADCRESULT0);
        }

        /* Gain: '<S1>/Gain' */
        rtb_Gain = (uint16_T)(((uint32_T)
          c2807x_2837xx_adcpwmasynctest_P.Gain_Gain *
          c2807x_2837xx_adcpwmasynctest_B.ADC) >> 13U);  // (40950>>13)*ADC采样结果 = 5*ADC_result

        /* S-Function (c2802xpwm): '<S1>/ePWM' */

        /*-- Update CMPA value for ePWM1 --*/
        {
   
          EPwm1Regs.CMPA.bit.CMPA = (uint16_T)(rtb_Gain);  //赋值给结果寄存器
        }
      }

      /* End of Outputs for S-Function (c28xisr_c2000): '<Root>/C28x Hardware Interrupt' */
    }
  }
}

/* Model step function */
void c2807x_2837xx_adcpwmasynctest_ert_step(void)
{
   
  /* (no output/update code required) */
}

/* Model initialize function */
void c2807x_2837xx_adcpwmasynctest_ert_initialize(void)
{
   
  /* Registration code */

  /* initialize error status */
  rtmSetErrorStatus(c2807x_2837xx_adcpwmasynctes_M, (NULL));

  /* block I/O */
  (void) memset(((void *) &c2807x_2837xx_adcpwmasynctest_B), 0,
                sizeof(B_c2807x_2837xx_adcpwmasyncte_T)); //给结构体赋值为0

  /* SystemInitialize for S-Function (c28xisr_c2000): '<Root>/C28x Hardware Interrupt' incorporates:
   *  SubSystem: '<Root>/ADC-PWM Subsystem'
   */

  /* System initialize for function-call system: '<Root>/ADC-PWM Subsystem' */

  /* Start for S-Function (c2802xadc): '<S1>/ADC' */
  if (MW_adcAInitFlag == 0) {
   
    InitAdcA();
    MW_adcAInitFlag = 1;
  } // 利用控制位只初始化ADC一次

  config_ADCA_SOC0 ();

  /* Start for S-Function (c2802xpwm): '<S1>/ePWM' */

  /*** Initialize ePWM1 modules ***/
  {
   
    /*  // Time Base Control Register
       EPwm1Regs.TBCTL.bit.CTRMODE              = 2;          // Counter Mode
       EPwm1Regs.TBCTL.bit.SYNCOSEL             = 3;          // Sync Output Select
       EPwm1Regs.TBCTL2.bit.SYNCOSELX           = 0;          // Sync Output Select - additional options

       EPwm1Regs.TBCTL.bit.PRDLD                = 0;          // Shadow select

       EPwm1Regs.TBCTL2.bit.PRDLDSYNC           = 0;          // Shadow select

       EPwm1Regs.TBCTL.bit.PHSEN                = 0;          // Phase Load Enable
       EPwm1Regs.TBCTL.bit.PHSDIR               = 0;          // Phase Direction Bit
       EPwm1Regs.TBCTL.bit.HSPCLKDIV            = 0;   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值