感谢您的响应和提醒,代码配置器生成什么不应该被认为是绝对正确的!U1BRG值无疑是一个问题。但是,即使我玩的价值,我不能让UART工作一致。例如,从先前的帖子中修正值不会给出工作结果。但是,如果我将主振荡器设置为4 MHz,并且设置BUG禁用的U1BRG= 0x18,则得到Prtff()以9600的波特率成功地工作。U1BRG是我理解的两倍,它应该是从数据表中计算出来的。我想弄清楚到底是怎么回事,但我还没到。我的振荡器初始化和UART初始化的代码都是为了验证振荡器的配置是否正确。有一件事我很难理解:主振荡器频率实际上是如何设置在寄存器上的?从参考手册中,我了解它是如何设置为XT的,它给出了一个4-10 MHz的范围,但我很难找出如何实际设置为4兆赫从那里(没有启用PLL)。即,位场或寄存器实际指定4 MHz对10 MHz或任何其他频率。4 MHz是我在代码配置器中选择的值。我真的很感激任何人提供的建议。谢谢!
以上来自于百度翻译
以下为原文
Thanks for your response and the reminder that what the code configurator generates should not be taken as definitely correct!
The U1BRG value is definitely an issue. But, even when I play around with the value I can't get the UART to work consistently. For example, correcting the value from the previous post does not give a working result. But, if I set the primary oscillator to 4 MHz, and set U1BRG = 0x18 with BRG disabled I get printf() to work successfully at a baud rate of 9600. U1BRG is double what I understand it should be in this case as calculated from the datasheet. I'm trying to figure out what's going on to cause this, but I haven't been able to as of yet. My code for oscillator initialization and UART initialization are below
// FOSCSEL
#pragma config FNOSC = PRI // Oscillator Source Selection->Primary Oscillator (XT, HS, EC)
#pragma config IESO = ON // Two-speed Oscillator Start-up Enable bit->Start up device with FRC, then switch to user-selected oscillator source
// FOSC
#pragma config POSCMD = XT // Primary Oscillator Mode Select bits->XT Crystal Oscillator Mode
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit->OSC2 is clock output
#pragma config IOL1WAY = ON // Peripheral pin select configuration->Allow only one reconfiguration
#pragma config FCKSM = CSDCMD // Clock Switching Mode bits->Both Clock switching and Fail-safe Clock Monitor are disabled
#pragma config PLLKEN = ON // PLL Lock Enable Bit->Clock switch to PLL source will wait until the PLL lock signal is valid
void OSCILLATOR_Initialize(void)
{
// CF no clock failure; NOSC PRI; CLKLOCK unlocked; OSWEN Switch is Complete;
__builtin_write_OSCCONL((uint8_t) (0x200 & 0x00FF));
// FRCDIV FRC/2; PLLPRE 5; DOZE 1:8; PLLPOST 1:8; DOZEN disabled; ROI disabled;
CLKDIV = 0x31C3;
// TUN Center frequency;
OSCTUN = 0x0;
// PLLDIV 148;
PLLFBD = 0x94;
// ENAPLL disabled; APSTSCLR 1:2; FRCSEL ASRCSEL determines input clock source; SELACLK Auxiliary Oscillators; ASRCSEL No clock input;
ACLKCON = 0x2600;
// LFSR 0;
LFSR = 0x0;
}
void UART1_Initialize(void)
{
/**
Set the UART1 module to the options selected in the user interface.
Make sure to set LAT bit corresponding to TxPin as high before UART initialization
*/
// STSEL 1; IREN disabled; PDSEL 8N; UARTEN enabled; RTSMD disabled; USIDL disabled; WAKE disabled; ABAUD disabled; LPBACK disabled; BRGH enabled; URXINV disabled; UEN TX_RX;
U1MODE = (0x8000 & ~(1<<15)); // disabling UARTEN bit
// UTXISEL0 TX_ONE_CHAR; UTXINV disabled; OERR NO_ERROR_cleared; URXISEL RX_ONE_CHAR; UTXBRK COMPLETED; UTXEN disabled; ADDEN disabled;
U1STA = 0x0;
// BaudRate = 9600; Frequency = 2000000 Hz; BRG 51;
U1BRG = 0x18;
U1MODEbits.UARTEN = 1; // enabling UARTEN bit
U1STAbits.UTXEN = 1;
}
I'm trying to verify that the oscillator is configured correctly. One thing I'm having trouble understanding: how is the primary oscillator frequency actually set in on of the registers? From the reference manual I understand how it is set to XT, which gives a range of 4-10 MHz, but I'm having trouble figuring out how is actually set to 4 MHz from there (without the PLL enabled). I.e., what bitfield or register actually specifies 4 MHz vs 10 MHz or any other frequency. 4 MHz is the value I selected in the code configurator. I would really appreciate any advice that anyone can offer. Thanks!
感谢您的响应和提醒,代码配置器生成什么不应该被认为是绝对正确的!U1BRG值无疑是一个问题。但是,即使我玩的价值,我不能让UART工作一致。例如,从先前的帖子中修正值不会给出工作结果。但是,如果我将主振荡器设置为4 MHz,并且设置BUG禁用的U1BRG= 0x18,则得到Prtff()以9600的波特率成功地工作。U1BRG是我理解的两倍,它应该是从数据表中计算出来的。我想弄清楚到底是怎么回事,但我还没到。我的振荡器初始化和UART初始化的代码都是为了验证振荡器的配置是否正确。有一件事我很难理解:主振荡器频率实际上是如何设置在寄存器上的?从参考手册中,我了解它是如何设置为XT的,它给出了一个4-10 MHz的范围,但我很难找出如何实际设置为4兆赫从那里(没有启用PLL)。即,位场或寄存器实际指定4 MHz对10 MHz或任何其他频率。4 MHz是我在代码配置器中选择的值。我真的很感激任何人提供的建议。谢谢!
以上来自于百度翻译
以下为原文
Thanks for your response and the reminder that what the code configurator generates should not be taken as definitely correct!
The U1BRG value is definitely an issue. But, even when I play around with the value I can't get the UART to work consistently. For example, correcting the value from the previous post does not give a working result. But, if I set the primary oscillator to 4 MHz, and set U1BRG = 0x18 with BRG disabled I get printf() to work successfully at a baud rate of 9600. U1BRG is double what I understand it should be in this case as calculated from the datasheet. I'm trying to figure out what's going on to cause this, but I haven't been able to as of yet. My code for oscillator initialization and UART initialization are below
// FOSCSEL
#pragma config FNOSC = PRI // Oscillator Source Selection->Primary Oscillator (XT, HS, EC)
#pragma config IESO = ON // Two-speed Oscillator Start-up Enable bit->Start up device with FRC, then switch to user-selected oscillator source
// FOSC
#pragma config POSCMD = XT // Primary Oscillator Mode Select bits->XT Crystal Oscillator Mode
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit->OSC2 is clock output
#pragma config IOL1WAY = ON // Peripheral pin select configuration->Allow only one reconfiguration
#pragma config FCKSM = CSDCMD // Clock Switching Mode bits->Both Clock switching and Fail-safe Clock Monitor are disabled
#pragma config PLLKEN = ON // PLL Lock Enable Bit->Clock switch to PLL source will wait until the PLL lock signal is valid
void OSCILLATOR_Initialize(void)
{
// CF no clock failure; NOSC PRI; CLKLOCK unlocked; OSWEN Switch is Complete;
__builtin_write_OSCCONL((uint8_t) (0x200 & 0x00FF));
// FRCDIV FRC/2; PLLPRE 5; DOZE 1:8; PLLPOST 1:8; DOZEN disabled; ROI disabled;
CLKDIV = 0x31C3;
// TUN Center frequency;
OSCTUN = 0x0;
// PLLDIV 148;
PLLFBD = 0x94;
// ENAPLL disabled; APSTSCLR 1:2; FRCSEL ASRCSEL determines input clock source; SELACLK Auxiliary Oscillators; ASRCSEL No clock input;
ACLKCON = 0x2600;
// LFSR 0;
LFSR = 0x0;
}
void UART1_Initialize(void)
{
/**
Set the UART1 module to the options selected in the user interface.
Make sure to set LAT bit corresponding to TxPin as high before UART initialization
*/
// STSEL 1; IREN disabled; PDSEL 8N; UARTEN enabled; RTSMD disabled; USIDL disabled; WAKE disabled; ABAUD disabled; LPBACK disabled; BRGH enabled; URXINV disabled; UEN TX_RX;
U1MODE = (0x8000 & ~(1<<15)); // disabling UARTEN bit
// UTXISEL0 TX_ONE_CHAR; UTXINV disabled; OERR NO_ERROR_cleared; URXISEL RX_ONE_CHAR; UTXBRK COMPLETED; UTXEN disabled; ADDEN disabled;
U1STA = 0x0;
// BaudRate = 9600; Frequency = 2000000 Hz; BRG 51;
U1BRG = 0x18;
U1MODEbits.UARTEN = 1; // enabling UARTEN bit
U1STAbits.UTXEN = 1;
}
I'm trying to verify that the oscillator is configured correctly. One thing I'm having trouble understanding: how is the primary oscillator frequency actually set in on of the registers? From the reference manual I understand how it is set to XT, which gives a range of 4-10 MHz, but I'm having trouble figuring out how is actually set to 4 MHz from there (without the PLL enabled). I.e., what bitfield or register actually specifies 4 MHz vs 10 MHz or any other frequency. 4 MHz is the value I selected in the code configurator. I would really appreciate any advice that anyone can offer. Thanks!
举报