打印

PIC18F4520串口收发字符长度有限制

[复制链接]
1867|10
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
lcy89815|  楼主 | 2015-12-11 09:52 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 lcy89815 于 2015-12-11 09:52 编辑

这两天在试PIC18F4520串口自动收发功能,发现在串口调试小助手只能发送28个以内的字符,超过的只能显示前28个,之后就无法接收,这会是什么原因造成的呢,谢谢大家了!


void main(void)
{
    OSCCON = 0x72;              //8MHz,Internal oscillator, however its actual value is 0x76        
    /*16-bit,asynchronous mode, desired baud rate = Fosc / (4 * ([SPBRGH:SPBRGL] + 1))*/
    SYNC = 0;                        //asynchronous mode
    BRG16 = 1;                     //16-bit baud rate generator is used
    BRGH = 1;                       //high speed
    SPBRGH = 0x00;             //baud rate 9600
    SPBRG = 0xcf;
    /*enable receiver*/  
    SPEN = 1;                       //enable eusart
    TRISC6 = 1;
    TRISC7 = 1;
    CREN = 1;                      //enable receiver circuitry of eusart
    TXEN = 1;                      //enable transmitter circuitry of eusart
    RCIE = 1;                       //enable usart receiver interrupt

    GIE = 1;                        //enable all active interrupts
    PEIE = 1;                      //enable all active peripheral interrupts   
  
    while(1)
    {
        ;
    }
    return;
}
void interrupt INT_ISR(void)
{
    if(RCIF && RCIE)
    {
     TXREG = RCREG;
     while(!TRMT);
    }
}




2.png (184.28 KB )

长度为28,收发正常

长度为28,收发正常

3.png (149.94 KB )

长度超过28,只能接收前28个,之后再也收不到数据

长度超过28,只能接收前28个,之后再也收不到数据
沙发
perry_peng| | 2015-12-11 10:08 | 只看该作者
RCIF要零吧。

使用特权

评论回复
板凳
shaoziyang| | 2015-12-11 10:10 | 只看该作者
印象中。PIC单片机在串口通信错误时(溢出、检验等),串口会停止工作。需要处理串口错误中断,重新初始化一下。

你检查一下串口状态寄存器看看。

使用特权

评论回复
地板
lcy89815|  楼主 | 2015-12-11 10:16 | 只看该作者

RCIF是只读位,不需要清零吧

使用特权

评论回复
5
lcy89815|  楼主 | 2015-12-11 10:40 | 只看该作者
shaoziyang 发表于 2015-12-11 10:10
印象中。PIC单片机在串口通信错误时(溢出、检验等),串口会停止工作。需要处理串口错误中断,重新初始化 ...

查看了一下,帧错误位FERR,和溢出错误位OERR都为零,没有出错呀

使用特权

评论回复
6
lcy89815|  楼主 | 2015-12-11 11:07 | 只看该作者
shaoziyang 发表于 2015-12-11 10:10
印象中。PIC单片机在串口通信错误时(溢出、检验等),串口会停止工作。需要处理串口错误中断,重新初始化 ...

仿真中的确没观测到OERR置1,尝试增加溢出处理,情况改善不少,但发现字符w一直丢失同时误码率很高,进一步该如何处理好呢
void interrupt INT_ISR(void)
{
    if(RCIF && RCIE)
    {
        if(OERR)
        {
            CREN = 0;
        }
        CREN = 1;
        TXREG = RCREG;
        while(!TRMT);
    }
}

4.png (214.53 KB )

4.png

使用特权

评论回复
7
shaoziyang| | 2015-12-11 14:30 | 只看该作者
lcy89815 发表于 2015-12-11 11:07
仿真中的确没观测到OERR置1,尝试增加溢出处理,情况改善不少,但发现字符w一直丢失同时误码率很高,进一 ...

是不是用RC振荡器,误差有点大?

使用特权

评论回复
8
lcy89815|  楼主 | 2015-12-11 14:35 | 只看该作者
shaoziyang 发表于 2015-12-11 14:30
是不是用RC振荡器,误差有点大?

首先多谢shaoziyang的耐心解答,我采用的是内部振荡器。
配置位如下:
#include<pic18f4520.h>
#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG1H
#pragma config OSC = INTIO67    // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = ON        // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bits (Brown-out Reset enabled and controlled by software (SBOREN is enabled))
#pragma config BORV = 2         // Brown Out Reset Voltage bits ()

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
//#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config STVREN = OFF
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)

使用特权

评论回复
9
JY-DX-JY| | 2015-12-11 18:33 | 只看该作者
配置成外部振荡器试一下。

使用特权

评论回复
10
sxyl181| | 2015-12-22 10:39 | 只看该作者
RCIF是只读位,不需要清零吧

使用特权

评论回复
11
QQ826220679| | 2016-4-2 00:25 | 只看该作者
波特率是不是准确?换一个串口调试软件试一下,有时候调试软件也有问题的。

使用特权

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

本版积分规则

1

主题

8

帖子

2

粉丝