using USART to help debug state of variables

Help
ssylee
2007-10-28
2013-03-12
  • ssylee

    ssylee - 2007-10-28

    Does anyone know how easy/hard it is to use USART to send back data to my computer about a PIC microcontroller's variable states, for example to the terminal?

     
    • zabooh

      zabooh - 2007-10-28

      On which PIC especially?
      Which baud rate?
      Continuous data streaming or sporadic byte sending?
      Receiving at the same time (full duplex)?
      What oscillator mode (internal RC or external clock/crystal)?
      At which core clock frequency?
      How many data per second?
      How many data at all?

       
    • kein0r

      kein0r - 2007-10-28

      Hi

      Its damn simple:

      just do
      #include <usart.h>
      later in your code open the usart with:

      usart_open( USART_TX_INT_OFF & USART_RX_INT_OFF & USART_BRGH_HIGH & USART_CONT_RX & USART_EIGHT_BIT & USART_ASYNCH_MODE, 51);
      stdout = STREAM_USART;
      stdin = STREAM_USART;

      Where the 51 is the divider for the baudrate (in this case its 115200 with a pic running at 48MHz). The stdout/stdin thing is needed to tell sdcc libs where to send the data to.
      Finaly you need to somehow overwrite the putchar function to adapt it to your chip. For a pic this is:
      void putchar(char c)
      {
              // Wait for TXREG to be ready
              while(!PIR1bits.TXIF);

              // Place char in TXREG - this starts transmition
              TXREG = c;
      }

      Now you can use normal printf and watch the output with minicom etc. BE carefull to have at least 0x64 stack when using printf and of course to use a crossed cable.

      If you want to read from the usart use
      if (usart_drdy()) {
            usart_gets(my_string, my_length);
      }

       
    • ssylee

      ssylee - 2007-10-31

      Didn't expect to get a reply that quickly, so many thanks for the helpful replies. I'm trying to verify the readings of the a/d converter. I'm running on a PIC18F2620 on an internal oscillator at 8 MHz I believe. The sampling frequency is calculated somehow by one of the sdcc enthusiasts to be 10 kHz. I haven't connected a circuit yet that would allow me to send data for USART yet, but I think presumably it's RS-232 and minicom display.

       

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks