任何人都可以给我发送用于在UART中发送字符的配置和代码吗?
我正在使用Pic32mz EF入门套件和MPLAB X-IDE v5.15。
我尝试了一个代码,但是它并没有给出任何UART或腻子剂。
// PIC32MZ2048EFM144 Configuration Bit Settings
// 'C' source line config statements
// DEVCFG3
#pragma config USERID = 0xFFFF // Enter Hexadecimal value (Enter Hexadecimal value)
#pragma config FMIIEN = OFF // Ethernet RMII/MII Enable (RMII Enabled)
#pragma config FETHIO = OFF // Ethernet I/O Pin Select (Alternate Ethernet I/O)
#pragma config PGL1WAY = OFF // Permission Group Lock One Way Configuration (Allow multiple reconfigurations)
#pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow multiple reconfigurations)
#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow multiple reconfigurations)
#pragma config FUSBIDIO = OFF // USB USBID Selection (Controlled by Port Function)
// DEVCFG2
#pragma config FPLLIDIV = DIV_3 // System PLL Input Divider (3x Divider)
#pragma config FPLLRNG = RANGE_5_10_MHZ // System PLL Input Range (5-10 MHz Input)
#pragma config FPLLICLK = PLL_POSC // System PLL Input Clock Selection (POSC is input to the System PLL)
#pragma config FPLLMULT = MUL_100 // System PLL Multiplier (PLL Multiply by 100)
#pragma config FPLLODIV = DIV_2 // System PLL Output Clock Divider (2x Divider)
#pragma config UPLLFSEL = FREQ_12MHZ // USB PLL Input Frequency Selection (USB PLL input is 12 MHz)
// DEVCFG1
#pragma config FNOSC = SPLL // Oscillator Selection Bits (System PLL)
#pragma config DMTINTV = WIN_127_128 // DMT Count Window Interval (Window/Interval value is 127/128 counter value)
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Disable SOSC)
#pragma config IESO = OFF // Internal/External Switch Over (Disabled)
#pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled)
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disabled, FSCM Disabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
#pragma config WDTSPGM = STOP // Watchdog Timer Stop During Flash Programming (WDT stops during Flash programming)
#pragma config WINDIS = NORMAL // Watchdog Timer Window Mode (Watchdog Timer is in non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled)
#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window size is 25%)
#pragma config DMTCNT = DMT31 // Deadman Timer Count Selection (2^31 (2147483648))
#pragma config FDMTEN = OFF // Deadman Timer Enable (Deadman Timer is disabled)
// DEVCFG0
#pragma config DEBUG = OFF // Background Debugger Enable (Debugger is disabled)
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)
#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2)
#pragma config TRCEN = OFF // Trace Enable (Trace features in the CPU are disabled)
#pragma config BOOTISA = MIPS32 // Boot ISA Selection (Boot code and Exception code is MIPS32)
#pragma config FECCCON = OFF_UNLOCKED // Dynamic Flash ECC Configuration (ECC and Dynamic ECC are disabled (ECCCON bits are writable))
#pragma config FSLEEP = OFF // Flash Sleep Mode (Flash is powered down when the device is in Sleep mode)
#pragma config DBGPER = PG_ALL // Debug Mode CPU Access Permission (Allow CPU access to all permission regions)
#pragma config SMCLR = MCLR_NORM // Soft Master Clear Enable bit (MCLR pin generates a normal system Reset)
#pragma config SOSCGAIN = GAIN_2X // Secondary Oscillator Gain Control bits (2x gain setting)
#pragma config SOSCBOOST = ON // Secondary Oscillator Boost Kick Start Enable bit (Boost the kick start of the oscillator)
#pragma config POSCGAIN = GAIN_2X // Primary Oscillator Gain Control bits (2x gain setting)
#pragma config POSCBOOST = ON // Primary Oscillator Boost Kick Start Enable bit (Boost the kick start of the oscillator)
#pragma config EJTAGBEN = NORMAL // EJTAG Boot (Normal EJTAG functionality)
// DEVCP0
#pragma config CP = OFF // Code Protect (Protection Disabled)
// SEQ3
#pragma config TSEQ = 0xFFFF
#pragma config CSEQ = 0xFFFF
// DEVADC0
#pragma config ADCFG = 0xFFFFFFFF
#include <xc.h>
#include <proc/p32mz2048efm144.h>
void main()
{
U2MODEbits.STSEL = 0;
U2MODEbits.PDSEL = 0b00;
IEC4bits.U2TXIE = 1;
IPC36bits.U2TXIP = 0b111;
IPC36bits.U2TXIS = 0b11;
U2STAbits.UTXISEL = 0b10;
U2STAbits.UTXEN = 1;
U2MODEbits.UEN = 0b00;
U2MODEbits.ON = 1;
U2MODEbits.BRGH = 0;
U2BRG = 129;
int i;
for(i=0;i<100;i++)
{
U2STAbits.UTXEN = 1; // Make sure transmitter is enabled
while(U2STAbits.UTXBF); // Wait while buffer is full
U2TXREG =(char) i;
}
}
我认为我的配置位和波特率有问题。您能帮我设置正确的波特率和配置位吗?
感谢与问候,
维布。
答案 0 :(得分:0)
我的一个我的项目有一个问题,我还没有弄清楚(我得到了乱码的数据),但这就是我在您的项目中看到的。您需要将TX和RX引脚的TRISx位设置为OUT和IN,此外,还需要通过U2RXR和RPDx寄存器将那些UART功能映射到物理引脚。您还需要在相应的ANSELx寄存器中清除这两个引脚的位以数字化。
答案 1 :(得分:0)
您可以使用以下公式计算波特率:
U2BRG = (unsigned short int)(((float)PERIPHERAL_CLOCK / ((float)BAUD_RATE * (float)BAUD_CLOCK)) - (float)0.5);
如果U2MODEbits.BRGH设置为高波特率,则您的BAUD_CLOCK应该为4,否则为16。
对于通过轮询TRMT标志编写字符的情况:
while(!U2STAbits.TRMT) {}
U2TXREG = (char) i;
您应该只设置一次以下位,因为这是一个初始化过程,所以每次发送字符时都无需这样做。
U2STAbits.UTXEN = 1; // Make sure transmitter is enabled
而且您也没有main while()循环,并且每次CPU执行完for循环时,您的程序都可能崩溃。