PDA

View Full Version : Help with final project



OvERKiLL
- 14th December 2006, 18:04
Hello, I am Working on a project (I call it Fusebox) and I have hit a brick programming wall. The device has a fairly simple function, but correct operation still eludes me. Being unable to find anything but the most basic documentation for this, I am at my wits end. (I have never attempted anything like this before) the device (a PIC16F684) is supposed to run off of the internal clock, wait for input from the OnOff pin, when it turns on, all of the 4 device pins should turn on also, when off, turn them off (Possibly with a delay), the pic is supposed to constantly measure the voltage on a pin (a car battery with a voltage divider) and sound a piezo buzzer if it drops below a value other specifics are commented in the code. If you are able, please help, this is my final project and I need to finish the software (to a functional point) ASAP so I can move on to designing the final project. Any help is appreciated! (BTW I have access to Picbasic pro and am coding in microcode studio) Here is the code:

'************************************************* *****************
'* Name : Fusebox.BAS *
'* Author : Jeff Munk *
'* Notice : Copyright (c) 2006 Jeff Munk *
'* : All Rights Reserved *
'* Date : 11/30/2006(started)-12/12/2006 *
'* Version : 1.0 *
'* Notes : This code should work with chips ohter than the *
'* : 16F684, but this is the chip originaly developed for*
'* : Hope this works... *
'************************************************* *****************

INCLUDE "modedefs.bas" ' Sets up serial communication for debugging

' Defines for setting up the ADC
DEFINE ADC_BITS 10 ' Sets the resolution of the ADC (10 Bits)
DEFINE ADC_CLOCK 3 ' Sets the clock to the internal R/C clock
DEFINE ADC_SAMPLEUS 50 ' Sets number of ms to allow clock to stabilize

' Sets ports to ADC
TRISA = %00000100 ' sets port0 as an input
ADCON0 = %00001001 ' sets port0 as an analog input (bit 4-2)
ANSEl = %00000100 ' sets porta.0 as an analog input
'-------------------------------------------------------------------------------

' Variables and constants
Serial var porta.0 ' Serial Port (for debugging)
Battery var porta.2 ' This is the ADC to be hooked to the referance voltage
Dev1 var porta.3 ' These Controlled
Dev2 var portc.5 ' Are Be
Dev3 var portc.4 ' The To
Dev4 var portc.3 ' Devices
Buzzer var portc.2 ' Warning buzzer
OnOff var portc.1 ' Input from Ignition (needs to be digital!!!!)
Cancel var portc.0 ' Gives the user ability to override lights-off
' command (Fusebox will continue to watch the
' voltage, upon voltage low, lights will be shut-down)
Btn var byte ' Button debounce var
Minimum con 1200 ' Min battery voltage (times 100 needs a 1/3
' voltage divider)
Voltage var word ' Variable to measure voltage
Delay var byte ' Variable to preform delay for certain events
'(like low voltage event)

'--------------------------------Prerequsites-----------------------------------

if onoff = 0 then
goto standby ' Checks for ignition status before boot
endif
' sets all circuts to on and reports via serial
High Dev1
serout serial, 0, ["1ST DEVICE ACTIVE"]
high Dev2
serout serial, 0, ["2ND DEVICE ACTIVE"]
high Dev3
serout serial, 0, ["3RD DEVICE ACTIVE"]
high Dev4
serout serial, 0, ["4TH DEVICE ACTIVE"]

'---------------------------------Main Program----------------------------------

Start:
if onoff = 0 then standby ' Checks for ignition status before program executes
gosub vm 'Measures voltage
if voltage < Minimum then
gosub battlow 'Battery low subroutine
endif
Vok: ' Voltage ok
goto start

