using USART to help debug state of variables

  • 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


      Its damn simple:

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

      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

              // 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