Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Simple PIC Interrupt

Status
Not open for further replies.

uncrocks

Newbie level 6
Joined
Sep 12, 2012
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,391
I'm writing a program for a PIC12HV615 to control the power to a computer and properly shut it down based on inputs from the ignition of a car and a momentary button. I'm writing the code in mBasic. Here's what I have so far:
Code:
'Raspberry Pi Control
STATE VAR BYTE
STATE = 0
MANOFF VAR BYTE
MANOFF = 0
MANON VAR BYTE
MANON = 0
IGN VAR BYTE
WORKBUT VAR BYTE
WORKBUT = 0
INDEX VAR BYTE
INDEX = 0
TIMER VAR BYTE
TIMER = 0

OUTPUT GP1 'RELAY
RELAY CON GP1
OUTPUT GP2 'SHUTDOWN
SHUTDOWN CON GP2
INPUT GP5 'IGNITION
INPUT GP0 'BUTTON

SLEEP:
	'SLEEP UNTIL GP5 OR GP0 GOES HIGH
	IGN = GP5
	IF IGN = 1 THEN
		MANON = 0
		GOTO POWERON
	ELSE
		BUTTON GP0,1,255,0,WORKBUT,1,SLEEPMANUAL
		PAUSE 20
	ENDIF
	GOTO SLEEP

SLEEPMANUAL:
	MANON = 1
	MANOFF = 0
	GOTO POWERON

ONLOOP:
	INPUT1 = GP5
	IF INPUT1 = 0 AND MANON = 0 THEN
		MANOFF = 0
		MANON = 0
		GOTO POWEROFF
	ELSE
		GOTO BUTTONCHECK
	ENDIF
	IF MANON = 1 AND INDEX < 141 THEN 'LOOP THROUGH .1 SECOND DELAY 142 SETS OF 255 TIMES ABOUT 1 HOUR
		TIMER = TIMER + 1
		IF TIMER = 255 THEN
			TIMER=0
			INDEX = INDEX + 1
		ENDIF
		PAUSE 100
	ELSE IF INDEX = 142
		GOTO POWEROFF
	ENDIF
	GOTO ONLOOP

BUTTONCHECK:
	BUTTON GP0,1,255,0,WORKBUT,1,MANUAL
	PAUSE 20
	GOTO ONLOOP
	
POWERON:
	HIGH RELAY
	LOW SHUTDOWN
	STATE = 1
	IF MANON = 1 THEN
		INDEX = 0
		TIMER = 0		
	ENDIF
	GOTO ONLOOP
	
POWEROFF:
	PAUSE 300000 '5 MINUTES
	IGN = GP5
	IF IGN = 1 THEN GOTO ONLOOP
	HIGH SHUTDOWN
	PAUSE 120000 '2 MINUTES
	LOW RELAY
	LOW SHUTDOWN
	STATE = 0
	INDEX = 0
	TIMER = 0
	IF MANOFF = 0 THEN
		GOTO SLEEP
	ELSE
		GOTO MANLOOP
	ENDIF

MANUAL:
	IF STATE = 1 THEN
		MANOFF = 1
		GOTO POWEROFF
	ELSE
		MANON = 1
		GOTO POWERON
	ENDIF

MANLOOP:
	IGN = GP5
	IF IGN = 1 THEN
		BUTTON GP0,1,255,0,WORKBUT,1,MANUAL
		PAUSE 20
		GOTO MANLOOP
	ELSE
		GOTO SLEEP
	ENDIF

The GP's are the pins (not sure if that's how I should refer to them?)
I have two questions:
1) In the SLEEP subsection, how do I actually make the chip go to sleep until a change occurs on GP0 or GP5 (I know the chip is capable of this).
2) Is a 300000ms pause too many bits? or do I need to loop through a pause of a shorter length?

Thanks in advance!
Also any other suggestions/code clean-ups are welcome :p
 

I don't use basic but does your code actually sleeps the uc or just waits until a change on GPx? If latter, you may put the uc in sleep by using the asm command 'sleep' somehow.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top