'------------------------------Subroutines--------------------------------------
'------------------------------Voltage Measure------------------------------
' Measure voltage subroutine
VM:
adcin battery, Voltage ' Measures voltage
voltage = voltage * 300 ' Removes decimal
serout serial, 0, ["Battery voltage:", Voltage] 'Reports Voltage to computer
return
'================================================= ==============================
'------------------------------Battery Low--------------------------------------
' Action for a battery low event and engine crank handling
Battlow:
for Delay = 1 to 10 ' Delays warning for a time (for vehicle starting)
serout serial, 0, ["Battery Low! (possible alternator problem):", Voltage]
gosub vm
if voltage >= Minimum then vok 'If voltage is acceptable, move on
pause 500 'Wait for half a second
next Delay
SOUND buzzer,[100,10,50,10] ' Send 2 sounds consecutively to Buzzer
pause 1000 ' Wait for 1 second
SOUND Buzzer,[100,10,50,10] ' Send 2 sounds consecutively to Buzzer
return
'================================================= ==============================
'-----------------------------Silent Start--------------------------------------
' Activate lights without startup event
Startup:
serout serial, 0, ["Starting up!"]
high dev1
high Dev2
high dev3
high dev4
goto watch
'================================================= ==============================
'-----------------------------Shutdown-----------------------------------------
' Shutdown after battery low event (also used to deactivate override)
Shutdown:
SOUND buzzer,[200,10,10,10]
For Delay = 1 to 100
button cancel, 0, 255,0 , btn, 1, Watch
next
pause 30000
ImmediateShutdown:
serout serial, 0, ["Shutting down!"]
low dev1
low Dev2
low dev3
low dev4
goto standby
'================================================= ==============================
'-----------------------------Battery Monitor-----------------------------------
'Watches battery voltage, if it drops to a certain point, the devices are disconnected
Watch:
button cancel, 0, 255, 0, btn, 1, ImmediateShutdown
gosub VM ' Measures voltage
if voltage < Minimum then
low dev1
low Dev2
low dev3
low dev4
goto standby
endif
goto watch
'================================================= ==============================
'---------------------------------Standby---------------------------------------
' Waits for Ignition/Manual on event
Standby:
serout serial, 0, ["Standby!"]
button cancel, 0, 255,0 , btn, 1, Startup
if onoff = 1 then
high dev1
high Dev2
high dev3
high dev4
goto start
endif
nap 6
goto standby
'================================================= ==============================
'-------------------------------------End----------------------------------------

skimask
- 14th December 2006, 19:18
Another final project....sounds like more money wasted....or another crappy instructor....

At any rate...
You've got voltage is a word.....
Somewhere in that coding mess you've got:
voltage = voltage * 300 ' Removes decimal
Now what happens when voltage = 219?

Also, you sample the voltage once and go with that number and only that number. Do you have any kind of filter on the line coming from the battery or is it straight from the battery thru a divider? Probably no filters or anything. Easiest thing to do here is to read the ADC a bunch of times and divide the result by the number of times you read it, average out the reading. Or what I do when I have to read a 'slowly' changing analog input....Say my last result was '100'. I read the ADC again, this time it's 110, I add 1 to my last results making it 101. I read the ADC, this time it's 130, I add another 1 to my last, making 102. I read again, this time it's 80, I subtract one. I would hope you get the picture. If the result I just read is within 1 of my last result, I don't change the number at all unless I get 2 successive reads that are both one off.
JDG

OvERKiLL
- 14th December 2006, 20:07
Ok, I have finally found somebody who can help me! I have no clue how to read the output of the ADC. I have a friend who is a compulsive buyer with too much money (thats how I got PBP) when he gets tired of something, he gives it to me. The nasty part about it is that I only got a programmer and the software, no manual! So all that I know I have taught myself. Never having used a ADC before I just took a wild shot in the dark that it would give me a definite voltage (but thinking about it now, I see that I was just wishfully thinking) are there any good guides on how to use them? I have been unable to find anything myself. And also, have I set up the analog input pin correctly for the PIC? the data sheet is kinda confusing. Thank you so much for replying!

Christopher4187
- 15th December 2006, 16:56
Goto http://www.melabs.com/support/index.htm and you can view the manual for free.

Archangel
- 15th December 2006, 20:35
TRISA = %00000100 ' sets port0 as an input
that code sets PortA.2 as input try TRISA = %00000001
also: since you are a student, I will ASSUME you do not have 30+ years of automotive experience, and make recommendations accordingly. Lay hands on an oscilliscope and go probe the output terminal of a running car alternator and learn what it is you are trying to measure, I believe it will be an eye opener.
Automotive systems are "Indian Country" to electronic devices full of spikes and dips and brownouts and oil and grease and high temps and dirt and . . . things that work on your bench often will not work there. JS