📄 main.c
字号:
//###########################################################################
// FILE: main.c
// RealSYS 2003/9/16
//###########################################################################
#include "..\DSP281x_Device.h"
#include "..\def28x.h"
#include <math.h>
#define ON_LED GpioDataRegs.GPFCLEAR.bit.GPIOF14=1
#define OFF_LED GpioDataRegs.GPFSET.bit.GPIOF14=1
#define T_LED GpioDataRegs.GPFTOGGLE.bit.GPIOF14=1
#define HI_LOAD GpioDataRegs.GPESET.bit.GPIOE2=1
#define LO_LOAD GpioDataRegs.GPECLEAR.bit.GPIOE2=1
// Prototype statements for functions found within this file.
interrupt void cpu_timer0_isr(void);
extern interrupt void scia_rx_isr(void);
extern interrupt void scia_tx_isr(void);
void key_check(void);
int mcnt=0,t0_cnt=0,key,tmp;
int xcnt1=0,xcnt2=0;
enum key_code {SW1_CODE,SW2_CODE,SW3_CODE,SW4_CODE,SW5_CODE,SW6_CODE,SW7_CODE,SW8_CODE};
#define KEY_MASK 0xff
#define NOKEY_CODE 0x0f
#define CODE_MASK 0x0f
#define KEY_PRESSED 0x80
#define KEY_CONT 0x40
#define AD_START AdcRegs.ADCTRL2.bit.SOC_SEQ1=1
#define IS_AD_BUSY AdcRegs.ADCST.bit.SEQ1_BSY
#define PI 3.1415927
BYTE key_code=NOKEY_CODE;
BYTE prev_key_code=NOKEY_CODE;
int key_count=0;
BYTE cont_key_cnt=0;
#define ARRAY_LEN 200
float sv[ARRAY_LEN],cv[ARRAY_LEN];
int idx=0;
WORD ad0;
char const msg[]="!!! DAC test !!!";
void init_gpio(void){
EALLOW;
GpioMuxRegs.GPAMUX.all=0x0000;
/* GPAMUX: GPIO_A function 0=IOP,1=FUN I(0)/O(1)
bit15 0: C3TRIP,PA15 ;IOP 1
bit14 0: C2TRIP,PA14 ;IOP 1
bit13 0: C1TRIP,PA13 ;IOP 1
bit12 0: TCLKINA,PA12 ;IOP 1
bit11 0: TDIRA,PA11 ;IOP 1
bit10 0: CAP3_QEPI1,PA10 ;IOP 1
bit9 0: CAP2_QEP2,PA9 ;IOP 0
bit8 0: CAP1_QEP1,PA8 ;IOP 0
bit7 0: T2PWM_T2CMP,PA7 ;IOP 1
bit6 0: T1PWM_T1CMP,PA6 ;IOP 1
bit5 0: PWM6,PA5 ;IOP 1
bit4 0: PWM5,PA4 ;IOP 1
bit3 0: PWM4,PA3 ;IOP 1
bit2 0: PWM3,PA2 ;IOP 1
bit1 0: PWM2,PA1 ;IOP 1
bit0 0: PWM1,PA0 ;IOP 1
*/
GpioMuxRegs.GPADIR.all=0xfcff; // PA DIR: 1=output,0=input
GpioMuxRegs.GPAQUAL.all=0x0000; // PA Input Qualification:
/* 0x00=No, 0x01=SYSCLK/2, 0x02=SYSCLK/4, 0xff=SYSCLK/510 */
GpioMuxRegs.GPBMUX.all=0x0000;
/* GPBMUX: GPIO_B function 0=IOP,1=FUN I(0)/O(1)
bit15 0: C6TRIP,PB15 ;IOP 1
bit14 0: C5TRIP,PB14 ;IOP 1
bit13 0: C4TRIP,PB13 ;IOP 1
bit12 0: TCLKINB,PB12 ;IOP 1
bit11 0: TDIRB,PB11 ;IOP 1
bit10 0: CAP6_QEPI2,PB10 ;IOP 1
bit9 0: CAP5_QEP4,PB9 ;IOP 1
bit8 0: CAP4_QEP3,PB8 ;IOP 1
bit7 0: T4PWM_T4CMP,PB7 ;IOP 1
bit6 0: T3PWM_T3CMP,PB6 ;IOP 1
bit5 0: PWM12,PB5 ;IOP 1
bit4 0: PWM11,PB4 ;IOP 1
bit3 0: PWM10,PB3 ;IOP 1
bit2 0: PWM9,PB2 ;IOP 1
bit1 0: PWM8,PB1 ;IOP 1
bit0 0: PWM7,PB0 ;IOP 1
*/
GpioMuxRegs.GPBDIR.all=0xffff; // PB DIR: 1=output,0=input
GpioMuxRegs.GPAQUAL.all=0x0000; // PB Input Qualification:
/* 0x00=No, 0x01=SYSCLK/2, 0x02=SYSCLK/4, 0xff=SYSCLK/510 */
GpioMuxRegs.GPDMUX.all=0x0021;
/* GPDMUX: GPIO_D function 0=IOP,1=FUN
bit6 0: T4CTRIP,PD6 ;IOP
bit5 1: T3CTRIP_PDPINTB,PD5 ;FUN
bit4 0: res
bit3 0: res
bit2 0: res
bit1 0: T2CTRIP,PD1 ;IOP
bit0 1: T1CTRIP_PDPINTA,PD0 ;FUN
*/
GpioMuxRegs.GPDDIR.all=0x5e; // DIR: 1=output,0=input
// 6(PD6),5(PDPINTB),4,3,2,1(PD1),0(PDPINTA)
// 1 0 1 1 1 1 0 = 0x5e
GpioMuxRegs.GPDQUAL.all=0x0000; // PD Input Qualification:
/* 0x00=No, 0x01=SYSCLK/2, 0x02=SYSCLK/4, 0xff=SYSCLK/510 */
GpioMuxRegs.GPEMUX.all=0x0003;
/* GPEMUX: GPIO_E function 0=IOP,1=FUN I(0)/O(1)
bit2 0: XNMI_XINT13,PE2 ;IOP 1
bit1 1: XINT2_ADCSOC,PE1 ;FUN 0
bit0 1: XINT1_XBIO,PE0 ;FUN 0
*/
GpioMuxRegs.GPEDIR.all=0x04; // DIR: 1=output,0=input
GpioMuxRegs.GPEQUAL.all=0x0002; // PE Input Qualification:
/* 0x00=No, 0x01=SYSCLK/2, 0x02=SYSCLK/4, 0xff=SYSCLK/510 */
GpioMuxRegs.GPFMUX.all=0x00ff;
/* GPFMUX: GPIO_F function 0=IOP,1=FUN I(0)/O(1)
bit14 0: XF,PF14 ;IOP 1(CPU_LED)
bit13 0: MDR,PF13 ;IOP 1
bit12 0: MDX,PF12 ;IOP 1
bit11 0: MFSR,PF11 ;IOP 1
bit10 0: MFSX,PF10 ;IOP 1
bit9 0: MCLKR,PF9 ;IOP 1
bit8 0: MCLKX,PF8 ;IOP 1
bit7 1: CANRX,PF7 ;FUN 0
bit6 1: CANTX,PF6 ;FUN 1
bit5 1: SCIRXDA,PF5 ;FUN 0
bit4 1: SCITXDA,PF4 ;FUN 1
bit3 1: SPISTE,PF3 ;FUN 1
bit2 1: SPICLK,PF2 ;FUN 1
bit1 1: SPISOMI,PF1 ;FUN 1
bit0 1: SPISIMO,PF0 ;FUN 1
*/
GpioMuxRegs.GPFDIR.all=0xff5f; // DIR: 1=output,0=input
GpioMuxRegs.GPGMUX.all=0x0030;
/* GPGMUX: GPIO_G function 0=IOP,1=FUN
bit5 1: SCIRXDB,PG5 ;FUN
bit4 1: SCITXDB,PG4 ;FUN
*/
GpioMuxRegs.GPGDIR.all=0x10; // DIR: 1=output,0=input
// 5(RXDB),4(TXDB),3,2,1,0
// 0 1 0 0 0 0 = 0x10
EDIS;
}
void dac_out(WORD ch,WORD dat){
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG); // wait if TX_BUF_FULL
if(ch&1) SpiaRegs.SPITXBUF = (0x3000 | (dat & 0xfff))<<2; // DAC_B
else SpiaRegs.SPITXBUF = (0x2000 | (dat & 0xfff))<<2; // DAC_A
while(!SpiaRegs.SPISTS.bit.INT_FLAG);
LO_LOAD;
tmp = SpiaRegs.SPIRXBUF;
HI_LOAD;
}
interrupt void cpu_timer0_isr(void){
CpuTimer0.InterruptCount++;
T_LED;
t0_cnt++;
if(++idx >= ARRAY_LEN) idx = 0;
sv[idx] = sin(2*PI*idx/(ARRAY_LEN-1)) * 2047 + 2048;
cv[idx] = cos(2*PI*idx/(ARRAY_LEN-1)) * 2047 + 2048;
dac_out(0,ad0); // DAC0: VR data out
dac_out(1,sv[idx]); // DAC1: sin
if((t0_cnt % 10) == 0) key_check(); // 10 msec interval key check
// Acknowledge this interrupt to recieve more interrupts from group 1
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}
interrupt void xint1_isr(void){
xcnt1++;
// Acknowledge this interrupt to recieve more interrupts from group 1
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}
interrupt void xint2_isr(void){
xcnt2++;
// Acknowledge this interrupt to recieve more interrupts from group 1
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}
void key_check(void){
BYTE tmp_key_code;
BYTE key;
key = IN_KEY & KEY_MASK;
if(key == KEY_MASK){
prev_key_code = key_code = NOKEY_CODE;
return;
}
if((key & BIT0) == 0) tmp_key_code = SW1_CODE;
else if((key & BIT1) == 0) tmp_key_code = SW2_CODE;
else if((key & BIT2) == 0) tmp_key_code = SW3_CODE;
else if((key & BIT3) == 0) tmp_key_code = SW4_CODE;
else if((key & BIT4) == 0) tmp_key_code = SW5_CODE;
else if((key & BIT5) == 0) tmp_key_code = SW6_CODE;
else if((key & BIT6) == 0) tmp_key_code = SW7_CODE;
else if((key & BIT7) == 0) tmp_key_code = SW8_CODE;
if(tmp_key_code != (prev_key_code & CODE_MASK)){
prev_key_code = tmp_key_code;
key_code = KEY_PRESSED | tmp_key_code;
key_count = 0;
}
else{
if(key_count < 100) key_count++;
else key_code |= KEY_CONT;
}
}
void key_process(){
switch(key_code & CODE_MASK){
case SW1_CODE :
break;
case SW2_CODE :
break;
case SW3_CODE :
break;
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;
}
void main(void){
// Initialize System Control registers, PLL, WatchDog, Clocks to default state:
// This function is found in the DSP28_SysCtrl.c file.
InitSysCtrl();
// 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
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
// 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
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();
// 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("!!! DAC test !!!");
lcd_puts(msg); // another method
AD_START;
while(1){
mcnt++;
lcd_gotoxy(0,1);
lcd_hex4(mcnt);
ad0 = AdcRegs.ADCRESULT0>>4;
lcd_gotoxy(5,1);
lcd_decimal_word(ad0);
key = IN_KEY;
OUT_LED(key);
if(key_code & KEY_PRESSED) key_process();
if(key_code & KEY_CONT) cont_key_process();
delay_ms(100);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -