Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

SIMPLE ROUTINE?

Status
Not open for further replies.

hjl4

Member
Hello again.

Is it possible to make a routine, where the pic reads the entire portA, then depending on which bit is set, goes to an individual routine for each pin. Only one pin will be set at anyone time.
In other words, portA=00000001 then to special routine because that pin is set. If portA=00000100 then the set pin makes the microcontroller goto that pins special routine?
Its hard to make sense of this.
Is it possible?

Thanks in advance.
 
Try something like,

Code:
    btfsc   PORTA,0
    goto   Routine0
    btfsc   PORTA,1
    goto   Routine1
 etc.

Mike.
 
The problem with testing the individual bits in PORTA is that they are effectively tested at different times. Granted the delay may be in microseconds.

How about
1. Read PORTA into W
2. Store W in a file register
3. Shift one bit left or right into the Carry Bit
4. Test the Carry Bit, skip on clear
5. Branch to Routine N if Carry is set
6. Repeat lines 3 thru 5 seven more times

Code:
        movf   PORTA,W
        movwf  shift_register
;  First Routine Dispatch
        rrf    shift_register,f
        btfsc  STATUS,C
        goto   Routine0
;  Second Routine Dispatch
        rrf    shift_register,f
        btfsc  STATUS,C
        goto   Routine1
.
.
.
and so on
 
Can this instruction not read the entire port in one shot?

movf PortA,0; read portA and move entire port to Working register.
Once read then compare with file and then check if Status bit is set, then off to specific routine?
Is it possible?
 
hjl4 said:
Can this instruction not read the entire port in one shot?

movf PortA,0; read portA and move entire port to Working register.
Once read then compare with file and then check if Status bit is set, then off to specific routine?
Is it possible?

Yes, but you're comparing the entire byte, not individual bits, which is what you asked.

Also don't use 'movf PortA, 0', use 'movf PortA, w' which makes far more sense - the include files are there to be used, and are standard - it makes sense to use them!.
 
Ok guys, I understand.

Thanks for the help.
To check individual bits is the way to go.
Also, yes movf porta,w makes more sense.

Thanks.

Also, how much time do you guys think would be taken by micro, if I had to read the port all the way down, to find out that bit 7 is set.
I know that btfss, or btfsc, uses two clock cycles. So would that be times 8? Plus the goto instruction times 8? =24ms????
I don't want to much of a delay. PortA will be changing bits every 50ms at the max.
 
hjl4 said:
I know that btfss, or btfsc, uses two clock cycles. So would that be times 8? Plus the goto instruction times 8? =24ms????
I don't want to much of a delay. PortA will be changing bits every 50ms at the max.

If it's running at 4MHz each instruction cycle is only 1uS, so speed isn't a concern - what are you actually trying to do?.
 
I want to use 16f628, and create different PWM signal depending on which pin is set high on portA.

Here is what I came up with with your help.
In the simulator, however, with no bits set on portA, bit 5 manages to jump to its routine.???
Need help here.
Also, I want 3khz minimum, and need to figure out how to calculate all this for delay times.

Thanks.
Code:
;16F628 
;DEVICE 16F628
    
    
     LIST P=16F628,
     #INCLUDE P16F628.inc
     
     
RP1                          EQU     0x06       
RP0                          EQU     0x05        
PORTA                        EQU     0x05        
PORTB                        EQU     0x06        
STATUS                       EQU     0x03        
TRISA                        EQU     0x85        
TRISB                        EQU     0x86          
PC                           EQU     0X02
C1                           EQU     0X30
C2                           EQU     0X31
C3                           EQU     0X32
C4                           EQU     0X33
C5                           EQU     0X34
C6                           EQU     0X35
C7                           EQU     0X36
C8                           EQU     0X37

     
;PORT SETUP

SETUP    
     BSF STATUS, RP0
     MOVLW 0xff       ;PORTA RA0-RA7 INPUTS
     MOVWF TRISA
     MOVLW 0x00
     MOVWF TRISB     ;PORTB ALL OUTPUTS
     BCF STATUS, RP1
     NOP
     NOP
     CLRF PORTA
     CLRF PORTB
     MOVLW D'255'
     MOVWF 0x30
     MOVLW D'200'
     MOVWF 0x31
     MOVLW D'150'
     MOVWF 0x32
     MOVLW D'100'
     MOVWF 0x33
     MOVLW D'75'
     MOVWF 0x34
     MOVLW D'50'
     MOVWF 0x35
     MOVLW D'25'
     MOVWF 0x36
     MOVLW D'13'
     MOVWF 0x37  
     
     
MAIN
    MOVF PORTA, 0 ; move porta into W
    btfsc   PORTA,0
    goto   Routine0
    btfsc   PORTA,1
    goto   Routine1 
    btfsc   PORTA,2
    goto   Routine2
    btfsc   PORTA,3
    goto   Routine3 
    btfsc   PORTA,4
    goto   Routine4
    btfsc   PORTA,5
    goto   Routine5 
    btfsc   PORTA,6
    goto   Routine6
    btfsc   PORTA,7
    goto   Routine7
    goto MAIN
    
 
 
Routine0
         bsf PORTB,0
  LOOP   decfsz C1,1
         GOTO LOOP
         bcf PORTB,0
         GOTO MAIN   
         
Routine1
         bsf PORTB,1
  LOOP1  decfsz C2,1
         GOTO LOOP1
         bcf PORTB,1
         GOTO MAIN  
         
Routine2
         bsf PORTB,2
  LOOP2  decfsz C3,1
         GOTO LOOP2
         bcf PORTB,2
         GOTO MAIN   
         
Routine3
         bsf PORTB,3
  LOOP3  decfsz C4,1
         GOTO LOOP3
         bcf PORTB,3
         GOTO MAIN 
Routine4
         bsf PORTB,4
  LOOP4  decfsz C5,1
         GOTO LOOP4
         bcf PORTB,4
         GOTO MAIN   
Routine5
         bsf PORTB,5
  LOOP5  decfsz C6,1
         GOTO LOOP5
         bcf PORTB,5
         GOTO MAIN  
         
Routine6
         bsf PORTB,6
  LOOP6  decfsz C7,1
         GOTO LOOP6
         bcf PORTB,6
         GOTO MAIN   
         
Routine7
         bsf PORTB,7
  LOOP7  decfsz C2,1
         GOTO LOOP7
         bcf PORTB,7
         GOTO MAIN 
                  
                             
END
 
You need to make sure that your pins are configured for doing IO. There is more to it then just setting up the data latch and the TRIS registers. You may have to turn the comparators or other peripherals off. Consult the datasheet for helpful hints in this regard.

You also need to reset your period registers in locations 30 through 37 since you are using them for both initial values and for decrementing. If the start from zero the next time they are activated they will have a long period.
 
Thanks guys.

Ok I have turned comparators off I hope.
All inputs on portA.
I'll see what happens.
 
Status
Not open for further replies.

Latest threads

Back
Top