Code example for TMS320LF2406

Here are some code examples for TI TMS320LF2406 in terms of functions including initial settings, timer, QEP, PWM, ADC, SPI (DAC), etc

Normally, a program is constructed as follows. The main objective of writing separate functions is to easily handle all codes for later use.

Example code:

#include “2406.h”

#include <math.h> // if necessary

{ control variables}

{Functions}

// Initialize DSP

void Init_DSP(){
asm(” SETC INTM”);
SCSR1 = 0x00ED;//4xFin,enable SCI, ADC, SPI
SCSR2 = (SCSR2 | 0x000B) & 0x000F;//
IMR = 0x0012;  //timer1, SCI(low-priority mode)
IFR = 0xFFFF;  // Interrupt flag Register 2-18
MCRA = 0x0FFF;//PWM1,QEP,SCI,other IO A,B
MCRB = 0x00FC;// SPISIMO, SPISOMI, SPICLK, SPISTE
MCRC = 0x0FFF;
// XINT1CR = 0x8003;//low priority, enable
PADATDIR = 0x0000;//Port A, input
PCDATDIR = 0xFF00;//Port C, output
asm(” CLRC INTM”);//enable all interrupt
}

// Delay function if used

void delay_ms(unsigned int ms){

unsigned long count1,temp1;

temp1=ms*2217;//1ms a cycle

for (count1=0;count1<temp1;count1++)

{asm(” nop”); }

}

// QEP configuration is used for reading encoder

void QEP_configuration(){

EVAIMRB = 0x0000;//no interrupt Timer2

T2PR = 0xFFFF; // den lay gia tri T2CNT truoc khi Timer reset

T2CON = 0x1878;//directional up/down-counting, QEP clock source, enable

CAPCONA = 0x6000;//enable QEP, disable capture units 1,2,3

T4PR = 0xFFFF; // den lay gia tri T2CNT truoc khi Timer reset

T4CON = 0x1878;//directional up/down-counting, QEP clock source, enable

CAPCONB = 0x6000;//enable QEP, disable capture units 1,2,3

}

// Timer configuration, including Timer 1 and 3

void Timer_configuration(){

EVAIMRA= 0x0181;//timer1 period interrupt,enalbe PDPINT

// EVBIMRA= 0x0181;//timer1 period interrupt,enalbe PDPINT

T1PR= 16000;//25ns x 000= 125us, Timer period

T1CON= 0xD042;//countinuous up count mode, x/1=40Mhz,enable timer,reload CMP when counter=0,

//disable timer compare

T3PR = 2000; //25ns x 2000= 50us, Timer period

T3CON = 0xC842;

GPTCONA=0x6046;//enable GP timer compare output,T1PWM active low

COMCONA=0x8200;//enable compare, enable

ACTRA=0x0666;//CMP1 active low

DBTCONA = 0x0AEC; // Dead-band is 2us

GPTCONB=0x6046;//enable GP timer compare output,T1PWM active low

COMCONB=0x8200;//enable compare, enable

ACTRB=0x0666;//CMP1 active low

DBTCONB = 0x0AEC; // Dead-band is 2us

}

// SCI configuration

void SCI_configuration(){

SCICCR = 0x07;

SCICTL1 = 0x23;

SCIHBAUD = 0x00;

SCILBAUD = 42; //baud rate 115200bps

SCICTL2 = 0x02;//

SCIPRI = 0x60; //low interrup priority for TX and RX

}

{Next is interrupt programs}

interrupt void c_int1(){} //ADC(high), External(high), SPI(high priority), SCI(high priority),

// CAN(high priority) interrupt

interrupt void c_int2(){} //Compare123, Timer 1, Compare456, Timer3

interrupt void c_int3(){} //Timer2, Timer4 interrupt

interrupt void c_int4(){}  //Capture 123456 interrupt

interrupt void c_int5(){} //SPI(low priority), SCI(low priority), CAN(low priority)

interrupt void c_int6(){} //ADC (low), External (low)

{Main program}

void main(void){

// Place your functions here, which have been written above

……

while(1)

{}

to be updated late!

Good luck!

One thought on “Code example for TMS320LF2406

Leave a comment