4523|0

5

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

上位机与dsp的sci通讯程序,小弟改的,初学者,大家帮我看下,那有问题!我改下。 [复制链接]

#include "DSP2833x_Device.h"     // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h"   // DSP2833x Examples Include File

#define SCI 1

#define UART_INT        0

// Prototype statements for functions found within this file.
void InitEPwm1(void);
void InitEPwm2(void);
void InitEPwm3(void);
interrupt void epwm1_isr(void);
interrupt void scicTxFifoIsr(void);
interrupt void scicRxFifoIsr(void);
interrupt void scibTxFifoIsr(void);
interrupt void scibRxFifoIsr(void);

void scib_fifo_init();
void scic_fifo_init();
void error(void);
void scib_xmit( Uint16 f);
void pwm(void);

#define PRD 25000/2
#define PI 3.1415926
// Global variables
Uint16 dataC;    // Received data for SCI-A
//Uint16 dataB;    // Rceived data for SCI-A
Uint16 N=60;
Uint16 g;
float M=0.8;
int i=0;
void main(void)
{
   Uint16 ReceivedChar;
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2833x_SysCtrl.c file.
   InitSysCtrl();

// Step 2. Initalize GPIO:
// This example function is found in the DSP2833x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
   InitGpio();
// Setup only the GP I/O only for SCI-A and SCI-B functionality
// This function is found in DSP2833x_Sci.c
   InitSciGpio();
   InitEPwm1Gpio();
   InitEPwm2Gpio();
   InitEPwm3Gpio();
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
   DINT;

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

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

// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in DSP2833x_DefaultIsr.c.
// This function is found in DSP2833x_PieVect.c.
   InitPieVectTable();
EALLOW;  
  PieVectTable.EPWM1_INT = &epwm1_isr;
// PieVectTable.ADCINT=&adc_isr;
   EDIS;   




   EALLOW;
   SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
   EDIS;

   InitEPwm1();
   InitEPwm2();
   InitEPwm3();
   //InitAD();
   

   EALLOW;
   SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
   EDIS;


  


  


#if UART_INT
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
   EALLOW;        // This is needed to write to EALLOW protected registers
#if SCI
   PieVectTable.SCIRXINTB = &scibRxFifoIsr;
   PieVectTable.SCITXINTB = &scibTxFifoIsr;
#else
   PieVectTable.SCIRXINTC = &scicRxFifoIsr;
   PieVectTable.SCITXINTC = &scicTxFifoIsr;
#endif  
   EDIS;   // This is needed to disable write to EALLOW protected registers
#endif
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2833x_InitPeripherals.c
// InitPeripherals(); // Not required for this example

#if SCI
    scib_fifo_init();  // Init SCI-B
#else
    scic_fifo_init();  // Init SCI-C
#endif

// Step 5. User specific code, enable interrupts:
#if UART_INT
// Enable interrupts required for this example
   PieCtrlRegs.PIECTRL.bit.ENPIE = 1;   // Enable the PIE block
#if SCI  
   PieCtrlRegs.PIEIER9.bit.INTx3=1;     // PIE Group 9, INT3 RE
   PieCtrlRegs.PIEIER9.bit.INTx4=1;     // PIE Group 9, INT4 TX
   IFR = 0x0000;   
   IER = 0x100;        // Enable CPU INT   
#else
   PieCtrlRegs.PIEIER8.bit.INTx5=1;     // PIE Group 8, int5 RE
   PieCtrlRegs.PIEIER8.bit.INTx6=1;     // PIE Group 8, INT6 TX
   IFR = 0x0000;   
   IER = 0x080;        // Enable CPU INT  
#endif
   EINT;
#endif
// Step 6. IDLE loop. Just sit and loop forever (optional):
   while(1)
   {
#if UART_INT==0
   #if SCI
       while(ScibRegs.SCIFFRX.bit.RXFFST == 0); { } // wait for RRDY/RXFFST =1 for 1 data available in FIFO
       ReceivedChar = ScibRegs.SCIRXBUF.all;
      // ScibRegs.SCITXBUF=dataB;
        //        f=dataB;
        N=3000/ReceivedChar;
    M=0.8*ReceivedChar/50;
      // while(ScibRegs.SCIFFTX.bit.TXFFST != 0);
       scib_xmit(ReceivedChar);
           if(ReceivedChar==0xc1)
           {pwm();}
          
   #else
       while(ScicRegs.SCIFFRX.bit.RXFFST == 0); { } // wait for RRDY/RXFFST =1 for 1 data available in FIFO
       dataC = ScicRegs.SCIRXBUF.all;
       ScicRegs.SCITXBUF= dataC;
       while(ScicRegs.SCIFFTX.bit.TXFFST != 0);
   #endif
#else
   for(;;);
#endif
   }

}
void pwm()
{
IER |= M_INT3;


PieCtrlRegs.PIEIER3.bit.INTx1 = 1;
  PieCtrlRegs.PIEIER1.bit.INTx6 = 1;
  IER |= M_INT1;

   EINT;   
   ERTM;  
}
void scib_fifo_init()
{
   
    ScibRegs.SCICCR.all =0x0007;    // 1 stop bit,  No loopback
                                   // No parity,8 char bits,
                                   // async mode, idle-line protocol
    ScibRegs.SCICTL1.all =0x0003;   // enable TX, RX, internal SCICLK,
                                   // Disable RX ERR, SLEEP, TXWAKE
#if UART_INT
    ScibRegs.SCICTL2.bit.TXINTENA =1;
    ScibRegs.SCICTL2.bit.RXBKINTENA =1;
    ScibRegs.SCIHBAUD    =0x0001;
    ScibRegs.SCILBAUD    =0x00e7;
    ScibRegs.SCICCR.bit.LOOPBKENA =0; // disenable loop back
    ScibRegs.SCIFFTX.all=0xC020;
    ScibRegs.SCIFFRX.all=0x0021;
    ScibRegs.SCIFFCT.all=0x00;

    ScibRegs.SCICTL1.all =0x0023;     // Relinquish SCI from Reset
    ScibRegs.SCIFFTX.bit.TXFIFOXRESET=1;
    ScibRegs.SCIFFRX.bit.RXFIFORESET=1;

#else
        ScibRegs.SCICTL2.all =0x0003;
        ScibRegs.SCICTL2.bit.TXINTENA =1;
        ScibRegs.SCICTL2.bit.RXBKINTENA =1;
    ScibRegs.SCIHBAUD    =0x0001;
    ScibRegs.SCILBAUD    =0x00e7;
        ScibRegs.SCICCR.bit.LOOPBKENA =0; // Disable loop back
        ScibRegs.SCICTL1.all =0x0023;     // Relinquish SCI from Reset

        ScibRegs.SCIFFTX.bit.TXFIFOXRESET=0;
    ScibRegs.SCIFFRX.bit.RXFIFORESET=0;
    ScibRegs.SCIFFTX.all=0xE040;
    ScibRegs.SCIFFRX.all=0x204f;
    ScibRegs.SCIFFCT.all=0x0;
#endif
}

