gpt4 book ai didi

c - PIC 打印垃圾字符而不是预期字符

转载 作者:行者123 更新时间:2023-11-30 17:32:40 25 4
gpt4 key购买 nike

我想用我的PIC18F4550、蓝牙模块HC-06和电脑做一个简单的测试。我的意思是,我想使用蓝牙模块将一个简单的字符从 PIC 发送到 PC。

它应该在终端中出现一个“A”,但出现的是十六进制的 F8(ASCII:看起来像一个内部有斜杠的“o”)。我尝试使用 printfputsfputs 来代替,但 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");
}
}

最佳答案

这是在 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);

}
}

关于c - PIC 打印垃圾字符而不是预期字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24045398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com