'**************************************************************** '* Name : VOLTS-AMPS.BAS * '* Author : Ed Cannady * '* Notice : Copyright (c) 2012 Copyright (c) 2012 * '* : All Rights Reserved * '* Date : 3/26/2012 * '* Version : 1.0 * '* Notes : 16F628A PIC and LTC1298 A/D * '* : Allegro ACS758LCB-050U * '**************************************************************** DEFINE LOADER_USED 1 DEFINE OSC 10 Include "Modedefs.bas" CK VAR PortA.0 ' Clock from Pin 17 to Pin 7 of the 1298 yellow DOUT VAR PortA.1 ' Data Out from Pin 18 to Pin 6 of the 1298 orange DIN VAR PortB.6 ' Data In from Pin 12 to Pin 5 of the 1298 violet CS VAR PortB.7 ' Chip Select From Pin 13 to Pin 1 of the 1298 blue addr VAR BYTE ' Channel address / mode result VAR WORD ' Reading from the A/D Vbits VAR WORD ' Variable to store A/D volts reading Abits VAR WORD ' Variable to store A/D Amps reading REF VAR word ' 0.0 Amps reference point AMPS VAR WORD ' Amps reading VOLTS VAR WORD ' Volts reading cmcon = 7 ' Set all the PIC inputs to be digital REF = 489 ' Start with a reference number AMPS = 0 ' Set the Amps value to zero VOLTS = 0 ' Set the Volts value to zero HIGH CS ' Set the A/D Chip select to inactive Goto mainloop ' Skip subroutines getad: CS = 0 ' A/D Chip select active ' ** Send address / mode - Start bit, 3 bit addr, null bit].** SHIFTOUT DIN, CK, MSBFIRST, [1\1, ADDR\3, 0\1] SHIFTIN DOUT, CK, MSBPRE, [RESULT\12] 'Get 12-bit results CS = 1 ' A/D Chip select inactive Return ' ** Subroutine to get Volts value (channel 0) GETVOLTS: addr = $05 ' Single ended, channel 0, MSBF high Gosub getad Vbits = result Return ' ** Subroutine to get Amps value (channel 1) GETAMPS: addr = $07 ' Single ended, channel 1, MSBF high Gosub getad Abits = result IF Abits < 494 THEN REF = Abits ' No Load cal. / sup. voltage / temp. comp. Return mainloop: Gosub GETVOLTS ' Get the Volts Reading gosub GETAMPS ' Get the AMPS Reading ' ** VOLTS SECTION VOLTS = ((Vbits*/ 500)>>4)*3 ' ** AMPS SECTION AMPS = (Abits - REF)* 20 GOTO mainloop