Menu

serial comm PIC to PC

Help
Scotty1226
2007-12-30
2013-05-30
  • Scotty1226

    Scotty1226 - 2007-12-30

    I have been trying to output simple numerical data from a PIC (16F716 or 16F628) using SerSend on a development board with RS232.  I am using PORTB.2 to output the data to the TxD pin of the MAX232 IC.  On the PC I am using the Hyperterminal set up to match InitSer.  In all cases I can receive the correct number of "characters" (e.g. if I program to send the number 5, 20 times, it sends 20 of "something", often interesting looking, but not the number 5.)  Have tried different baud rates with same result.  Maybe someone has some simple snippets of generic GCBASIC code to do this seemingly simple task.

     
    • kent_twt4

      kent_twt4 - 2007-12-30

      Are you changing your decimal to ascii prior to sending?(i.e. 5+48=53 in ascii).

       
    • Scotty1226

      Scotty1226 - 2007-12-30

      Yes, I did that from the very start. I do get "characters" like a music note symbol, happy faces, etc - very entertaining.  Here is an example of a prgm that is typical of what I have tried:  (any recommendations will be appreciated, thanks) 

      ' 16F716 PIC operating on a DH Micro R18XLi prototype board
      ' RB2 connected to TxD

      'Chip model
      #chip 16F716, 4

      #define SendAHigh SET PORTB.2 ON 
      #define SendALow SET PORTB.2 OFF 

      dir PORTB.2 out                        'serial out

      Temp = 53
      InitSer (1, r1200, 1, 8, 1, none, normal) 

      Main: 
      For cntr = 1 to 20

      SerSend (1, Temp)

      Wait 2 sec
      Next
        
      end

       
    • Nobody/Anonymous

      It could be a slight variation in transmit speed, you could try adjusting the delay in the rs232.h
      Or...

      Use Steve Bell's or  the hardware usart library.
      Or...

      Here is a port of Nigel Goodwin's (http://www.winpicprog.co.uk/pic_tutorial.htm) software serial routine along with GCBasics ascii routine (used here for byte sized variables).

      'Chip model
      #chip 18f4620,20
      ;#config MCLRE=ON, OSC=HS, LVP=OFF

      #define Led PortD.1
      dir PortD.1 out
      ;For the Olimex Pic-40 LED to work PCFG3:0 must be set to "011X" in the assembler file
      ;#define Led PortA.0
      ;dir PortA.0 out
      Ser_Init  ;Verify Port Pin choice

      Main:
      ;if Button On then
        Set Led Off
        Xmit_RS232 55
      ;End if
      wait 1 s
      ;if Button Off then
        cereal = 255
        ascii cereal
        Set Led On
      ;End if
      wait 1 s
      goto Main

      ;*************SUBS******************

      Sub Ser_Init
      ;104us is the us delay for 9600 baud rate
      #define baud 103  ;May have to adjust here depending on the osc and baud used
      ;also using a variable for us delay only works on higher speed osc(8 & 20mhz works)
      #define SerTxHigh Set PortB.2 On
      #define SerTxLow Set PortB.2 Off
      dir PortB.2 out
      SerTxHigh  ;Initial RS232 idle state
      end sub

      Sub XMIT_RS232(Xmit_Byte)#NR
        SerTxLow        ;Start bit
        wait baud us
      For cntr = 1 to 8
        Rotate Xmit_Byte Right
        If Status.C ON Then SerTxHigh
        If Status.C Off Then SerTxLow
        wait baud us
      Next
        SerTxHigh        ;Stop bit
        wait baud us
      end sub

      ;Sub Rcv_RS232
      ;Rcv_Wait:
      ;  BTFSC   PORTB, 7              ;wait for start bit
      ;  GOTO    Rcv_RS232
      ;  CALL    Start_Delay              ;do half bit time delay
      ;  BTFSC   PORTB, 7              ;check still in start bit
      ;  GOTO    Rcv_Wait
      ;  MOVLW   0x08                  ;set up to read 8 bits
      ;  MOVWF   Bit_Cntr
      ;  CLRF    Rcv_Byte
      ;Next_RcvBit:
      ;  CALL    Bit_Delay
      ;  BTFSS   PORTB, 7
      ;  BCF     STATUS    , C
      ;  BTFSC   PORTB, 7
      ;  BSF     STATUS    , C
      ;  RRF     Rcv_Byte  , f
      ;  DECFSZ  Bit_Cntr  , f         ;test if all done
      ;  GOTO    Next_RcvBit
      ;  CALL    Bit_Delay
      ;  MOVF    Rcv_Byte, W
      ;  RETURN

      ;Sub Start_Delay
      ;  MOVLW   0x0C
      ;  MOVWF   Delay_Count
      ;Start_Wait:
      ;  NOP
      ;  DECFSZ  Delay_Count , f
      ;  GOTO    Start_Wait
      ;  RETURN

      sub ascii(LCDValue)#NR 
      ;SERDECMIL = 0
      ;SERMIL = 0
      SERCEN = 0
      SERDEC = 0
      SERUN = 0
      LCDValueTemp = 0
      ;IF LCDValue >= 10000 then
      ; LCDValueTemp = LCDValue / 10000
      ; SERDECMIL = LCDValueTemp + 48
      ; SerSend (1,SERDECMIL)
      ; LCDValue = LCDValue - LCDValueTemp * 10000
      ;End if
      ;If LCDValue >= 1000 Then
      ; LCDValueTemp = LCDValue / 1000
      ; SERMIL = LCDValueTemp + 48
      ; SerSend (1,SERMIL)
      ; LCDValue = LCDValue - LCDValueTemp * 1000
      ;End if
      ;IF LCDValueTemp > 0 OR LCDValue >= 100 then 
      If LCDValue >= 100 Then
      LCDValueTemp = LCDValue / 100
      SERCEN = LCDValueTemp + 48
      XMIT_RS232 SERCEN
      LCDValue = LCDValue - LCDValueTemp * 100
      end if
      IF LCDValueTemp > 0 OR LCDValue >= 10 then
      LCDValueTemp = LCDValue / 10
      SERDEC = LCDValueTemp + 48
      XMIT_RS232 SERDEC
      LCDValue = LCDValue - LCDValueTemp * 10
      end if
      SERUN = LCDValue + 48
      XMIT_RS232 SERUN
      end sub 

       

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.