16F676 port problems

Discussion in 'Embedded Systems and Microcontrollers' started by lazerspot, Feb 16, 2008.

  1. lazerspot

    Thread Starter New Member

    Feb 16, 2008
    1
    0
    Greetings !! I am having a very funny problem with the port A configuration of a 16F676 micro. Normally I would blame it on finger problems, and that still maybe the case, but when I clrf ansel RA0 still wants to remain as an analog input. How do I know this ... I have a simulator made by Oshon, which I find to be an excellent simulator, and when it executes the code i.e. clrf ansel, bank 1 selected, it shows what I explained above about RA0. Is the simulator correct, I think it is as basically the circuit I have consists of 4 switches connected to RA0, 3, 4, and RC4. Using btfss, all the other switches do as programmed but when I include the code for RA0 it locks up because it still thinks it is an analog input.
    Any help would be greatly appreciated ... Below is some of the initialization code:

    ;****** CODE BEGINS *********************************************************

    start:

    bsf STATUS,RP0 ;set register bank 1

    ;next is VERY IMPORTANT for the 16F676 device.
    ;by default, the inputs are analog inputs on RESET.
    ;we must make them digital. ( this info I got from an app. note)

    CLRF ANSEL ; this is the code that doesn't appear to function

    ; we make the outputs = 0, inputs = 1



    movlw b'00011001' ; port A bit 0 = input opto
    ; port A bit 1 = Q1 drive output
    ; port A bit 2 = Q2 drive output
    ; port A bit 3 = down input switch
    ; port A bit 4 = up input switch
    ; port A bit 5 = disp latch output
    ; port A bit 6 = N/A
    ; port A bit 7 = N/A
    movwf TRISA ;do it!

    movlw b'00100000' ; port C bit 0 = A output
    ; port C bit 1 = B output
    ; port C bit 2 = C output
    ; port C bit 3 = D output
    ; port C bit 4 = Q3 drive output
    ; port C bit 5 = reset input
    ; port C bit 6 = N/A
    ; port C bit 7 = N/A
    movwf TRISC ;do it!


    bcf STATUS,RP0 ;set register bank 0
    bsf LATCH ;set the display driver latch Hi
    bcf Q1 ;set Q1 transistor drive lo
    bcf Q2 ;set Q2 trisistor driver lo
    bcf Q3 ;set Q3 trisistor driver lo
    clrf PORTC
    CLRF UNITS
    CLRF TENS
    CLRF HUNS
    BSF LATCH
    NOP
    NOP
    NOP
    BCF LATCH
    clrwdt ;clr WDT PREP PRESCALE ASSIGN

    bsf STATUS,RP0 ;set register bank 1
    movlw b'11010100' ;setup timer and prescale
    movwf OREG ;the timer has been setup to update the display
    ;every 16 mS this value controls the brightness of the display
    bcf STATUS,RP0 ;set register bank 0
    bcf INTCON,2 ;CLEAR TRM0 INTERRUPT FLAG
    bsf INTCON,7 ;ENABLE GLOBAL INTERRUPTS
    bsf INTCON,5 ;ENABLE TMRO INTERRUPT
    clrf TMR0 ;START TIMER/COUNTER,CLEAR PRESCALER


    WAIT:

    BTFSS SWUP ; wait in loop until SWITCH closure is sensed
    GOTO UP ; do the up thing
    BTFSS SWDOWN
    GOTO DOWN ; do the down thing
    BTFSS SWRESET
    GOTO RESET1 ; reset the counters
    BTFSS SWOPTO
    GOTO OPTO ; This is where the code locks up
    ; if I remove this code the other stuff
    ; works as it should
    GOTO WAIT ;Stay here if switchs not closed

    Also I have another question ... the code above is used in a 3 digit BCD counter so only the nibble of the counter byte is used for Port C to drive the BCD inputs of a 7 segment display driver MC14511 and the other two
    ports to drive a couple of transistors, can I XORWF my nibble with port c so I don't change the state of the driver transistors ?
    Anyway thanks in advance for any help ... take care ... Vic
     
  2. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    Peculiar problem. Are you sure the problem was the ANSEL and not other peripherals such as the comparator? It might be worth a try to isolate the problem further, like the RA1 and RA2 (also used by the comparator) or do a test with minimum code.

    It's not recommended to set part of the port using xorwf, especially if you have a mix of output and input pins in one port or have at least one slow changing output. This might trigger the read-modify-write problem. Other instructions like bcf, bsf, iorwf and andwf are also susceptible. movwf is safe, but you would have to track the state of the pins. In fact, this is the recommended approach, using a shadow register to keep track of the state of a port.

    Search read-modify-write problem for more information.
     
Loading...