+ Reply to Thread
Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2010
    Posts
    50

    Default SERIN Not Working, SEROUT Working

    Hi everyone (any help would be greatly appreciated),

    I have this line of code in my program to read a serial input from my pc:

    SERIN rcx,N2400,[t11,t12,t13,t14,t21,t22,t23,t24,a11,a12,a13,a14,a2 1,a22,a23,a24,m11,m12,m13,m14,m21,m22,m23,m24,mb1]

    I set the RTS line on my pc to enabled and read it on the pic end (PIC18F4520). Once I detect that the RTS line has gone High, I use the pic to set the CTS line high. Once my pc detects that the CTS line has gone high the message is sent to the pic.
    All of the variables t11-mb1 are defined as bytes (t11 VAR BYTE).
    The inclusion of the serin command above reads in what appears to be all zeros. For trouble shooting I added the following line to send the data back to my pc:

    SEROUT tcx,N2400,[t11,t12,t13,t14,t21,t22,t23,t24,a11,a12,a13,a14,a2 1,a22,a23,a24,m11,m12,m13,m14,m21,m22,m23,m24,mb1]

    I also added some additional SEROUT commands to tell me what in the program was executing.

    The SEROUT commands are sucessfully displayed on my pc, but the values for my variables are all zero (not what i sent).

    I am perplexed? Any ideas?

    Thanks.

  • #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    It's hard to tell with only 1 line of the code.
    But I'm guessing you just need to add ...

    ADCON1 = $0F
    CMCON = 7


    hth,
    DT

  • #3
    Join Date
    Sep 2010
    Posts
    50

    Default SERIN Problem Code

    Thanks for taking a look. Looks like all the indentations are lost when posting.

    I think it took care of the ADCs and the comparators. Here's the upfront part of the code, including the problem SERIN, if I comment out the SERIN statement everything works using the temporary variables I set at the beginning. If I un-comment the SERIN statement, the program just freezes at the SERIN statement???
    Code:
     
    @ CONFIG OSC = INTIO67 'config for internal osc
    @ CONFIG PBADEN = ON ' turn off port b analog inputs
    @ CONFIG MCLRE = ON 'activate master clear
    @ CONFIG WDT = OFF 'turn off watchdog timer
    @ CONFIG STVREN = ON 'turn off reset on overflow/underflow
    @ CONFIG LVP = OFF 'turn off low voltage programming
    @ CONFIG CP0 = OFF 
    @ CONFIG CP1 = OFF 
    @ CONFIG CP2 = OFF
    @ CONFIG CPB = OFF
    @ CONFIG WRT0 = OFF 
    @ CONFIG WRT1 = OFF 
    @ CONFIG WRT2 = OFF 
    @ CONFIG WRT3 = OFF 
    @ CONFIG WRTB = OFF 
    @ CONFIG WRTC = OFF 
     
     
     
    Include "modedefs.bas"
    OSCCON = %01100000 'set internal oscillator to 4 mhz and select it as system clock
    ADCON1 = %00001111 'turn off adcs set to digital i/o
    ADCON0 = %00000000
    CCP1CON = %00000000
    CCP2CON = %00000000
    TRISA = %00000000 'set port A to all outputs
    TRISB = %10100000 'set port B I/O directions
    TRISC = %00000000 'set port C to all outputs
    TRISD = %00000000 'set port D to all outputs
    TRISE = %00000000 'set port E to all outputs
    PORTA = %11111111 'set all outputs on port a to 0
    PORTB = %00011111 'set outputs on port b to 0, except leds on 4&5
    PORTC = %11111111
    PORTD = %00000000 'set all outputs on port d to 0
    PORTE = %00000000 'set all outputs on port e to 0
    WDTCON = %00000000 'turn off wdt
    INTCON = %00000000
    INTCON2= %00000000
    INTCON3= %00000000
    CMCON = 7 
     
     
    while OSCCON.2=0
    'wait for the oscillator stable bit to be set
    pause 1
    WEND 
     
     
    led_run var PORTB.0 'led indicates transmission status, blink=transmit
    led_pwr VAR PORTB.3 'led indicates power on status, on=powered
    i var word 'used for loop counts
    Z VAR BYTE
    rcx var PORTB.7 'rs-232 input on portc.7
    tcx var PORTB.6 'rs-232 output 
    rts var PORTB.5 'read rts signal from simulation
    cts var PORTB.4 'set cts signal to simulation
    newdata var byte 'flag to indicate new data
    t11 var byte 'tac 1 first input character
    t12 var byte 'tac 1 second input character
    t13 var byte 'tac 1 third input character
    t14 var byte 'tac 1 fourth input character
    tac1HW var word 'tac 1 high word
    tac1HP var byte 'tac 1 high pointer
    tac1LW var byte 'tac 1 low word (byte)
    tac1LP var byte 'tac 1 low pointer
    tac1tuned var bit
    t21 var byte
    t22 var byte
    t23 var byte
    t24 var byte
    tac2HW var word
    tac2HP var byte
    tac2LW var byte
    tac2LP var byte
    tac2tuned var bit
    a11 var byte
    a12 var byte
    a13 var byte
    a14 var byte
    adf1HW var word
    adf1HP var byte
    adf1LW var byte
    adf1LP var byte
    adf1tuned var bit
    a21 var byte
    a22 var byte
    a23 var byte
    a24 var byte
    adf2HW var word
    adf2HP var byte
    adf2LW var byte
    adf2LP var byte
    adf2tuned var bit
    m11 var byte
    m12 var byte
    m13 var byte
    m14 var byte
    mmr1HW var word
    mmr1HP var byte
    mmr1LW var byte
    mmr1LP var byte
    mmr1tuned var bit
    m21 var byte
    m22 var byte
    m23 var byte
    m24 var byte
    mmr2HW var word
    mmr2HP var byte
    mmr2LW var byte
    mmr2LP var byte
    mmr2tuned var bit
    radioout var byte[75] 'radios output array
    cnt var byte 'counter
    mb1 var byte 'marker beacon input character
    mbout var word[50] 'marker beacon output array
    mbcnt var word 'counter for marker beacon array build
    tmpstr var byte 'temp var to store morse code
    tmplen var byte 'temp var to hold length of morse code
    passval var byte 'value passed during morse code generation
    tmpword var word
    tmpbyte var byte
    framelen var byte
     
    'temporary set values to test building outputs
    A11=64'83 'S
    A12=64'79 'O
    A13=64'83 'S
    A14=64'48 '0
    A21=68 'D
    A22=66 'B
    A23=78 'N
    A24=64 '@
    t11=83 'S
    t12=79 'O
    T13=83 'S
    T14=83 '64 '@
    t21=75 'K
    t22=87 'W
    T23=82 'R
    T24=66 'B
    M11=75 'K
    M12=65 'A
    M13=84 'T
    M14=76 'L
    M21=75 'K
    M22=76 'L
    M23=65 'A
    M24=88 'X
    MB1=50 '2=MIDDLE
     
     
     
    FOR I=1 TO 5
    TOGGLE led_pwr 'setting pin low turns on led
    PAUSE 250
    led_run=0
    NEXT I
    framelen=75
    CTS=0
    newdata=0 'set flag to get new data when starting
    audio_loop:
     
    if NEWDATA=1 then 'if request to send is high then set clear to send and get data
    SEROUT tcx,n2400,["In new data if statement"]
    tac1tuned=1
    tac2tuned=1
    adf1tuned=1
    adf2tuned=1
    mmr1tuned=1
    mmr2tuned=1
    led_run=0
    LED_PWR=1
    if t11=64 and t12=64 and t13=64 and t14=64 then tac1tuned=0 'all @ signs
    if t21=64 and t22=64 and t23=64 and t24=64 then tac2tuned=0
    if a11=64 and a12=64 and a13=64 and a14=64 then adf1tuned=0
    if a21=64 and a22=64 and a23=64 and a24=64 then adf2tuned=0
    if m11=64 and m12=64 and m13=64 and m14=64 then mmr1tuned=0
    if m21=64 and m22=64 and m23=64 and m24=64 then mmr2tuned=0 
    led_run=1
    for i=0 to framelen-1
    radioout(i)=0
    next i
    tac1hw=0
    tac1hp=16
    tac1lw=0
    tac1lp=8
    tac2hw=0
    tac2hp=16
    tac2lw=0
    tac2lp=8
    adf1hw=0
    adf1hp=16
    adf1lw=0
    adf1lp=8
    adf2hw=0
    adf2hp=16
    adf2lw=0
    adf2lp=8
    mmr1hw=0
    mmr1hp=16
    mmr1lw=0
    mmr1lp=8
    mmr2hw=0
    mmr2hp=16
    mmr2lw=0
    mmr2lp=8
    if mb1<>48 then 'if marker beacon is not set to zero=off 
    gosub buildmb
    else
    for i=0 to 49
    mbout(i)=1
    next i
    endif
    gosub buildr
    SEROUT tcx,n2400,["Just did buildr"]
    gosub buildo
    SEROUT tcx,n2400,["Just did buildo"]
    NEWDATA=2
    SEROUT tcx,n2400,[#newdata]
    endif
    'start outputting 
    IF NEWDATA=2 THEN
    PORTA=radioout(cnt)
    PORTC=mbout(mbcnt)
    pause 100
    cnt=cnt+1
    mbcnt=mbcnt+1
    if mbcnt>49 then
    mbcnt=0
    endif
    if cnt=framelen then
    cnt=0
    endif
    ENDIF 
    if rts=1 then
    led_run=0
    cts=1 
    serin rcx,N2400,[t11,t12,t13,t14,t21,t22,t23,t24,a11,a12,a13,a14,a21,a22,a23,a24,m11,m12,m13,m14,m21,m22,m23,m24,mb1]
    pause 250
    SEROUT tcx,n2400,["Received: "]
    pause 1000
    serout tcx,N2400,[t11,t12,t13,t14,t21,t22,t23,t24,a11,a12,a13,a14,a21,a22,a23,a24,m11,m12,m13,m14,m21,m22,m23,m24,mb1] 
    newdata=1
    cts=0
    toggle LED_run
    endif
    if newdata=1 then
    SEROUT tcx,n2400,["Got out of the if statement"]
    endif
    toggle led_run
    goto audio_loop
    Last edited by ScaleRobotics; - 25th October 2010 at 19:32. Reason: added code tags [_code] and [_/code] without the "_"

  • #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,926

    Default

    Try removing the brackets
    serin rcx,N2400,t11,t12,t13,t14,t21,t22,t23,t24,a11,a12, a13,a14,a21,a22,a23,a24,m11,m12,m13,m14,m21,m22,m2 3,m24,mb1
    Dave
    Always wear safety glasses while programming.

  • #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    Quote Originally Posted by mackrackit View Post
    Try removing the brackets
    Good catch Dave, I couldn't see that.
    Brackets make it a WAIT qualifier with SERIN.

    So used to looking at SERIN2. And SEROUT uses brackets. It's all confusing.
    I'll blame Parallax.
    DT

  • #6
    Join Date
    Sep 2010
    Posts
    50

    Default SERIN - brackets removed

    Quote Originally Posted by Darrel Taylor View Post
    Good catch Dave, I couldn't see that.
    Brackets make it a WAIT qualifier with SERIN.

    So used to looking at SERIN2. And SEROUT uses brackets. It's all confusing.
    I'll blame Parallax.
    Looks like removing the brackets kept it from getting stuck, thanks, however, it is still not receiving the data correctly. I have a 22k resistor in line to the serin pin. I have used this on other pics and it seems to work (16F636). I noticed that the serial port on my pc is +/- 12 volts, so I tried adding a blocking diode and a zener to drop the voltage down to around 6 volts on the positive side and 0 volts on the low end (which did not seem to make a difference) (pc----diode(>|)----resistor-----serin, with zener from serin pin to ground).

    One potential source could be that I have wires running from my pc on the floor to the circuit on top of my desk----Interference? I will try to locate a shielded serial cable and try again.

    Also, you might have noticed in my configuration settings I have the PBADEN fuse set to ON. It appears that somewhere between PBP, Microcode Studio, and the MACH X loader I am using this fuse gets inverted. I am pretty sure that I am setting it to off, because the other PORTB pins are correctly functioning as CTS, RTS, and SEROUT. If I compile with the PBADEN set to OFF and look at the device configuration settings in always tells me that the PORT B ADCs are enabled, ON=disabled.....

    Thanks for your time and help.

  • #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,926

    Default

    Code:
    ADCON1 = %00001111 'turn off adcs set to digital i/o
    The above line takes over after the configs so no matter what PBADEN is the pins should be digital.

    You have
    Code:
    @ CONFIG PBADEN = ON ' turn off port b analog inputs
    but you want to set it to OFF if you want it digital on POR
    From the P18F4520.INC
    ; PORTB A/D Enable bit:
    ; PBADEN = OFF PORTB<4:0> pins are configured as digital I/O on Reset
    ; PBADEN = ON PORTB<4:0> pins are configured as analog input channels on Reset
    Like Darrel, it has been awhile since I have used SERIN. You may want to try a simple routine sending just one variable to trouble shoot this.

    The limiting resistor like the manual shows is all you need. Pull the diode.

    If I think of something else I will let you know.
    Dave
    Always wear safety glasses while programming.

  • #8
    Join Date
    Sep 2010
    Posts
    50

    Default Serin

    Quote Originally Posted by mackrackit View Post
    Code:
    ADCON1 = %00001111 'turn off adcs set to digital i/o
    The above line takes over after the configs so no matter what PBADEN is the pins should be digital.

    You have
    Code:
    @ CONFIG PBADEN = ON ' turn off port b analog inputs
    but you want to set it to OFF if you want it digital on POR
    From the P18F4520.INC

    Like Darrel, it has been awhile since I have used SERIN. You may want to try a simple routine sending just one variable to trouble shoot this.

    The limiting resistor like the manual shows is all you need. Pull the diode.

    If I think of something else I will let you know.

    I pull the diode, thanks. I'll keep on trying, thanks for you help.

  • #9
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,926

    Default

    I bugged together a test because I can not remember.

    From a terminal on both of the following examples I am sending
    Code:
    83,79
    Example #1
    Code:
    V1	VAR	BYTE
    V2	VAR	BYTE
    T_MAIN:
    SERIN PORTC.7,N2400,V1,V2
    PAUSE 1000
    SEROUT PORTC.6,N2400,[V1,V2,10]
    PAUSE 250
    GOTO T_MAIN
    Displays
    83

    Example #2
    Code:
    V1	VAR	BYTE
    V2	VAR	BYTE
    T_MAIN:
    SERIN PORTC.7,N2400,#V1,#V2
    PAUSE 1000
    SEROUT PORTC.6,N2400,[V1,V2,10]
    PAUSE 250
    GOTO T_MAIN
    Displays
    SO
    Dave
    Always wear safety glasses while programming.

  • #10
    Join Date
    Sep 2010
    Posts
    50

    Default Finally - Success!

    Quote Originally Posted by cc1984 View Post
    I pull the diode, thanks. I'll keep on trying, thanks for you help.
    I made a reduced size test program and still got the same results. Finally, with nothing else to try I changed the SERIN pin from PORTB.7 to another pin I was not using (PORTC.1). Works like a charm.

    I guess it is either a bad chip or some obscure setting for portb.7 I missed. Thanks for all the help.

  • #11
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,926

    Default

    Quote Originally Posted by cc1984 View Post
    I made a reduced size test program and still got the same results. Finally, with nothing else to try I changed the SERIN pin from PORTB.7 to another pin I was not using (PORTC.1). Works like a charm.

    I guess it is either a bad chip or some obscure setting for portb.7 I missed. Thanks for all the help.
    Interesting. I still do not see the problem in your code (PORTB.7) so it must be hardware...
    Or maybe the wrong pin,,, typo???
    rcx var PORTB.7 'rs-232 input on portc.7
    Or a bad connection?
    Dave
    Always wear safety glasses while programming.

  • #12
    Join Date
    Sep 2010
    Posts
    50

    Default

    Quote Originally Posted by mackrackit View Post
    Interesting. I still do not see the problem in your code (PORTB.7) so it must be hardware...
    Or maybe the wrong pin,,, typo???


    Or a bad connection?

    Could be a bad connection. I have it installed on a test board. I am making the permanent board which should only be traces, not a bunch of wires. Hopefully, that will remove my ghost.

    Thanks for all the help, unfortunately the code was the fun part, drawing up the circuit and making the cca is not as fun.

  • Members who have read this thread : 0

    You do not have permission to view the list of names.

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts