gpt4 book ai didi

c - 为什么我的程序不能识别 PORTbits.RCx == 0 条件?

转载 作者:行者123 更新时间:2023-12-05 04:38:30 25 4
gpt4 key购买 nike

void UART_init(void){

ANSELB = 0; //set PORT B to digital port
TRISBbits.TRISB5 = 1; //set RX pin to input
TRISBbits.TRISB7 = 0; //set TX pin as output

SPBRGH = 0;
SPBRGL = 25; //set baud rate to 9600

BRGH = 0;
BRG16 = 0;
SYNC = 0;
SPEN = 1; //enable serial port pins

TX9 = 0; //set 9 bit tranmission
RX9 = 0; //set 9 bit receive

TXEN = 1; //enable transmission
CREN = 1; //enable receiver

}

void UART_write(char data){
while(TRMT == 0);

TXREG = data;
}

void UART_write_string(char *text){
for(int i=0; text[i] != '\0'; i++){
UART_write(text[i]);
}
}

char UART_read(){
while(RCIF == 0);
return RCREG;
}

char URAT_read_string(char *stringprimit, int lungime){
for(int i=0; i < lungime; i++){
stringprimit[i] = UART_read();
}
}

int main(int argc, char** argv) {
OSCCONbits.IRCF = 0b1111; //set operating frequency to 31kHz (0b1111) for 16MHz
//WDTCONbits.WDTPS = 0b01110;
//CLRWDT();
//0b01100; //set WTD interval at 4s

UART_init();

//Activam pull-up
OPTION_REGbits.nWPUEN = 0;
WPUCbits.WPUC2 = 1;
WPUCbits.WPUC6 = 1;
WPUCbits.WPUC7 = 1;
WPUCbits.WPUC0 = 0;
WPUCbits.WPUC1 = 0;
WPUCbits.WPUC3 = 1;


//Led-uri
TRISAbits.TRISA1 = 0; // set as output
TRISAbits.TRISA2 = 0; // set as output

ANSELAbits.ANSA1 = 0; //pin digital
ANSELAbits.ANSA2 = 0; //pin digital

ANSELAbits.ANSA0 = 1; //set to analogic pin
TRISAbits.TRISA0 = 1; //set as input

ANSELCbits.ANSC0 = 0; //set to digital pin
ANSELCbits.ANSC1 = 0; //set to digital pin

TRISCbits.TRISC0 = 0; //set as output
TRISCbits.TRISC1 = 0; //set as output

ANSELCbits.ANSC2 = 0; //set to digital pin
ANSELCbits.ANSC6 = 0; //set to digital pin
ANSELCbits.ANSC7 = 0; //set to digital pin

TRISCbits.TRISC2 = 1; //set as input
TRISCbits.TRISC6 = 1; //set as input
TRISCbits.TRISC7 = 1; //set as input

PORTCbits.RC0 = 1;
PORTCbits.RC1 = 0;

PORTAbits.RA1 = 0;
PORTAbits.RA2 = 1;

char user_input;

UART_write_string("1. rotate right ");
UART_write('\r');
UART_write_string("2. rotate left");
UART_write('\r');
UART_write_string("3. stop");
UART_write('\r');
UART_write_string("4. Deactivate UART");
UART_write('\r');
UART_write_string("select:");



while(1){

//CLRWDT();

user_input = UART_read();




if(PORTCbits.RC7 == 0 ){ //motor rotates right

user_input = '1';
PORTCbits.RC0 = 1;
PORTCbits.RC1 = 0;

PORTAbits.RA1 = 0;
PORTAbits.RA2 = 1;

}

if(PORTCbits.RC6 == 0 ){ //motor rotates left

user_input = '2';
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 1;

PORTAbits.RA1 = 1;
PORTAbits.RA2 = 0;

}

if(PORTCbits.RC2 == 0 ){ //motor stop


user_input = '3';
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 0;

PORTAbits.RA1 = 0;
PORTAbits.RA2 = 0;


}

if(user_input =='1') {//motor rotates right
PORTCbits.RC0 = 1;
PORTCbits.RC1 = 0;

PORTAbits.RA1 = 0;
PORTAbits.RA2 = 1;


}

if( user_input =='2'){ //motor rotates left
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 1;

PORTAbits.RA1 = 1;
PORTAbits.RA2 = 0;


}

if(user_input =='3'){ //motor stop
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 0;

PORTAbits.RA1 = 0;
PORTAbits.RA2 = 0;


}
if(user_input =='4'){ //deactivate UART
CREN = 0;
}



}



return (EXIT_SUCCESS);
}

在这个项目中,我尝试用两种方法控制直流电机,第一种方法是使用用户输入的 UART,第二种方法是使用按钮。范围:当我从键盘上按下数字 1 时,我希望电机向右旋转,2- 向左,3- 停止。这工作正常,但问题出在按钮上,当我按下 RC7 按钮时没有任何反应。RC7 - 表示电机向右旋转,RC6 - 表示电机向左旋转,RC2 - 表示电机停止据我所知,我的程序甚至看不到这些条件,例如 PORTbits.RC7 == 0。

最佳答案

从代码中,我可以看出函数:

char UART_read(){
while(RCIF == 0);
return RCREG;
}

阻塞 while(RCIF == 0);

我认为这就是您的按钮不工作的原因,因为在 while(1) 的最开始您调用了 UART_read();

您自己提到 UART 数据的输出符合预期,因此这回答了您的问题。

要使其正常工作,您必须编写一个 UART Rx 中断,这样您的程序就不会阻塞/等待 UART 接收数据。

编辑:
根据您的评论,您正在使用 CREN 禁用 UART 接收,那么您还应该在读取 Rx 数据寄存器之前检查接收器是否已启用,您可以修改 UART 接收功能,如下所示:

char UART_read(){

if(CREN == 0) {
return '\0'; //Return NULL Character
}
while(RCIF == 0);
return RCREG;
}

关于c - 为什么我的程序不能识别 PORTbits.RCx == 0 条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70589971/

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