Autor Tema: C18: Configuración USART asíncrona  (Leído 2324 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado Drod

  • PIC16
  • ***
  • Mensajes: 136
C18: Configuración USART asíncrona
« en: 27 de Septiembre de 2011, 20:24:27 »
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:

Código: C
  1. void sio_setup(void)
  2. {
  3.         #define BAUD 19200
  4.         //#define SPBRG_VALUE ((PIC_CLK/(16UL * BAUD) -1))
  5.         #define SPBRG_VALUE ((PIC_CLK/(16UL * BAUD) -1))
  6.         #define BRGH_VALUE 1
  7.  
  8.  
  9.         SPBRG=SPBRG_VALUE;
  10.         SPBRGH=BRGH_VALUE;      //data rate for sending (see 16F876 manual under 'USART')
  11.         BAUDCONbits.BRG16 = 0;
  12.         TXSTAbits.SYNC=0;                                               //asynchronous
  13.         RCSTAbits.SPEN=1;                                               //enable serial port pins
  14.         RCSTAbits.CREN=1;                                               //enable reception
  15.         RCSTAbits.SREN=0;                                               //no effect
  16.         PIE1bits.TXIE=0;                                                //disable tx interrupts
  17.         PIE1bits.RCIE=1;                                                //enable rx interrupts
  18.         RCONbits.IPEN=1;                                                //enable interrupt priority
  19.         IPR1bits.RCIP=1;                                                //make receive interrupt high priority
  20.         INTCONbits.GIEH=1;                                      //enable all high priority interrupts
  21.         TXSTAbits.TX9D=0;                                               //8-bit transmission
  22.         RCSTAbits.RX9=0;                                                //8-bit reception
  23.         TXSTAbits.TXEN=0;                                               //reset transmitter
  24.         TXSTAbits.TXEN=1;                                               //enable the transmitter
  25.         //
  26.         rx_flagsbits.DataAvailable = 0;
  27.         rx_ptr=0;
  28. }

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):

Código: C
  1. void main(void)
  2. {
  3.         int p=0;
  4.         int q=0;
  5.         unsigned char ch;
  6.         //
  7.         OSCCONbits.IRCF0 = 1;
  8.         OSCCONbits.IRCF1 = 1;
  9.         OSCCONbits.IRCF2 = 1;           // 8MHz clock
  10.         ADCON1=255;
  11.         TRISA=0;
  12.         TRISB=0;
  13.         TRISC=0;
  14.         PORTC=0;
  15.         TRISCbits.TRISC7 = 1;
  16.         PORTA=255;
  17.         PORTB=255;
  18.         //
  19.         sio_setup();
  20.  
  21.        
  22.  
  23.         gpib_init();
  24.         while(1)
  25.         sio_puts("Test");
  26. }

Y luego:

Código: C
  1. void sio_puts(unsigned char *s)
  2. {
  3.         while(*s != 0)
  4.         {
  5.                 sio_putch(*s);
  6.                 ++s;
  7.         }
  8. }

que llama a:

Código: C
  1. //writes a character to the serial port
  2. void sio_putch(unsigned char c)
  3. {
  4.         while(!PIR1bits.TXIF)                   //set when register is empty
  5.         {
  6.                 clear_usart_errors_inline();
  7.                 ClrWdt();
  8.         }
  9.         TXREG=c;
  10. }

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.pdf
Os adjunto el proyecto completo del MPLAB para que hecheis un vistazo!

Muchas gracias! Soy nuevo con el C18 y tengo muchas dudas!

« Última modificación: 27 de Septiembre de 2011, 20:55:39 por Drod »

Desconectado Suky

  • Moderador Local
  • DsPIC33
  • *****
  • Mensajes: 6758
Re: C18: Configuración USART asíncrona
« Respuesta #1 en: 27 de Septiembre de 2011, 22:30:41 »
Revisa el ejemplo del siguiente tutorial, puede servir de ayuda  ;-)

http://www.todopic.com.ar/foros/index.php?topic=31611.0

Saludos!
No contesto mensajes privados, las consultas en el foro


 

anything