Menu

RS-232 comm problems

Help
2009-02-25
2013-05-30
  • Edward LaBudde

    Edward LaBudde - 2009-02-25

    Hi all, newbie here.  I am trying out the serial comm stuff.  Using the templates for send and receive from the help files, I set up one PIC to send another to receive.  I can see (led) that the send unit is outputting data every 100ms as it should (also checked with ‘oscope).  I get no indication of input data from the receiver (led).  I am baffled as this is right out of the help files.  Can anyone see the problem?

    Here is the send code:
    'A testing rs-232 send

    'Chip model
    #chip 16F88, 8
    #config INTRC_IO

    #define SendAHigh Set PORTB.2 on
    #define SendALow Set PORTB.2  off
       
        Dir PORTB.2 Out    'pin 8
       
    InitSer 1, r2400, 1+WaitForStart, 8, 1, none, normal
           
        Do
         SerSend 1, 17
         wait 100 ms
         sersend 1, 25         
         wait 100 ms
         'serprint 1, "High Billie"
        'Wait 100 ms
       Loop

    Here is the Rec code:
    'A testing rs-232 recieve

    'Chip model
    #chip 16F88, 8
    #config INTRC_IO

    #define RecAHigh PORTB.2 ON
    #define RecALow PORTB.2 OFF
    #define LED PORTB.0
       
        Dir PORTB.0 Out 'PIN 6
        Dir PORTB.2 In    'PIN 8
       
        InitSer 1, r2400, 1+WaitForStart, 8, 1, none, normal 'Same as send
        Do
            SerReceive 1, Temp
            If Temp <= 20 Then Set LED Off  'Sending 17
            If Temp > 20 Then Set LED On    'Sending 25
        Loop

     
    • kent_twt4

      kent_twt4 - 2009-02-25

      Ed, a long time ago, I got the SerSend, SerReceive routines to work with the 16f88, using a 20Mhz crystal at 9600 baud.

      Presumably in the code, Temp is reading a Pot or something.  So, Temp = ReadAD(AN0), need to define Temp, and make and input.

      Do a forum search, you will see numerous examples, explanations on getting your serial comms going.  In a nutshell, if you want rock solid serial, use a crystal, along with the hardware serial routine explained in the contributors section.  There is also another alternate software routine there too.

      When outputing to a serial device, including an LCD, binary numbers need to be changed to ASCII numbers.  In the LCD routines, this is done automatically in the lcd.h file .  Any code relating to converting from binary to ascii, and receiving back, ascii to binary, will employ some form of the lcd.h routines.

      Like many things, getting your serial LCD going, or hyperterminal for that matter, will be a journey.

       
    • Edward LaBudde

      Edward LaBudde - 2009-02-25

      Kent thanks, for the tip. Let you know about the crystals, but my scope says the int osc is within 1%, will check it with a freq meter tomorrow.  Regards, Ed.

       
    • Edward LaBudde

      Edward LaBudde - 2009-02-26

      Kent, I put in 20 MHz resonators.  They are within .05% of each other.

      Have verified that the transmitted signal is correct to the RS-232 protocal, with regards to high/low postions and times.  Have verified that the receiver chip is getting the same exact sequence of High/low.

      The receiver decodes any command input to the value of 255, regardless of the input!

      I am at a loss!  Am also out of operation as my programmer is on the fritz!

      Any Ideas?  Ed
      Transmitter code:
      'Chip model
      #chip 16F88, 20
      #define SendAHigh Set PORTB.2 on
      #define SendALow Set PORTB.2  off
         
          Dir PORTB.2 Out    'pin 8
         
          Set portB.2 on        ‘Verify output is working
          Wait 100 ms
          set portB.2 off
         
          InitSer 1, r2400, 1+WaitForStart, 8, 1, none, normal
             
          Do
            SerSend 1, 175
           Wait 100 ms
         Loop

      Receiver code:
      'Chip model
      #chip 16F88, 20

      #define RecAHigh PORTB.2 ON
      #define RecALow PORTB.2 OFF
      #define LED PORTB.0
         
          Dir PORTB.0 Out 'PIN 6
          Dir PORTB.2 In    'PIN 8

          test = 255    ‘test value

        Set Led on    ‘ check port operation
        wait 200 ms
        set led off
       
          InitSer 1, r2400, 1+WaitForStart, 8, 1, none, normal 'Same as send

         Do
          SerReceive 1, Temp
          If Temp = test Then Set LED on  'Check value
         Loop

       
    • kent_twt4

      kent_twt4 - 2009-02-27

      Ed, I would put the LCD back on and just concentrate on the sending a value out first.  Try the 9600 baud setting if your LCD supports it.  If that doesn't work, then the SerSend and SerReceive routines are broke.

      Instead, use the hardware usart routine in contributors section.  The alternate software routine also works, but may need small adjustment from the ideal buadrate delay.  Examples for both are given.

       
    • Edward LaBudde

      Edward LaBudde - 2009-02-27

      Kent, Thanks for the reply.  Are you suggesting that there may be a problem with rs-232 routines?  Is that why they are on Hugh's todo list? 

      If my routines are broke that means everyones must be too.  Santigo says he got his working!

      As near as I can tell, sersend is working ok, as far as the bit stream.

      I will do more testing when I figure out what ia going on with my programmer, may be this weekend. 

      Regards, Ed.

       
    • Santiago

      Santiago - 2009-02-27

      Hi ED.

      I used the HARDWARE routines, you can find them in the contributors forum.

      If your PIC has Usart harware.. why using software routines?

       
    • Edward LaBudde

      Edward LaBudde - 2009-02-27

      Santiago, I do not know much about programming, I do not know how to use the hardware routine you mention.  Do you use it as an include file? 

      You will have to give me some guidance.  Your help is much appreciated.  Regards, Ed.

       
    • Santiago

      Santiago - 2009-02-28

      Sorry Ed...

      For what i know GCBasic has a software-based serial library, where you can use any pin to transmitt or receive data.
      Depending on the PIC there is a built-in hardware for serial comunications ussually called Usart, you can have a look to the datasheet when you want to know how it works: Addressable Universal Synchronous Asynchronous Receiver Transmitter (AUSART).

      Anyway you don't need to read it... all what you have to know is that in pic16f88 the pins involved are:

      RB5 for transmission
      RB2 for reception

      Then you have to set the direction of RB5 as output and RB2 as input.

      In order to you don't have to configure every register needed to have Usart running, there is a library of functions that do it for you. I used the one that is posted in this thread: https://sourceforge.net/forum/forum.php?thread_id=2487534&forum_id=629990
      With the modification suggested in the second post of the thread.

      It works for me in a pic16f876a, i think it should also work for pic16f88.

      You have to use it as an #include
      For using it you need to define baudrate, init usart and send/receive data, example:

      #chip 16F88, 8
      #config INTRC_IO

      Dir PORTB.5 Out 'only needed if you want transmitt
      Dir PORTB.2 In 'only needed if you want receive

      #define baudrate 9600
      InitUSART

      'Main loop:
      do

      'Send a byte
      my_var = 145
      HSerSend  my_var

      'Receive a byte if avialable, otherwise return zero
      received_data = HSerReceive

      'Send a string:
      HserPrint "my message"

      loop
      ______________________________________________________________________

      As you see, defining baudrate and InitUSART just need to be done at the starting of the program, unless you want to change configuration.

      There are also  functions to send data as ascii code and some others.

      The PIC hardware does not provide RS-232 compatible voltage levels, then for comunication with a RS-232 line (PC serial port) you need a max232 or any other RS-232/TTL adapter, the adaptor circuit is quite simple, just an max232 and some capacitors and resistors.
      For PIC to PIC just using some resistor to avoid problems with possible output-output wiring.

      Keep enjoying...

       
    • Edward LaBudde

      Edward LaBudde - 2009-02-28

      Santiago, thank you so much.  I will try this out Sunday if I can get my programmer working again.  I will let you know, Ed.

       
    • mmotte

      mmotte - 2009-03-01

      I looked at the "leaner version" of Hardware usart.

      Is there a problem in the receive?

      'Receive a byte if avialable, otherwise return zero
      '
      Function HSerReceive
      HSerReceive = 0
      If RCIF Off Then HSerReceive = RCREG 
      End Function

      The RCIF is set to 1 when the buffer is full, a byte is ready
      Reading the RCREG resets the flag.

      So should the If check  off???  or on??

      I haven't tried it so i may be all wet!

      mike

       
    • Nobody/Anonymous

      Mike i think you are right, it should be:

      If RCIF On Then HSerReceive = RCREG

      I didn't try this function as i use interrupts to forget testing RCIF flag all the time...

      You will be totally sure when you try it... but in my opinion it looks very clear that there is an error in that function.
      I think you should report it in the related thread if you test it.

      Ed:
      Take this in account when you try the receive function.

       
    • Edward LaBudde

      Edward LaBudde - 2009-03-01

      Hi all, newbie here.  I have had success with sending a signal between two chips! 

      The problem was a corrupt Crimson editor file for the receive chip!  This was the cause of my programmer crashing as well!!!

      I made a new version and all the problems went away (for now). 

      I am still not able to get the LCD to work right though?

      I tried the Hardware usart but it did not compile.  The errors are:

      compiled.asm:43:Error [103] syntax error
      compiled.asm:48:Error [128] Missing argument(s).

      For the line : InitUSART

      Santiago, any clues?   I am sure I will have more questions as I get into this more!  Regards, Ed.

      Here is the code:
      ‘A testing hardware rs-232 send

      'Chip model
      #chip 16F88, 20
      #include "H_WUsart.h"

      Dir PORTB.5 Out 'pin 11 only needed if you want transmitt
      'Dir PORTB.2 In 'pin 8 only needed if you want receive

      #define baudrate 2400
      InitUSART

      'Main loop:
      do

      'Send a byte 
      my_var = 75
      HSerSend my_var
      wait 100 ms

      'Receive a byte if avialable, otherwise return zero 
      'received_data = HSerReceive 

      'Send a string:
      'HserPrint "my message"

      loop

       

       
    • Santiago

      Santiago - 2009-03-02

      Hi again...

      Ed,  i think that's a problem of a very low baudrate (2400) with a very fast clock (20MHz)
      Try baudrate 9600, or lower clock  if you need that low speeds.

       
    • Edward LaBudde

      Edward LaBudde - 2009-03-02

      Santiago, thanks for the response, but it did not change the error message on "initusart."  

      What is the benefit of using Hardware over software for rs-232?  Regards, Ed.

       
    • Santiago

      Santiago - 2009-03-03

      For me hardware functions compiles ok with:

      #chip 16F88, 20
      #define baudrate 9600

      with baudrate 2400 i have an error, but is different than the error you have.

      There is a thing i didn't notice, i think for reaching archives in "include" folder you should do this way (only for files in "include" folder):
      #include <H_WUsart.h>

      Or for paths relative to GCBASIC folder that can be in any folder ( i think in windows is this way):
      #include "include\H_WUsart.h"

      In LInux:
      #include "include/H_WUsart.h"

      This mean that for example you can create a "custom" folder and place there files.h, then you can include this way:
      #include "custom\files.h"

      The hardware does the work of sending data ifself, then the processor is free to do other things, the software functions keep the processor working and are perhaps less precises and more errors are possible. Also the generated code is larger using software usart.

       
    • Edward LaBudde

      Edward LaBudde - 2009-03-03

      Santiago, Thanks, for the reply.  I use #include "H_WUsart.h"  as I have it in a different folder.  I will have to do more work on this, maybe tomorrow.  Regards, Ed.

       
    • Nobody/Anonymous

      If you have tH_WUsart.h out of the GCGASIC folder then i think you should use an absolute path, something like:

      #include "C:\Path\to\the\folder\H_WUsart.h"

      I think the general rules for #include directive are something like this.:
      For files situated in "include" folder (the one inside GCBASIC folder):
      #include <file.h>

      For files in other folder but inside GCBASIC folder:
      #include "other_folder\file.h"

      For files in any other folder (outside GCBASIC folder):
      #include "C:\Path\to\the\folder\file.h"

      Regards, Santiago.

       
    • Edward LaBudde

      Edward LaBudde - 2009-03-04

      Santiago, Thanks, I will try your suggestions.  Ed.

       
    • Edward LaBudde

      Edward LaBudde - 2009-03-12

      Hi all, newbie here. 

      Wanted to report that I have the hardware usart working.  My compiler (gputil) did not like "initusart"  had to change the name to "InitializeUSART " for it to work.

      Santiago, the #define baudrate command did not work, had to edit the .h file.  I Set BRGH off and used #define SPBRG_Val 129 for 2400 buad at 20MHz.

      Also ordered a different LCD, will have it soon.

      Best regards,  Ed.

       
    • Nobody/Anonymous

      Hi Ed.

      For the #define baudrate to work yo have to edit the .h file and do the modification suggested in the second post of the thread:

      https://sourceforge.net/forum/forum.php?thread_id=2487534&forum_id=629990

      ___________________________________________________________________________________

      I just used this:
      #define SPBRG_Val (ChipMHz*1000000/baudrate-16)/16

      Instead of:
      #define SPBRG_Val 8 '57600 Baud @8Mhz 
      ___________________________________________________________________________________

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.