Reading interrupt flags

StigOfTheDump

Senior Member
Hi

I have this code on a 40X2 that causes a hardware interrupt when one of 3 buttons (B.0, B.1 or B.2) is pressed. This sends a message to the AXE133Y to say that the program is interrupted, then when the button is released it carries on counting from where it left off.

I see the setintflags is one byte and has a bit for each button. Can I read this into a variable so that I could have a select case in the interrupt routine to tell me which button had been pressed, if it is not still pressed. Something along the lines of the commented out first line of my interrupt .

Code:
#picaxe 40x2

pullup %11111111
hintsetup %00000111
setint OR %00000000,%00000111,B

pause 500
serout A.4,N2400, (254,1)
pause 10

main:

for w1=0 to 65535
serout A.4,N2400, (254,128,#w1)
pause 10
next w1

serout A.4,N2400, (254,1)

goto main

blue:

serout A.4,N2400, (254,1)
pause 10
serout A.4,N2400, (254,128,"Blue")
pause 1000
serout A.4,N2400, (254,1)
pause 10
return

interrupt:

'if setintflags = %00000000,00000001,B then gosub blue

serout A.4,N2400, (254,1)
pause 10
serout A.4,N2400, (254,128,"Interrupted")
pause 1000
serout A.4,N2400, (254,1)
pause 10


setint OR %00000000,%00000111,B
return
 

tarzan

Senior Member
The system ‘flags’ byte is broken down into individual bit variable. i.e. flags, flag0, flag1, flag2, flag3 etc

Code:
select case flags
case %00000001
serout A.4,N2400, (254,128,"Blue")
case %00000010
serout A.4,N2400, (254,128,"Green")
case %00000100
serout A.4,N2400, (254,128,"Yellow")
endselect
 

geoff07

Senior Member
Rather than sending serial data from the interrupt routine, you might want to do that in the main program. ISRs should be as short as possible. They should detect the change, interpret it, set flags, and exit, That way all the key program logic is in one place, driven by flags that are set invisibly by the ISR. Easier to follow, and less likely that as the code expands, the ISR will take an appreciable time and be unable to keep up with the faster-moving external world.
 

tarzan

Senior Member
Untested code; should display button colour while button is depressed and halt counter until button is released.

Code:
	pullup %00000111
	hintsetup %00000111
	
	pause 500
	
	serout A.4,N2400, (254,1)
	
	pause 10
	
	setintflags %00001000,%00001000

main:

	for w1 = 0 to 65535
	
		serout A.4,N2400, (254,128,#w1)
		
		pause 10
		
	next w1
	
	serout A.4,N2400, (254,1)

goto main

blue:
	
	serout A.4,N2400, (254,128,"Blue")

return

green:
	
	serout A.4,N2400, (254,128,"Green")

return

yellow:
	
	serout A.4,N2400, (254,128,"Yellow")

return

interrupt:

	if flag0 = 1 then 
	
		gosub blue
		
	elseif flag1 = 1 then 
	
		gosub green
		
	elseif flag2 = 1 then 
	
		gosub yellow
	
	endif

	flags = 0

	setintflags %00001000,%00001000
	
	serout A.4,N2400, (254,1)
	
	pause 10	

return
Or replace if statement with case select.

Code:
	select case flags
	
		case %00001001
		
			gosub blue
		
		case %00001010
		
			gosub green
		
		case %00001100
		
			gosub yellow
	
	endselect
 
Last edited:

StigOfTheDump

Senior Member
Thanks for the help. That works now.

Even though I had page 13 of manual 2 open on my screen I did not realise that the name of the flags variable was 'flags'. Thick or what?

In a lot of programming books I have read they use different fonts and colours for different instructions, variables etc. It would probably be a phenomenal task to update the manuals so that it replicated the default programming editor colours. It might be an idea for future additions.

The flags byte variable is made up of 8 bit variables

flag0.....hint0flag.....X2 only - interrupt on B.0
flag1.....hint1flag.....X2 only - interrupt on B.1
flag2.....hint2flag.....X2 only - interrupt on B.2
flag3.....hintflag.......X2 only - interrupt on any of above
flag4.....compflag.....X2 only - occurs on any comparator change
flag5.....hserflag.......hserial background receive has occurred
flag6.....hi2cflag.......hi2c write has occurred (slave mode)
flag7.....toflag.........timer overflow flag

I did try pasting a page of the manual into the programming editor. This highlighted what were variables and key words. This was also very useful as I hadn't realised that hint0flag was also a system variable. It lost the alignment and showed up keywords (such as for) in the middle of sentences. If copied from the original documentation rather than a pdf it might just be a case of adding appropriate comment signs.

Thanks again

Stig
 

srnet

Senior Member
To many different colours in manuals should be avoided at all costs in my view.

And they ought to be made clear, without fancy colours, for those that are colour blind.
 
Top