【发布时间】:2014-05-20 22:13:06
【问题描述】:
无法发送字符“C”并显示在接收器图片上。 LCD打印一些特殊字符,没有'C'... ;(
发射器:
char uart_rd;
int uart_rdi;
sbit LCD_RS at LATB0_bit;
sbit LCD_EN at LATB1_bit;
sbit LCD_D4 at LATB5_bit;
sbit LCD_D5 at LATB4_bit;
sbit LCD_D6 at LATB3_bit;
sbit LCD_D7 at LATB2_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
void main() {
ANSELA = 0; // Configure AN pins as digital
ANSELB = 0;
ANSELC = 0;
Lcd_Init();
UART2_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
while (1) {
UART2_Write('C');
}
}
接收者:
char uart_rd;
int uart_rdi;
sbit LCD_RS at LATB0_bit;
sbit LCD_EN at LATB1_bit;
sbit LCD_D4 at LATB5_bit;
sbit LCD_D5 at LATB4_bit;
sbit LCD_D6 at LATB3_bit;
sbit LCD_D7 at LATB2_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
void main() {
ANSELA = 0; // Configure AN pins as digital
ANSELB = 0;
ANSELC = 0;
Lcd_Init();
UART2_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
while (1) {
if (UART2_Data_Ready()) { // If data is received,
uart_rdi = UART2_Read(); // read the received data,
uart_rd = uart_rdi;
LCD_Out(1,1, uart_rd);
}
}
}
Unsing 两张 18F26K22,串口新手... 不知道如何测试它,因为没有设备,我的代码是好的吗?我没有使用 Proteus,在金属上工作。谢谢。
【问题讨论】:
-
您能否将调试器附加到接收器 PIC 上,看看它是否正确接收字符与只是没有正确显示到 LCD 上?或者,如果它看到一个字符和/或正确的字符,则打开/关闭灯。此外,UART 模块不需要时间来“稳定”,您可以划分系统时钟并最终获得特定的比特率。在处理器启动并开始执行代码时,时钟不稳定性可以忽略不计。
-
调试器是什么意思,好像你在说示波器,我没有...
-
我从发送图片中得到了一些东西,但我无法在接收者图片上显示它,因为它显示了一些符号而不是 C 字母...
-
在你的写循环中添加一个延迟(可能是 10 毫秒),这样你就不会背靠背地写字符。你知道你要发送多少个停止位吗?