void scic_fifo_init()
{
   ScicRegs.SCICCR.all =0x0007;   // 1 stop bit,  No loopback
                                  // No parity,8 char bits,
                                  // async mode, idle-line protocol
   ScicRegs.SCICTL1.all =0x0003;  // enable TX, RX, internal SCICLK,
                                  // Disable RX ERR, SLEEP, TXWAKE
#if UART_INT
   ScicRegs.SCICTL2.bit.TXINTENA =1;
   ScicRegs.SCICTL2.bit.RXBKINTENA =1;
   ScicRegs.SCIHBAUD = 0x0001;
   ScicRegs.SCILBAUD = 0x00e7;
   ScicRegs.SCICCR.bit.LOOPBKENA =0; // Disenable loop back
   ScicRegs.SCIFFTX.all=0xC020;
   ScicRegs.SCIFFRX.all=0x0021;
   ScicRegs.SCIFFCT.all=0x00;

   ScicRegs.SCICTL1.all =0x0023;     // Relinquish SCI from Reset
   ScicRegs.SCIFFTX.bit.TXFIFOXRESET=1;
   ScicRegs.SCIFFRX.bit.RXFIFORESET=1;
#else
        ScicRegs.SCICTL2.all =0x0003;
        ScicRegs.SCICTL2.bit.TXINTENA =1;
        ScicRegs.SCICTL2.bit.RXBKINTENA =1;
    ScicRegs.SCIHBAUD    =0x0001;
    ScicRegs.SCILBAUD    =0x00e7;
        ScicRegs.SCICCR.bit.LOOPBKENA =0;// Disable loop back
        ScicRegs.SCICTL1.all =0x0023;    // Relinquish SCI from Reset
   
    ScicRegs.SCIFFTX.bit.TXFIFOXRESET=0;
    ScicRegs.SCIFFRX.bit.RXFIFORESET=0;
    ScicRegs.SCIFFTX.all=0xE040;
    ScicRegs.SCIFFRX.all=0x204f;
    ScicRegs.SCIFFCT.all=0x0;
#endif
}
void InitEPwm1()
{
        EPwm1Regs.TBPRD = PRD; // Period = 2500 TBCLK counts
        EPwm1Regs.TBPHS.half.TBPHSHR = 0;// Set Phase register to zero
        EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
        EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Master module
        EPwm1Regs.TBCTL.bit.PRDLD = TB_SHADOW;
        EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO;
        EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
        EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
        EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; // load on CTR=Zero
        EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; // load on CTR=Zero


        EPwm1Regs.AQCTLA.bit.CAU = AQ_SET; // set actions for EPWM1A
        EPwm1Regs.AQCTLA.bit.CAD = AQ_CLEAR;

    EPwm1Regs.DBCTL.bit.OUT_MODE = DB_FULL_ENABLE; // enable Dead-band module
        EPwm1Regs.DBCTL.bit.POLSEL = DB_ACTV_HIC; // Active Hi complementary
        EPwm1Regs.DBFED = 50; // FED = 50 TBCLKs
        EPwm1Regs.DBRED = 50; // RED = 50 TBCLKs


        EPwm1Regs.ETSEL.bit.INTEN=1;
        EPwm1Regs.ETSEL.bit.INTSEL=0x1;

    EPwm1Regs.ETPS.bit.INTPRD=ET_1ST;

        EPwm1Regs.ETSEL.bit.SOCAEN = 1;        // Enable SOC on A group
        EPwm1Regs.ETSEL.bit.SOCASEL= 1;       // Select SOC from CTR=0
        EPwm1Regs.ETPS.bit.SOCAPRD = 1;        // Generate pulse on 1st event


}

