我最近在微控制器中介绍了 adc 和 lcd 并进入了 USART 和往常一样,我制作的第一个代码不起作用,我需要一些帮助来发现问题,这里是代码:
发射器代码:
主.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;
}
}
}
收货人代码:
主.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 是一个头文件,我在其中设置晶体振荡器的频率和配置位,因此这两个项目的文件/代码相同
配置.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,但是当我将它连接到 isis 接收器的 TX 引脚每半秒不断闪烁高低(红色和蓝色),但是当我按下按钮时,高(红色)信号会保持更长的时间,但会一直闪烁高低和我假设这是用于启动位但接收器没有打开 LED 那是错误部分我不知道这里出了什么问题