/***********************************
功能:uart测试程序
MCU:dsPIC30F4011
TOOLS:MPLAB X IDE V3.15 + XC16 V1.25
震荡器:外部4M晶振
芯艺设计室 http://www.chipart.cn
2015-12-03
***********************************/
#include "xc.h"
#include <stdint.h>
#include <stdio.h>
#define XTFREQ 4000000 // xtal = 4Mhz;
#define PLLMODE 16 // PLLx16
#define FCY XTFREQ*PLLMODE/4 // Instruction Cycle Frequency
#include <libpic30.h>
#define UART_BAUD 9600
#define LED_PORT_INIT TRISBbits.TRISB6=0
#define LED_ON LATBbits.LATB6=1
#define LED_OFF LATBbits.LATB6=0
#define LED_FLASH LATBbits.LATB6^=1
_FOSC(CSW_FSCM_OFF & XT_PLL16);
_FWDT(WDT_OFF);
#define DelayMs __delay_ms
#define APP 1 //1:POLL 2:INTERRUPT 3:PRINTF
#if APP==1
void Uart1Init(void)
{
U1MODE=0x0400; //格式:N,8,1,无校验 使用备用引脚RC13,RC14
U1BRG = ((FCY/16)/UART_BAUD)-1; //波特率设置
U1STAbits.UTXISEL = 1; //发送过程中发送缓冲器空时产生中断
U1STAbits.URXISEL = 0; //接收一个字节后中断
//IEC0bits.U1RXIE = 1; //接收中断允许
//IEC0bits.U1TXIE = 1; //发送中断允许
U1MODEbits.UARTEN = 1; //发送允许
U1STAbits.UTXEN = 1; //使能
}
uint8_t uart_getc(void)
{
uint8_t tmp;
while(IFS0bits.U1RXIF == 0); //????
tmp=U1RXREG;
IFS0bits.U1RXIF = 0;
return tmp;
}
void uart_putc(uint8_t c)
{
U1TXREG=c; //??
while(IFS0bits.U1TXIF ==0);//??????
IFS0bits.U1TXIF = 0;
}
int main( void )
{
LED_PORT_INIT;
LED_ON;
Uart1Init();
while(1)
{
uart_putc(uart_getc());
}
return 0;
}
#endif
#if APP==2
uint8_t g_Recv;
volatile uint8_t g_RecvFlag;
void Uart1Init(void)
{
U1MODE=0x0400; //格式:N,8,1,无校验 使用备用引脚RC13,RC14
U1BRG = ((FCY/16)/UART_BAUD)-1; //波特率设置
U1STAbits.UTXISEL = 1; //发送过程中发送缓冲器空时产生中断
U1STAbits.URXISEL = 0; //接收一个字节后中断
IEC0bits.U1RXIE = 1; //接收中断允许
IEC0bits.U1TXIE = 1; //发送中断允许
U1MODEbits.UARTEN = 1; //发送允许
U1STAbits.UTXEN = 1; //使能
}
void __attribute__((__interrupt__,no_auto_psv))_U1RXInterrupt(void)
{
g_Recv=U1RXREG;
g_RecvFlag=1;
IFS0bits.U1RXIF = 0;
}
void __attribute__((__interrupt__,no_auto_psv))_U1TXInterrupt(void)
{
IFS0bits.U1TXIF = 0;
}
int main( void )
{
LED_PORT_INIT;
LED_ON;
Uart1Init();
while(1)
{
if(g_RecvFlag)
{
U1TXREG=g_Recv;
g_RecvFlag=0;
LED_FLASH;
}
}
return 0;
}
#endif
#if APP==3
void Uart1Init(void)
{
U1MODEbits.STSEL = 0; // 1????
U1MODEbits.PDSEL = 0; // N , 8, 1
U1MODEbits.ABAUD = 0; // ???????
U1MODEbits.ALTIO = 1; // ????????
U1BRG = ((FCY/16)/UART_BAUD)-1; //?????
U1STAbits.UTXISEL = 1; // ?????????
U1STAbits.URXISEL1 = 0; // ?????????
U1STAbits.URXISEL0 = 0;
//IEC0bits.U1RXIE = 1; // ??????
//IEC0bits.U1TXIE = 1; // ??????
U1MODEbits.UARTEN = 1; // ????
U1STAbits.UTXEN = 1; // ????1
}
int main( void )
{
uint16_t i=0;
LED_PORT_INIT;
Uart1Init();
while(1)
{
DelayMs(1000);
LED_FLASH;
printf("%d ",i++);
}
return 0;
}
#endif
|
|