This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

does PWM not work with SPI?



hello everyone,

especially F2812 Gurus :)

it is possible to use PWM and SPI same app.  PWM and SPI are working fine in separate programs. but I can not run them together. here is my code.  is there anyone's help me pls. thanks in advance

#include "DSP281x_Device.h"
#include "DSP281x_Examples.h"   // DSP281x Examples Include File
// Prototype statements for functions found within this file.

Uint16 RB0=0,RB1=0,RBT; 
char OrnekSayisi=15;  
long i,timeout,per;
long Volt,TVolt;
	
void Gpio_select(void);
void InitSystem(void);    
void SPI_Init(void);
void ADC_Oku();
void init_eva(void);

interrupt void cpu_timer0_isr(void); // Prototype for Timer 0 Interrupt Service Routine

void main(void)
{
	   
	
	InitSystem();		// Initialize the DSP's core Registers
	
	// Speed_up the silicon A Revision. 
	// No need to call this function for Rev. C  later silicon versions

	
	Gpio_select();		// Setup the GPIO Multiplex Registers
	
	InitPieCtrl();		// Function Call to init PIE-unit ( code : DSP281x_PieCtrl.c)
	
	InitPieVectTable(); // Function call to init PIE vector table ( code : DSP281x_PieVect.c )
	
	// re-map PIE - entry for Timer 0 Interrupt 
	EALLOW;  // This is needed to write to EALLOW protected registers
   	PieVectTable.TINT0 = &cpu_timer0_isr;
   	EDIS;    // This is needed to disable write to EALLOW protected registers
	
	InitCpuTimers();
	
	// Configure CPU-Timer 0 to interrupt every 50 ms:
	// 150MHz CPU Freq, 50000 µseconds interrupt period
    ConfigCpuTimer(&CpuTimer0, 150, 50000);
    
    // Enable TINT0 in the PIE: Group 1 interrupt 7
   	PieCtrlRegs.PIEIER1.bit.INTx7 = 1;

    
	// Enable CPU INT1 which is connected to CPU-Timer 0:
    IER = 1;
    
	// Enable global Interrupts and higher priority real-time debug events:
   	EINT;   // Enable Global interrupt INTM
   	ERTM;   // Enable Global realtime interrupt DBGM
   	
   	CpuTimer0Regs.TCR.bit.TSS = 0;
   	
   	SPI_Init();
    init_eva();
	while(1)
	{    
	    while(CpuTimer0.InterruptCount < 3); // wait for Timer 0
  		CpuTimer0.InterruptCount = 0;      
  		
  		ADC_Oku();
   		
  	  	EALLOW;
    	SysCtrlRegs.WDKEY = 0xAA;		// and serve watchdog #2		
	    EDIS;
	}
} 	

void Gpio_select(void)
{
	EALLOW;
	GpioMuxRegs.GPAMUX.all = 0x00FF;	// PWM 1-6 Enable
    GpioMuxRegs.GPBMUX.all = 0x0;   
    GpioMuxRegs.GPDMUX.all = 0x0; 
    
    GpioMuxRegs.GPFMUX.all = 0xF;		 
    GpioMuxRegs.GPEMUX.all = 0x0; 
    GpioMuxRegs.GPGMUX.all = 0x0;			
										
    GpioMuxRegs.GPBDIR.all = 0x00FF;	// GPIO Port B15-B8 input , B7-B0 output
    GpioMuxRegs.GPDDIR.all = 0x0;	// GPIO PORT  as input
    GpioMuxRegs.GPDDIR.bit.GPIOD0 = 1;  // /CS for DAC
    GpioMuxRegs.GPDDIR.bit.GPIOD6 = 1;	// /CS for EEPROM	
    GpioMuxRegs.GPEDIR.all = 0x0;	// GPIO PORT  as input
    GpioMuxRegs.GPFDIR.all = 0x0;	// GPIO PORT  as input
    GpioMuxRegs.GPGDIR.all = 0x0;	// GPIO PORT  as input

	GpioDataRegs.GPBDAT.all = 0x0000;	// Switch off LED's ( B7...B0) 
	GpioDataRegs.GPDDAT.bit.GPIOD0 = 1;	// deactivate /CS for the DAC 
	GpioDataRegs.GPDDAT.bit.GPIOD5 = 1; // deactivate /CS for the EEPROM
	
    GpioMuxRegs.GPAQUAL.all = 0x0;	// Set GPIO input qualifier values to zero
    GpioMuxRegs.GPBQUAL.all = 0x0;
    GpioMuxRegs.GPDQUAL.all = 0x0;
    GpioMuxRegs.GPEQUAL.all = 0x0;
    EDIS;
}     


