我正在尝试实现PC(linux)与用于rs232的PIC18F4620的通信。传输(PIC -> 到 PC 很好)。但是,当我尝试将某些东西从 PC 传递到 PIC 时,PIR1bits.RCIF 标志永远不会设置,并且不会发生中断。
那是我的(PIC18f)代码(一个版本):
/*
* sdcc --use-non-free -mpic16 -p18f4620 test_lcd_serial.c
* stty -F /dev/ttyUSB0 9600
*/
#include <pic18fregs.h>
#include <pic18f4620.h>
#include <delay.h>
#include <stdio.h>
#include <usart.h>
#include "lcd.c"
//__code char __at 0x300000 _conf0 = 0x00;
//~ __code char __at __CONFIG1H _conf1 = 0x06;// oscilador HS-PLL
__code char __at __CONFIG1H _conf1 = 0x02;// oscilador HS
__code char __at __CONFIG2L _conf2 = 0x1e;
__code char __at __CONFIG2H _conf3 = 0x1e;
__code char __at 0x300004 _conf4 = 0x00;
__code char __at __CONFIG3H _conf5 = 0x01;
__code char __at __CONFIG4L _conf6 = 0x81;
__code char __at 0x300007 _conf7 = 0x00;
__code char __at __CONFIG5L _conf8 = 0x0f;
__code char __at __CONFIG5H _conf9 = 0xc0;
__code char __at __CONFIG6L _confA = 0x0f;
__code char __at __CONFIG6H _confB = 0xe0;
__code char __at __CONFIG7L _confC = 0x0f;
__code char __at __CONFIG7H _confD = 0x40;
char buf[255];
void main(void)
{
unsigned char count;
unsigned char caracter;
lcd_init( FOURBIT_MODE );
lcd_home();lcd_puts("Start Test"); delay_s(1);
lcd_home();lcd_puts("Start Test."); delay_s(1);
lcd_home();lcd_puts("Start Test.."); delay_s(1);
lcd_home();lcd_puts("Start Test..."); delay_s(1);
INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;
usart_open(
USART_TX_INT_OFF & //0x7f
USART_RX_INT_ON & //0xff
USART_BRGH_HIGH & //0xff
USART_EIGHT_BIT & // 0xfd
USART_ASYNCH_MODE, //0xfe
129 // 9600 at 20MHz
);
stdout = STREAM_USART;
count = 1;
while(1){ //This works fine :)
printf("To PC: %i\r", count);
sprintf( buf, "Sent: %i", count );
//even if I print (in LCD) the PIR1bits.RCIF is always 0
// sprintf( buf, "Sent: %i,%i", count, PIR1bits.RCIF );
lcd_home();lcd_puts( " " );
lcd_home();lcd_puts( buf );
delay_ms(500);
count++;
}
}
void printrx(){
unsigned char pnt = 0;
while(PIR1bits.RCIF){
buf[ pnt++ ] = RCREG;
// clear CREN
// RCSTAbits.CREN = 0;
// RCSTAbits.CREN = 1;
}
if( pnt ){
sprintf( buf, "GOT1 %s", buf );
lcd_home();lcd_puts( " " );
lcd_home();lcd_puts( buf );
delay_ms(1000);
}
}
void receive_intr1() interrupt 1 { //this is never call
if( PIR1bits.RCIF ) // serial received
printrx();
}
void receive_intr2() interrupt 2 {
if( PIR1bits.RCIF ) // serial received
printrx();
}
在 PC 中我做了:
>cat /dev/ttyUSB
To PC: 1
To PC: 2
To PC: 3
To PC: 4
To PC: 5
To PC: 6
To PC: 7
To PC: 8
To PC: 9
To PC: 10
...
所以,PIC到PC是可以的。但是当我尝试时:
回声“某事”> /dev/ttyUSB0
或者
cat > /dev/ttyUSB0
something
或者即使我编写代码将 /dev/ttyUSB0 作为文件打开并写入,使用 python 串行模块,也没有任何反应。