📄 main.c
字号:
case SW4_CODE :
break;
case SW5_CODE :
break;
case SW6_CODE :
break;
case SW7_CODE :
break;
case SW8_CODE :
break;
default:
break;
}
key_code &= ~KEY_PRESSED; /* Clear key flag */
}
void cont_key_process(){
cont_key_cnt++;
}
void init_adc(void){
// ADC init
AdcRegs.ADCTRL3.bit.ADCBGRFDN = 0x3; // Power up bandgap/reference circuitry
delay_ms(5); // Delay before powering up rest of ADC
AdcRegs.ADCTRL3.bit.ADCPWDN = 1; // Power up rest of ADC
delay_us(20); // Delay after powering up ADC
AdcRegs.ADCTRL1.bit.ACQ_PS = 2; // Acquition window size = 3 clock
AdcRegs.ADCTRL1.bit.CPS = 1; // Tclk = CLK/2
AdcRegs.ADCTRL3.bit.ADCCLKPS = 4; // ADC core clock divider, HSPCLK(75MHz)/(2*4) = 9.3728MHz
AdcRegs.ADCMAXCONV.all = 0x000f; // 16 ch.
AdcRegs.ADCTRL1.bit.SEQ_CASC = 1; // Cascaded mode,SEQ1 and SEQ2 = single 16-state
AdcRegs.ADCTRL1.bit.CONT_RUN = 1; // Cont. conv. mode
AdcRegs.ADCCHSELSEQ1.all = 0x3210;
AdcRegs.ADCCHSELSEQ2.all = 0x7654;
AdcRegs.ADCCHSELSEQ3.all = 0xBA98;
AdcRegs.ADCCHSELSEQ4.all = 0xFEDC;
}
void init_dac(void){
// SPI init for DAC (DAC7612)
SpiaRegs.SPICCR.bit.SPISWRESET=0; // SPI SW RESET = 0
SpiaRegs.SPICTL.all = 0x06; // Master mode,without delay
SpiaRegs.SPIBRR = 7; // 0~2=LSPCLK(37.5MHz)/4=9.375Mbps, 3~127=LSPCLK/(SPIBRR+1)
// 7: 37.5MHz/8= 4.6875MHz
SpiaRegs.SPICCR.all = 0x4d; // CLOCK_POLARITY(1)=falling, 14bit length
SpiaRegs.SPICCR.bit.SPISWRESET=1; // SPI SW RESET = 1
HI_LOAD; // /LOAD = 1;
}
Uint16 t2_prd=4096,t2_cmp=1;
void init_ev(void){
// EVA Configure T1PWM, T2PWM, PWM1-PWM6
// Step 1 Initalize the timers
// Initalize EVA Timer1
EvaRegs.T1PR = PWM_AMP*2; // Timer1 period
EvaRegs.T1CMPR = PWM_AMP; // Timer1 compare
EvaRegs.T1CNT = 0; // Timer1 counter
// TMODE = continuous up/down
// Timer enable
// Timer compare
EvaRegs.T1CON.all = 0x0840;
// Initalize EVA Timer2
EvaRegs.T2PR = t2_prd; // Timer2 period
EvaRegs.T2CMPR = t2_cmp; // Timer2 compare
EvaRegs.T2CNT = 0x0000; // Timer2 counter
// TMODE = continuous up/down
// Timer enable
// Timer compare enable
EvaRegs.T2CON.all = 0x1042;
// Step 2 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;
// Step 3 Enable compare for PWM1-PWM6
EvaRegs.CMPR1 = 500;
EvaRegs.CMPR2 = 500;
EvaRegs.CMPR3 = 500;
// 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 = 0x0999; // change 0x0666 => 0x0999
EvaRegs.DBTCONA.all = 0x09f4; // Deadband: enable,1.92usec
EvaRegs.COMCONA.all = 0xA600;
// Initalize EVA Timer2 for Encoder
EvaRegs.T2PR = 0xffff; // Timer2 period
EvaRegs.T2CNT = 0x0000; // Timer2 counter
EvaRegs.T2CON.all = 0xd870; // FREE=SOFT=1, Dir_UP_DN,x/1,QEP
// Enable Underflow interrupt bits for GP timer 1
EvaRegs.EVAIMRA.bit.T1UFINT = 1; // Enable interrupt
EvaRegs.EVAIFRA.bit.T1UFINT = 1; // Reset flag
////////////////////////////////////////////////////////
// EVB Configure T3PWM, T4PWM, PWM7-PWM12
// Step 1 Initalize the timers
// Initalize EVB Timer1
EvbRegs.T3PR = 4095; // Timer3 period
EvbRegs.T3CMPR = 0; // Timer3 compare
EvbRegs.T3CNT = 0; // Timer3 counter
// TMODE = continuous up/down
// Timer enable
// Timer compare enable
EvbRegs.T3CON.all = 0x0842;
// Step 2 Setup T3PWM and T4PWM
// Drive T1/T2 PWM by compare logic
EvbRegs.GPTCONB.bit.TCMPOE = 1;
// Polarity of GP Timer 3 Compare = Active Low
EvbRegs.GPTCONB.bit.T3PIN = 1;
}
/**********************************************/
void pwm_out(void){
table_dt = TABLE_NUM * DT * freq;
theta_tbl += table_dt;
if(theta_tbl >= TABLE_NUM) theta_tbl -= TABLE_NUM;
table_u = (int)theta_tbl;
table_v = ((int)theta_tbl + (TABLE_NUM / 3)) % TABLE_NUM;
table_w = ((int)theta_tbl + (2*TABLE_NUM / 3)) % TABLE_NUM;
u = SIN_TABLE(table_u);
v = SIN_TABLE(table_v);
w = SIN_TABLE(table_w);
uu = u * k_vf;
vv = v * k_vf;
ww = w * k_vf;
EvaRegs.CMPR1 = (int)(PWM_FACT * uu + PWM_AMP);
EvaRegs.CMPR2 = (int)(PWM_FACT * vv + PWM_AMP);
EvaRegs.CMPR3 = (int)(PWM_FACT * ww + PWM_AMP);
da0_val = (DA_MAX/2) + DA_FACT * uu;
da1_val = (DA_MAX/2) + DA_FACT * vv;
dac_out(0,da0_val);
dac_out(1,da1_val);
}
/**********************************************/
int isr_ticker = 0;
interrupt void EvaTimer1(void){
ON_LED;
isr_ticker++; /* this variable is to verify interrupt working correctly */
pwm_out();
OFF_LED;
// Note: To be safe, use a mask value to write to the entire
// EVAIFRA register. Writing to one bit will cause a read-modify-write
// operation that may have the result of writing 1's to clear
// bits other then those intended.
EvaRegs.EVAIFRA.all = BIT9; // Clear flag
// Acknowledge interrupt to recieve more interrupts from PIE group 2
PieCtrlRegs.PIEACK.all = PIEACK_GROUP2;
}
void main(void){
// Initialize System Control registers, PLL, WatchDog, Clocks to default state:
// This function is found in the DSP28_SysCtrl.c file.
InitSysCtrl();
// HISPCP prescale register settings, normally it will be set to default values
EALLOW; // This is needed to write to EALLOW protected registers
SysCtrlRegs.HISPCP.all = 0x0000; /* SYSCLKOUT/1 */
EDIS; // This is needed to disable write to EALLOW protected registers
// Init.GPIO
init_gpio();
// Disable and clear all CPU interrupts:
DINT;
IER = 0x0000;
IFR = 0x0000;
// Initialize Pie Control Registers To Default State:
InitPieCtrl();
// Initialize the PIE Vector Table To a Known State:
// This function populates the PIE vector table with pointers
// to the shell ISR functions found in DSP28_DefaultIsr.c.
InitPieVectTable();
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.TINT0 = &cpu_timer0_isr; // for TINT0
PieVectTable.XINT1 = &xint1_isr; // for XINT1
PieVectTable.XINT2 = &xint2_isr; // for XINT2
PieVectTable.T1UFINT = &EvaTimer1;
EDIS; // This is needed to disable write to EALLOW protected registers
InitCpuTimers();
ConfigCpuTimer(&CpuTimer0, 150, 1000); // 150MHz CPU Freq, 1 msec Period (in uSeconds)
StartCpuTimer0();
// Interrupt Enable Register
IER |= M_INT1; // for TINT0, XINT1, XINT2
IER |= M_INT2; // for T1UFINT
// PIE : Peripheral Interrupts setting
PieCtrlRegs.PIEIER1.bit.INTx7 = 1; // for TINT0
PieCtrlRegs.PIEIER1.bit.INTx4 = 1; // for XINT1
PieCtrlRegs.PIEIER1.bit.INTx5 = 1; // for XINT2
PieCtrlRegs.PIEIER2.bit.INTx6 = 1; // for T1UFINT
XIntruptRegs.XINT1CR.bit.POLARITY = 0; // 0=falling,1=rising
// XIntruptRegs.XINT1CR.bit.POLARITY = 1; // 0=falling,1=rising
XIntruptRegs.XINT1CR.bit.ENABLE = 1; // 1=enable
XIntruptRegs.XINT2CR.bit.POLARITY = 0; // 0=falling,1=rising
// XIntruptRegs.XINT2CR.bit.POLARITY = 1; // 0=falling,1=rising
XIntruptRegs.XINT2CR.bit.ENABLE = 1; // 1=enable
// for SCI debug
sci_debug_init();
// for XINTF: External Interface
InitXintf();
// for LCD
init_lcd();
// for ADC
init_adc();
// for DAC(SPI)
init_dac();
// for PWM(EV)
init_ev();
// for SCI-B
sci_init();
// Enable global Interrupts and higher priority real-time debug events:
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
lcd_gotoxy(0,0);
lcd_puts("adc0 T2CNT rpm");
AD_START;
while(1){
mcnt++;
ad0 = AdcRegs.ADCRESULT0>>4;
k_vf = (float)ad0 / (float)AD_MAX;
freq = (float)ad0 * FREQ_MAX / (float)AD_MAX;
EvbRegs.T3CMPR = ad0;
lcd_gotoxy(0,1);
lcd_hex4(ad0);
lcd_gotoxy(5,1);
lcd_hex4(EvaRegs.T2CNT);
lcd_gotoxy(10,1);
if(enc_dir==1) lcd_putc('R'); // Right direction
else lcd_putc('L'); // Left direction
lcd_gotoxy(11,1);
lcd_decimal_int(rpm); // display rpm
delay_ms(10);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -