打印

PIC24F串口接收的问题

[复制链接]
6392|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
soicer|  楼主 | 2009-9-2 10:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我用的PIC24F系列的单片机,8M晶振.但是使用UART时发现一个问题,就是设定完波特率,发送数据没有问题,但是接收有时候是乱码. 调试时发现串口状态位中的FREE被置位,查资料说是帧错误,但是完全不明白什么是"当期字符的帧错误". 串口工作在高速模式. 现在只有9600收发没有问题,其他波特率都有问题.

请各位指教.谢谢.
沙发
soicer|  楼主 | 2009-9-3 09:44 | 只看该作者
完全没人知道吗? 现在怀疑是用ICD2调试的时候才有错误.

使用特权

评论回复
板凳
tomytxm| | 2009-9-4 14:05 | 只看该作者
我刚完成了PIC24F16KA101+MRF49XA设计的无线抄表项目,用到串口功能,好用着呢,你可以检查下硬件问题!

void ConsoleInit(void)//串口初始化
{
    TRISBbits.TRISB2 = 1;
    TRISBbits.TRISB7 = 0;
        U1MODEbits.UARTEN = 0;        // Bit15 TX, RX DISABLED, ENABLE at end of func
        U1MODEbits.USIDL = 0;        // Bit13 Continue in Idle
        U1MODEbits.IREN = 0;        // Bit12 No IR translation
        U1MODEbits.RTSMD = 0;        // Bit11 Simplex Mode
        U1MODEbits.UEN = 0;                // Bits8,9 TX,RX enabled, CTS,RTS not
        U1MODEbits.WAKE = 0;        // Bit7 No Wake up (since we don't sleep here)
        U1MODEbits.LPBACK = 0;        // Bit6 No Loop Back
        U1MODEbits.ABAUD = 0;        // Bit5 No Autobaud (would require sending '55')
        U1MODEbits.RXINV = 0;        // Bit4 IdleState = 1
        U1MODEbits.BRGH = 1;        // Bit3 16 clocks per bit period
        U1MODEbits.PDSEL = 0;        // Bits1,2 8bit, No Parity
        U1MODEbits.STSEL = 0;        // Bit0 One Stop Bit
       
        U1BRG = 103;        // baud rate

        U1STAbits.UTXISEL1 = 0;        //Bit15 Int when Char is transferred (1/2 config!)
        U1STAbits.UTXINV = 0;        //Bit14 N/A, IRDA config
        U1STAbits.UTXISEL0 = 0;        //Bit13 Other half of Bit15
        U1STAbits.UTXBRK = 0;        //Bit11 Disabled
        U1STAbits.UTXEN = 0;        //Bit10 TX pins controlled by periph
        U1STAbits.UTXBF = 0;        //Bit9 *Read Only Bit*
        U1STAbits.TRMT = 0;                //Bit8 *Read Only bit*
        U1STAbits.URXISEL = 0;        //Bits6,7 Int. on character recieved
        U1STAbits.ADDEN = 0;        //Bit5 Address Detect Disabled
        U1STAbits.RIDLE = 0;        //Bit4 *Read Only Bit*
        U1STAbits.PERR = 0;                //Bit3 *Read Only Bit*
        U1STAbits.FERR = 0;                //Bit2 *Read Only Bit*
        U1STAbits.OERR = 0;                //Bit1 *Read Only Bit*
        U1STAbits.URXDA = 0;        //Bit0 *Read Only Bit*
        IFS0bits.U1TXIF = 0;        // Clear the Transmit Interrupt Flag
        IEC0bits.U1TXIE = 0;        // Enable Transmit Interrupts
        IFS0bits.U1RXIF = 0;        // Clear the Recieve Interrupt Flag
        IEC0bits.U1RXIE = 1;        // Enable Recieve Interrupts
        U1MODEbits.UARTEN = 1;        // And turn the peripheral on
        U1STAbits.UTXEN = 1;
}

void ConsolePut(BYTE c)//单字节发送
{
    while( !ConsoleIsPutReady() );
    U1TXREG = c;
}
void __attribute__ ((interrupt, no_auto_psv)) _U1RXInterrupt(void) //接收中断函数
{
    unsigned char tmp;
        tmp = U1RXREG;
        if(FIFOPtr < FIFOMax)
    {
     RCBuffer[FIFOPtr++]=tmp;
    }
    IFS0bits.U1RXIF = 0;       
}

使用特权

评论回复
地板
soicer|  楼主 | 2009-9-6 22:50 | 只看该作者
问题已经找出来了,是芯片的BUG。
我用的是PIC24FJ32GA002,后来查了这个系列的勘误表,发现某些批次的芯片UART,高速模式波特率计算会有问题。就是说我程序里边的波特率都给错了。无奈,只好用低速模式了。

使用特权

评论回复
5
lhzw2001| | 2012-3-15 15:35 | 只看该作者
我也正在调串口,现在是中断可以发送数据,但接收数据,产生不了中断,也没有uxif标志。。 不知道还有哪里要注意的呢,

配置跟这个是一样的:
void ConsoleInit(void)//串口初始化
{
    TRISBbits.TRISB2 = 1;
    TRISBbits.TRISB7 = 0;
        U1MODEbits.UARTEN = 0;        // Bit15 TX, RX DISABLED, ENABLE at end of func
        U1MODEbits.USIDL = 0;        // Bit13 Continue in Idle
        U1MODEbits.IREN = 0;        // Bit12 No IR translation
        U1MODEbits.RTSMD = 0;        // Bit11 Simplex Mode
        U1MODEbits.UEN = 0;                // Bits8,9 TX,RX enabled, CTS,RTS not
        U1MODEbits.WAKE = 0;        // Bit7 No Wake up (since we don't sleep here)
        U1MODEbits.LPBACK = 0;        // Bit6 No Loop Back
        U1MODEbits.ABAUD = 0;        // Bit5 No Autobaud (would require sending '55')
        U1MODEbits.RXINV = 0;        // Bit4 IdleState = 1
        U1MODEbits.BRGH = 1;        // Bit3 16 clocks per bit period
        U1MODEbits.PDSEL = 0;        // Bits1,2 8bit, No Parity
        U1MODEbits.STSEL = 0;        // Bit0 One Stop Bit
        
        U1BRG = 103;        // baud rate

        U1STAbits.UTXISEL1 = 0;        //Bit15 Int when Char is transferred (1/2 config!)
        U1STAbits.UTXINV = 0;        //Bit14 N/A, IRDA config
        U1STAbits.UTXISEL0 = 0;        //Bit13 Other half of Bit15
        U1STAbits.UTXBRK = 0;        //Bit11 Disabled
        U1STAbits.UTXEN = 0;        //Bit10 TX pins controlled by periph
        U1STAbits.UTXBF = 0;        //Bit9 *Read Only Bit*
        U1STAbits.TRMT = 0;                //Bit8 *Read Only bit*
        U1STAbits.URXISEL = 0;        //Bits6,7 Int. on character recieved
        U1STAbits.ADDEN = 0;        //Bit5 Address Detect Disabled
        U1STAbits.RIDLE = 0;        //Bit4 *Read Only Bit*
        U1STAbits.PERR = 0;                //Bit3 *Read Only Bit*
        U1STAbits.FERR = 0;                //Bit2 *Read Only Bit*
        U1STAbits.OERR = 0;                //Bit1 *Read Only Bit*
        U1STAbits.URXDA = 0;        //Bit0 *Read Only Bit*
        IFS0bits.U1TXIF = 0;        // Clear the Transmit Interrupt Flag
        IEC0bits.U1TXIE = 0;        // Enable Transmit Interrupts
        IFS0bits.U1RXIF = 0;        // Clear the Recieve Interrupt Flag
        IEC0bits.U1RXIE = 1;        // Enable Recieve Interrupts
        U1MODEbits.UARTEN = 1;        // And turn the peripheral on
        U1STAbits.UTXEN = 1;
}

使用特权

评论回复
6
astudent| | 2014-11-23 16:54 | 只看该作者
先关注

使用特权

评论回复
7
colorful88| | 2014-11-23 21:16 | 只看该作者
我用pic的时候uart也经常接收错误,所以写了uart的restart函数放在程序

使用特权

评论回复
8
Vereesa| | 2014-11-26 15:37 | 只看该作者
我也有个类似的问题,接受正常,但是发送数据就是乱码

使用特权

评论回复
9
foretek| | 2015-1-2 01:15 | 只看该作者
我用PIC24F中断,收发都没问题.相关程序如下..

    _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
    _CONFIG2( FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_XT & FNOSC_PRIPLL & IOL1WAY_ON)        //PRIPLL makes it 4X faster
    _CONFIG3( WPCFG_WPCFGDIS & WPDIS_WPDIS)     //Disable erase/write protect of all memory regions.

..... .....
/****************************************************************************
void InitUART1(void)
comment: to initialize UART1 daisy chain with UART2 to communicate to ModBus VB master.
*****************************************************************************/
void InitUART1(void)
{
    // configure U2MODE
    U1MODEbits.UARTEN = 0;  // Bit15 TX, RX DISABLED, ENABLE at end of func
    U1MODEbits.USIDL = 0;   // Bit13 Continue in Idle
    U1MODEbits.IREN = 0;    // Bit12 No IR translation
    U1MODEbits.RTSMD = 0;   // Bit11 Simplex Mode
    U1MODEbits.UEN = 0;     // Bits8,9 TX,RX enabled, CTS,RTS not
    U1MODEbits.WAKE = 0;    // Bit7 No Wake up (since we don't sleep here)
    U1MODEbits.LPBACK = 0;  // Bit6 No Loop Back
    U1MODEbits.ABAUD = 0;   // Bit5 No Autobaud (would require sending '55')
    U1MODEbits.RXINV = 0;   // Bit4 IdleState = 1
    U1MODEbits.PDSEL = 0;   // Bits1,2 8bit, No Parity
    U1MODEbits.STSEL = 0;   // Bit0 One Stop Bit

    U1MODEbits.BRGH = 0;    // Bit3 16 clocks per bit period
    U1BRG = BAUDRATEREG2;   // baud rate 38400
//        U1MODEbits.BRGH = 1;    // Bit3 4 clocks per bit period
//        U1BRG = BAUDRATEREG1;   // baud rate 115200

    // Load all values in for U1STA SFR
    U1STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
    U1STAbits.UTXINV = 0;   //Bit14 N/A, IRDA config
    U1STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15
    U1STAbits.UTXBRK = 0;   //Bit11 Disabled
    U1STAbits.UTXEN = 0;    //Bit10 TX pins controlled by periph
    U1STAbits.UTXBF = 0;    //Bit9 *Read Only Bit*
    U1STAbits.TRMT = 0;     //Bit8 *Read Only bit*
    U1STAbits.URXISEL = 0;  //Bits6,7 Int. on character recieved
    U1STAbits.ADDEN = 0;    //Bit5 Address Detect Disabled
    U1STAbits.RIDLE = 0;    //Bit4 *Read Only Bit*
    U1STAbits.PERR = 0;     //Bit3 *Read Only Bit*
    U1STAbits.FERR = 0;     //Bit2 *Read Only Bit*
    U1STAbits.OERR = 0;     //Bit1 *Read Only Bit*
    U1STAbits.URXDA = 0;    //Bit0 *Read Only Bit*

    IFS0bits.U1TXIF = 0;    // Clear the Transmit Interrupt Flag
    IEC0bits.U1TXIE = 1;    // Enable Transmit Interrupts
    IFS0bits.U1RXIF = 0;    // Clear the Recieve Interrupt Flag
    IEC0bits.U1RXIE = 1;    // Enable Recieve Interrupts

    IPC2bits.U1RXIP = 2;    // set Interrupt priority  to 2

    U1MODEbits.UARTEN = 1;  // And turn the peripheral on

    U1STAbits.UTXEN = 1;
}

/************************ for uart1, daisy chain ModBus ***********************/

void __attribute__ ((interrupt, no_auto_psv)) _U1RXInterrupt(void) {
    unsigned char byte1;

        byte1 = U1RXREG;
        U2TXREG = byte1;        // start sending from TX isr
    IFS0bits.U1RXIF = 0;
}
void __attribute__ ((interrupt, no_auto_psv)) _U1TXInterrupt(void) {
    IFS0bits.U1TXIF = 0;
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

13

主题

50

帖子

0

粉丝