我想用PIC18F4550,蓝牙模块HC-06和电脑进行简单的测试。 我的意思是,我想使用蓝牙模块从PIC向PC发送一个简单的字符。
它应该在终端中显示为“A”,但看起来是HEX中的F8(ASCII:看起来像是带有斜线的“o”)。我尝试使用printf
,puts
或fputs
,但F8仍会显示加上此符号“€”。
我的代码和原理图是下一个......有人可以帮助我找到我的错误吗?
(我正在使用RCom Serial或CoolTerminal)
#include <18f4550.h>
#include <stdio.h>
#fuses XT, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP, NOCPD, NOWRT
#use delay (clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N)
void main() {
while (1) {
printf("A");
}
}
答案 0 :(得分:0)
这是通用代码,用于在UART端口上打印调试消息。 注意:
#include <xc.h>
// Use your uC header file
#include <pic16f877a.h>
#include <stdio.h>
#include <string.h>
#define _XTAL_FREQ 20000000
#define FREQ _XTAL_FREQ
#define baud 9600
#define spbrg_value (((FREQ/64)/baud)-1)
unsigned char UARTdata;
void serial_init()
{
SPBRG=spbrg_value;
//SPBRG=31;
//TXSTAbits.TXEN = 1;
//CSRC: Clock Source Select bit 0
//TX9 : 9-bit Transmit Enable bit 0
//TXEN: Transmit Enable 0
//SYNC: EUSART Mode Select bit 0
//SENDB: Send Break Character bit 0
//BRGH: High Baud Rate Select bit 0
//TRMT: Transmit Shift Register Status bit 1
//TX9D: Ninth bit of Transmit Data 0
TXSTA=0x20;
//RCSTAbits.SPEN = 1;
//RCSTAbits.CREN = 1
//bit 7 SPEN: Serial Port Enable bit 1
//bit 6 RX9: 9-bit Receive Enable bit 0
//bit 5 SREN: Single Receive Enable bit 0
//bit 4 CREN: Continuous Receive Enable bit 1
//bit 3 ADDEN: Address Detect Enable bit 0
//bit 2 FERR: Framing Error bit 0
//bit 1 OERR: Overrun Error bit 0
//bit 0 RX9D: 9th bit of Received Data 0
RCSTA=0x90;
//Setting
TRISCbits.TRISC6=0;
TRISCbits.TRISC7=1;
}
void putch(unsigned char UARTdata)
{
while( ! TXIF)
continue;
TXREG = UARTdata;
}
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
void main()
{
serial_init();
printf("DRIVER Enjoy Direct print method!");
while(1)
{
__delay_ms(1000);
printf("A");
__delay_ms(1000);
}
}