void InitEPwm2()
{
        EPwm2Regs.TBPRD = PRD; // Period = 1600 TBCLK counts
        EPwm2Regs.TBPHS.half.TBPHS = 0; // Set Phase register to zero
        EPwm2Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Symmetrical mode
        EPwm2Regs.TBCTL.bit.PHSEN = TB_ENABLE; // Slave module
        EPwm2Regs.TBCTL.bit.PRDLD = TB_SHADOW;
        EPwm2Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_IN; // sync flow-through???
        EPwm2Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
        EPwm2Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
        EPwm2Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; // load on CTR=Zero
        EPwm2Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; // load on CTR=Zero
        EPwm2Regs.AQCTLA.bit.CAU = AQ_SET; // set actions for EPWM2A
        EPwm2Regs.AQCTLA.bit.CAD = AQ_CLEAR;
        EPwm2Regs.DBCTL.bit.OUT_MODE = DB_FULL_ENABLE; // enable Dead-band module
        EPwm2Regs.DBCTL.bit.POLSEL = DB_ACTV_HIC; // Active Hi complementary
        EPwm2Regs.DBFED = 50; // FED = 50 TBCLKs
        EPwm2Regs.DBRED = 50; // RED = 50 TBCLKs
        EPwm2Regs.ETSEL.bit.INTEN=0;

//        EPwm2Regs.ETSEL.bit.SOCAEN = 1;        // Enable SOC on A group
//        EPwm2Regs.ETSEL.bit.SOCASEL = 2;       // Select SOC from from CPMA on upcount
//        EPwm2Regs.ETPS.bit.SOCAPRD = 1;        // Generate pulse on 1st event  ??
}

