PIC18F字符串到蓝牙到Tera术语

时间:2014-06-11 15:07:25

标签: bluetooth embedded pic

我正在尝试从PIC的USART输出一个字符串,并将其显示在Tera Term上。我正在使用:

  • PIC18F4331
  • Sparkfun Bluesmirf RN-42
  • MPLAB v8.85
  • Tera Term

我已经在这段代码上工作了几天,而且我没有看到任何一个回复。我认为可能导致问题的一些事情是波特率和/或没有中断例程。但如果我只是试图传输,是否需要中断?请有人指导我吗?此外,当使用printf时,我看到通过蓝牙的响应,但是以奇怪的符号形式。例如,þþþ。

该代码是对在线发现的代码的修改。

// Libraries
#include <p18f4331.h>
#include <stdio.h>

// Configuations 
#pragma config OSC      = XT
#pragma config WDTEN    = OFF  
#pragma config PWRTEN   = OFF 
#pragma config FCMEN    = OFF
#pragma config IESO     = OFF
#pragma config BOREN    = ON
#pragma config BORV     = 27
#pragma config WDPS     = 128
#pragma config T1OSCMX  = ON
#pragma config PWMPIN   = ON
#pragma config MCLRE    = ON
#pragma config LVP      = OFF
#pragma config STVREN   = OFF
#pragma config PWM4MX   = RD5

// Definitions
#define _XTAL_FREQ 4000000
#define BAUDRATE 9600


void EUSART(void)
{
    TRISC = 0b10000000;
    SPBRG = 25;
    TXSTAbits.CSRC = 0; // Baud Rate Generated Externally
    TXSTAbits.TX9 = 0; // 8-Bit Transmission
    TXSTAbits.TXEN = 1; // Transmit Enabled
    TXSTAbits.SYNC = 0; // Asynchronous Mode
    TXSTAbits.BRGH = 1; // High Baud Rate
    TXSTAbits.TRMT = 0; // Transmit Shift Register When TSR Is Full
    RCSTAbits.SPEN = 1; // Serial Port Enabled
    RCSTAbits.RX9 = 0; // 8-Bit Reception
    RCSTAbits.CREN = 1; // Enables Receive
}

void SendByteSerially(unsigned char Byte) // Writes a character to the serial port
{
    while(!PIR1bits.TXIF) ; // wait for previous transmission to finish
    TXREG = Byte;
}

unsigned char ReceiveByteSerially(void) // Reads a character from the serial port
{
    while(!PIR1bits.RCIF) continue;   // Wait for transmission to receive
    return RCREG;
}

void SendStringSerially(const rom unsigned char* st)
{
    while(*st) SendByteSerially(*st++);
}

void delayMS(unsigned int x)
{   
    unsigned char y;
        for(;x > 0; x--) for(y=0; y< 82;y++);
}

void main(void)
{
    unsigned char SerialData;
    EUSART(); 
    SendStringSerially("Hello World");

    while(1)
    {
        SerialData = ReceiveByteSerially();
        SendByteSerially(SerialData);
        delayMS(1000);
    }
}

1 个答案:

答案 0 :(得分:0)

您正在使用PIC18,请确保BRG16等于0,因为您正在使用BRGH

BAUDCTL.BRG16 = 0;

因为SPBRGH16是SPBRG的高字节,这可能会改变USART的波特率值。

另外,请确保您已进入PIR1银行。在MPLAB中,那将是

banksel PIR1; //Not sure if there's an ending coma

我正确初始化时通过UART传输的两个函数(在MikroC中):

void vTx232 (UC ucSend)
 {
    STATUS.RP0 = PIR1;     //Sure we're in PIR1
    while (PIR1.TXIF == 0);//While last TX not done
    TXREG = ucSend;        //Input param into TXREG
 }

void vTxString(UC *ucpString)
 {
   while (*ucpString!= 0x00)    //While string not at its end
    {
      vTx232(*ucpString);       //Send string character
      ucpString++;              //Increm string pointer
    }
 }