PIC Odometer - docs (long)

Simon Bosworth simonb at primenet.com
Sat Jun 27 15:37:09 GMT 1998


This is a multi-part message in MIME format.
--------------37B53641F82D3200405C4D9C
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hello all,
for anyone interested in the PIC odometer code I sent out the other day,
here is a text file that goes into some
detail of how the code works.

Cheers,
Simon

--
------------------------
Simon Bosworth
simonb at primenet.com


--------------37B53641F82D3200405C4D9C
Content-Type: text/plain; charset=us-ascii; name="odom.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="odom.txt"


		PIC Based Odometer
		------------------

Author: Simon Bosworth, June 1998
-------

Description:
------------

  The odometer consists of a PIC16F84 micro and a 16X1 LCD display. It provides 3 counters, 
two of which can be reset by the user and a calibration value, (also user settable), that
determines the number of input pulses per 1/100 mile.
  The non-resettable counter is intended as a true odometer, the other two counters as 
'trip meters'. The counters are saved when the ignition is turned off.

  The operator controls consist of 3 push-buttons.  The 'function' button selects which counter
,(or the calibration value), is displayed.  The 'Reset' button clears either of the two 
trip meters.  If the calibration value, (pulses per 1/100 mile), is displayed, then the reset
button works as a 'down' button to decrement the value and the remaining button is the 'up'
button to increment the value.

  Another input signal is 'ignition on'.  This used signal that the car igniton has been
turned off.  The processor should be powered either from a circuit that is always on, (it has a low current drain),
or from the accessory circuit.  When the code sees that the ignition has been turned off,
the contents of the counters and calibration value will be saved to the processor's intrnal
EEPROM.  When the ignition is turned on again, the values will be retrieved from EEPROM.


The Code:
---------
  The program is written in 'C' for the CCS compiler. I will give a brief description of 
each function in the program.

Main()
------
  The program starts here. The data direction registers for PORTA are set and then the
LCD module is initialised. The EEPROM data is moved to the working counters. Interrupts
are enabled.  The program then goes into its main loop which first checks for the ignition
to be on and then displays whichever counter is currently selected. The function then 
looks for any buttons that might be pressed.  If a button is pressed the appropriate
function is called to handle it.

DisplayCounter()
----------------
  This function displays one of the three counters. The counters are stored in BCD form, 
4 bits per digit.  This avoids binary to decimal conversion of 32 bit numbers, which the 
CCS compiler will not handle.  

IncCounter()
------------
  This function is called by the Interrupt Service Routine (ISR).  It just adds 1 to
one of the 3 BCD counters.

extint_isr()
------------
  This is the interrupt service routine.  It is called whenever a pulse is detected on
the external interrupt pin, (PB0).  It calls IncCounter() to add 1 to each of the three
BCD counters.

ButtonPressed()
---------------
  This function is used to see if a particular button is pressed.  If the specified button
is at a logic 0, (pressed), the function will wait 40mS and check again, (debounce). If
the button is still pressed then the function will return TRUE.

DoFunction()
------------
  This function is called when the 'Function' key has been pressed.  It adds one to the
flag which selects which counter to display, thus causing main() to display the new counter.
If the next counter is the calibration value, then the code will enter a loop that does the
following:  checks for the 'Up' and 'Down/Reset' buttons and if they are pressed, increments
or decrements the cal. counter. If the 'Function' button is pressed the loop will exit and
set the display flag to the first counter.

DisplayCal()
------------
  This function does a binary to decimal conversion of the calibration value and displays
it on the LCD module.

DoPowerOff()
------------
  This function saves the counters and calibration data to the 16F84 internal EEPROM.
It waits for the ignition to be turned on again before exiting.

DoStartup()
-----------
  This function reads the data from the EEPROM and sets the counters and calibration value.
If this is the first time that the program has run, the EEPROM data will be all '1's and
the function detects this and sets the counters to 0.

ZeroCounter()
-------------
  This function will set a BCD counter to 0.

LcdInit()
---------
  This function intialises the LCD module.  The LCD is set to use 'nibble', (4 bit), mode
to save on I/O lines.

SendLcdData()
-------------
  This function will send either a command or data to the LCD.

LCDtglclk()
-----------
  This function toggles the LCD 'Enable' line to write data, (4 bits), to the LCD.  

BusyCheck()
-----------
  This function checks to see if the LCD is still processing the last command it was sent.

lcd_putc()
----------
  This function will send a char, or string of chars to the LCD.




--------------37B53641F82D3200405C4D9C--




More information about the Diy_efi mailing list