Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

dspic30f4013 UART ,Pls give me a hand!

Status
Not open for further replies.

kenny_zhou

Junior Member level 2
Joined
Jan 12, 2007
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
china
Activity points
1,401
fcy pll

hello everyone !please give me a hand!
this is a UART code,when PC sned character to dsPIC30F4013 it is ok,but when the dsPIC30F4013 couldn't send the character"0x41" to the PC,what should i do?Thanks a lot!

#include "p30f4013.h"
#define FCY 14745600
_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
void ini232(void);
void __attribute__((__interrupt__)) _U1RXInterrupt(void); /*Declare 232 interrupt ISRs*/
unsigned char rxdata;
int main (void)
{
unsigned char i;
ini232(); /* Call function to initialize the 232 Interrupts */
for(i=32;i!=0;i--); //wait a moment,
while(1)
{
U1TXREG=0x41;
while(U1STAbits.UTXBF);
IFS0bits.U1TXIF=0;
}
}
void ini232(void)
{

TRISFbits.TRISF2 =1;
TRISFbits.TRISF3 =0;
U1BRG =95; //9600 baud rate
IPC2bits.U1RXIP=7;
IEC0bits.U1TXIE=0;
IEC0bits.U1RXIE=1;
U1MODE =0x8020;

U1STAbits.UTXEN =1;

}
void __attribute__((__interrupt__)) _U1RXInterrupt(void)
{
IFS0bits.U1RXIF = 0;
rxdata=(char)U1RXREG;

}
 

dspic30f4013 uart

kenny_zhou said:
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);
}
}
 

    kenny_zhou

    Points: 2
    Helpful Answer Positive Rating
xtfreq

dear mehdi_an:
Thank you very much!
Now i have find the error , the code is right.the bug is in my PCB.
I confuse the line2 and line4 of the DB9.So MCU can receive data from PC use the lien 3,but could sent data.the data be transmite in the line4,so the PC cann't capture it .
hehe !what a dumpish mistake!
Now you can "cancel" the "help me" flag!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top