Problems with RC2 and RC3


Closed Thread
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default

    Ok, I don't like to post the code because it's not written very well and I am not an expert but maybe someone can help me. Here is the code:

    @ DEVICE PIC16F870, HS_OSC, WDT_OFF, PWRT_ON, BOD_ON, LVP_OFF , DEBUG_OFF, PROTECT_OFF

    DEFINE OSC 20
    DEFINE HSER_BAUD 2400
    DEFINE HSER_RCSTA 90h
    DEFINE HSER_TXSTA 20h
    DEFINE HSER_CLROERR 1
    ADCON1=7
    b1 var WORD 'first of 4 vars to store verified good data
    b2 var WORD
    b3 var WORD
    b4 var WORD
    in1 var WORD 'first of 4 vars to store incoming data
    in2 var WORD
    in3 var WORD
    in4 var WORD
    start var byte 'first incoming byte - should be zero
    sum var WORD 'incoming checksum from the sending PIC
    datasum var WORD 'used to add values of 4 incoming data bytes
    errorflag var bit 'flag to indicate bad data (1 = bad or untested data)


    TRISA = %00000000
    TRISC = %10000000
    TRISB = %00000000
    TRISD = %00000000
    pause 100

    PORTB.0=0
    PORTB.1=0
    PORTB.2=0
    PORTB.3=0
    PORTB.4=0
    PORTB.5=0
    PORTB.6=0
    PORTB.7=0
    PORTA.0=0
    PORTA.1=0
    PORTA.2=0
    PORTA.3=0
    PORTA.4=0
    PORTA.5=0
    PORTA.6=0
    PORTA.7=0
    PORTC.0=0
    PORTC.1=0
    PORTC.2=0
    PORTC.3=0
    PORTC.4=0
    PORTC.5=0
    PORTC.6=0






    mainloop:
    IF RCSTA.1 = 1 THEN 'if USART overrun error flag OERR is set...
    RCSTA.4 = 0 'clear and then set CREN bit to recover
    RCSTA.4 = 1
    in1 = RCREG 'read data to clear out RCREG
    IF PIR1.5 = 1 THEN in1 = RCREG 'clear possible 2nd byte in RCREG
    ENDIF

    WHILE PIR1.5 = 0 'stay here until USART receives data

    'LOW portc.3 'portc.3 has diagnostic LED
    WEND


    'HIGH portc.3 'data in RCREG; turn on diagnostic LED

    errorflag = 1
    GOSUB getdata

    IF errorflag = 0 THEN 'only updates relays if data judged to be good


    if b1=0 and b2=1 and b3=11 THEN GOSUB ONN1
    if b1=0 and b2=1 and b3=12 THEN GOSUB OFF1
    if b1=0 and b2=2 and b3=11 THEN GOSUB ONN2
    if b1=0 and b2=2 and b3=12 THEN GOSUB OFF2
    if b1=0 and b2=3 and b3=11 THEN GOSUB ONN4
    if b1=0 and b2=3 and b3=12 THEN GOSUB OFF4
    if b1=0 and b2=4 and b3=11 THEN GOSUB ONN3
    if b1=0 and b2=4 and b3=12 THEN GOSUB OFF3
    if b1=0 and b2=5 and b3=11 THEN GOSUB ONN5
    if b1=0 and b2=5 and b3=12 THEN GOSUB OFF5
    if b1=0 and b2=6 and b3=11 THEN GOSUB ONN6
    if b1=0 and b2=6 and b3=12 THEN GOSUB OFF6
    if b1=0 and b2=7 and b3=11 THEN GOSUB ONN7
    if b1=0 and b2=7 and b3=12 THEN GOSUB OFF7
    if b1=0 and b2=8 and b3=11 THEN GOSUB ONN8
    if b1=0 and b2=8 and b3=12 THEN GOSUB OFF8
    if b1=0 and b2=9 and b3=11 THEN GOSUB ONN0
    if b1=0 and b2=9 and b3=12 THEN GOSUB OFF0
    if b1=1 and b2=0 and b3=11 THEN GOSUB ONN9
    if b1=1 and b2=0 and b3=12 THEN GOSUB OFF9
    if b1=1 and b2=1 and b3=11 THEN GOSUB ONN11
    if b1=1 and b2=1 and b3=12 THEN GOSUB OFF11
    if b1=1 and b2=2 and b3=11 THEN GOSUB ONN13
    if b1=1 and b2=2 and b3=12 THEN GOSUB OFF13
    if b1=1 and b2=3 and b3=11 THEN GOSUB ONN12
    if b1=1 and b2=3 and b3=12 THEN GOSUB OFF12
    if b1=1 and b2=4 and b3=11 THEN GOSUB ONN14
    if b1=1 and b2=4 and b3=12 THEN GOSUB OFF14
    if b1=1 and b2=5 and b3=11 THEN GOSUB ONN16
    if b1=1 and b2=5 and b3=12 THEN GOSUB OFF16
    if b1=1 and b2=6 and b3=11 THEN GOSUB ONN15
    if b1=1 and b2=6 and b3=12 THEN GOSUB OFF15
    if b1=9 and b2=9 and b3=11 then gosub allon
    if b1=9 and b2=9 and b3=12 then gosub alloff
    ENDIF

    goto mainloop:


    getdata:
    IF RCSTA.1 = 1 THEN xfer 'USART overflow error - bad data
    HSERIN 40, xfer, [start, in1, in2, in3, sum]
    IF start <> 0 THEN xfer 'if first byte isn't 0 assume bad data
    datasum = in1 + in2 + in3 'add the 4 bytes and compare to sum
    IF datasum = sum THEN errorflag = 0 'if compare is good, assume good data
    xfer:
    IF errorflag = 0 THEN
    B1 = in1
    b2 = in2
    b3 = in3


    ENDIF
    RETURN

    ONN1:
    HIGH PORTB.7
    goto mainloop
    OFF1:
    LOW PORTB.7
    GOTO MAINLOOP

    ONN2:
    HIGH PORTB.3
    goto mainloop
    OFF2:
    LOW PORTB.3
    GOTO MAINLOOP

    ONN3:
    HIGH PORTB.4
    goto mainloop
    OFF3:
    LOW PORTB.4
    GOTO MAINLOOP

    ONN4:
    HIGH PORTA.3
    goto mainloop
    OFF4:
    LOW PORTA.3
    GOTO MAINLOOP

    ONN5:
    HIGH PORTB.5
    goto mainloop
    OFF5:
    LOW PORTB.5
    GOTO MAINLOOP

    ONN6:
    HIGH PORTA.2
    goto mainloop
    OFF6:
    LOW PORTA.2
    GOTO MAINLOOP

    ONN7:
    HIGH PORTC.2
    goto mainloop
    OFF7:
    LOW PORTC.2
    GOTO MAINLOOP

    ONN8:
    HIGH PORTC.3
    goto mainloop
    OFF8:
    LOW PORTC.3
    GOTO MAINLOOP:

    ONN9:
    HIGH PORTC.1
    GOTO MAINLOOP
    OFF9:
    LOW PORTC.1
    goto mainloop

    ONN0:
    HIGH PORTC.0
    GOTO MAINLOOP
    OFF0:
    LOW PORTC.0
    goto mainloop

    ONN11:
    HIGH PORTA.5
    GOTO MAINLOOP
    OFF11:
    LOW PORTA.5
    goto mainloop

    ONN12:
    HIGH PORTB.1
    GOTO MAINLOOP
    OFF12:
    LOW PORTB.1
    goto mainloop

    ONN13:
    HIGH PORTB.0
    GOTO MAINLOOP
    OFF13:
    LOW PORTB.0
    goto mainloop

    ONN14:
    HIGH PORTC.5
    GOTO MAINLOOP
    OFF14:
    LOW PORTC.5
    goto mainloop

    ONN15:
    HIGH PORTB.2
    GOTO MAINLOOP
    OFF15:
    LOW PORTB.2
    goto mainloop

    ONN16:
    HIGH PORTC.4
    GOTO MAINLOOP
    OFF16:
    LOW PORTC.4
    goto mainloop

    alloff:
    LOW PORTA.2
    LOW portc.4
    LOW portb.2
    LOW portc.5
    LOW portb.0
    LOW portb.1
    LOW PORTB.5
    LOW porta.5
    LOW portc.0
    LOW portc.1
    LOW portc.2
    LOW portc.3
    LOW porta.3
    LOW portb.3
    LOW portb.4
    LOW portb.7
    goto mainloop

    allon:

    PORTB.0=1
    PORTB.1=1
    PORTB.2=1
    PORTB.3=1
    PORTB.4=1
    PORTB.5=1
    PORTB.6=1
    PORTB.7=1
    PORTA.0=1
    PORTA.1=1
    PORTA.2=1
    PORTA.3=1
    PORTA.4=1
    PORTA.5=1
    PORTA.6=1
    PORTA.7=1
    PORTC.0=1
    PORTC.1=1
    PORTC.2=1
    PORTC.3=1
    PORTC.4=1
    PORTC.5=1
    PORTC.6=1

    goto mainloop

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    O.k, there's few thing to be removed first..
    Code:
    mainloop:
        IF RCSTA.1 = 1 THEN 'if USART overrun error flag OERR is set...
           RCSTA.4 = 0 'clear and then set CREN bit to recover
           RCSTA.4 = 1
           in1 = RCREG 'read data to clear out RCREG
           IF PIR1.5 = 1 THEN in1 = RCREG 'clear possible 2nd byte in RCREG
           ENDIF
    DEFINE HSER_CLROERR 1 do this for you. Remove those line.

    Code:
        WHILE PIR1.5 = 0 'stay here until USART receives data
              'LOW portc.3 'portc.3 has diagnostic LED
              WEND
    
        'HIGH portc.3 'data in RCREG; turn on diagnostic LED
    While your program do nothing usefull there, simply remove those line and replace it simply for HSERIN.

    Code:
    if b1=0 and b2=1 and b3=11 THEN GOSUB ONN1
    .
    .
    .
    Should be writen like..
    Code:
    if (b1=0) and (b2=1) and (b3=11) THEN GOSUB ONN1
    Code:
    ONN1:
    HIGH PORTB.7
    goto mainloop
    You jump to ONN1 with a gosub, it must be terminated with a RETURN to avoid stack overflow. Replace GOTO MAINLOOP by a simple RETURN.

    Multiple PORTA.0=0, PORTA.1=0, ... can be replace with a single line. In your example you set all PORTA bit to zero. It's working sure, but you can still use PORTA=0. Same for all PORTx to 1. Can be replace with PORTx=%11111111, PORTx=$FF or PORTx=255.

    Personnaly i would change it to something different..., let's try something.
    Code:
        TRISA  = %00000000
        TRISB  = %00000000
        TRISC  = %10000000
        ADCON1 = 7
    
        PORTx    var byte
        PORTbit  var byte
        BitState var byte    
    
        PORTA = 0
        PORTB = 0
        PORTC = 0
    
    mainloop:
        hserin 500,mainloop,[PORTx,DEC1 portbit, DEC1 bitstate]
        select case portx
               case "A"
                    PORTA.0(PORTBIT)=BITSTATE
               CASE "B"
                    PORTB.0(PORTBIT)=BITSTATE
               CASE "C"
                    PORTC.0(PORTBIT)=BITSTATE     
               END SELECT
        GOTO MAINLOOP
    With few chunk around it may be something to play with

    EDIT: also, there's no PORTD on the F870.. no warning or error using PM? I don't know how it's handle by PM but for sure, this will return you an error message when using MPASM...

    @Darrel,
    Yet another reason
    Last edited by mister_e; - 23rd May 2006 at 15:16.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Can't get RC2 to go high with PORTC.2 = 1
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 13th September 2009, 08:26

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