//void InitUART1(void); // 申明UART1初始化程序 void Configure_UART (void) {
asm volatile ( "MOV #OSCCON,w1
" "MOV #0x46,w2
" "MOV #0x57,w3
" "MOV.b w2,[w1]
" "MOV.b w3,[w1]
" "BCLR OSCCON,#6"); // configure input funcitions // assign u1rx to pin rp2 RPINR18bits.U1RXR = 2; // assign u1tx to pin rp3 RPOR1bits.RP3R = 3;
asm volatile ( "MOV #OSCCON,w1
" "MOV #0x46,w2
" "MOV #0x57,w3
" "MOV.b w2,[w1]
" "MOV.b w3,[w1]
" "BSET OSCCON, #6" );
AD1PCFG = 0XFFFF; // 模拟通道全部配置为I/O通道 TRISBbits.TRISB2 = INPUT; // UART - U1RX TRISBbits.TRISB3 = OUTPUT; // UART - U1TX
}
void Uart_Init() { U1MODE=0x8800; U1STA=0x8000; U1BRG=51; //19200 IFS0bits.U1TXIF=0; IFS0bits.U1RXIF=0; IEC0bits.U1TXIE=1; //UART1 发送器中断允许位 IEC0bits.U1RXIE=1; //UART1 接收器中断允许位 IPC2bits.U1RXIP=2; //中断优先级高于定时器优先级 IPC3bits.U1TXIP=2; }
void Timer1Init()
void delay() { unsigned int k=0; while(k<10000) k++; }
/********************************* //__attribute__((__interrupt__))_U1RXInterrupt() //串口1接收中断服务子程序 **********************************/ void __attribute__((__interrupt__))_U1RXInterrupt() { IFS0bits.U1RXIF=0; if(U1STAbits.OERR) { U1STAbits.OERR=0; } else { if(U1_receive_count<700) { U1_receive[U1_receive_count]=U1RXREG; U1_receive_count++; } else { U1_receive_count=0; } } } /********************************** //__attribute__((__interrupt__))_U1TXInterrupt() //发送中断服务子程序 *********************************/
void __attribute__((__interrupt__))_U1TXInterrupt() { IFS0bits.U1TXIF=0; while(U1_send_count<=6)// { delay(); U1TXREG =U1_receive[U1_send_count] ;// while(!U1STAbits.TRMT); U1_send_count++; } if(U1_send_count>6)//send_count { U1STAbits.UTXEN=0; U1_send_count=0; return; }
}
int main(void) { Configure_UART(); // 设置RP2,RP3为UART1通讯口 Uart_Init(); TRISAbits.TRISA0 = 0; TRISBbits.TRISB12 = 0; TRISBbits.TRISB13 = 0; LATAbits.LATA0 = 1; LATBbits.LATB12 = 1; LATBbits.LATB13 = 1; while(1) { if((U1_receive_count%2) ==0) { LATBbits.LATB13 = !LATBbits.LATB13 ; // LED D3 }
LATAbits.LATA0 = !LATAbits.LATA0 ; // LED D1 delay(); delay(); U1STAbits.UTXEN = 1; delay(); } }接收可以正常 但是发送引脚没有输出! |