我最近在微控制器中介绍了adc和lcd并进入了USART,像往常一样,我制作的第一个代码不起作用,我需要一些帮助来发现问题,这里是代码:
发射器代码:
main.c中:
#include "config.h"
void main(){
TRISCbits.TRISC6 = 1;
TRISCbits.TRISC7 = 1;
TRISDbits.TRISD0 = 1;
SPBRG = 25;
TXSTAbits.TX9 = 0;
TXSTAbits.SYNC = 0;
TXSTAbits.BRGH = 1;
TXSTAbits.TXEN = 1;
RCSTAbits.SPEN = 1;
RCSTAbits.RX9 = 0;
RCSTAbits.CREN = 0;
while(1){
while(TRMT == 0);
if(PORTDbits.RD0 == 1){
TXREG = 0xFF;
}else{
TXREG = 0;
}
}
}
收件人代码:
main.c中:
#include "config.h"
char recieve;
void main(){
TRISCbits.TRISC6 = 1;
TRISCbits.TRISC7 = 1;
TRISDbits.TRISD0 = 0;
PORTDbits.RD0 = 0;
SPBRG = 25;
TXSTAbits.TX9 = 0;
TXSTAbits.SYNC = 0;
TXSTAbits.BRGH = 1;
TXSTAbits.TXEN = 1;
RCSTAbits.SPEN = 1;
RCSTAbits.RX9 = 0;
RCSTAbits.CREN = 0;
while(1){
RCREG = recieve;
if(recieve == 0xFF){
PORTDbits.RD0 = 1;
}else{
PORTDbits.RD0 = 0;
}
}
}
对于发送器和接收器项目,config.h是一个头文件,我在其中设置晶体振荡器和配置位的频率,因此它与两个项目的文件/代码相同
的config.h:
/*
* File: config.h
* Author: Fady
*
* Created on August 25, 2014, 1:53 PM
*/
// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT 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 enabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#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)
#define _XTAL_FREQ 4000000
修改
我正在尝试通信2个pic微控制器,如果按下发送器D0上的按钮,它会发送一个0xFF数据然后接收器检查接收到的数据是否为== 0xFF,如果是,它会打开D0上的LED接收器,但当我连接它时,接收器的TX引脚每半秒保持高亮和低(红色和蓝色)闪烁,但是当我按下按钮时,高(红色)信号保持一段时间,但保持闪烁高和低,我想这是开始位,但接收器没有打开LED这是错误部分我不知道这里有什么问题
答案 0 :(得分:0)
这只是我写的一个非常愚蠢的编程错误:
RCREG =收到;
将获取接收的值并将其分配给RCREG,而不应该是
接收= RCREG;
将指定RCREG的值在构建之后接收和工作...