-- JAL 2.4i  - Windctrl.jal
-- purpose                : Windmill controller/logger with 12F675
-- hardware author        : Glenn (http://www.thebackshed.com)
-- PIC software author    : Vasile Guta Ciucur
-- date      : 02-MAY-2009
-- used: Jallib package 0.2.0 (http://code.google.com/p/jallib/)

include 12f675 
-- set fuses
pragma target clock       4_000_000
pragma target OSC         INTOSC_NOCLKOUT
pragma target WDT         DISABLED
pragma target PWRTE       DISABLED
pragma target MCLR        INTERNAL
pragma target CPD         DISABLED
pragma target CP          DISABLED
pragma target BROWNOUT    DISABLED
pragma target BG          HIGHEST_BANDGAP_VOLTAGE
-- pragma target fuses       0x3f94

-- variable declarations
var word Volt   
var word Amp
var word RPM
var word WSpeed      
var byte Mode    

-- ADC settings
const ADC_hardware_Nchan      = 2         -- number of selected channels
const ADC_hardware_NVref      = 0         -- number of external references
const ADC_hardware_Rsource    = 10_000    -- maximum source resistance (10K)
const ADC_hardware_high_resolution = true -- true = high resolution = 10 bits
                                          -- false = low resolution = 8 bits
var volatile byte ADCON1 is adcon0
-- -- get the library, after defining the constants
include adc_hardware

-- define transmission settings
const Serial_SW_Baudrate = 4800
const Serial_SW_invert   = false
-- define the software transmit pin
var volatile bit  Serial_SW_tx_pin is GPIO_GP0
-- use following line to deactivate rx if you don't need it
var volatile bit Serial_SW_rx_pin
include Serial_Software
include format             

-- registers_setup (INIT routine in assembler)
assembler
  -- ========================
  bsf _status, _rp0 -- bank 1
  -- ========================
  call  0x3ff   -- get the factory calibrated OSC value
  movwf osccal  -- for accurate timing
  -- ------------- start -------------------
  -- gp1, gp2. gp3 and gp4 as input
  -- gp0 and gp5 as output
  movlw 0b_0001_1110
  movwf trisio
  -- ------------- end ---------------------
  clrf wpu      -- wpu = 0
  -- -------===-- start ---------
  -- an0(gp0) and an2(gp2) set as digital
  -- an1(gp1) and an3(gp4) as analog
  movlw 0b_0011_1010  
  movwf ansel
  -- ------------ end -----------
  -- ========================
  bcf _status, _rp0 -- bank 0
  -- ========================
  clrf gpio     -- init gpio
  -- -------===-- start ---------
  movlw 0b_0000_0111 -- 7 decimal,
  movwf cmcon   -- that mean comparator off
  -- ------------ end -----------
  -- we don't set adcon0 register because is
  -- set by adc_read() routine
end assembler
-- end registers_setup

-- initialize the AD converter according to the above parameters
ADC_init
-- start initializations if functions are used
Serial_SW_init             
 
-- timer dependant counter
const timer0_isr_rate = 1000
include picaxe_count
timer0_isr_init()             

forever loop
  -- read data
  Volt   = adc_read(3) -- (GP4)
  Amp    = adc_read(1) -- (GP1) 
  -- init data
  RPM    = 0
  WSpeed = 0
  -- see if charging/diverting is needed
  if Volt > 571 then -- 14.4V - dummy load on
    GPIO_GP5 = 1
    Mode = 1
  end if
  if Volt < 558 then -- 13V - charge, dummy load off
    GPIO_GP5 = 0
    Mode = 0                          
  end if
  -- count F&P coil pulses on positive edge over a one second period.
  my_count(GPIO_GP3 , 1000 , RPM)
  -- count anemometer pulses on positive edge over a one second period.
  my_count(GPIO_GP2 , 1000 , WSpeed)
  -- send battery volt value to PC
  serial_sw_data = "["
  serial_sw_data = "<"
  serial_sw_data = "V"
  serial_sw_data = ">"
  format_word_dec(serial_sw_data, Volt, 4, 0)
  serial_sw_data = "<"
  serial_sw_data = "/"
  serial_sw_data = "V"
  serial_sw_data = ">"
  -- send battery amps value to PC 
  serial_sw_data = "<"
  serial_sw_data = "I"
  serial_sw_data = ">"
  format_word_dec(serial_sw_data, Amp, 4, 0)
  serial_sw_data = "<"
  serial_sw_data = "/"
  serial_sw_data = "I"
  serial_sw_data = ">"  
  -- send Windmill RPM value to PC
  serial_sw_data = "<"
  serial_sw_data = "R"
  serial_sw_data = ">"
  format_word_dec(serial_sw_data, RPM, 4, 0)
  serial_sw_data = "<"
  serial_sw_data = "/"
  serial_sw_data = "R"
  serial_sw_data = ">"
  -- send Windspeed value to PC
  serial_sw_data = "<"
  serial_sw_data = "S"
  serial_sw_data = ">"
  format_word_dec(serial_sw_data, WSpeed, 4, 0)
  serial_sw_data = "<"
  serial_sw_data = "/"
  serial_sw_data = "S"
  serial_sw_data = ">"
  -- send Mode value to PC
  serial_sw_data = "<"
  serial_sw_data = "M"
  serial_sw_data = ">"
  format_word_dec(serial_sw_data, Mode, 1, 0)
  serial_sw_data = "<"
  serial_sw_data = "/"
  serial_sw_data = "M"
  serial_sw_data = ">"
  serial_sw_data = "]"
  -- carriage return and line feed
  -- comment these lines if you want Glenn's logger
  -- compatibility (needed if you want to use his software).
  -- use them only when you do some voltage calibration
  -- for easy data reading.
  -- serial_sw_write(13)
  -- serial_sw_write(10)
  --
end loop