Microchip
直播中

陈敏杰

7年用户 263经验值
私信 关注
[问答]

从uart发送字符错误

我想从UART发送一个字符。问题是,在我的输出中,我只得到???????我使用的是DSPIC33 FJ64 GP206A,XC16和8MHZEL中的外部晶体是我的代码:我认为我的问题是ISU2BRG速度。

以上来自于百度翻译


      以下为原文

    I'm trying to send just a single character from uart. The problem is that in my output i get only ?????.
I'm using DSPIC33FJ64GP206A, XC16 and External crystal in 8mhz

Here is my code:

#include
#include
#include
#include

#define _XTAL_FREQ 32000000
#define FOSC _XTAL_FREQ
#define FCY _XTAL_FREQ/2
#define BAUDRATE 9600
#define myBAUDRATE (FCY/(16*BAUDRATE)) - 1

void WriteUart( char txData)
{
   
    while (U1STAbits.UTXBF == 1); //wait until TXIF is set => TXBUF ready
    U2TXREG = txData;
   
}

void main(void) {
   
OSCCON =0x3000;
CLKDIV=0x3040;
PLLFBD=0x30;
OSCTUN=0x0000;

while (_LOCK ==0);
{
}

   
    TRISFbits.TRISF5 = 0;//TX
    TRISFbits.TRISF6 = 1;//RX
    TRISGbits.TRISG1=0; //rs485 control pin
    LATGbits.LATG1=1; //rs485 set high for writing
   
    U2BRG = myBAUDRATE;
    U2STA = 0x0000;
    U2MODE = 0x0000;
    U2MODEbits.UARTEN = 1; //UARTx is enabled; UARTx pins are controlled by UARTx as defined by the UEN<1:0> and UTXEN control bits
    U2STAbits.UTXEN = 1; //UARTx transmitter enabled; UxTX pin is controlled by UARTx (if UARTEN = 1)
    U2MODEbits.PDSEL = 0; //01 = 8-bit data, even parity, 11 = 9-bit data, no parity
    IEC1bits.U2RXIE = 1;//Enable Interrupts

   
    while(1)
    {
        
     WriteUart("1");
     
      
    }
}
#pragma config BWRP = WRPROTECT_OFF // Boot Segment Write Protect (Boot Segment may be written)
#pragma config BSS = NO_FLASH // Boot Segment Program Flash Code Protection (No Boot program Flash segment)
#pragma config RBS = NO_RAM // Boot Segment RAM Protection (No Boot RAM)

// FSS
#pragma config SWRP = WRPROTECT_OFF // Secure Segment Program Write Protect (Secure segment may be written)
#pragma config SSS = NO_FLASH // Secure Segment Program Flash Code Protection (No Secure Segment)
#pragma config RSS = NO_RAM // Secure Segment Data RAM Protection (No Secure RAM)

// FGS
#pragma config GWRP = OFF // General Code Segment Write Protect (User program memory is not write-protected)
#pragma config GSS = OFF // General Segment Code Protection (User program memory is not code-protected)

// FOSCSEL
#pragma config FNOSC = PRIPLL // Oscillator Mode (Primary Oscillator (XT, HS, EC) w/ PLL)
#pragma config IESO = ON // Two-speed Oscillator Start-Up Enable (Start up with FRC, then switch)

// FOSC
#pragma config POSCMD = XT // Primary Oscillator Source (XT Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled)

// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler (1:32,768)
#pragma config WDTPRE = PR128 // WDT Prescaler (1:128)
#pragma config WINDIS = OFF // Watchdog Timer Window (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = ON // Watchdog Timer Enable (Watchdog timer always enabled)

// FPOR
#pragma config FPWRT = PWR128 // POR Timer Value (128ms)

// FICD
#pragma config ICS = PGD1 // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG is Disabled)



I think that my problem is U2BRG speed

回帖(11)

吴键洪

2018-10-9 11:23:20
ChangetoRight,现在,您正在发送字符串地址的低字节。

以上来自于百度翻译


      以下为原文

    Change
    WriteUart("1");
to
    WriteUart('1');


 
Right now, you are sending the low byte of the address of the string.
 
举报

吴键洪

2018-10-9 11:32:16
注意,这些问题应该在DSPIC3F主题论坛中,而不是在MPLABX论坛中,因为它们与IDE无关。

以上来自于百度翻译


      以下为原文

    Note, these questions should be in the dsPIC33F Topics forum, not here in the MPLABX forum, as they have nothing to do with the IDE.
 
举报

李建宁

2018-10-9 11:44:30
我改变了,但我把错误字符

以上来自于百度翻译


      以下为原文

    I change that but i'm taking wrong characters unspecified characters
举报

吴键洪

2018-10-9 11:50:24
改为发送0x55,并观察范围内的TX引脚。(0x55=0B01010101,因此您将看到波特率的一半的方波)。

以上来自于百度翻译


      以下为原文

    Change it to send 0x55 instead, and observe the TX pin with a scope.
(0x55 = 0b01010101, so you will see a square wave at half the baud rate).
 
举报

更多回帖

发帖
×
20
完善资料,
赚取积分