找回密码
 立即注册

QQ登录

只需一步,快速开始

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

DSP28335邮箱使用代码,已测试可用

[复制链接]
跳转到指定楼层
楼主
ID:234805 发表于 2017-9-22 10:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有需要的童鞋请下载
SP3Muc_ccs5.3_test2MW
下载:
SP3Muc_ccs5.3_test2MW_20150707.rar (361.38 KB, 下载次数: 13)

  1. /*==============================================================================*
  2. *  Copyright(c) Future Power Technology Co. Ltd.  ALL RIGHTS RESERVED
  3. *
  4. *  Product   : Main control board of 3MW modular switch power
  5. *  File name : main.c
  6. *  Purpose   : Main function and interrupt function include cup timer0 and pwm1
  7. *              underflow.
  8. *
  9. *  History:
  10. *  Date            Version        Author            Note
  11. *  2015-04-24      V1.0           Heaton            Created.
  12. *
  13. *==============================================================================*/

  14. /*===========================Include header files===============================*/
  15. #include "GlobalVariableRefs.h"
  16. #include "GlobalVariableDefs.h"
  17. #include "Main.h"
  18. #include "FunctionCall.h"
  19. /*========================Prototype statements for functions====================*/
  20. interrupt void Epwm1_Int_isr(void);
  21. interrupt void Epwm1_Tz1_isr(void);
  22. void Task1ms(void);
  23. void Task2ms(void);
  24. void Task5ms(void);
  25. void Task10ms(void);
  26. void Task50ms(void);
  27. void InitXintf(void);
  28. void InitI2C(void);
  29. void ECANComm(void);


  30. /*================================Macro definition==============================*/
  31. //1/3
  32. #define INV_3 0.3333333
  33. //Run from RAM or FLASH selection with cmd file changed maunually /////////!!!!!!!!!!!
  34. #define   FLASH_TYPE   1
  35. //Debug model///////////////!!!!!!!!!
  36. #define   DEBUG_MODE   2        //0:Voltage and current closed loop  1:Current closed loop  2:Open loop


  37. /*===========================Global variables in this file======================*/
  38. int16 i161msCount = 0;        //Counter of 1ms in main interrupt of 100us/10KHz
  39. int16 i163TmainCount = 0;        //Counter of 3 times of main interrupt(71.4us/14KHz)
  40. int16 i163TmainFlag = 0;        //Flag of 214.2us period
  41. float32 f32CurADSum = 0;        //Current vaule sum from CPLD in 71.4us

  42. //Periodic task structure definition
  43. //Add periodic task array in function list
  44. //and responsible identification in ID list
  45. //Array definition in sub member:run flag, timer, period, fucntion
  46. static PRD_TSK PrdTskList[] =
  47. {
  48.         {0, 1, 1, Task1ms},
  49.         {0, 1, 2, Task2ms},
  50.         {0, 1, 5, Task5ms},
  51.         {0, 1, 10, Task10ms},
  52.         {0, 1, 50, Task50ms},
  53.         //Add task array here...
  54. };
  55. typedef enum PRDTSKID
  56. {
  57.     TASK_1ms,
  58.     TASK_2ms,
  59.     TASK_5ms,
  60.     TASK_10ms,
  61.     TASK_50ms,
  62.     //Add task ID here...
  63.     PRDTSK_MAX        //Number of periodic tasks
  64. } PRD_TSK_ID;


  65. /*=============================================================================*
  66. * Function: main
  67. * Purpose : Initialization and main loop
  68. * Input:
  69. *     void
  70. * Output:
  71. *     void
  72. * Calls:
  73. *     System and peripheral initialization functions
  74. *
  75. * Called by:
  76. *     void
  77. *============================================================================*/
  78. void main(void)
  79. {
  80.         Uint16 i;

  81.         //Initialize System Control:PLL, WatchDog, enable Peripheral Clocks
  82.         InitSysCtrl();

  83.         // Disable CPU interrupts
  84.         DINT;

  85.         //Initialize the PIE control registers to their default state.
  86.         InitPieCtrl();

  87.         //Disable CPU interrupts and clear all CPU interrupt flags:
  88.         IER = 0x0000;
  89.         IFR = 0x0000;

  90.         //Initialize the PIE vector table with pointers to the shell ISR
  91.         InitPieVectTable();

  92.         //Interrupt configuration:
  93.         EALLOW;
  94.         //PieVectTable.TINT0 = &Cpu_Timer0_isr;  //CPU timer0 interrupt
  95.         PieVectTable.EPWM1_TZINT = &Epwm1_Tz1_isr;        //EPWM1 tz1 interrupt
  96.         PieVectTable.EPWM1_INT = &Epwm1_Int_isr;        //EPWM1 int interrupt
  97.         EDIS;

  98.         //Device peripheral initializaiton
  99.         InitXintf();        ///////!!!!!Initialize XINTF tbd...
  100.         InitSci();        ///////!!!!!//Initialize the SCIA&SCIB tbd...
  101.         InitECan();        //Initialize the ECANA&ECANB
  102.         InitI2C();        //Initialize the I2C module
  103.         InitPWM(PWM_PERIOD,PWM_DEADBAND);        //Initialize the EPWM1 only for interrupt
  104.         InitGpio();        //GPIO configure for other use///////////////!!!!!!!! tbd........

  105.         ////////////!!!!!!!!!!!!!!1
  106.         #if FLASH_TYPE
  107.         //Copy all FLASH sections that need to run from RAM
  108.         MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);        //////!!!!!!!!!for debug

  109.         //Initialize the FLASH
  110.         InitFlash();        //////!!!!!!!!!for debug
  111.         #endif
  112.         /////////////!!!!!!!!!!!111111
  113.         //Algorithm initial
  114.         ParaInitTrans();        //User defined parameters, variables and structures initialization

  115.         //Enable interrupt:
  116.         IER |= (M_INT2 | M_INT3);
  117.         PieCtrlRegs.PIEIER2.bit.INTx1 = 1;// Enable epwm1 tz int in the PIE: Group 2 interrupt 1
  118.         PieCtrlRegs.PIEIER3.bit.INTx1 = 1;// Enable epwm1 int in the PIE: Group 3 interrupt 1

  119.         // Enable global Interrupts and higher priority real-time debug events:
  120.         EINT;   // Enable Global interrupt INTM
  121.         ERTM;   // Enable Global realtime interrupt DBGM

  122.         //EepTskInit();        ///////!!!!!Initialization must be after global interrupt is enabled

  123.         //Main loop
  124.         while(1)
  125.         {
  126.                 for(i = 0; i < PRDTSK_MAX; i++)
  127.                 {
  128.                         if(PrdTskList[i].u16RunFlag == 1)
  129.                         {
  130.                                 PrdTskList[i].TaskHook();        //Run the function
  131.                                 PrdTskList[i].u16RunFlag = 0;        //Clear the flag for next running loop
  132.                         }
  133.                 }
  134.         }
  135. }
  136. /*================================function end================================*/


  137. /*=============================================================================*
  138. * Function: Epwm1_Int1_isr
  139. * Purpose : Main period interrupt of 71.4us/14KHz,
  140. *                          module                                                 period
  141. *                          current read from CPLD     71.4us
  142. *                          current PI regulation                71.4*3=214.2us
  143. *                          ECAN                                                71.4*3=214.2us
  144. * Input:
  145. *     AD_UDCOUT
  146. * Output:
  147. *     pi_cur.f32Out
  148. *     PrdTskList[i].u16RunFlag
  149. * Calls:
  150. *     void
  151. * Called by:
  152. *     void
  153. *============================================================================*/
  154. interrupt void Epwm1_Int_isr(void)
  155. {
  156.         Uint16 i, u16PwmCmp, u16PwmCmpBoost;
  157.         float32 f32BuckDutyTmp, f32BoostDutyTmp, f32Tmp;

  158.         //Count for 3*71.4=214.2us
  159.         i163TmainCount++;
  160.         if(i163TmainCount >= 3)
  161.         {
  162.                 i163TmainCount = 0;
  163.                 i163TmainFlag = 1;
  164.         }

  165.         //AD convertion
  166.         ADMeasure();

  167.         //214us module
  168.         if(i163TmainFlag == 1)
  169.         {
  170.                 //Average value calculation
  171.                 f32UdcOut = f32UdcOutSum * INV_3;
  172.                 f32IL[0] = f32ILSum[0] * INV_3;
  173.                 f32IL[1] = f32ILSum[1] * INV_3;
  174.                 f32IL[2] = f32ILSum[2] * INV_3;
  175.                 //f32CurADSum = (f32IL[0] + f32IL[1]  + f32IL[2]);
  176.                 f32UdcOutSum = 0;
  177.                 f32ILSum[0] = 0;
  178.                 f32ILSum[1] = 0;
  179.                 f32ILSum[2] = 0;

  180.                 /////////////!!!!!!!!!!!!!!!!!
  181.                 #if(DEBUG_MODE == 0)        //Two closed loop
  182.                 f32Debug1 += 0.2;
  183.                 if(f32Debug1 >= f32UdcInRef)
  184.                 {
  185.                         f32Debug1 = f32UdcInRef;
  186.                 }

  187.                 //Voltage PI regulation
  188.                 pi_vol.f32Ref = f32Debug1 * INV_VOL_BASE;
  189.                 pi_vol.f32Fdb = f32UdcOut * INV_VOL_BASE;

  190.                 //Current PI regulation
  191.                 pi_cur1.f32Ref = pi_vol.f32Out;
  192.                 //////////////!!!!!!!!!!!!1for debug
  193.                 //pi_cur1.f32Ref = u16EcanCurRef * INV_CUR_BASE;
  194.                 pi_cur1.f32Fdb = f32IL[0] * INV_CUR_BASE;

  195.                 pi_cur2.f32Ref = u16EcanCurRef * INV_CUR_BASE;
  196.                 pi_cur2.f32Fdb = f32IL[1] * INV_CUR_BASE;

  197.                 pi_cur3.f32Ref = u16EcanCurRef * INV_CUR_BASE;
  198.                 pi_cur3.f32Fdb = f32IL[2] * INV_CUR_BASE;

  199.                 if(MSW.bit.RUNNING)
  200.                 {
  201.                         pi_vol.calc(&pi_vol);
  202.                         pi_cur1.calc(&pi_cur1);
  203.                         pi_cur2.calc(&pi_cur2);
  204.                         pi_cur3.calc(&pi_cur3);
  205.                 }
  206.                 else
  207.                 {
  208.                         pi_vol.f32Ui = 0;
  209.                         pi_vol.f32Out = 0;
  210.                         pi_cur1.f32Ui = 0;
  211.                         pi_cur1.f32Out = 0;
  212.                         pi_cur2.f32Ui = 0;
  213.                         pi_cur2.f32Out = 0;
  214.                         pi_cur3.f32Ui = 0;
  215.                         pi_cur3.f32Out = 0;

  216.                         f32Debug1 = 0;
  217.                         f32Debug2 = 0;
  218.                 }
  219.                 #endif

  220.                 #if(DEBUG_MODE == 1)        //Current closed loop
  221.                 f32Debug1 += 0.2;
  222.                 if(f32Debug1 >= 5)
  223.                 {
  224.                         f32Debug1 = 5;
  225.                 }

  226.                 //Current PI regulation
  227.                 //////////////////////!!!!!!!!!!for debug
  228.                 //pi_cur1.f32Ref = f32Debug1 * INV_CUR_BASE;
  229.                 pi_cur1.f32Ref = u16EcanCurRef * INV_CUR_BASE;
  230.                 pi_cur1.f32Fdb = f32IL[0] * INV_CUR_BASE;

  231.                 pi_cur2.f32Ref = u16EcanCurRef * INV_CUR_BASE;
  232.                 pi_cur2.f32Fdb = f32IL[1] * INV_CUR_BASE;

  233.                 pi_cur3.f32Ref = u16EcanCurRef * INV_CUR_BASE;
  234.                 pi_cur3.f32Fdb = f32IL[2] * INV_CUR_BASE;

  235.                 if(MSW.bit.RUNNING)
  236.                 {
  237.                         pi_vol.calc(&pi_vol);
  238.                         pi_cur1.calc(&pi_cur1);
  239.                         pi_cur2.calc(&pi_cur2);
  240.                         pi_cur3.calc(&pi_cur3);
  241.                 }
  242.                 else
  243.                 {
  244.                         pi_vol.f32Ui = 0;
  245.                         pi_vol.f32Out = 0;
  246.                         pi_cur1.f32Ui = 0;
  247.                         pi_cur1.f32Out = 0;
  248.                         pi_cur2.f32Ui = 0;
  249.                         pi_cur2.f32Out = 0;
  250.                         pi_cur3.f32Ui = 0;
  251.                         pi_cur3.f32Out = 0;

  252.                         f32Debug1 = 0;
  253.                         f32Debug2 = 0;
  254.                 }
  255.                 #endif

  256.                 #if(DEBUG_MODE == 2)        //Open loop
  257.                 f32Tmp = (float32)u16EcanCurRef / 150;/////////////for debug
  258.                 f32Debug1 += 2e-5;
  259.                 if(f32Debug1 >= f32Tmp)
  260.                 {
  261.                         f32Debug1 = f32Tmp;
  262.                 }
  263.                 /*if(f32Debug1 >= 0.8)
  264.                 {
  265.                         f32Debug1 = 0.8;
  266.                 }*/


  267.                 if(MSW.bit.RUNNING)
  268.                 {
  269.                         pi_vol.calc(&pi_vol);
  270.                         pi_cur1.calc(&pi_cur1);
  271.                         pi_cur2.calc(&pi_cur2);
  272.                         pi_cur3.calc(&pi_cur3);
  273.                 }
  274.                 else
  275.                 {
  276.                         pi_vol.f32Ui = 0;
  277.                         pi_vol.f32Out = 0;
  278.                         pi_cur1.f32Ui = 0;
  279.                         pi_cur1.f32Out = 0;
  280.                         pi_cur2.f32Ui = 0;
  281.                         pi_cur2.f32Out = 0;
  282.                         pi_cur3.f32Ui = 0;
  283.                         pi_cur3.f32Out = 0;

  284.                         f32Debug1 = 0;
  285.                         f32Debug2 = 0;
  286.                 }
  287.                 pi_cur1.f32Out = f32Debug1;
  288.                 #endif

  289.                 //PWM cmpr update
  290.                 if(pi_cur1.f32Out > DUTY_BOOST_START)
  291.                 {
  292.                         if(pi_cur1.f32Out > 1)
  293.                         {
  294.                                 f32BuckDutyTmp = 1;
  295.                         }
  296.                         else
  297.                         {
  298.                                 f32BuckDutyTmp = pi_cur1.f32Out;
  299.                         }
  300.                         f32BoostDutyTmp = pi_cur1.f32Out - DUTY_BOOST_START;
  301.                 }
  302.                 else
  303.                 {
  304.                         f32BuckDutyTmp = pi_cur1.f32Out;
  305.                         f32BoostDutyTmp = 0;
  306.                 }
  307.                 u16PwmCmp = (Uint16)(f32BuckDutyTmp * PWM_PERIOD);
  308.                 u16PwmCmpBoost = (Uint16)(f32BoostDutyTmp * PWM_PERIOD);
  309.                 EPwm1Regs.CMPA.half.CMPA = u16PwmCmp;        //Buck1
  310.                 EPwm4Regs.CMPA.half.CMPA = u16PwmCmpBoost;        //Boost1

  311.                 u16BuckDuty = (Uint16)(f32BuckDutyTmp * 100);
  312.                 u16BoostDuty = (Uint16)(f32BoostDutyTmp * 100);

  313.                 //ECAN communication
  314.                 ECANComm();

  315.                 //DA convertion for debug
  316.                 DAConverter();

  317.                 i163TmainFlag = 0 ;        //Clear flag for next loop
  318.         }        //214us end

  319.         ////////////////!!!!!!!!!!!!!!!!!
  320.         //EPwm4Regs.AQCTLA.all = 0x002A;
  321.         //AQCTL assignment
  322.         if(MSW.bit.RUNNING)
  323.         {
  324.                 EPwm1Regs.AQCTLA.all = AQCTL_RUN_STATE;
  325.                 ///////////////!!!!!!!!!!!!for debug
  326.                 //EPwm4Regs.AQCTLA.all = AQCTL_RUN_STATE;
  327.                 if(pi_cur1.f32Out > DUTY_BOOST_START)
  328.                 {
  329.                         EPwm4Regs.AQCTLA.all = AQCTL_RUN_STATE;
  330.                 }
  331.                 else
  332.                 {
  333.                         EPwm4Regs.AQCTLA.all = AQCTL_STOP_STATE;
  334.                 }
  335.         }
  336.         else
  337.         {
  338.                 EPwm1Regs.AQCTLA.all= AQCTL_STOP_STATE;
  339.                 EPwm2Regs.AQCTLA.all= AQCTL_STOP_STATE;
  340.                 EPwm3Regs.AQCTLA.all= AQCTL_STOP_STATE;
  341.                 EPwm4Regs.AQCTLA.all= AQCTL_STOP_STATE;
  342.                 EPwm5Regs.AQCTLA.all= AQCTL_STOP_STATE;
  343.                 EPwm6Regs.AQCTLA.all= AQCTL_STOP_STATE;
  344.         }

  345.         //1ms counter
  346.         i161msCount++;
  347.         if(i161msCount>=14)
  348.         {
  349.                 i161msCount=0;

  350.                 //Periodic task process
  351.                 for(i = 0; i < PRDTSK_MAX; i++)
  352.                 {
  353.                         PrdTskList[i].i16Timer--;
  354.                         if(PrdTskList[i].i16Timer <= 0)
  355.                         {
  356.                                 PrdTskList[i].u16RunFlag = 1;        //Enbable function run
  357.                                 PrdTskList[i].i16Timer = PrdTskList[i].u16Period_ms;
  358.                         }
  359.                 }
  360.         }        //1ms count end

  361.         // Clear INT flag for this timer
  362.         EPwm1Regs.ETCLR.bit.INT = 1;

  363.         // Acknowledge this interrupt to receive more interrupts from group 3
  364.         PieCtrlRegs.PIEACK.all |= PIEACK_GROUP3;
  365. }
  366. /*================================function end================================*/


  367. /*=============================================================================*
  368. * Function: Task1ms
  369. * Purpose : Periodic task fucntion of 1ms
  370. * Input:
  371. *     void
  372. * Output:
  373. *     void
  374. * Calls:
  375. *     CtrLogic()
  376. *     Monitor()
  377. * Called by:
  378. *     main()
  379. *============================================================================*/
  380. void Task1ms(void)
  381. {
  382.         Uint16 u16OutCurTmp;

  383.         //Fault and alarm
  384.         Monitor();

  385.         //Control logic, this function must be after Monitor()
  386.         CtrLogic();

  387.         //Filter
  388.         //Output current filter
  389.         /////////////!!!!!!!!!!!!!!need to modify
  390.         u16OutCurTmp = (Uint16)(CUR_BASE * pi_cur1.f32Fdb);
  391.         R_u16FltCurOut += (Uint16)((u16OutCurTmp - R_u16FltCurOut) * OUT_CUR_FLT_CON);
  392. }
  393. /*================================function end================================*/


  394. /*=============================================================================*
  395. * Function: Task2ms
  396. * Purpose : Periodic task fucntion of 2ms
  397. * Input:
  398. *     void
  399. * Output:
  400. *     void
  401. * Calls:
  402. *     CtrCmdSrc()
  403. * Called by:
  404. *     main()
  405. *============================================================================*/
  406. void Task2ms(void)
  407. {
  408.         //Contrl source
  409.         CtrCmdSrc();
  410. }
  411. /*================================function end================================*/


  412. /*=============================================================================*
  413. * Function: Task5ms
  414. * Purpose : Periodic task fucntion of 5ms
  415. * Input:
  416. *     void
  417. * Output:
  418. *     void
  419. * Calls:
  420. *     void
  421. * Called by:
  422. *     main()
  423. *============================================================================*/
  424. void Task5ms(void)
  425. {
  426.         //DO process
  427.         //DOFunc();
  428.         //I2CEEFunc();
  429. }
  430. /*================================function end================================*/


  431. /*=============================================================================*
  432. * Function: Task10ms
  433. * Purpose : Periodic task fucntion of 10ms
  434. * Input:
  435. *     void
  436. * Output:
  437. *     void
  438. * Calls:
  439. *     void
  440. * Called by:
  441. *     main()
  442. *============================================================================*/
  443. void Task10ms(void)
  444. {
  445.         //DO process
  446.         //DOFunc();
  447.         //I2CEEFunc();
  448.         /////////////!!!!!!!!!!!!485 for debug
  449.         RS485IntHost();
  450. }
  451. /*================================function end================================*/


  452. /*=============================================================================*
  453. * Function: Task50ms
  454. * Purpose : Periodic task fucntion of 50ms
  455. * Input:
  456. *     void
  457. * Output:
  458. *     void
  459. * Calls:
  460. *     ParaInputTrans()
  461. *     ParaListTrans()
  462. * Called by:
  463. *     main()
  464. *============================================================================*/
  465. void Task50ms(void)
  466. {
  467.         //Parameter input transformation
  468.         ParaInputTrans();

  469.         //Parameter display transformation
  470.         ParaListTrans();
  471. }
  472. /*================================function end================================*/


  473. /*=============================================================================*
  474. * Function: Epwm1_Tz1_isr
  475. * Purpose : EPWM1 tz1 interrupt when IPM sends a fualt output signal
  476. * Input:
  477. *     void
  478. * Output:
  479. *     void
  480. * Calls:
  481. *     void
  482. * Called by:
  483. *     void
  484. *============================================================================*/
  485. interrupt void Epwm1_Tz1_isr(void)
  486. {
  487.         // Clear INT flag for this timer
  488.         EPwm1Regs.TZCLR.bit.OST = 1;
  489.         EPwm1Regs.TZCLR.bit.INT = 1;

  490.         // Acknowledge this interrupt to receive more interrupts from group 2
  491.         PieCtrlRegs.PIEACK.all |= PIEACK_GROUP2;
  492. }
  493. /*================================function end================================*/



  494. /*===========================================================================*
  495. * End of file.
  496. *===========================================================================*/
复制代码


评分

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

查看全部评分

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

使用道具 举报

沙发
ID:235495 发表于 2017-9-25 14:51 | 只看该作者
学习一下
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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