[PIC®/AVR®/dsPIC®产品]

PIC18F4550串口一直出现发送的数据跟接收到的数据不一致

[复制链接]
518|1
手机看帖
扫描二维码
随时随地手机跟帖
LSP90|  楼主 | 2020-4-3 18:03 | 显示全部楼层 |阅读模式
/*
PIC18F4550串口一直出现发送的数据跟接收到的数据不一致,例如串口调试组手发送0X30,MCU接收到是0XD8;MCU发送0X31,串口调试组手接收0X61。
困扰很久,不知道问题出在哪,麻烦各位大师帮忙解答一下,谢谢!
*/
/*
编译器:MCC18
版本:MPLAB-C18-v3_02
晶振:20MHZ
*/
#include <p18cxxx.h>
#include <delays.h>
#include <usart.h>


/****** CONFIG1L Options ********************************************/

#pragma config      PLLDIV      = 5                //5 分频(20 MHz 振荡器输入)
#pragma config      CPUDIV      = OSC4_PLL6  //96 MHz PLL 6 分频作为系统时钟
#pragma config      USBDIV      = 2

/****** CONFIG1H Options ********************************************/

#pragma config      FOSC        = HSPLL_HS        //HS振荡器,使能PLL (HSPLL)
#pragma config      FCMEN       = ON
#pragma config      IESO        = ON

/****** CONFIG2L Options ********************************************/

#pragma config      PWRT        = ON
#pragma config      BOR         = ON
#pragma config      BORV        = 3
#pragma config      VREGEN      = ON

/****** CONFIG2H Options ********************************************/

#pragma config      WDT         = OFF
#pragma config      WDTPS       = 32768

/****** CONFIG3H Options ********************************************/

#pragma config      MCLRE       = OFF
#pragma config      LPT1OSC     = ON
#pragma config      PBADEN      = OFF
#pragma config      CCP2MX      = ON

/****** CONFIG4L Options ********************************************/

#pragma config      STVREN      = ON
#pragma config      LVP         = OFF
#pragma config      ICPRT       = OFF
#pragma config      XINST       = OFF

/****** CONFIG5L Options ********************************************/

#pragma config      CP0         = OFF
#pragma config      CP1         = OFF
#pragma config      CP2         = OFF
#pragma config      CP3         = OFF

/****** CONFIG5H Options ********************************************/

#pragma config      CPB         = OFF
#pragma config      CPD         = OFF

/****** CONFIG6L Options ********************************************/

#pragma config      WRT0        = ON
#pragma config      WRT1        = ON
#pragma config      WRT2        = ON
#pragma config      WRT3        = ON

/****** CONFIG6H Options ********************************************/

#pragma config      WRTB        = ON
#pragma config      WRTC        = ON
#pragma config      WRTD        = OFF

/****** CONFIG7L Options ********************************************/

#pragma config      EBTR0       = OFF
#pragma config      EBTR1       = OFF
#pragma config      EBTR2       = OFF
#pragma config      EBTR3       = OFF

/****** CONFIG7H Options ********************************************/
#pragma config      EBTRB       = OFF
#define uchar unsigned char
#define uint  unsigned int
//---------主函数定义区---------
void InitializeSystem(void);//系统初始化
void UserInit(void);  //用户初始化程序
void USARTInit(void);  //USART初始化程序
void InterruptHandlerHigh (void);//高优先级中断服务函数
void InterruptHandlerLow (void);//低优先级中断服务函数

//定义输出
#define        LED        RA1                //LED

//定义变量
unsigned int  count_timer2;
unsigned char timer2_flag;
unsigned char flag_rx,flag_tx;
uchar recive;

//中断函数
// High priority interrupt vector
#pragma code InterruptVectorHigh = 0x08
void
InterruptVectorHigh (void)
{
  _asm
    goto InterruptHandlerHigh //jump to interrupt routine
  _endasm
}
#pragma code InterruptVectorLow = 0x18
InterruptVectorLow (void)
{
  _asm
    goto InterruptHandlerLow //jump to interrupt routine
  _endasm
}
#pragma code
//低优先级中断
#pragma interruptlow InterruptHandlerLow

void InterruptHandlerLow ()
{
        if(PIR1bits.TMR2IF)
        {                                   //check for TMR2 overflow
                PIR1bits.TMR2IF = 0;            //clear interrupt flag
                timer2_flag = 1;            //indicate timeout
                if(++count_timer2>500)
                {
                        LATAbits.LATA1 = !LATAbits.LATA1; //toggle LED on RA1
                        count_timer2=0;
                }
        }
}
//高优先级中断
#pragma interrupt InterruptHandlerHigh

void InterruptHandlerHigh ()
{
        if (PIR1bits.RCIF)
    {                                   //check for TMR2 overflow
             PIR1bits.RCIF = 0;            //clear interrupt flag
               
                /* Get the character received from the USART */
                  recive =RCREG;        // ReadUSART();
                  if (recive >= 0x30 && recive <= 0x39)
            {
                      flag_rx=1;
            }
                  else
            {
                    flag_tx=1;
            }
    }
}
//主函数
void main (void)
{
        InitializeSystem();
        while (1)
        {
                if(flag_tx)
                {
                        putcUSART (0x31);
                        flag_tx=0;       
                }
                if(flag_rx)
                {
                        putcUSART (0x32);
                        flag_rx=0;       
                }       
                while (!timer2_flag)
                        timer2_flag=0;
        }
}
/***************************
* 系统初始化函数
****************************/
void InitializeSystem()
{
        USARTInit();
        ADCON1=0x0f;            //把模拟端口全部设置为数字端口
        /* Enable interrupt priority */
        RCONbits.IPEN = 1;

        /* Clear the peripheral interrupt flags */
        PIR1 = 0;
        /* Enable the timer interrupt */
        PIE1bits.TMR2IE = 1;
        IPR1bits.TMR2IP = 0;

        /* Enable interrupts */
        INTCONbits.GIEH = 1;
        INTCONbits.GIEL = 1;
        INTCONbits.PEIE = 1;

        PR2=249;
        T2CONbits.T2OUTPS2=1;        //后分频比1:5
        T2CONbits.T2CKPS0=1;        //后分频比1:4
        /* Enable the timer */
        T2CONbits.TMR2ON = 1;
       
        putcUSART (0x38);

        UserInit();               //用户初始化
}
void USARTInit()
{
        TRISCbits.TRISC6=0;                //设置为输入引脚
        TRISCbits.TRISC7=1;                //设置为输入引脚
       
        /* Make receive interrupt high priority */
          IPR1bits.RCIP = 1;

  OpenUSART (USART_TX_INT_OFF &                //发送中断OFF
             USART_RX_INT_ON &                //接收中断ON
             USART_ASYNCH_MODE &        //异步模式
             USART_EIGHT_BIT &                //8bit
             USART_CONT_RX &                //连续传送
             USART_BRGH_HIGH, 129);        //高传输模式,9600,速率=Fosc/(16*(spbrg+1))

}
void UserInit()
{
        TRISB=0x0F;                    //设置为输入引脚,按钮输入
        TRISD=0xFF;                    //设置为输入引脚,按钮输入
        LATD &= 0xF0; TRISD &= 0xF0;  //LED指示灯
        TRISE=0x00;                                //设置为输出引脚,点阵显示
        TRISAbits.TRISA1=0;     //设置为输出
        PORTAbits.RA1=0;       
}

使用特权

评论回复
LSP90|  楼主 | 2020-4-3 18:53 | 显示全部楼层
已经解决了,波特率算错了,系统时钟是16MHZ

使用特权

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

本版积分规则

1

主题

2

帖子

0

粉丝