打印

我用PICkit 2 DEBUG,发现U2RXREG已经到接收脚

[复制链接]
2388|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
z372846985|  楼主 | 2012-2-26 15:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 yewuyi 于 2012-2-27 08:48 编辑

#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!='\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;
}
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

20

帖子

0

粉丝