Hola!
Estoy intentando montar un conversor GPIB-Serial, con un ejemplo de USART asincrona hecho en C18, pero la comunicación no me sale bien.
La verdad es que me estoy volviendo loco, jejeje
En el "main.c" llamo a "sio_setup();" (para configurar la comunicación)
En el "sio_setup" tengo:
void sio_setup(void)
{
#define BAUD 19200
//#define SPBRG_VALUE ((PIC_CLK/(16UL * BAUD) -1))
#define SPBRG_VALUE ((PIC_CLK/(16UL * BAUD) -1))
#define BRGH_VALUE 1
SPBRG=SPBRG_VALUE;
SPBRGH=BRGH_VALUE; //data rate for sending (see 16F876 manual under 'USART')
BAUDCONbits.BRG16 = 0;
TXSTAbits.SYNC=0; //asynchronous
RCSTAbits.SPEN=1; //enable serial port pins
RCSTAbits.CREN=1; //enable reception
RCSTAbits.SREN=0; //no effect
PIE1bits.TXIE=0; //disable tx interrupts
PIE1bits.RCIE=1; //enable rx interrupts
RCONbits.IPEN=1; //enable interrupt priority
IPR1bits.RCIP=1; //make receive interrupt high priority
INTCONbits.GIEH=1; //enable all high priority interrupts
TXSTAbits.TX9D=0; //8-bit transmission
RCSTAbits.RX9=0; //8-bit reception
TXSTAbits.TXEN=0; //reset transmitter
TXSTAbits.TXEN=1; //enable the transmitter
//
rx_flagsbits.DataAvailable = 0;
rx_ptr=0;
}
Luego en el bucle en main tengo (puse el "sio_puts("Test");" antes de todoel resto para testar. Si esto no me sale bien, nada va a salir, jeje):
void main(void)
{
int p=0;
int q=0;
unsigned char ch;
//
OSCCONbits.IRCF0 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF2 = 1; // 8MHz clock
ADCON1=255;
TRISA=0;
TRISB=0;
TRISC=0;
PORTC=0;
TRISCbits.TRISC7 = 1;
PORTA=255;
PORTB=255;
//
sio_setup();
gpib_init();
while(1)
sio_puts("Test");
}
Y luego:
void sio_puts(unsigned char *s)
{
while(*s != 0)
{
sio_putch(*s);
++s;
}
}
que llama a:
//writes a character to the serial port
void sio_putch(unsigned char c)
{
while(!PIR1bits.TXIF) //set when register is empty
{
clear_usart_errors_inline();
ClrWdt();
}
TXREG=c;
}
Pero no me sale bien! Pongo 19200,8,1,N,N, y no me sale "Test" sino que varios caracteres aleatorios...
Estou usando un cristal de 20MHz. Este es el schematico:
http://www.dalton.ax/gpib/gpib_schematic.pdfOs adjunto el proyecto completo del MPLAB para que hecheis un vistazo!
Muchas gracias! Soy nuevo con el C18 y tengo muchas dudas!