Interrupts

Xarren

Member
Heyah, I have an interrupt that I want to fire every second, would you guys be able to tell me how long this interrupt would take to run? I want the main process to carry on meantime, so Im hoping its not too long.

Code:
interrupt:

	inc w6
	
	if w6<10 then
		serout 1,N2400,(254,198,"00",#w6)
	else if w6<100 then
		serout 1,N2400,(254,198,"0",#w6)
	else
		serout 1,N2400,(254,198,#w6)
	endif
	
	pause display.debounce
	setint %00000010,%00000010
	return
Cheers x
 

inglewoodpete

Senior Member
That is something you will have to test yourself. That's what beadboards are for.

By the look of it, most of the time will be used by the serout. Each 2400 baud character takes 4.2 mS to transmit. Then there are some comparatively small overheads of code execution.
 

BeanieBots

Moderator
The pause (you don't give a value) will be the dominant factor.
The serout time will depend on whatever the data is but the variation will probably be quite small compared to any pause value.

Why do you need a pause anyway?
 

SilentScreamer

Senior Member
Code:
high 0
for w0 = 0 to 65535

	inc w6
	
	if w6<10 then
		serout 1,N2400,(254,198,"00",#w6)
	else if w6<100 then
		serout 1,N2400,(254,198,"0",#w6)
	else
		serout 1,N2400,(254,198,#w6)
	endif
	
	pause display.debounce
	setint %00000010,%00000010
		next
	low 0
Change the high/low 0 to an appropriate pin so you know when the code has finished and simply time it between the LED coming on then off. Alternatively use a timer to measure the time taken and debug to the programming editor. Divide this time by 65536 and you have a rough answer.
 

Xarren

Member
Cheers guys, and that pause is 10miliseconds, the code in the AXE033 datasheet seemed to have a 10ms pause after some of the serout commands, so I put ones into mine just to be sure.
Do i not need them? x
 

hippy

Ex-Staff (retired)
You can optimise your code for speed ....

Code:
Interrupt:
  Inc w6
  SetInt %00000010,%00000010
  If w6 >= 100 Then
    SerOut 1, N2400, (254,198,#w6)
    Return
  End If
  If w6 >= 10 Then
    SerOut 1, N2400, (254,198,"0",#w6)
    Return
  End If
  SerOut 1, N2400, (254,198,"00",#w6)
  Return
 

westaust55

Moderator
In terms of approximate timing, the SEROUT command is, in your case operating at 2400 baud. That equates to ~10 bits per byte (start, byte, stop) and 2400 bits per second.
You have 2 control bytes 254,198 and then seemingly 3 byes for the number in w6. So that equates to 50 bits or approx 21 ms. The SEROUT command would also have some setup time.

Your pause is another 10ms

Beaniebots in a long ago thread, gave some relative timing data for for various BASIC commands.
Have a read of this post I did as a tutorial on Forum searching and . . .by perchance it points to BB's post on command timing

http://www.picaxeforum.co.uk/showthread.php?t=11053
 
Last edited:

westaust55

Moderator
An while hippy has optimised for speed,
if space is a concern then code can be reduced for the interrupt using:
Code:
Interrupt:
  Inc w6
  SetInt %00000010,%00000010
  SerOut 1, N2400, (254,198)
  If w6 >= 100 Then
    SerOut 1, N2400, (#w6)
    Return
  End If
  If w6 >= 10 Then
    SerOut 1, N2400, ("0",#w6)
    Return
  End If
  SerOut 1, N2400, ("00",#w6)
  Return
even more bytes can be saved maybe at the expense of speed
 

Xarren

Member
Cheers guys, and no memory is fine, still got loads left, I just wanted to make sure the rest of the code will run fast enough with the interrupt kicking in every second. The interupt pretty much counts time in seconds and displays it on the LCD, I dont suppose there is a faster/more standard way to do that? (I have pulse in from the LCD going into input1 at 1hz).
 

QuIcK

Senior Member
not sure how accurate the resonator/counter is for this. there is a time keeping ic that you can get. runs on i2c.
you could query the time, filter seconds, and update your lcd at the end/start of each main code loop.
altho, perhaps an interrupt is better for relative seconds (not absolute).
depends how long you want the code to run (ie how many seconds to count), and how much you care about accuracy
 

hippy

Ex-Staff (retired)
The interupt pretty much counts time in seconds and displays it on the LCD, I dont suppose there is a faster/more standard way to do that? (I have pulse in from the LCD going into input1 at 1hz).
That's one way to do it and there should be plenty of time to process the interrupt, update the display and be back ready for the next signal going high ... however, you aren't waiting in your interrupt routine to make sure the pulse has gone away so return too quickly and you'll get another interrupt immediately.

There are two solutions - Wait for the signal to expire, which probably means waiting half a second doing nothing if it's a 50% duty pulse, or switch the interrupt to occur first on the positive going signal and then the negative. That gets a bit more complicated.

Alternatively, poll the signal and avoid interrupts altogether. It really depends on what else your code has to do when not being interrupted.
 

eclectic

Moderator
@Xarren.

If the interrupt is triggered by the AXE033 onboard DS1307 PULS,
then the timing will be excellent.

Your interrupt Sertxd code will take ~ 30 – 35 milliseconds.
(I did a rough test earlier).

However, the important question is your main code.
Can it tolerate a 35ms break?

Could you post that code please, so that people can check?

e
 

Xarren

Member
Code:
#picaxe 18X
symbol write.debounce=1000					'Debounce when writing to EEPROM
symbol switch.debounce=100					'Debounce when accepting input from a switch
symbol display.debounce=10					'Debounce when communicating with AXE033


init.LCD:								'Save prewritten messages on AXE033 EEPROM

	pause 500
	
	serout 1,N2400,(253,3,"Enter Password: ")
	pause write.debounce
	
	serout 1,N2400,(253,1,"T  C LL         ")
	pause write.debounce
	
	serout 1,N2400,(253,2,"Timer=   s      ")
	pause write.debounce
	
	serout 1,N2400,(253,4,"   INCORRECT    ")
	pause write.debounce
	
	serout 1,N2400,(253,5,"    PASSWORD    ")
	pause write.debounce
	
	serout 1,N2400,(254,1)
	pause display.debounce

security:								

	b0=0								'Initialize variables
	b1=0
	b2=0
	b3=197
	b4=0
	
	serout 1,N2400,(253,3)					'Display "Enter Password: " on the LCD
	pause display.debounce
	
	for b2=1 to 4						'Initialize the password entry loop
	
security.underscore:

	b3=b3+1
	serout 1,N2400,(254,b3,"_")				'Display an entry prompt "_" accounting for LCD cursor position
	b3=b3-1
	bit5=0							'Reset bit5 (bit 5 adjusts "_" position)
	
security.entry:							'Entry of password

		do
									
			if input6=1 then				'If backspace pressed delete last character
				gosub security.back
			endif
			
			readadc 2,b1				'Check password input switches
			
		loop while b1<20					'Loop until there is an input

security.selectcase:

	select case b2						'Compares input against the correct input, bit marked if input is correct
	
		case 1
		
			if b1>60 AND b1<80 then 		'Range provided for relability
				bit0=1
			endif
			
		case 2
		
			if b1>80 AND b1<100 then
				bit1=1
			endif
			
		case 3
		
			if b1>100 AND b1<120 then
				bit2=1
			endif
			
		case 4
		
			if b1>120 AND b1<140 then
				bit3=1
			endif
			
	endselect

	pause switch.debounce
	b3=b3+1							'Move the cursor forward by one
	serout 1,N2400,(254,b3,"X")				'Display "X" at current cursor position to mark entered character
	pause display.debounce
	
	next b2							'End password entry process
	
	do								'Loop waiting for password confirmation using enter
	
		if input6=1 then					'If backspace is pressed mark bit5 to adjust cursor position
			bit5=1					'and go back to the password entry phase, from which the user
			goto security.entry			'will go straight to the backspace function, as backspace is
		endif							'still down
		
	loop while input7=0					'Exit loop when enter is pressed, confirming the password to be checked
	
	if b0=%00001111 then 					'Check password against a constant, if all 4 bits were marked password is correct			
					
		setint %00000010,%00000010
		
		serout 1,N2400,(254,1)				'Clear the LCD of old now irrelevant text
		pause display.debounce
		
		serout 1,N2400,(253,1)				'Display "T  C LL         " on line one of the LCD 
		pause display.debounce
		
		serout 1,N2400,(253,2)				'Display "Timer=   s      " on line two of the LCD 
		pause display.debounce
		
		goto main
	else								'If password is incorrect
		high 0						'turn on a red LED
		serout 1,N2400,(253,4)				'and display "   INCORRECT    " on line one of the LCD
		pause display.debounce
		serout 1,N2400,(253,5)				'with "    PASSWORD    " on the second line of the LCD
		wait 5						'Display this message and keep the red LED on for 5 sec
		serout 1,N2400,(254,1)				'After 5 sec clear the LCD
		low 0							'and turn the red LED off
		goto security					'Reset the character entry process
	endif
	
security.back:							'The backspace password entry character deletion function

	if b2>=2 then						'If we are not already at position 1 of the LCD 
		pause switch.debounce
		serout 1,N2400,(254,b3,"    ")		'Delete everything after the cursor
		b3=b3-1						'Move cursor and password entry variables back to accustom
		b2=b2-1						'for the character deletion
	else
		return
	endif
	
	if bit5=1 then
		goto security.underscore
	else
		b3=b3+1
		serout 1,N2400,(254,b3,"_")
		b3=b3-1
		goto security.entry
	endif
	
main:

	if pin2=1 then
		w6=0
	endif
	
	readtemp 1,b1
	
	if b1<10 then
		serout 1,N2400,(254,131,"00",#b1)
	else if b1<100 then
		serout 1,N2400,(254,131,"0",#b1)
	else
		serout 1,N2400,(254,131,#b1)
	endif
		
	pause display.debounce
	
	readadc 1,b2 
	b2=b2*100/255
	
		if b2<10 then
	serout 1,N2400,(254,137,"00",#b2)
	else if b2<100 then
		serout 1,N2400,(254,137,"0",#b2)
	else
		serout 1,N2400,(254,137,#b2)
	endif
	
	pause display.debounce
	goto main

interrupt:

	inc w6
	
	if w6<10 then
		serout 1,N2400,(254,198,"00",#w6)
	else if w6<100 then
		serout 1,N2400,(254,198,"0",#w6)
	else
		serout 1,N2400,(254,198,#w6)
	endif
	
	pause display.debounce
	setint %00000010,%00000010
	return
Thats the code, sorry havent finished annotating it all :).
And cant breadboard it right now, silly me I ve got everything appart of a voltage regulator :'(. I was accually looking forward to seeing how it performs :).
 

Xarren

Member
And yes its running of a DS1703 of an AXE033 board, pulseout connected to input1. To answer previous questions, accuracy will be quite important as its timing a science experinment, and the max it will ever need to measure should be around 10 mins (600s).

The first bit of code is a password protection, science equipment tends to get nicked alot.
 
Top