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。