|
最近写了个串口通信小程序,在做软件仿真时,数据能够存入发送寄存器,但连接芯片做在线仿真时,数据就不能够存入发送寄存器了.想请教一下大家.
一下是程序部分:
#include "p30F5011.h" //调用头文件
/*-------------------------------------------------------------------------
全局变量定义
-------------------------------------------------------------------------*/
unsigned char rxBuff[8],inputReg[80];
unsigned char txBuff[8]= {9,0,3,4,5,6,7,8};
unsigned char RxTxPTR=0,overflow;
/*--------------------------------------------------------------------------
全局变量定义
---------------------------------------------------------------------------*/
#define LED1 _LATD5
#define LED2 _LATD4
#define LED3 _LATD3
#define LED4 _LATD2
#define LED5 _LATD1
/*--------------------------------------------------------------------
配置位定义
--------------------------------------------------------------------*/
_FOSC(CSW_FSCM_OFF & XT_PLL4); //disable Clock switching and clock monitor
_FWDT(WDT_OFF & WDTPSA_64 & WDTPSB_10); //open WDT,
_FBORPOR(MCLR_EN & PBOR_ON); //Power-on Reset Value=128mS
_FBS(CODE_PROT_OFF);
_FSS(CODE_PROT_OFF);
_FGS(CODE_PROT_OFF);
/*--------------------------------------------------------------------
上电初始化
--------------------------------------------------------------------*/
void initialize (void)
{
IFS0 = 0; //中断标志清零
IEC0 = 0; //中断允许控制寄存器
TRISB = 0XFE3F; //RB0-RB13为输入,RB14-RB15为输出
TRISC = 0X0006;
TRISD = 0X0C00;
TRISF = 0;
TRISG = 0X83C0;
PORTC=0;
PORTD=0;
PORTF=0;
PORTG=0;
initUART1(); //485接口
}
/*------------------------------------------------------------------------------
串口1初始化
------------------------------------------------------------------------------*/
void initUART1(void)
{
IPC2bits.U1RXIP = 3; /* set priority */
IPC2bits.U1TXIP = 3;
U1MODE = 0; //8位
U1MODEbits.UARTEN=1; //打开UART1
IEC0bits.U1RXIE =1; /* enable interrupt */
IEC0bits.U1TXIE =1; /* disable interrupt */
U1STA=0;
U1STAbits.UTXISEL=0; //发送缓冲器空产生中断
U1STAbits.URXISEL=0B00; //接收缓冲器接收到1个字节产生中断
U1STAbits.UTXEN=1; //发送器
U1BRG=64; //波特率为9600
IFS0bits.U1RXIF = 0; /* clear IF flags */
IFS0bits.U1TXIF = 0;
}
/*--------------------------------------------------------------------------
USART发送中断
--------------------------------------------------------------------------*/
void __attribute__((interrupt, no_auto_psv)) _U1TXInterrupt(void)
{
LED4=~LED4;
IFS0bits.U1TXIF = 0;
U1TXREG=txBuff[RxTxPTR];
RxTxPTR++;
LED1==1;
LED2=1;
}
/*--------------------------------------------------------------------------
USART接收中断
--------------------------------------------------------------------------*/
void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void)
{
unsigned char inregaddr,i;
unsigned int rxCRC;
IFS0bits.U1RXIF = 0;
LED1=1; //通讯指示灯亮
if(overflow) RxTxPTR=0;
overflow=0;
TMR4=0;
while (U1STAbits.URXDA==1) //读接收的数据直到缓冲器为空
{
rxBuff[RxTxPTR]=U1RXREG;
RxTxPTR++;
}
}
int main(void)
{
initialize();
}
|
|