找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5931|回复: 9
收起左侧

DSPF28335关于SPWM的源代码

[复制链接]
ID:247388 发表于 2018-3-16 15:08 | 显示全部楼层 |阅读模式
关于DSPF28335写的SPWM代码分享给大家
0.png
源程序如下:
  1. /*
  2. * main.c
  3. */

  4. #include "DSP28x_Project.h"     // Device Headerfile and Examples Include File

  5. // Prototype statements for functions found within this file.
  6. void InitEPwm1Example(void);
  7. extern void InitSpwm(void);

  8. void main(void) {
  9. // Step 1. Initialize System Control:
  10. // PLL, WatchDog, enable Peripheral Clocks
  11. // This example function is found in the DSP2833x_SysCtrl.c file.
  12.    InitSysCtrl();

  13. // Step 2. Initialize GPIO:
  14. // This example function is found in the DSP2833x_Gpio.c file and
  15. // illustrates how to set the GPIO to it's default state.
  16. // InitGpio();  // Skipped for this example

  17. // For this case just init GPIO pins for ePWM1, ePWM2, ePWM3
  18. // These functions are in the DSP2833x_EPwm.c file
  19.    InitEPwm1Gpio();


  20. // Step 3. Clear all interrupts and initialize PIE vector table:
  21. // Disable CPU interrupts
  22.    DINT;

  23. // Initialize the PIE control registers to their default state.
  24. // The default state is all PIE interrupts disabled and flags
  25. // are cleared.
  26. // This function is found in the DSP2833x_PieCtrl.c file.
  27.    InitPieCtrl();

  28. // Disable CPU interrupts and clear all CPU interrupt flags:
  29.    IER = 0x0000;
  30.    IFR = 0x0000;

  31. // Initialize the PIE vector table with pointers to the shell Interrupt
  32. // Service Routines (ISR).
  33. // This will populate the entire table, even if the interrupt
  34. // is not used in this example.  This is useful for debug purposes.
  35. // The shell ISR routines are found in DSP2833x_DefaultIsr.c.
  36. // This function is found in DSP2833x_PieVect.c.
  37.    InitPieVectTable();

  38. // Interrupts that are used in this example are re-mapped to
  39. // ISR functions found within this file.
  40. // EALLOW;  // This is needed to write to EALLOW protected registers
  41. // PieVectTable.EPWM1_INT = &EPWM1_INT_ISR;

  42. // EDIS;    // This is needed to disable write to EALLOW protected registers

  43. // Step 4. Initialize all the Device Peripherals:
  44. // This function is found in DSP2833x_InitPeripherals.c
  45. // InitPeripherals();  // Not required for this example

  46. // For this example, only initialize the ePWM

  47.    EALLOW;
  48.    SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
  49.    EDIS;

  50.    InitEPwm1Example();
  51.    InitSpwm();

  52.    EALLOW;
  53.    SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
  54.    EDIS;

  55. // Step 5. User specific code, enable interrupts:

  56. // Enable CPU INT3 which is connected to EPWM1-3 INT:
  57.    IER |= M_INT3;

  58. // Enable EPWM INTn in the PIE: Group 3 interrupt 1-3
  59.    PieCtrlRegs.PIEIER3.bit.INTx1 = 1;

  60. // Enable global Interrupts and higher priority real-time debug events:
  61.    EINT;   // Enable Global interrupt INTM
  62.    ERTM;   // Enable Global realtime interrupt DBGM

  63. // Step 6. IDLE loop. Just sit and loop forever (optional):
  64.    for(;;)
  65.    {
  66.            __asm("          NOP");
  67.    }

  68. }
  69. void InitEPwm1Example()
  70. {
  71.    // Setup TBCLK
  72.    EPwm1Regs.TBPRD = 0xffff;           // Set timer period 801 TBCLKs
  73.    EPwm1Regs.TBPHS.half.TBPHS = 0x0000;           // Phase is 0
  74.    EPwm1Regs.TBCTR = 0x0000;                      // Clear counter

  75.    // Set Compare values
  76.    EPwm1Regs.CMPA.half.CMPA = 0x7fff;     // Set compare A value
  77.    EPwm1Regs.CMPB = 0x7fff;               // Set Compare B value

  78.    // Setup counter mode
  79.    EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Count up
  80.    EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;        // Disable phase loading
  81.    EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV2;       // Clock ratio to SYSCLKOUT
  82.    EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV8;

  83.    // Setup shadowing
  84.    EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
  85.    EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
  86.    EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;  // Load on Zero
  87.    EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;


  88.    // Set actions
  89.    EPwm1Regs.AQCTLA.bit.CAU = AQ_SET;             // Set PWM1A on event A, up count
  90.    EPwm1Regs.AQCTLA.bit.CAD = AQ_CLEAR;           // Clear PWM1A on event A, down count

  91.    EPwm1Regs.AQCTLB.bit.CBU = AQ_SET;             // Set PWM1B on event B, up count
  92.    EPwm1Regs.AQCTLB.bit.CBD = AQ_CLEAR;           // Clear PWM1B on event B, down count

  93.    // Interrupt where we will change the Compare Values
  94.    EPwm1Regs.ETSEL.bit.INTSEL = ET_CTRU_CMPA;      // Select INT on Zero event
  95.    EPwm1Regs.ETSEL.bit.INTEN = 1;                 // Enable INT
  96.    EPwm1Regs.ETPS.bit.INTPRD = ET_1ST;            // Generate INT on 3rd event

  97. }

复制代码

所有资料51hei提供下载:
SPWM.zip (91.11 KB, 下载次数: 99)
回复

使用道具 举报

ID:15377 发表于 2018-3-29 06:39 | 显示全部楼层
谢谢分享!
回复

使用道具 举报

ID:247467 发表于 2018-4-18 20:02 | 显示全部楼层
也是今天才看了,PWM这个部分,感觉还不太深入,可以看看这个程序,好好在学习一下
回复

使用道具 举报

ID:324611 发表于 2018-12-20 10:58 | 显示全部楼层
正在学习spwm,收藏了。
回复

使用道具 举报

ID:284570 发表于 2019-7-20 17:53 | 显示全部楼层
赞!!
回复

使用道具 举报

ID:742944 发表于 2020-5-2 18:34 | 显示全部楼层
不错,学习了
回复

使用道具 举报

ID:746506 发表于 2020-5-7 22:33 | 显示全部楼层
请问无法打开“Cover.h”源文件是什么意思?
回复

使用道具 举报

ID:746506 发表于 2020-5-7 22:34 | 显示全部楼层
你好,请问无法打开“Cover.h”文件,是不是少了这个头文件?请问楼主有这个文件吗,可以发我一下吗
回复

使用道具 举报

ID:755142 发表于 2020-5-29 19:55 | 显示全部楼层
大佬,我想请教,加个好友呗
回复

使用道具 举报

ID:755142 发表于 2020-5-29 19:58 | 显示全部楼层
JerZY 发表于 2020-5-7 22:33
请问无法打开“Cover.h”源文件是什么意思?

用啥打开呀
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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