主函数:
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
#include "systimer.h"
#define LED1 GpioDataRegs.GPBDAT.bit.GPIO60
#define LED2 GpioDataRegs.GPBDAT.bit.GPIO61
#define Trig GpioDataRegs.GPADAT.bit.GPIO12
#define Echo GpioDataRegs.GPADAT.bit.GPIO13
interrupt void xint2_isr(void);
void scic_echoback_init(void);
void scic_fifo_init(void);
void scic_xmit(int a);
void scic_msg(char *msg);
void InitLED(void);
void Init_ultrasonic(void);
Uint32 begin_time=0;
Uint32 over_time=0;
Uint32 period=0;
float displacement=0.0;
Uint32 H_displacement=0;
Uint32 L_displacement=0;
Uint16 flag=0;
void main(void)
{
char *msg;
// 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(); // Skipped for this example
InitScicGpio();
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize the 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;
InitPieVectTable();
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.XINT2 = &xint2_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
// Step 4. Initialize the Device Peripheral. This function can be
// found in DSP2833x_CpuTimers.c
InitCpuTimers(); // For this example, only initialize the Cpu Timers
InitSystimer(&CpuTimer1); //初始化CPUTimer1
// To ensure precise timing, use write-only instructions to write to the entire register. Therefore, if any
// of the configuration bits are changed in ConfigCpuTimer and InitCpuTimers (in DSP2833x_CpuTimers.h), the
// below settings must also be updated.
StartCpuTimer1();
// Step 5. User specific code, enable interrupts:
PieCtrlRegs.PIECTRL.bit.ENPIE = 1; // Enable the PIE block
PieCtrlRegs.PIEIER1.bit.INTx5 = 1; // Enable PIE Gropu 1 INT2
IER |= M_INT1;
// Enable global Interrupts and higher priority real-time debug events:
EINT; // Enable Global interrupt INTM
// ERTM; // Enable Global realtime interrupt DBGM
scic_fifo_init(); // Initialize the SCI FIFO
scic_echoback_init(); // Initalize SCI for echoback
// msg = "\r\n\n\nHello\0";
// scic_msg(msg);
InitLED();
Init_ultrasonic();
LED1=0;
LED2=1;
Trig=0;
// Step 6. IDLE loop. Just sit and loop forever (optional):
for(;;)
{
Trig=1;
DELAY_US(10);
Trig=0;
/* while(1)
{
if(Echo==1)
{
//begin_time=CpuTimer1Regs.PRD.all;
//StartCpuTimer1();
begin_time=CpuTimer1Regs.TIM.all;
flag=1;
break;
}
if(flag==1&&Echo==0)
{
//StopCpuTimer1();
over_time=CpuTimer1Regs.TIM.all;
period=begin_time-over_time;
displacement=340*period*100/1000/2;
H_displacement=(displacement>>16);
L_displacement=displacement;
scic_xmit(H_displacement>>8);
scic_xmit(H_displacement);
scic_xmit(L_displacement>>8);
scic_xmit(L_displacement);
flag=0;
}
break;
}*/
//period=begin_time-over_time;
displacement=170*period*0.00667*1000;
H_displacement=((int)displacement>>16);
L_displacement=(int)displacement;
scic_xmit(H_displacement>>8);
scic_xmit(H_displacement);
scic_xmit(L_displacement>>8);
scic_xmit(L_displacement);
}
}
void scic_echoback_init()
{
// Note: Clocks were turned on to the SCIA peripheral
// in the InitSysCtrl() function
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
ScicRegs.SCICTL2.all =0x0003;
ScicRegs.SCICTL2.bit.TXINTENA = 1;
ScicRegs.SCICTL2.bit.RXBKINTENA =1;
#if (CPU_FRQ_150MHZ)
ScicRegs.SCIHBAUD =0x0001; // 9600 baud @LSPCLK = 37.5MHz.
ScicRegs.SCILBAUD =0x00E7;
#endif
#if (CPU_FRQ_100MHZ)
ScicRegs.SCIHBAUD =0x0001; // 9600 baud @LSPCLK = 20MHz.
ScicRegs.SCILBAUD =0x0044;
#endif
ScicRegs.SCICTL1.all =0x0023; // Relinquish SCI from Reset
}
// Transmit a character from the SCI
void scic_xmit(int a)
{
while (ScicRegs.SCIFFTX.bit.TXFFST != 0) {}
ScicRegs.SCITXBUF=a;
}
void scic_msg(char * msg)
{
int i;
i = 0;
while(msg != '\0')
{
scic_xmit(msg);
i++;
}
}
// Initalize the SCI FIFO
void scic_fifo_init()
{
ScicRegs.SCIFFTX.all=0xE040;
ScicRegs.SCIFFRX.all=0x204f;
ScicRegs.SCIFFCT.all=0x0;
}
void InitLED(void)
{
EALLOW;
GpioCtrlRegs.GPBMUX2.bit.GPIO60 = 0; // GPIO60 = GPIO60
GpioCtrlRegs.GPBDIR.bit.GPIO60 = 1;
GpioCtrlRegs.GPBMUX2.bit.GPIO61 = 0; // GPIO61 = GPIO61
GpioCtrlRegs.GPBDIR.bit.GPIO61 = 1;
EDIS;
}
void Init_ultrasonic(void)
{
EALLOW;
GpioCtrlRegs.GPAMUX1.bit.GPIO12 = 0; // GPIO12 = GPIO12
GpioCtrlRegs.GPADIR.bit.GPIO12 = 1; // Output
GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 0; // GPIO13 = GPIO13
GpioCtrlRegs.GPADIR.bit.GPIO13 = 0; // Input
GpioCtrlRegs.GPAQSEL1.bit.GPIO13 = 0; // XINT2 Synch to SYSCLKOUT only
EDIS;
EALLOW;
GpioIntRegs.GPIOXINT2SEL.bit.GPIOSEL = 13; // XINT2 is GPIO13
EDIS;
EALLOW;
XIntruptRegs.XINT2CR.bit.POLARITY = 3; //Rising & Falling edge interrupt
XIntruptRegs.XINT2CR.bit.ENABLE =1; //Enable Xint2
EDIS;
}
interrupt void xint2_isr(void)
{
LED1=~LED1;
if(flag==0)
{
//StartCpuTimer1();
begin_time=CpuTimer1Regs.TIM.all;
flag=1;
LED2=~LED2;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}
else
{
//StopCpuTimer1();
over_time=CpuTimer1Regs.TIM.all;
period=begin_time-over_time;
//displacement=340*period*100/1000/2;
flag=0;
LED2=~LED2;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}
} |