void InitSystem(void)
{
   	EALLOW;
   	SysCtrlRegs.WDCR= 0x00AF;		// Setup the watchdog 
   									// 0x00E8  to disable the Watchdog , Prescaler = 1
   									// 0x00AF  to NOT disable the Watchdog, Prescaler = 64
   	SysCtrlRegs.SCSR = 0; 			// Watchdog generates a RESET	
   	SysCtrlRegs.PLLCR.bit.DIV = 10;	// Setup the Clock PLL to multiply by 5
    
   	SysCtrlRegs.HISPCP.all = 0x1; // Setup Highspeed Clock Prescaler to divide by 2
   	SysCtrlRegs.LOSPCP.all = 0x2; // Setup Lowspeed CLock Prescaler to divide by 4
      	
   	// Peripheral clock enables set for the selected peripherals.   
   	SysCtrlRegs.PCLKCR.bit.EVAENCLK=1;
   	SysCtrlRegs.PCLKCR.bit.EVBENCLK=0;
   	SysCtrlRegs.PCLKCR.bit.SCIAENCLK=0;
   	SysCtrlRegs.PCLKCR.bit.SCIBENCLK=0;
   	SysCtrlRegs.PCLKCR.bit.MCBSPENCLK=0;
   	SysCtrlRegs.PCLKCR.bit.SPIENCLK=1;
   	SysCtrlRegs.PCLKCR.bit.ECANENCLK=0;
   	SysCtrlRegs.PCLKCR.bit.ADCENCLK=0;
   	EDIS;
}

interrupt void cpu_timer0_isr(void)
{
    CpuTimer0.InterruptCount++;
   	// Serve the watchdog every Timer 0 interrupt
   	EALLOW;
	SysCtrlRegs.WDKEY = 0x55;		// Serve watchdog #1
	EDIS;

   // Acknowledge this interrupt to receive more interrupts from group 1
   PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
} 

void SPI_Init(void)
{
	SpiaRegs.SPICCR.all = 0x0047;	
	// Bit 7 , Reset = 0 : 
	// Bit 6 , Clock Polarity = 1 : data output on falling edge of CLK
	//       , together with CLOCK PHASE = 1 : data output is one half cycle before
	//       , falling edge pf SPICLK
	// Bit 5 , reserved
	// Bit 4 , SPILBK = 0 : no loopback mode
	// Bit 3-0 , Chars = 1111 : 16 bit data transfer
	SpiaRegs.SPICTL.all =0x0006;
	// Bit 7-5 : reserved
	// Bit 4 , Overrun INT Enable = 0 : Disable Receiver Overrun Interrupt
	// Bit 3 , Clock-Phase = 1 , one half clock delay
	// Bit 2 , Master/Slave = 1 ,  MASTER
	// Bit 1 , Talk = 1 ,  Enable Transmission
	// Bit 0 , SPI INT ENA = 0 , No SPI - Interrupts
	SpiaRegs.SPIBRR = 100;
	// SPI Baud Rate =  LSPCLK / ( SPIBRR + 1)
	//				 =  37,5 MHz / ( 124 + 1 )
	//			     =  300 kHz
	SpiaRegs.SPICCR.bit.SPISWRESET = 1; // relinquish SPI from reset
	
	
}  

void ADC_Oku()
{

	GpioDataRegs.GPDDAT.bit.GPIOD0 = 0;	// activate /CS for the DAC 
	
	SpiaRegs.SPITXBUF =  0x1800;// transmit data to ADC buffer
	while (SpiaRegs.SPISTS.bit.INT_FLAG != 1) ; // wait for end of transmission
	RBT = SpiaRegs.SPIRXBUF;
	
        
    SpiaRegs.SPITXBUF =  0x00;// transmit data to DAC-Buffer
	while (SpiaRegs.SPISTS.bit.INT_FLAG != 1) ; // wait for end of transmission
	RB0 = SpiaRegs.SPIRXBUF;
	
    SpiaRegs.SPITXBUF =  0x00;// transmit data to DAC-Buffer
	while (SpiaRegs.SPISTS.bit.INT_FLAG != 1) ; // wait for end of transmission
	RB1 = SpiaRegs.SPIRXBUF;
	
	
	
	for (i=0;i<100;i++);            // wait for DAC to finish off
	GpioDataRegs.GPDDAT.bit.GPIOD0 = 1;	// deactivate /CS for the DAC 
	
	Volt = (long)(  ( ( (RB0<<8) | RB1>>4)) );
	
	TVolt = Volt *  2990 /16153 ;
}

void init_eva()
{

// EVA Configure T1PWM, T2PWM, PWM1-PWM6 
// Initalize the timers
   // Initalize EVA Timer1 
   EvaRegs.T1PR = 1875;       // Timer1 period 5KHZ
   EvaRegs.T1CMPR = 100;     // Timer1 compare %10 duty
   EvaRegs.T1CNT = 0x0000;      // Timer1 counter
   // TMODE = continuous up/down
   // Timer enable
   // Timer compare enable
   EvaRegs.T1CON.all = 0x1042;   


  // Setup T1PWM and T2PWM
  // Drive T1/T2 PWM by compare logic
  EvaRegs.GPTCONA.bit.TCMPOE = 1;
  // Polarity of GP Timer 1 Compare = Active low
  EvaRegs.GPTCONA.bit.T1PIN = 1;
  // Polarity of GP Timer 2 Compare = Active high
  EvaRegs.GPTCONA.bit.T2PIN = 2;

  // Enable compare for PWM1-PWM6
  EvaRegs.CMPR1 = 100; //%10 duty

    
  // Compare action control.  Action that takes place
  // on a cmpare event
  // output pin 1 CMPR1 - active high
  // output pin 2 CMPR1 - active low
  // output pin 3 CMPR2 - active high
  // output pin 4 CMPR2 - active low
  // output pin 5 CMPR3 - active high
  // output pin 6 CMPR3 - active low
  EvaRegs.ACTRA.all = 0x0666;
  EvaRegs.DBTCONA.all = 0x0000; // Disable deadband
  EvaRegs.COMCONA.all = 0xA600;


} 

#include "DSP281x_Device.h"
#include "DSP281x_Examples.h"   // DSP281x Examples Include File
// Prototype statements for functions found within this file.

Uint16 RB0=0,RB1=0,RBT; 
char OrnekSayisi=15;  
long i,timeout,per;
long Volt,TVolt;
	
void Gpio_select(void);
void InitSystem(void);    
void SPI_Init(void);
void ADC_Oku();
void init_eva(void);

interrupt void cpu_timer0_isr(void); // Prototype for Timer 0 Interrupt Service Routine

void main(void)
{
	   
	
	InitSystem();		// Initialize the DSP's core Registers
	
	// Speed_up the silicon A Revision. 
	// No need to call this function for Rev. C  later silicon versions

	
	Gpio_select();		// Setup the GPIO Multiplex Registers
	
	InitPieCtrl();		// Function Call to init PIE-unit ( code : DSP281x_PieCtrl.c)
	
	InitPieVectTable(); // Function call to init PIE vector table ( code : DSP281x_PieVect.c )
	
	// re-map PIE - entry for Timer 0 Interrupt 
	EALLOW;  // This is needed to write to EALLOW protected registers
   	PieVectTable.TINT0 = &cpu_timer0_isr;
   	EDIS;    // This is needed to disable write to EALLOW protected registers
	
	InitCpuTimers();
	
	// Configure CPU-Timer 0 to interrupt every 50 ms:
	// 150MHz CPU Freq, 50000 �seconds interrupt period
    ConfigCpuTimer(&CpuTimer0, 150, 50000);
    
    // Enable TINT0 in the PIE: Group 1 interrupt 7
   	PieCtrlRegs.PIEIER1.bit.INTx7 = 1;

    
	// Enable CPU INT1 which is connected to CPU-Timer 0:
    IER = 1;
    
	// Enable global Interrupts and higher priority real-time debug events:
   	EINT;   // Enable Global interrupt INTM
   	ERTM;   // Enable Global realtime interrupt DBGM
   	
   	CpuTimer0Regs.TCR.bit.TSS = 0;
   	
   	SPI_Init();
    init_eva();
	while(1)
	{    
	    while(CpuTimer0.InterruptCount < 3); // wait for Timer 0
  		CpuTimer0.InterruptCount = 0;      
  		
  		ADC_Oku();
   		
  	  	EALLOW;
    	SysCtrlRegs.WDKEY = 0xAA;		// and serve watchdog #2		
	    EDIS;
	}
} 	

