Code:
Pause 10 ' This statement is placed at beginning of code for ICD use
;--- if you un-comment these, you must comment the ones in the .inc file--
ASM ; 18F2550/4550, 8mhz crystal
__CONFIG _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC4_PLL6_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _PBADEN_OFF_3H ; PortB resets as digital
__CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
ENDASM
' Set Registers
TRISA = 0 ' PortA output connections are used for LCD interface
TRISB = %00000100 ' PORTB.2 is an input from switch grounding
TRISC.0 = 0 ' PortC.0 is used for a Heartbeat LED output
TRISC.2 = 0 ' PortC.2 is used for the LCD R/W connection
DEFINE OSC 16
DEFINE I2C_SLOW 1 ' Set i2c to the standard speed
DEFINE I2C_HOLD 1 ' Enable recieving i2c device to pause communication
Include "Modedefs.Bas"
INCLUDE "ALLDIGITAL.pbp" ' Sets all registers for digital ops.
' * NOTES for use with EasyPic6 development board:
' Make sure that SW6.8 on the EasyPic6 is set to ON position before powerup
' for LCD's requiring backlight.
'For use with COG 2x16 LCD:
' - Turn on Port Expander switches SW6.1, SW6.2, SW6.3, SW6.4 and SW6.5.
' - Turn on COG LCD 2x16 switches SW10.1, SW10.2, SW10.3, SW10.4, SW10.5 and SW10.6.
'---------------------- 18F4550 Port/PIN Connections ]--------------------
'I/O pin connections to the 18F4550 MCU are as follows:
'PORTA.0 (02) RA0 connected to LCD D4.
'PORTA.1 (03) RA1 connected to LCD D5.
'PORTA.2 (04) RA2 connected to LCD D6.
'PORTA.3 (05) RA3 connected to LCD D7.
'PORTA.4 (06) RA4 not connected
'PORTA.5 (07) RA5 not connected
'PORTB.0 (21) Connected as I2C SDA output to DS1337
'PORTB.1 (22) Connected as I2C SCL output to DS1337
'PORTB.2 (23) Connected as INT2 interrupt from pin-7 (_INTA) of DS1337 RTC.
'PORTB.3 (24) RB3 not connected
'PORTB.4 (25) RB4 not connected
'PORTB.5 (26) RB5 not connected
'PORTB.6 (27) Connected as PGC for ICSP connection
'PORTB.7 (28) Connected as PGD for ICSP connection
'PORTC.0 (11) RC0 not connected.
'PORTC.1 (12) RC1 connected as an output to a Red LED for battery low.
'PORTC.2 (13) RC2 connected to LCD R/W.
'PORTC.3 (14) RC3 not provided on 18F4550.
'PORTC.4 (15) not connected.
'PORTC.5 (16) not connected.
'PORTC.6 (17) RC6 normally connected to poll grounding of external switch.
'PORTC.7 (18) normally not connected.
'--------------[ Declare Variables, Aliases & Constants ]---------------
' Variables & Aliases for Pins other than RTC use
' ===============================================
Digit1 var byte ' Used to parse 3-digit integer into LCD characters
Digit2 var byte ' NOTICE ALL OF THESE DECLARED VARIABLES!!
Digit3 var byte
ext_pwr VAR PORTC.6 ' Alias to control power to Ultrasonic sensor
rng0 VAR w0.Byte0 ' LSB of range measurement when right justified
rng1 VAR w0.byte1 ' MSB of range measurement when right justified
rngNum1 Var Byte ' 1st numeral of rng0 for display on LCD as character
rngNum2 VAR Byte ' 2nd numeral of rng0 for display on LCD as character
rngNum3 VAR BYTE ' 34d numeral of rng0 for display on LCD as character
SCL VAR PORTB.1 ' I2C clock pin
SDA VAR PORTB.0 ' I2C data pin
Spare_1 VAR PORTB.7 ' PGD for ICSP & Spare I/O for normal ops
Spare_2 VAR PORTB.6 ' PGC for ICSP & Spare I/O for normal ops
w0 var word ' W0 is the word value to store the range data
' Constants
'==========
srfdevice CON $E0 ' Device address for SRF02 ultrasonic range finder
Vthr CON 46 ' Threshold (3.3v) to trigger battery low voltage warning
' ...this is dictated by the drop-out and output voltage
' of the voltage regulator used in the power circuit.
' Initialize the display
Pause 500 ' Wait for LCD to startup after power on
GOSUB InitializeDisplay
mainloop:
Lcdout $fe, 1 ' Clear LCD screen
PORTC.0 = 1 ' Blink Heartbeat LED during mainloop
Pause 500 ' Wait 0.5 second
PORTC.0 = 0
If PORTB.2 = 0 Then
Gosub Range
Endif
Goto mainloop ' Do it forever
' ----------------------[ START LIST OF SUBROUTINES ]-------------------
'***********************************************************************
InitializeDisplay: ' Commented out till LCD installed
'=================
'--SETUP FOR USING 2x16 LCD THAT IS INSTALLED IN EASYPIC6-----
' LCD DEFINES FOR USING 2x16 LCD with PortA in EASYPIC6
'======================================================
DEFINE LCD_DREG PORTA ' Use PORTA for LCD Data
DEFINE LCD_DBIT 0 ' Use lower(4) 4 bits of PORTA
' PORTA.0 thru PORTA.3 connects to
' LCD DB4 thru LCD DB-7 respectively
DEFINE LCD_RSREG PORTA ' PORTA for RegisterSelect (RS) bit
DEFINE LCD_RSBIT 4 ' PORTA.4 pin for LCD's RS line
DEFINE LCD_RWREG PORTC ' LCD read/write port
DEFINE LCD_RWBIT 2 ' LCD read/write bit
DEFINE LCD_EREG PORTA ' PORTA for Enable (E) bit
DEFINE LCD_EBIT 5 ' PORTA.5 pin for LCD's E line
DEFINE LCD_BITS 4 ' Using 4-bit bus
DEFINE LCD_LINES 2 ' Using 2 line Display
DEFINE LCD_COMMANDUS 1500 ' Command Delay (uS)
DEFINE LCD_DATAUS 44 ' Data Delay (uS)
' DEFINE LCD Control Constants
Line1 CON 128 ' Point to beginning of line 1 ($80)
Line2 CON 192 ' Point to beginning of line 2 ($C0)
' Test the LCD during initialization
LCDOut $FE,1:FLAGS=0:Pause 250 ' Clear Display
LCDOut $FE,Line1+3," LCD TEST " ' Display "Power On" on 1st line
Pause 500
LCDOut $FE,Line2+2,"..Power On..!!" ' Display "LCD Test" on 2nd line
PAUSE 1000
Return
Move_Delay: ' Subroutine used during text moving on an LCD
'==========
' Function used for text moving
Pause 500 ' You can change the moving speed here
Return
Range:
'=====
ENABLE DEBUG
High PORTC.0 ' Turn on Test LED to indicate Ranging routine entered
HIGH ext_pwr ' Turn on power to the SRF02 in order to use the I2C bus
' without distortion.
PAUSE 3000 ' Delay to let I2C bus stabilize after SRF02 turn on
'DEFINE WRITE_USED 1 'Required if only writing WORD variables in v2.6
' Make SRF02 Range Measurement
I2CWRITE SDA,SCL,$E0,0,[80]' Request start of ranging in inches
pause 100 ' Wait for ranging to finish
I2CREAD SDA,SCL,$E0,2,[rng1,rng0] ' Get ranging results
pause 10
WRITE 253,rng1 ' Write range to EEPROM
PAUSE 10
Write 254,rng0
PAUSE 10
'Convert measurement to LCD numerals in inches
Digit1 = rng0 DIG 0
Digit2 = rng0 DIG 1
Digit3 = rng0 DIG 2
' Display range measurement on LCD display in inches
LCDOut $FE,1 ' Clear display
Pause 50
LCDOut $FE,$80,"Range:" ' Display on 1st line
PAUSE 10
LCDOut $FE,$C0,Digit1,Digit2,Digit3," in." ' Display on 2nd line
PAUSE 1000 ' Allow Time for user to view
LOW ext_pwr ' Turn off power to the SRF02 at end of display
' This statement will distort the I2C bus and ext_pwr
' must be HIGH before any attempts are made to use the
' I2C bus.
PAUSE 1000 ' Delay to permit I2C bus to stabilize after SRF02 off
DISABLE DEBUG
' Resume Main Program
Return
End ' Safety measure to insure program stops if reaches here
Bookmarks