void InitEPwm3()
{
        EPwm3Regs.TBPRD = PRD; // Period = 1600 TBCLK counts
        EPwm3Regs.TBPHS.half.TBPHSHR = 0; // Set Phase register to zero
        EPwm3Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;// Symmetrical mode
        EPwm3Regs.TBCTL.bit.PHSEN = TB_ENABLE; // Slave module
        EPwm3Regs.TBCTL.bit.PRDLD = TB_SHADOW;
        EPwm3Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_IN; // sync flow-through
        EPwm3Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
        EPwm3Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
        EPwm3Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; // load on CTR=Zero
        EPwm3Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; // load on CTR=Zero
        EPwm3Regs.AQCTLA.bit.CAU = AQ_SET; // set actions for EPWM3A
        EPwm3Regs.AQCTLA.bit.CAD = AQ_CLEAR;

        EPwm3Regs.DBCTL.bit.OUT_MODE = DB_FULL_ENABLE; // enable Dead-band module
        EPwm3Regs.DBCTL.bit.POLSEL = DB_ACTV_HIC; // Active Hi complementary
        EPwm3Regs.DBFED = 50; // FED = 50 TBCLKs
        EPwm3Regs.DBRED = 50; // RED = 50 TBCLKs
        EPwm3Regs.ETSEL.bit.INTEN=0;
}

interrupt void epwm1_isr(void)
{
    i++;

EPwm1Regs.CMPA.half.CMPA=PRD-PRD/2*(1+M*sin(2*PI*i/N));
EPwm2Regs.CMPA.half.CMPA=PRD-PRD/2*(1+M*sin(2*PI*i/N-2*PI/3));
EPwm3Regs.CMPA.half.CMPA=PRD-PRD/2*(1+M*sin(2*PI*i/N+2*PI/3));

        if(i==N)
    i=0;
        EPwm1Regs.ETCLR.bit.INT=1;

        PieCtrlRegs.PIEACK.all=PIEACK_GROUP3;
}
/*interrupt void scibTxFifoIsr(void)
{
    ScibRegs.SCIFFTX.bit.TXFFINTCLR=1;  // Clear Interrupt flag
        PieCtrlRegs.PIEACK.all|=0x100;      // Issue PIE ACK
}

interrupt void scibRxFifoIsr(void)
{
        dataB=ScibRegs.SCIRXBUF.all;         // Read data
    ScibRegs.SCITXBUF=dataB;     // Send data

        ScibRegs.SCIFFRX.bit.RXFFOVRCLR=1;  // Clear Overflow flag
        ScibRegs.SCIFFRX.bit.RXFFINTCLR=1;         // Clear Interrupt flag
        PieCtrlRegs.PIEACK.all|=0x100;          // Issue PIE ack
   
}*/
void scib_xmit( Uint16 f)
{
    ScibRegs.SCITXBUF=f;
        g=f;
}

interrupt void scicTxFifoIsr(void)
{
        ScicRegs.SCIFFTX.bit.TXFFINTCLR=1;        // Clear SCI Interrupt flag
        PieCtrlRegs.PIEACK.all|=0x080;      // Issue PIE ACK
}

interrupt void scicRxFifoIsr(void)
{
        dataC=ScicRegs.SCIRXBUF.all;         // Read data
        ScicRegs.SCITXBUF=dataC;     // Send data

        ScicRegs.SCIFFRX.bit.RXFFOVRCLR=1;   // Clear Overflow flag
        ScicRegs.SCIFFRX.bit.RXFFINTCLR=1;   // Clear Interrupt flag

        PieCtrlRegs.PIEACK.all|=0x080;       // Issue PIE ack
}


void error(void)
{
    asm("     ESTOP0"); // Test failed!! Stop!
    for (;;);
}
程序主要实现的的是 上位机发送c1的时候,进入pwm()函数,在发送其他数,能够发出spwm波,应该有很多问题,大家帮我看下,非常感谢!!
点赞 关注
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表