void Gpio_select(void)
{
	EALLOW;
	GpioMuxRegs.GPAMUX.all = 0x00FF;	// PWM 1-6 Enable
    GpioMuxRegs.GPBMUX.all = 0x0;   
    GpioMuxRegs.GPDMUX.all = 0x0; 
    
    GpioMuxRegs.GPFMUX.all = 0xF;		 
    GpioMuxRegs.GPEMUX.all = 0x0; 
    GpioMuxRegs.GPGMUX.all = 0x0;			
										
    GpioMuxRegs.GPBDIR.all = 0x00FF;	// GPIO Port B15-B8 input , B7-B0 output
    GpioMuxRegs.GPDDIR.all = 0x0;	// GPIO PORT  as input
    GpioMuxRegs.GPDDIR.bit.GPIOD0 = 1;  // /CS for DAC
    GpioMuxRegs.GPDDIR.bit.GPIOD6 = 1;	// /CS for EEPROM	
    GpioMuxRegs.GPEDIR.all = 0x0;	// GPIO PORT  as input
    GpioMuxRegs.GPFDIR.all = 0x0;	// GPIO PORT  as input
    GpioMuxRegs.GPGDIR.all = 0x0;	// GPIO PORT  as input

	GpioDataRegs.GPBDAT.all = 0x0000;	// Switch off LED's ( B7...B0) 
	GpioDataRegs.GPDDAT.bit.GPIOD0 = 1;	// deactivate /CS for the DAC 
	GpioDataRegs.GPDDAT.bit.GPIOD5 = 1; // deactivate /CS for the EEPROM
	
    GpioMuxRegs.GPAQUAL.all = 0x0;	// Set GPIO input qualifier values to zero
    GpioMuxRegs.GPBQUAL.all = 0x0;
    GpioMuxRegs.GPDQUAL.all = 0x0;
    GpioMuxRegs.GPEQUAL.all = 0x0;
    EDIS;
}     


void InitSystem(void)
{
   	EALLOW;
   	SysCtrlRegs.WDCR= 0x00AF;		// Setup the watchdog 
   									// 0x00E8  to disable the Watchdog , Prescaler = 1
   									// 0x00AF  to NOT disable the Watchdog, Prescaler = 64
   	SysCtrlRegs.SCSR = 0; 			// Watchdog generates a RESET	
   	SysCtrlRegs.PLLCR.bit.DIV = 10;	// Setup the Clock PLL to multiply by 5
    
   	SysCtrlRegs.HISPCP.all = 0x1; // Setup Highspeed Clock Prescaler to divide by 2
   	SysCtrlRegs.LOSPCP.all = 0x2; // Setup Lowspeed CLock Prescaler to divide by 4
      	
   	// Peripheral clock enables set for the selected peripherals.   
   	SysCtrlRegs.PCLKCR.bit.EVAENCLK=1;
   	SysCtrlRegs.PCLKCR.bit.EVBENCLK=0;
   	SysCtrlRegs.PCLKCR.bit.SCIAENCLK=0;
   	SysCtrlRegs.PCLKCR.bit.SCIBENCLK=0;
   	SysCtrlRegs.PCLKCR.bit.MCBSPENCLK=0;
   	SysCtrlRegs.PCLKCR.bit.SPIENCLK=1;
   	SysCtrlRegs.PCLKCR.bit.ECANENCLK=0;
   	SysCtrlRegs.PCLKCR.bit.ADCENCLK=0;
   	EDIS;
}

interrupt void cpu_timer0_isr(void)
{
    CpuTimer0.InterruptCount++;
   	// Serve the watchdog every Timer 0 interrupt
   	EALLOW;
	SysCtrlRegs.WDKEY = 0x55;		// Serve watchdog #1
	EDIS;

   // Acknowledge this interrupt to receive more interrupts from group 1
   PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
} 

