找回密码
 立即注册

QQ登录

只需一步,快速开始

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

DSP2812产生三相SPWM波程序

[复制链接]
跳转到指定楼层
楼主
ID:573493 发表于 2019-6-27 15:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
DSP2812产生三相SPWM波,有需要的拿走

单片机源程序如下:
  1. /*
  2. 本程序在220V 3400转 5对级直流无刷电机调试下通过
  3. */
  4. #include "DSP281x_Device.h"     // DSP281x Headerfile Include File
  5. #include "DSP281x_Examples.h"   // DSP281x Examples Include File
  6. #include "math.h"

  7. #define C_TIME 20
  8. #define pwm_half_per 3750 /*3750 pwm=20khz*//*sytemclk=150MHz*/
  9.                           /*1875 pwm=40kHz*/

  10. #define pole 2  /*电机的级对数*/
  11. #define timer2_per 60000//234375/* timer2 period with a 1/128 timer prescaler and 150MHz CPUCLK*/
  12. #define PI 3.1415926
  13. unsigned int adc_res;
  14. unsigned int dir=1;
  15. unsigned int ldd=0;
  16. unsigned int PWM_DUTY=2000;

  17. unsigned int I_result[2048];
  18. unsigned int i=0;
  19. unsigned int hall=0x000;
  20. unsigned int time_cnt=C_TIME;
  21. unsigned int hallspeed=0;
  22. unsigned int prehall=0;
  23. unsigned int speed;
  24. unsigned int a=0,b=0,c=0,d=0;
  25. unsigned int displaytime=0;
  26. unsigned int displayflag=0;

  27. unsigned int fc=10000;
  28. unsigned int f=50;
  29. float M=0.9;
  30. unsigned int k=0;
  31. // Prototype statements for functions found within this file.
  32. void init_eva(void);
  33. void eva_timer1_isr(void);

  34. //void init_evb(void);

  35. // Global counts used in this example


  36. void main(void)
  37. {

  38. // Step 1. Initialize System Control:
  39. // PLL, WatchDog, enable Peripheral Clocks
  40. // This example function is found in the DSP281x_SysCtrl.c file.
  41.    InitSysCtrl();

  42. // Step 2. Initalize GPIO:
  43. // This example function is found in the DSP281x_Gpio.c file and
  44. // illustrates how to set the GPIO to it's default state.
  45. // InitGpio();  // Skipped for this example  

  46. // Initialize only GPAMUX and GPBMUX for this test
  47.    EALLOW;
  48.    // Enable PWM pins
  49.    GpioMuxRegs.GPAMUX.all = 0x003F; // EVA PWM 1-6  pins
  50.    GpioMuxRegs.GPAQUAL.all=0x0000;
  51.    EDIS;
  52.    
  53. // Step 3. Clear all interrupts and initialize PIE vector table:
  54. // Disable CPU interrupts
  55.    DINT;

  56. // Initialize PIE control registers to their default state.
  57. // The default state is all PIE interrupts disabled and flags
  58. // are cleared.  
  59. // This function is found in the DSP281x_PieCtrl.c file.
  60.    InitPieCtrl();
  61.    IER = 0x0000;
  62.    IFR = 0x0000;
  63.    
  64.   // Interrupts that are used in this example are re-mapped to
  65. // ISR functions found within this file.
  66.    EALLOW;  // This is needed to write to EALLOW protected registers
  67.    PieVectTable.T1PINT = &eva_timer1_isr;

  68.    EDIS;  



  69. // Disable CPU interrupts and clear all CPU interrupt flags:


  70. // Initialize the PIE vector table with pointers to the shell Interrupt
  71. // Service Routines (ISR).  
  72. // This will populate the entire table, even if the interrupt
  73. // is not used in this example.  This is useful for debug purposes.
  74. // The shell ISR routines are found in DSP281x_DefaultIsr.c.
  75. // This function is found in DSP281x_PieVect.c.
  76.    InitPieVectTable();
  77.    EvaRegs.T1CON.all=0x0000;//disable time1
  78. // Step 4. Initialize all the Device Peripherals:
  79. // This function is found in DSP281x_InitPeripherals.c
  80. // InitPeripherals(); // Not required for this example
  81.    init_eva();
  82. //   init_evb();

  83. // Step 5. User specific code, enable interrupts:
  84. // Enable PIE group 2 interrupt 4 for T1PINT
  85.     PieCtrlRegs.PIEIER2.all = M_INT4;


  86.     // Enable CPU INT2 for T1PINT, INT3 for T2PINT, INT4 for T3PINT
  87.     // and INT5 for T4PINT:
  88.     IER |= M_INT2;

  89.     // Enable global Interrupts and higher priority real-time debug events:
  90.     EINT;   // Enable Global interrupt INTM
  91.     ERTM;   // Enable Global realtime interrupt DBGM
  92.     EvaRegs.EVAIMRA.bit.T1PINT = 1;
  93.     EvaRegs.EVAIFRA.bit.T1PINT = 1;
  94.   // Just sit and loop forever:
  95.   // PWM pins can be observed with a scope.       
  96.    
  97.   for(;;)
  98.   {
  99.   }

  100. }

  101. void init_eva()
  102. {

  103. // EVA Configure T1PWM, T2PWM, PWM1-PWM6
  104. // Initalize the timers
  105.    

  106.    EvaRegs.GPTCONA.bit.TCMPOE = 1;//0
  107.    EvaRegs.GPTCONA.bit.T1PIN = 2;//0
  108.    // Initalize EVA Timer1
  109.    //EvaRegs.T1PR=0.0001;
  110.    EvaRegs.T1PR = pwm_half_per;       // Timer1 period
  111.    EvaRegs.T1CMPR=0;
  112.    //EvaRegs.T1CMPR = PWM_DUTY;     // Timer1 compare
  113.    EvaRegs.T1CNT = 0x0000;      // Timer1 counter
  114.    EvaRegs.DBTCONA.all=0x0000;      //deadband units off
  115.    EvaRegs.ACTRA.all  =0x0fff; //pwm pin set active high*/
  116.    // Enable compare for PWM1-PWM6
  117.    EvaRegs.CMPR1=0;
  118.    //EvaRegs.CMPR1=PWM_DUTY;
  119.    EvaRegs.CMPR2=PWM_DUTY;
  120.    EvaRegs.CMPR3=PWM_DUTY;
  121.    EvaRegs.COMCONA.all = 0xA600;
  122.    EvaRegs.T1CON.all = 0x842;//0x840   

  123.    
  124.   // Compare action control.  Action that takes place
  125.   // on a cmpare event
  126.   // output pin 1 CMPR1 - active high
  127.   // output pin 2 CMPR1 - active low
  128.   // output pin 3 CMPR2 - active high
  129.   // output pin 4 CMPR2 - active low
  130.   // output pin 5 CMPR3 - active high
  131.   // output pin 6 CMPR3 - active low

  132.   EvaRegs.DBTCONA.all = 0x1000; // Disable deadband



  133. }


  134. void eva_timer1_isr(void)
  135. {
  136.    
  137.    EvaRegs.EVAIMRA.bit.T1PINT = 1;

  138.    EvaRegs.EVAIFRA.all = BIT7;
  139.    EvaRegs.ACTRA.all =0x0666;//0x0c02
  140.    //EvaRegs.CMPR3=pwm_half_per-200;
  141.    //EvaRegs.CMPR1=PWM_DUTY;
  142.    //EvaRegs.CMPR2=PWM_DUTY;
  143.    EvaRegs.CMPR1=(pwm_half_per/2)*(1+0.9*sin(PI*k/100));
  144.    EvaRegs.CMPR2=(pwm_half_per/2)*(1+0.9*sin(PI*k/100+2*PI/3));
  145.    EvaRegs.CMPR3=(pwm_half_per/2)*(1+0.9*sin(PI*k/100+4*PI/3));
  146.    k++;
  147.    if(k==200)
  148.    {
  149.      k=0;
  150.    }
  151. //  EvaRegs.CMPR1=PWM_DUTY;
  152. //  EvaRegs.CMPR2=PWM_DUTY;
  153. //  EvaRegs.CMPR3=PWM_DUTY;
  154.    
  155.    PieCtrlRegs.PIEACK.all = PIEACK_GROUP2;
  156. }

  157.        
复制代码

所有资料51hei提供下载:
75448169dsp2812spwm.zip (422.74 KB, 下载次数: 59)


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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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