hello,everyone.Could you give me a hand!
i write a UART code use the dsp30f4013,but it is not work,i don't know what is the probelm.It waste me many time.Please give me some advice!Thanks a lot!
#include <p30f4013.h>
#define XTFREQ 7372800 //On-board Crystal frequency
#define PLLMODE 8 //On-chip PLL setting
#define FCY XTFREQ*PLLMODE/4 //Instruction Cycle Frequency
#define BAUDRATE 9600 //set baudrate
#define BRGVAL ((FCY/BAUDRATE)/16)-1 //((FCY/BAUDRATE)/16)-1=95,initial baudrate register value
//config bits
you refer to site
www.ldv.ir
_FOSC(CSW_FSCM_OFF & XT_PLL8);//use a external crystal ,the instruction cycle =1/(FOSC*PLL/4)
_FWDT(WDT_OFF);
_FBORPOR(PBOR_OFF & MCLR_EN);//disable power off pretect,enable MCLR
_FGS(CODE_PROT_OFF);//disable code pretect
//unsigned char DisplayData[] = {"dsPIC30F4013Demo"};
unsigned char *UARTCharPtr;
unsigned char DisplayData[] = {"dsP"};
void delay_ms(unsigned char);
void delay_us(unsigned int);
void WriteUART_to_RS232(void);
void InitUART1(void);
void __attribute__((__interrupt__)) _U1TXInterrupt(void);
int main(void)
{
ADPCFG=0xFFFF; //ensure the ports are digital for ICD2 debugging
InitUART1(); //initial the UART
while(1)
{
WriteUART_to_RS232(); //updata RS232 via UART
delay_ms(250);
}
return 0; //code nerver reaches here!
}
void InitUART1(void)
{
unsigned int U1MODEvalue;
unsigned int U1STAvalue;
U1MODE=0x0000; //clear UART1 MODE registers
U1STA=0x0000; //clear UART1 STATUS and CONTROL register
U1MODEbits.ALTIO=0; //disable U1ATX and U1ARX ,the U1TX and U1RX pins be used to UART
U1MODEbits.UARTEN=1; //enable UART1 module
U1BRG=BRGVAL; //load UART1 Baud Rate Generator
IFS0bits.U1RXIF=0; //clear UART1 receiver interrupt flag
IFS0bits.U1TXIF=0; //clear UART1 transmitter interrupt flag
IEC0bits.U1RXIE=0; //disable UART1 receiver ISR
IEC0bits.U1TXIE=1; //enable UART1 transmitter ISR
IPC2bits.U1TXIP=6;
IPC2bits.U1RXIP=0;
U1STAbits.UTXISEL=1; //an interrupt is gernerateed when a character is transferred to the Transmit Shift Register and the transmit buffer is empty
U1STAbits.UTXEN=1; //enable UART1 transmitter
UARTCharPtr=&DisplayData[0];//initial UARTCharPtr to point to the first character in the Display buffer
}
//WriteUART_to_RS232() triggers interrupt-driven UART communication by writing the first character in the Display buffer to the UART Transmit register
void WriteUART_to_RS232(void)
{
UARTCharPtr=&DisplayData[0];//re-initial UART display buffer pointer to point to the first character
U1TXREG=*UARTCharPtr++;//Load the UART transmit register with the first character
}
//_U1TXInterrupt() is UART1 TX Interrupt Service Routine
//The routine must have global scope in order to be an ISR.
//The ISR name is the same as provided for the module in the device linker script.
//The UART1 ISR loads the UART1 4-deep FIFO buffers with the next 4 charracters in the Display buffer unless it encounters a null character.
void __attribute__((__interrupt__)) _U1TXInterrupt(void)
{
int i=0;
while((*UARTCharPtr!='\0')&&(i<4))
{
U1TXREG=(*UARTCharPtr++);
i++;
}
IFS0bits.U1TXIF=0; //clear the UART1 TX interrupt flag
}
/* delay millisecond time */
void delay_us(unsigned int m)
{
for(;m>0;m--)
{//delay 1us
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
}
}
/* delay microsecond time */
void delay_ms(unsigned char n)
{
for(;n>0;n--)
{
delay_us(982);
}
}