void SPI_Init(void)
{
	SpiaRegs.SPICCR.all = 0x0047;	
	// Bit 7 , Reset = 0 : 
	// Bit 6 , Clock Polarity = 1 : data output on falling edge of CLK
	//       , together with CLOCK PHASE = 1 : data output is one half cycle before
	//       , falling edge pf SPICLK
	// Bit 5 , reserved
	// Bit 4 , SPILBK = 0 : no loopback mode
	// Bit 3-0 , Chars = 1111 : 16 bit data transfer
	SpiaRegs.SPICTL.all =0x0006;
	// Bit 7-5 : reserved
	// Bit 4 , Overrun INT Enable = 0 : Disable Receiver Overrun Interrupt
	// Bit 3 , Clock-Phase = 1 , one half clock delay
	// Bit 2 , Master/Slave = 1 ,  MASTER
	// Bit 1 , Talk = 1 ,  Enable Transmission
	// Bit 0 , SPI INT ENA = 0 , No SPI - Interrupts
	SpiaRegs.SPIBRR = 100;
	// SPI Baud Rate =  LSPCLK / ( SPIBRR + 1)
	//				 =  37,5 MHz / ( 124 + 1 )
	//			     =  300 kHz
	SpiaRegs.SPICCR.bit.SPISWRESET = 1; // relinquish SPI from reset
	
	
}  

void ADC_Oku()
{

	GpioDataRegs.GPDDAT.bit.GPIOD0 = 0;	// activate /CS for the DAC 
	
	SpiaRegs.SPITXBUF =  0x1800;// transmit data to ADC buffer
	while (SpiaRegs.SPISTS.bit.INT_FLAG != 1) ; // wait for end of transmission
	RBT = SpiaRegs.SPIRXBUF;
	
        
    SpiaRegs.SPITXBUF =  0x00;// transmit data to DAC-Buffer
	while (SpiaRegs.SPISTS.bit.INT_FLAG != 1) ; // wait for end of transmission
	RB0 = SpiaRegs.SPIRXBUF;
	
    SpiaRegs.SPITXBUF =  0x00;// transmit data to DAC-Buffer
	while (SpiaRegs.SPISTS.bit.INT_FLAG != 1) ; // wait for end of transmission
	RB1 = SpiaRegs.SPIRXBUF;
	
	
	
	for (i=0;i<100;i++);            // wait for DAC to finish off
	GpioDataRegs.GPDDAT.bit.GPIOD0 = 1;	// deactivate /CS for the DAC 
	
	Volt = (long)(  ( ( (RB0<<8) | RB1>>4)) );
	
	TVolt = Volt *  2990 /16153 ;
}

void init_eva()
{

// EVA Configure T1PWM, T2PWM, PWM1-PWM6 
// Initalize the timers
   // Initalize EVA Timer1 
   EvaRegs.T1PR = 1875;       // Timer1 period 5KHZ
   EvaRegs.T1CMPR = 100;     // Timer1 compare %10 duty
   EvaRegs.T1CNT = 0x0000;      // Timer1 counter
   // TMODE = continuous up/down
   // Timer enable
   // Timer compare enable
   EvaRegs.T1CON.all = 0x1042;   


  // Setup T1PWM and T2PWM
  // Drive T1/T2 PWM by compare logic
  EvaRegs.GPTCONA.bit.TCMPOE = 1;
  // Polarity of GP Timer 1 Compare = Active low
  EvaRegs.GPTCONA.bit.T1PIN = 1;
  // Polarity of GP Timer 2 Compare = Active high
  EvaRegs.GPTCONA.bit.T2PIN = 2;

  // Enable compare for PWM1-PWM6
  EvaRegs.CMPR1 = 100; //%10 duty

    
  // Compare action control.  Action that takes place
  // on a cmpare event
  // output pin 1 CMPR1 - active high
  // output pin 2 CMPR1 - active low
  // output pin 3 CMPR2 - active high
  // output pin 4 CMPR2 - active low
  // output pin 5 CMPR3 - active high
  // output pin 6 CMPR3 - active low
  EvaRegs.ACTRA.all = 0x0666;
  EvaRegs.DBTCONA.all = 0x0000; // Disable deadband
  EvaRegs.COMCONA.all = 0xA600;


}