我用PICkit 2 DEBUG,发现U2RXREG始终是0.但是用示波器观察电脑发送的数据已经到接收脚,PIN18脚。
然后就是进不了中断,程序等在while(!DataAvailable);这里了。
请帮忙看看是哪里出了问题,谢谢!
#include "p24fj64ga002.h"
//#include<uart.h>
//此程序适用I2C1 ,SDA1,SCL1 (备用口)
//_CONFIG1(ICS_PGx1&FWDTEN_OFF)
_CONFIG2(FNOSC_FRC&FCKSM_CSDCMD&I2C1SEL_SEC)//fast RC oscillator;clock switching and Fail-Safe clock monitor are disable
void cUART2(void);
void UART2INT(void);
void configUART2(void);
unsigned char DataAvailable;
unsigned char Txdata[]= "hello";
unsigned char Rxdata[10];
void __attribute__ ((interrupt,no_auto_psv)) _U2TXInterrupt(void)
{
static unsigned char i=0;
IFS1bits.U2TXIF = 0;//clear flag
if(Txdata[i]!='\0')
{
while(U2STAbits.UTXBF);
U2TXREG = Txdata[i++];
}
else
{
while(!U2STAbits.TRMT);
//DisableIntU2TX;
IEC1bits.U2TXIE = 0;
}
}
void __attribute__ ((interrupt,no_auto_psv)) _U2RXInterrupt(void)
{
static unsigned char j=0;//why set to static?
IFS1bits.U2RXIF = 0;
if(U2STAbits.OERR)
{U2STAbits.OERR = 0;}
while(!U2STAbits.URXDA);
Rxdata[j++] = U2RXREG;
DataAvailable=1;
}
void uart(void)
{
asm volatile (
"MOV #OSCCON, W1 \n"
"MOV #0X46, W2 \n"
"MOV #0X57,W3 \n"
"MOV.b W2, [W1] \n"
"MOV.B W3, [W1] \n"
"BCLR OSCCON,#6"
);
//Assign U2RX to pin RP9
RPINR19bits.U2RXR = 9;
//assign U2TX to pin RP4
RPOR2bits.RP4R = 5;
asm volatile (
"MOV #OSCCON, W1 \n"
"MOV #0X46, W2 \n"
"MOV #0X57,W3 \n"
"MOV.b W2, [W1] \n"
"MOV.B W3, [W1] \n"
"BSET OSCCON,#6"
);
//Close UART2
cUART2();
/*Enable UART intruupts*/
UART2INT();
/*UART initialized to 9600 baudrate @BRGH=0, 8bit,no parity and 1 stopbit,interrupt is set on RST transfer,making the receive full*/
configUART2();
/* Data Transmit and receiving the data using interrupts*/
//IFS1bits.U2TXIF = 1;//what is this used for?
while(!DataAvailable);/*wait till data is received*/
DataAvailable=0;
cUART2();
}
void main(void)
{
OSCCONbits.COSC=0;
CLKDIV=0;
TRISBbits.TRISB15= 0; //P1B5 is output
LATBbits.LATB15 = 0;
uart();
if(Rxdata[0]== 1)
LATBbits.LATB15 = 1;
else
LATBbits.LATB15 = 0;
// while(1)
//{;}
}
void cUART2(void)
{
U2MODEbits.UARTEN = 0;
IEC1bits.U2TXIE = 0;
IEC1bits.U2RXIE = 0;
IFS1bits.U2TXIF = 0;
IFS1bits.U2RXIF = 0;
}
void UART2INT(void)
{
IEC1bits.U2TXIE = 1;
IEC1bits.U2RXIE = 1;
IPC7bits.U2TXIP = 1;
IPC7bits.U2RXIP = 2;
}
void configUART2(void)
{
U2MODEbits.UARTEN = 1;
//U2STAbits.UTXEN = 1;
U2STAbits.URXISEL = 0;
U2BRG = 25;
}
|