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.

20 seconds timer

Status
Not open for further replies.

Floris

New Member
Hi all,

I'm doing this project for school to control a few servo motors and I am fairly new to PIC programming.

I just have one question I hope someone can help me with:

Is it possible to run a program on your PIC but let it execute a different part after 20 seconds and then stop everything?
Edit: I am using a 16F84 PIC

So its functionality would look like this (of course it looks nothing like the actual code):

timer 20 seconds
if not at 0 goto mainprogram
if at zero goto subprogram

mainprogram
control the servos
goto mainprogram

subprogram
put the servos in a locked position

end


Kind regards,
Floris
 
Last edited:
use a timer with the largest delay and count the Timer interrupt flags , when a certain number is reached -> your 20 seconds are over

grts
 
the same pic , just a loop :

int i = 0;

while ( i!=20){

--> start timer
--> clear interrupt flag
i++;
.....

}
 
But that would require another PIC as a counter right? Is it not possible without another PIC?

Nope just one pic with a timer which all of them just some are not 16 bit timers. You just set a flag and inc every 1000 ticks.
 
Ok I don't really get it :p
Maybe someone can take a look at it for me? I have uploaded the .asm file.

The mainline program should run for 20 seconds after which it goes to rollSM30
 

Attachments

  • fb4mhzmodified.asm
    18.3 KB · Views: 142
Ok I don't really get it :p
Maybe someone can take a look at it for me? I have uploaded the .asm file.

The mainline program should run for 20 seconds after which it goes to rollSM30

What don't you get that code you didn't write but you think it needs a 20 sec delay
 
What don't you get that code you didn't write but you think it needs a 20 sec delay

It is a project for school to control a small glider aircraft, but I need to modify it so that after 20 seconds the aileron (roll) servo extend to maximum position to slow the aircraft down (speedbrakes). So after 20 seconds there has to be an interrupt which activates the speedbrakes.
In the code there are already a few interrupts but I don't know how I can add one to interrupt the program after 20 seconds while leaving the main program and interrupts the same.
 
OK I think I need something like this:
- TMR0 set to 200 and prescaler set to 200
- Put the numbers 100 and 5 in a register
- When TMR0 overflows it interrupts and decreases the 100 by one. It also sets the TMR0 to 200 again and mainline must continue.
- When the 100 reaches 0 it will decrease the 5 by one and reset itself to 100
- When the 5 reaches 0 it will call the speedbrakes

Now I have 200*200*100*5 = 20 seconds

Is something like this possible? And there is already an interrupt routine in the code, can I just add this without messing up the existing code?

This is the code that is already in the program:

Code:
;****** INTERRUPT ROUTINE ******
;Interrupt Handler - come here every 18 msec to update servo positions
int   		movwf  _w                     ;save the registers
 		movf   status, w
 		movwf  _status

  		movlw  0x020                  ;disable further interrupts
  		movwf  intcon

; 		movlw  H'0F'                   ;turn on all the pulses
; 		iorwf  portb
		bsf	porta,S0
		bsf 	porta,S1
		bsf	porta,S2
		bsf	portb,S3

;Output a 1ms start pulse to each servo (4MHz crystal)
;Radio control servos require a 1mS hi pulse before the command pulse
  		movlw  143                    ;wait 143x7us=1 msec 
  		movwf  count
  		goto   $ + 1                  ;seven cycle delay
  		goto   $ + 1
  		decfsz count
   		goto  $ - 3

;Output the command pulse to each servo (4MHz crystal)
;The value range of each servo is between D0 to D50. It takes 20uS to
;complete one pass of the loop, so the output to the servo is
;between 0x20us=0mS and 50x20us=1mS. The code can drive 4 servos.
  		movlw  D'50'                  ;load master count  
 		movwf  count                  ;
intloop                        		      ;
  		movf   count, w               ;fetch master count
  		subwf  servopc, w             ;subtract from servo count
  		btfsc  status, C              ;done 50?
   		bcf   porta, S0		      ;yes, clear pitch command pulse
  		movf   count, w		      ;no, keep pulse going and	
		subwf  servor, w	      ;do next servo
  		btfsc  status, C
   		bcf   porta, S1		      ;roll servo
  		movf   count, w
  		subwf  servoy, w
  		btfsc  status, C
   		bcf   porta, S2		      ;yaw servo
  		movf   count, w
  		subwf  servox, w
  		btfsc  status, C
		bcf   portb, S3
   		nop				;a 4th servo (bcf porta,SX)
  		goto   $ + 1                 	;a two cycle delay
  		decfsz count
   		goto  intloop                	;do the next count
  		movlw  0x000                  	;turn off servo lines
  		andwf  porta

;wait another 20ms 
 		movlw  178            		;see above
 		movwf  TMR0
  		movlw  0x0A0                	;re enable interrupt
  		movwf  intcon
intend                          		;tidy and exit
	  	movf   _status, w
  		movwf  status
  		swapf  _w
  		swapf  _w, w
	  	retfie

Thanx!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top