Home  |  Contents 
Microcontroller and PC projects
  Forum Index : Microcontroller and PC projects         Section
Subject Topic: AS3935 lightning detector Post ReplyPost New Topic
<< Prev Page of 5 Next >>
Author
Message << Prev Topic | Next Topic >>
TassyJim
Guru
Guru


Joined: 07 August 2011
Location: Australia
Online Status: Offline
Posts: 2221
Posted: 27 June 2017 at 2:53am | IP Logged Quote TassyJim

Curtis,
I am having problems getting the SPI to talk.
Can you post your SPI settings or a snippet of code showing a read or write to the chip?

SPI is not familiar territory for me.

Jim

__________________
It all started with the ZX81....
VK7JH
http://www.c-com.com.au/MMedit.htm


Back to Top View TassyJim's Profile Search for other posts by TassyJim Visit TassyJim's Homepage
 
Justplayin
Senior Member
Senior Member
Avatar

Joined: 31 January 2014
Location: United States
Online Status: Offline
Posts: 248
Posted: 27 June 2017 at 5:00am | IP Logged Quote Justplayin

Here is a quick brute force clip which will read the main settings registers from the chip. I actually did my testing by reading the data table (0x09 to 0x32) so I could compare the values and verify I was actually reading the registers correctly.

Geoff provides two ways to read and write SPI, I find this method work best for me.


  DIM AA(10) AS Integer
  '**** SPI Port Setting ****
  Spi Open 1000000, 1, 8
  '  pin 3 is SPI Out for MicroMite 170
  '  pin 25 is SPI Clock for MicroMite 170
  '  Pin 14 is SPI In for MicroMite 170 
  LightCS = 26  'Chip Select for Lightening Detector board
  Setpin LightCS, Dout
  Pin(LightCS) = 1 'Deselect Ligtening Detector CS with high
  PAUSE 50 

  Pin(LightCS) = 0  'select Ligtening Detector
  PAUSE 50   
  SPI WRITE 1, 64 'Read starting at register 0
  SPI READ 9, AA() 'Read 8 bytes (registers 0 to 8) 
  Print AA(0), AA(1), AA(2), AA(3), AA(4), AA(5), AA(6), AA(7),  AA(8)
  Pin(LightCS) = 1  'Deselect Ligtening Detector



* unfortunately I'm unable to test this clip on the hardware I have on hand, but it should work as I copied it from my test program and added a few comments.

--Curtis

__________________
I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!!

CGCOLORMAX2, CGMMSTICK1, DUINOMITE-MINI, MICROMITE-150, MICROMITE-170, SKINNYMITE, Explore64
Back to Top View Justplayin's Profile Search for other posts by Justplayin
 
CaptainBoing
Guru
Guru
Avatar

Joined: 07 September 2016
Location: United Kingdom
Online Status: Offline
Posts: 300
Posted: 27 June 2017 at 5:29am | IP Logged Quote CaptainBoing

TassyJim wrote:

I still think that having a dog frantically trying to climb up my leg is a more reliable detector.


poor pup I had a greyhound bitch years ago (she was a real sweetie and smart too) but we knew there was a storm coming long before we could hear it. She just dissolved into a trembling, panting mess, clearly terrified.

(just had her dew-claws removed and feeling very sorry for herself)



__________________

all you need is duct tape and WD40 (if it moves and shouldn't, use duct tape; if it doesn't move and should, use WD40)
Back to Top View CaptainBoing's Profile Search for other posts by CaptainBoing
 
TassyJim
Guru
Guru


Joined: 07 August 2011
Location: Australia
Online Status: Offline
Posts: 2221
Posted: 27 June 2017 at 7:27am | IP Logged Quote TassyJim

All working on SPI now Curtis.
I had the SPI setting correct but had the 'read/write' bit wrong. Serves me right for coding with a bad head cold.

This code is set for SPI but has the I2C code there as well.
change I2Cmode = 1 and you are in I2C land but it will not work on the current micromites because of the 'illegal' device address used.

There are functions to do most things and a small main program to setup and listen.

' lightning detector code translated from Arduino code
  ' by Jim Hiley tassyjim on TBS forum
  '
  '*   SEN-39001-R01 (universal applications)
  '*   ---> http://www.playingwithfusion.com/productview.php?pdid=22
  '*
  '* Copyright  2015 Playing With Fusion, Inc.
  '* SOFTWARE LICENSE AGREEMENT: This code is released under the MIT License.
  '*
  '* Permission is hereby granted, free of charge, to any person obtaining a
  '* copy of this software and associated documentation files (the "Software"),
  '* to deal in the Software without restriction, including without limitation
  '* the rights to use, copy, modify, merge, publish, distribute, sublicense,
  '* and/or sell copies of the Software, and to permit persons to whom the
  '* Software is furnished to do so, subject to the following conditions:
  '*
  '* The above copyright notice and this permission notice shall be included in
  '* all copies or substantial portions of the Software.
  '*
  '* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  '* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  '* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  '* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  '* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  '* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  '* DEALINGS IN THE SOFTWARE.
  '* **************************************************************************
  '* REVISION HISTORY:
  '* Author          Date          Comments
  '* J. Steinlage          2015Jul20       I2C release based on SPI example
  '*
  '* Playing With Fusion, Inc. invests time and resources developing open-source
  '* code. Please support Playing With Fusion and continued open-source
  '* development by buying products from Playing With Fusion!
  '*
  '* **************************************************************************
  '* APPLICATION SPECIFIC NOTES (READ THIS!!!):
  '* - This file configures then runs a program on a micromite to interface with
  '*   an AS3935 Franklin Lightning Sensor manufactured by AMS.
  '*    - Perform setup for AS3935 chip
  '*      --- capacitance registers for tuning (based on cal value provided)
  '*      --- configurations for your application specifics (indoor/outdoor, etc)
  '*    - read status/info from sensor
  '*    - Write formatted information to serial port
  '* - Set configs for your specific needs using the #defines for wiring, and
  '*   review the setup() function for other settings (indoor/outdoor, for example)
  '* - I2C specific note: This example uses the I2C interface via the I2C lib, not
  '*   the 'Wire' lib included with the Arduino IDE.
  '*
  '* Circuit:
  '     I2C             SPI         -->  SEN-39001: AS3935 Breakout
  '*    SDA             MOSI        -->  MOSI/SDA
  '      nc             MISO        -->  MISO
  '*    SCK             SCK         -->  SCK/SCL
  '*    3.3V            GND         -->  SI  select interface; GND=SPI, VDD=I2C
  '*    IRQ_PIN         IRQ_PIN     -->  IRQ use a count pin for reading freq when calibrating
  '*    GND             CS_PIN      -->  CS  pull CS to ground for I2C
  '*    GND             GND         -->  GND
  '*    3.3V            3.3V        -->  Micromite I/O is at 3.3V, so power board from 3.3V
  '**************************************************************************/
  OPTION EXPLICIT
  'option default none
  DIM AS INTEGER I2Cmode              = 0   ' 1 = I2C  0 = SPI
  ' defines for hardware config
  DIM AS INTEGER CS_PIN               = 4        ' CS used for SPI mode, GND for I2C
  DIM AS INTEGER IRQ_PIN              = 6        ' any suitable pin for interrupt
  DIM AS INTEGER AS3935_ADD           = &H03     ' x03 - standard PWF SEN-39001-R01 config
  DIM AS INTEGER AS3935_CAPACITANCE   = 96       ' <-- SET THIS VALUE TO THE NUMBER LISTED ON YOUR BOARD
  SETPIN CS_PIN, DOUT
  PIN(CS_PIN) = 1-I2Cmode
  
  ' defines for general chip settings
  'dim as integer AS3935_INDOORS       = 0
  DIM AS INTEGER AS3935_OUTDOORS      = 0        ' 1 = outdoors 0 = indoor
  'dim as integer AS3935_DIST_DIS      = 0
  DIM AS INTEGER AS3935_DIST_EN       = 1
  DIM AS INTEGER re, AS3935_ISR_Trig, int_src, lightning_dist_km
  
  AS3935_setup
  SETPIN IRQ_PIN, INTH, AS3935_ISR
  '
  DO
    ' This program only handles an AS3935 lightning sensor. It does nothing until
    ' an interrupt is detected on the IRQ pin.
    IF AS3935_ISR_Trig = 1 THEN ' lightning sensor triggered
      PAUSE 5
      ' reset interrupt flag
      AS3935_ISR_Trig = 0
      ' now get interrupt source
      int_src = AS3935_GetInterruptSrc()
      SELECT CASE int_src
        CASE 0
          PRINT "interrupt source result not expected"
        CASE 1
          lightning_dist_km = AS3935_GetLightningDistKm();
          PRINT "Lightning detected! Distance to strike: ";
          PRINT lightning_dist_km;
          PRINT " kilometers"
        CASE 2
          PRINT "Disturber detected"
        CASE 3
          PRINT "Noise level too high"
        CASE ELSE
          PRINT "OOPS!! The module shouldn't return  "+STR$(int_src)
      END SELECT
      re = AS3935_PrintAllRegs() ' for debug...
    ENDIF
  LOOP
  
END
  
SUB AS3935_setup
  PRINT "Playing With Fusion: AS3935 Lightning Sensor, SEN-39001-R01"
  PRINT "beginning boot procedure...."
  re = AS3935_DefInit()   ' set registers to default
  ' now update sensor cal for your application and power up chip
  re = AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_OUTDOORS, AS3935_DIST_EN)
  ' AS3935_ManualCal Parameters:
  '   --> capacitance, in pF (marked on package)
  '   --> indoors/outdoors (AS3935_INDOORS:0 / AS3935_OUTDOORS:1)
  '   --> disturbers (AS3935_DIST_EN:1 / AS3935_DIST_DIS:2)
  ' function also powers up the chip
  re = AS3935_PrintAllRegs()
  AS3935_ISR_Trig = 0           ' clear trigger
END SUB
  
  
SUB AS3935_ISR
  ' this is irq handler for AS3935 interrupts,
  ' always make code in interrupt handlers fast and short
  AS3935_ISR_Trig = 1
END SUB
  
FUNCTION AS3935_reg_read(RegAdd)
  LOCAL RegData
  IF I2Cmode = 1 THEN
    I2C OPEN 400, 1000
    I2C WRITE AS3935_ADD, 1, 1, RegAdd
    PAUSE 1
    I2C READ AS3935_ADD, 1, 1, RegData
    I2C CLOSE
  ELSE
    SPI OPEN 1000000, 1, 8
    PIN(CS_PIN) = 0
    PAUSE 1
    RegData = SPI((RegAdd AND &H3F) OR &H40)
    RegData = SPI(&H00)
    PIN(CS_PIN) = 1
    SPI CLOSE
  ENDIF
  AS3935_reg_read = RegData
END FUNCTION
  
FUNCTION AS3935_reg_write(RegAdd, DataMask, RegData)
  LOCAL OrigRegData, NewRegData, re
  ' start by reading original register data (only modifying what we need to)
  OrigRegData = AS3935_reg_read(RegAdd)
  ' calculate new register data... 'delete' old targeted data, replace with new data
  ' note: 'DataMask' must be bits targeted for replacement
  ' add'l note: this function does NOT shift values into the proper place... they need to be there already
  NewRegData = ((OrigRegData AND (&HFF XOR DataMask)) OR (RegData AND DataMask))
  ' finally, write the data to the register
  IF I2Cmode = 1 THEN
    I2C OPEN 400, 1000
    PAUSE 1
    I2C WRITE AS3935_ADD, 1, 2, RegAdd, NewRegData
    AS3935_reg_write = MM.I2C
    I2C CLOSE
  ELSE
    SPI OPEN 1000000, 1, 8
    PIN(CS_PIN) = 0
    PAUSE 1
    SPI WRITE 2, (RegAdd AND &H3F),NewRegData
    PIN(CS_PIN) = 1
    SPI CLOSE
  ENDIF
  PRINT "wrt: ";HEX$(NewRegData,2);" Act: ";HEX$(AS3935_reg_read(RegAdd),2)' for debugging
END FUNCTION
  
FUNCTION AS3935_DefInit()
  LOCAL re
  ' run PRESET_DEFAULT Direct Command to set all registers in default state
  IF I2Cmode = 1 THEN
    I2C OPEN 400, 1000
    PAUSE 1
    I2C WRITE AS3935_ADD, 1, 2, &H3C, &H96
    AS3935_DefInit = MM.I2C
    I2C CLOSE
  ELSE
    SPI OPEN 1000000, 1, 8
    PIN(CS_PIN) = 0
    PAUSE 1
    SPI WRITE 2, &H3C,&H96
    PIN(CS_PIN) = 1
    SPI CLOSE
  ENDIF
  PAUSE 2                         ' wait 2ms to complete
END FUNCTION
  
FUNCTION CalRCO()
  ' run ALIB_RCO Direct Command to cal internal RCO
  IF I2Cmode = 1 THEN
    I2C OPEN 400, 1000
    PAUSE 1
    I2C WRITE AS3935_ADD, 1, 2, &H3D, &H96
    CalRCO = MM.I2C
    I2C CLOSE
  ELSE
    SPI OPEN 1000000, 1, 8
    PIN(CS_PIN) = 0
    PAUSE 1
    SPI WRITE 2, &H3D,&H96
    PIN(CS_PIN) = 1
    SPI CLOSE
  ENDIF
  PAUSE 2                         ' wait 2ms to complete
END FUNCTION
  
FUNCTION AS3935_PowerUp()
  LOCAL re
  ' power-up sequence based on datasheet, pg 23/27
  ' register 0x00, PWD bit: 0 (clears PWD)
  re = AS3935_reg_write(&H00, &H01, &H00)
  re = CalRCO()                                                 ' run RCO cal cmd
  re = AS3935_reg_write(&H08, &H20, &H20)     ' set DISP_SRCO to 1
  PAUSE 2
  re = AS3935_reg_write(&H08, &H20, &H00)     ' set DISP_SRCO to 0
  AS3935_PowerUp = 1
END FUNCTION
  
FUNCTION AS3935_PowerDown()
  LOCAL re
  ' register 0x00, PWD bit: 0 (sets PWD)
  re = AS3935_reg_write(&H00, &H01, &H01)
  PRINT "AS3935 powered down"
  AS3935_PowerDown = re
END FUNCTION
  
FUNCTION AS3935_DisturberEn()
  LOCAL re
  ' register 0x03, PWD bit: 5 (resets MASK_DIST)
  re = AS3935_reg_write(&H03, &H20, &H00)
  PRINT "disturber detection enabled"
  AS3935_DisturberEn = re
END FUNCTION
  
FUNCTION AS3935_DisturberDis()
  LOCAL re
  ' register 0x03, PWD bit: 5 (sets MASK_DIST)
  re = AS3935_reg_write(&H03, &H20, &H20)
  PRINT "disturber detection disabled"
  AS3935_DisturberDis = re
END FUNCTION
  
FUNCTION AS3935_SetIRQ_Output_Source(irq_select)
  LOCAL re
  ' set interrupt source - what to displlay on IRQ pin
  ' reg 0x08, bits 5 (TRCO), 6 (SRCO), 7 (LCO)
  ' only one should be set at once, I think
  ' 0 = NONE, 1 = TRCO, 2 = SRCO, 3 = LCO
  SELECT CASE irq_select
    CASE 1
      re = AS3935_reg_write(&H08, &HE0, &H20)               ' set only TRCO bit
    CASE 2
      re = AS3935_reg_write(&H08, &HE0, &H40)               ' set only SRCO bit
    CASE 3
      re =AS3935_reg_write(&H08, &HE0, &H80)               ' set only LCO bit
    CASE ELSE ' assume 0
      re =AS3935_reg_write(&H08, &HE0, &H00)               ' clear IRQ pin display bits
  END SELECT
  AS3935_SetIRQ_Output_Source = re
END FUNCTION
  
FUNCTION AS3935_SetTuningCaps(cap_val)
  LOCAL re
  ' Assume only numbers divisible by 8 (because that's all the chip supports)
  IF cap_val >= 120 THEN     ' cap_value out of range, assume highest capacitance
    re =AS3935_reg_write(&H08, &H0F, &H0F)               ' set capacitance bits to maximum
  ELSE
    re =AS3935_reg_write(&H08, &H0F, (cap_val>>3))     ' set capacitance bits
  ENDIF
  re = AS3935_reg_read(&H08) AND &H0F
  PRINT "capacitance set to 8x";STR$(re);" = ";STR$(re * 8);"pF"
  AS3935_SetTuningCaps = re
END FUNCTION
  
FUNCTION AS3935_GetInterruptSrc()
  LOCAL re, int_src
  ' definition of interrupt data on table 18 of datasheet
  ' for this function:
  ' 0 = unknown src, 1 = lightning detected, 2 = disturber, 3 = Noise level too high
  PAUSE 3                              ' wait 3ms before reading (min 2ms per pg 22 of datasheet)
  int_src = (AS3935_reg_read(&H03) AND &H0F)     ' read register, get rid of non-interrupt data
  SELECT CASE int_src
    CASE &H08
      re =  1                         ' lightning caused interrupt
    CASE &H04
      re = 2                         ' disturber detected
    CASE &H01
      re = 3                         ' Noise level too high
    CASE ELSE
      re = 0                         ' interrupt cause unknown
  END SELECT
  AS3935_GetInterruptSrc = re
END FUNCTION
  
FUNCTION AS3935_GetLightningDistKm()
  LOCAL strike_dist
  strike_dist = (AS3935_reg_read(&H07) AND &H3F)     ' read register, get rid of non-distance data
  AS3935_GetLightningDistKm = strike_dist         ' 63 equals out of range
END FUNCTION
  
FUNCTION AS3935_GetStrikeEnergyRaw()
  LOCAL energy_raw
  energy_raw = (AS3935_reg_read(&H06) AND &H1F) << 8     ' MMSB, shift 8  bits left, make room for MSB
  energy_raw = energy_raw OR AS3935_reg_read(&H05)          ' read MSB
  energy_raw = energy_raw << 8                                                            ' shift 8 bits left, make room for LSB
  energy_raw = energy_raw OR AS3935_reg_read(&H04)          ' read LSB, add to others
  AS3935_GetStrikeEnergyRaw = energy_raw
END FUNCTION
  
FUNCTION AS3935_SetMinStrikes(min_strk)
  LOCAL re
  ' This function sets min strikes to the closest available number, rounding to the floor,
  ' where necessary, then returns the physical value that was set. Options are 1, 5, 9 or 16 strikes.
  ' see pg 22 of the datasheet for more info (#strikes in 17 min)
  SELECT CASE min_strk
    CASE 1
      re = AS3935_reg_write(&H02, &H30, &H00)
      re = 1
    CASE min_strk < 9
      re = AS3935_reg_write(&H02, &H30, &H10)
      re = 5
    CASE min_strk < 16
      re = AS3935_reg_write(&H02, &H30, &H20)
      re = 9
    CASE ELSE
      re = AS3935_reg_write(&H02, &H30, &H30)
      re = 16
  END SELECT
  AS3935_SetMinStrikes = re
END FUNCTION
  
FUNCTION AS3935_SetIndoors()
  ' AFE settings addres 0x00, bits 5:1 (10010, based on datasheet, pg 19, table 15)
  ' this is the default setting at power-up (AS3935 datasheet, table 9)
  AS3935_SetIndoors = AS3935_reg_write(&H00, &H3E, &H24)
  PRINT "set up for indoor operation"
END FUNCTION
  
FUNCTION AS3935_SetOutdoors()
  ' AFE settings addres 0x00, bits 5:1 (01110, based on datasheet, pg 19, table 15)
  AS3935_SetOutdoors = AS3935_reg_write(&H00, &H3E, &H1C)
  PRINT "set up for outdoor operation"
END FUNCTION
  
FUNCTION AS3935_ClearStatistics()
  LOCAL re
  ' clear is accomplished by toggling CL_STAT bit 'high-low-high' (then set low to move on)
  re = AS3935_reg_write(&H02, &H40, &H40)               ' high
  re = AS3935_reg_write(&H02, &H40, &H00)               ' low
  re = AS3935_reg_write(&H02, &H40, &H40)               ' high
  AS3935_ClearStatistics = 1
END FUNCTION
  
FUNCTION AS3935_GetNoiseFloorLvl()
  LOCAL reg_raw
  ' NF settings addres 0x01, bits 6:4
  ' default setting of 010 at startup (datasheet, table 9)
  reg_raw = AS3935_reg_read(&H01)          ' read register 0x01
  AS3935_GetNoiseFloorLvl = (reg_raw AND &H70)>>4                    ' should return value from 0-7, see table 16 for info
END FUNCTION
  
FUNCTION AS3935_SetNoiseFloorLvl(nf_sel)
  ' NF settings addres 0x01, bits 6:4
  ' default setting of 010 at startup (datasheet, table 9)
  IF nf_sel <= 7 THEN                                        ' nf_sel within expected range
    AS3935_SetNoiseFloorLvl = AS3935_reg_write(&H01, &H70, ((nf_sel AND &H07)<<4))
  ELSE                              ' out of range, set to default (power-up value 010)
    AS3935_SetNoiseFloorLvl = AS3935_reg_write(&H01, &H70, &H20)
  ENDIF
END FUNCTION
  
FUNCTION AS3935_GetWatchdogThreshold()
  LOCAL reg_raw
  ' This function is used to read WDTH. It is used to increase robustness to disturbers,
  ' though will make detection less efficient (see page 19, Fig 20 of datasheet)
  ' WDTH register: add 0x01, bits 3:0
  ' default value of 0001
  ' values should only be between 0x00 and 0x0F (0 and 7)
  reg_raw = AS3935_reg_read(&H01)
  AS3935_GetWatchdogThreshold = reg_raw AND &H0F
END FUNCTION
  
FUNCTION AS3935_SetWatchdogThreshold(wdth)
  ' This function is used to modify WDTH. It is used to increase robustness to disturbers,
  ' though will make detection less efficient (see page 19, Fig 20 of datasheet)
  ' WDTH register: add 0x01, bits 3:0
  ' default value of 0001
  ' values should only be between 0x00 and 0x0F (0 and 7)
  AS3935_SetWatchdogThreshold = AS3935_reg_write(&H01, &H0F, (wdth AND &H0F))
END FUNCTION
  
FUNCTION AS3935_GetSpikeRejection()
  LOCAL reg_raw
  ' This function is used to read SREJ (spike rejection). Similar to the Watchdog threshold,
  ' it is used to make the system more robust to disturbers, though will make general detection
  ' less efficient (see page 20-21, especially Fig 21 of datasheet)
  ' SREJ register: add 0x02, bits 3:0
  ' default value of 0010
  ' values should only be between 0x00 and 0x0F (0 and 7)
  reg_raw = AS3935_reg_read(&H02)
  AS3935_GetSpikeRejection = reg_raw AND &H0F
END FUNCTION
  
  
FUNCTION AS3935_SetSpikeRejection(srej)
  ' This function is used to modify SREJ (spike rejection). Similar to the Watchdog threshold,
  ' it is used to make the system more robust to disturbers, though will make general detection
  ' less efficient (see page 20-21, especially Fig 21 of datasheet)
  ' WDTH register: add 0x02, bits 3:0
  ' default value of 0010
  ' values should only be between 0x00 and 0x0F (0 and 7)
  AS3935_SetSpikeRejection = AS3935_reg_write(&H02, &H0F, (srej & &H0F))
END FUNCTION
  
FUNCTION AS3935_SetLCO_FDIV(fdiv)
  ' This function sets LCO_FDIV register. This is useful in the tuning of the antenna
  ' LCO_FDIV register: add 0x03, bits 7:6
  ' default value: 00
  ' set 0, 1, 2 or 3 for ratios of 16, 32, 64 and 128, respectively.
  ' See pg 23, Table 20 for more info.
  AS3935_SetLCO_FDIV = AS3935_reg_write(&H03, &HC0, ((fdiv AND &H03) << 5))
END FUNCTION
  
FUNCTION AS3935_PrintAllRegs()
  'local energy_val
  PRINT ""
  PRINT TIME$;"  ";DATE$
  PRINT "Reg &H00: ";HEX$(AS3935_reg_read(&H00),2)
  PRINT "Reg &H01: ";HEX$(AS3935_reg_read(&H01),2)
  PRINT "Reg &H02: ";HEX$(AS3935_reg_read(&H02),2)
  PRINT "Reg &H03: ";HEX$(AS3935_reg_read(&H03),2)
  PRINT "Reg &H04: ";HEX$(AS3935_reg_read(&H04),2)
  PRINT "Reg &H05: ";HEX$(AS3935_reg_read(&H05),2)
  PRINT "Reg &H06: ";HEX$(AS3935_reg_read(&H06),2)
  PRINT "Reg &H07: ";HEX$(AS3935_reg_read(&H07),2)
  PRINT "Reg &H08: ";HEX$(AS3935_reg_read(&H08),2)
  '
  PRINT "AFE Gain Boost:       ";BIN$((AS3935_reg_read(&H00)AND &B00111110)>>1,5)
  PRINT "Power-down:           ";BIN$((AS3935_reg_read(&H00)AND &B00000001),1)
  PRINT "Noise Floor Level:    ";BIN$((AS3935_reg_read(&H01)AND &B01110000)>>4,3)
  PRINT "Watchdog threshold:   ";BIN$((AS3935_reg_read(&H01)AND &B00001111),4)
  PRINT "Clear statistics:     ";BIN$((AS3935_reg_read(&H02)AND &B01000000)>>6,1)
  PRINT "Minimum num lightning:";BIN$((AS3935_reg_read(&H02)AND &B00110000)>>4,2)
  PRINT "Spike rejection:      ";BIN$((AS3935_reg_read(&H02)AND &B00001111),4)
  PRINT "Freq div -ant tuning: ";BIN$((AS3935_reg_read(&H03)AND &B11000000)>>6,2)
  PRINT "Mask Disturber:       ";BIN$((AS3935_reg_read(&H03)AND &B00100000)>>5,1)
  PRINT "Interrupt:            ";BIN$((AS3935_reg_read(&H03)AND &B00001111),4)
  PRINT "Energy LSB:           ";BIN$(AS3935_reg_read(&H04)AND &B11111111,8)
  PRINT "Energy MSB:           ";BIN$(AS3935_reg_read(&H05)AND &B11111111,8)
  PRINT "Energy MMSB:          ";BIN$(AS3935_reg_read(&H06)AND &B00011111,5)
  PRINT "Distance estimation:  ";BIN$(AS3935_reg_read(&H07)AND &B00111111,6)
  PRINT "Display LCO on IRQ:   ";BIN$((AS3935_reg_read(&H08)AND &B10000000)>>7,1)
  PRINT "Display SRCO on IRQ:  ";BIN$((AS3935_reg_read(&H08)AND &B01000000)>>6,1)
  PRINT "Display TRCO on IRQ:  ";BIN$((AS3935_reg_read(&H08)AND &B00100000)>>5,1)
  PRINT "Internal Tuning Caps: ";BIN$(AS3935_reg_read(&H08)AND &B00001111,4)
  PRINT "Energy:   ";STR$(AS3935_GetStrikeEnergyRaw())
END FUNCTION
  
FUNCTION AS3935_ManualCal(capacitance, location, disturber)
  LOCAL re
  ' start by powering up
  re = AS3935_PowerUp()
  ' indoors/outdoors next...
  IF location = 1 THEN    ' set outdoors if 1
    re = AS3935_SetOutdoors()
  ELSE    ' set indoors if anything but 1
    re = AS3935_SetIndoors()
  ENDIF
  ' disturber cal
  IF disturber = 0 THEN                                   ' disabled if 0
    re = AS3935_DisturberDis()
  ELSE                                                  ' enabled if anything but 0
    re = AS3935_DisturberEn()
  ENDIF
  re = AS3935_SetIRQ_Output_Source(0)
  
  PAUSE 500
  ' capacitance first... directly write value here
  re = AS3935_SetTuningCaps(capacitance)
  PRINT "AS3935 manual cal complete"
END FUNCTION
  


When an event is detected, it prints a dump of the registers
Disturber detected

17:02:34  27-06-2017
Reg &H00: 24
Reg &H01: 22
Reg &H02: C2
Reg &H03: 00
Reg &H04: 35
Reg &H05: FC
Reg &H06: 07
Reg &H07: 3F
Reg &H08: 0C
AFE Gain Boost:       10010
Power-down:           0
Noise Floor Level:    010
Watchdog threshold:   0010
Clear statistics:     1
Minimum num lightning:00
Spike rejection:      0010
Freq div -ant tuning: 00
Mask Disturber:       0
Interrupt:            0000
Energy LSB:           00110101
Energy MSB:           11111100
Energy MMSB:          00111
Distance estimation:  111111
Display LCO on IRQ:   0
Display SRCO on IRQ:  0
Display TRCO on IRQ:  0
Internal Tuning Caps: 1100
Energy:   523317


I haven't played around with calibrating yet.
Maybe tomorrow.



__________________
It all started with the ZX81....
VK7JH
http://www.c-com.com.au/MMedit.htm
Back to Top View TassyJim's Profile Search for other posts by TassyJim Visit TassyJim's Homepage
 
CircuitGizmos
Guru
Guru
Avatar

Joined: 08 September 2011
Location: United States
Online Status: Offline
Posts: 976
Posted: 27 June 2017 at 2:41pm | IP Logged Quote CircuitGizmos

TassyJim wrote:

I still think that having a dog frantically trying to climb up my leg is a more reliable detector.

Jim


Impossible to find in SMT.

__________________
Micromites and Maximites! - Beginning Maximite
Back to Top View CircuitGizmos's Profile Search for other posts by CircuitGizmos Visit CircuitGizmos's Homepage
 
Phil23
Guru
Guru


Joined: 27 March 2016
Location: Australia
Online Status: Offline
Posts: 1094
Posted: 27 June 2017 at 9:31pm | IP Logged Quote Phil23

Looking good,

Can't wait to give it a try.
Am I right in thinking I can't use it in SPI mode on an LCD Backpack if I also need the SPI bus for the display?

Or will both co-exist with the CS overriding the display as required.

Have you worked out what the individual module calibration is about yet?
I've only read of it on their website & can't say I noticed it in the Arduino code.

Quote:
We store calibration values for each board shipped. This information can be provided at your request by contacting Technical Support and referencing your original order number.


Phil.
Back to Top View Phil23's Profile Search for other posts by Phil23
 
TassyJim
Guru
Guru


Joined: 07 August 2011
Location: Australia
Online Status: Offline
Posts: 2221
Posted: 27 June 2017 at 11:03pm | IP Logged Quote TassyJim

Phil,
My unit came with the calibration value printed on the package - 96pF
Once I have it in a suitable case, I will re-calibrate.

The unit is a receiver centered on 500kHz. In calibration mode, the tuned circuit frequency is output on the INT pin so it can easily be measured.
Simply read the frequency and keep changing the capacitance value register until the frequency is close to 500kHz.
The functions to do this are in my code.
All I need to do is to put together a main program to use the functions. Maybe today sometime.
The micromite should be OK to use for the measurement provided the CLOCKTRIM has been set.

Using multiple devices on the same SPI bus should be OK provided the CS pin is used on ALL devices. Once the lightning detector is running, any communications with it are not time critical so fitting reads between display activity should be easy enough.

Jim


__________________
It all started with the ZX81....
VK7JH
http://www.c-com.com.au/MMedit.htm
Back to Top View TassyJim's Profile Search for other posts by TassyJim Visit TassyJim's Homepage
 
Justplayin
Senior Member
Senior Member
Avatar

Joined: 31 January 2014
Location: United States
Online Status: Offline
Posts: 248
Posted: 28 June 2017 at 1:30am | IP Logged Quote Justplayin

Looks great Jim! Doesn't work for me, but it looks great!

Right now I am getting a constant scrolling screen with "Noise level too high". I think the problem is the WDTH value. The docs say the default should be 1, but as read from the register on startup it's 2. In my own code, I set it to 1 which nearly eliminated all the noise level messages.

I don't have any time tonight to play around with your code... maybe tomorrow (darn job!) .

--Curtis

__________________
I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!!

CGCOLORMAX2, CGMMSTICK1, DUINOMITE-MINI, MICROMITE-150, MICROMITE-170, SKINNYMITE, Explore64
Back to Top View Justplayin's Profile Search for other posts by Justplayin
 
TassyJim
Guru
Guru


Joined: 07 August 2011
Location: Australia
Online Status: Offline
Posts: 2221
Posted: 28 June 2017 at 3:06am | IP Logged Quote TassyJim

I have had a play with setting the tuning capacitor.
It assumes that your INT pin is also a counting pin.
Replace the code starting after the DIM statements to the end of the main loop with:
Quote:
DIM newtune, k$
AS3935_setup
SETPIN IRQ_PIN, INTH, AS3935_ISR
'
DO
' This program only handles an AS3935 lightning sensor. It does nothing until
' an interrupt is detected on the IRQ pin.
IF AS3935_ISR_Trig = 1 THEN ' lightning sensor triggered
PAUSE 5
' reset interrupt flag
AS3935_ISR_Trig = 0
' now get interrupt source
int_src = AS3935_GetInterruptSrc()
SELECT CASE int_src
CASE 0
PRINT "interrupt source result not expected"
CASE 1
lightning_dist_km = AS3935_GetLightningDistKm();
PRINT "Lightning detected! Distance to strike: ";
PRINT lightning_dist_km;
PRINT " kilometers"
CASE 2
PRINT "Disturber detected"
CASE 3
PRINT "Noise level too high"
CASE ELSE
PRINT "OOPS!! The module shouldn't return "+STR$(int_src)
END SELECT
re = AS3935_PrintAllRegs()
' for debug...
ENDIF
PAUSE 5
k$ =
INKEY$
IF UCASE$(k$) = "C" THEN setcap
LOOP

END

' check tuning
SUB setcap
SETPIN IRQ_PIN, FIN
re = AS3935_SetIRQ_Output_Source(
3)
re = AS3935_CAPACITANCE
DO
PAUSE 2000
PRINT "Frequency: ";STR$(PIN(IRQ_PIN)*16)
INPUT "New tuning capacitor (1 - 120pF)? ", newtune
IF newtune > 0 AND newtune <121 THEN
re = AS3935_SetTuningCaps(newtune)
PRINT "Capacitor set to ";STR$(re*8);"pF"
PRINT ""
ENDIF

LOOP UNTIL newtune <0
AS3935_CAPACITANCE = re
PAUSE 1000
AS3935_setup
END SUB




The oscillator frequency appears on the iNT pin so you can connect a meter or use the 'mite to read it.
The frequency is the nominal 500kHz divided by 16 = 31250 Hz, well within the range of the 'mites.

pressing 'C' while in the main loop will initiate the setcap SUB.
It displays the current frequency and asks for a new capacitor value. Valid values are from 1 - 120. The software changes your entered value to a multiple of 8 and sets the register.
It then displays the new frequency.
entering -1 exits the setcap SUB and returns to the main loop.

You should then alter the code with your shiny new capacitor value.

A session looks like:
Quote:
Frequency: 503168
New tuning capacitor? 0 - 120pF110
wrt: 8D Act: 8D
capacitance set to 8x13 = 104pF
Capacitor set to 104pF

Frequency: 501568
New tuning capacitor? 0 - 120pF120
wrt: 8F Act: 8F
capacitance set to 8x15 = 120pF
Capacitor set to 120pF

Frequency: 498368
New tuning capacitor? 0 - 120pF116
wrt: 8E Act: 8E
capacitance set to 8x14 = 112pF
Capacitor set to 112pF

Frequency: 499952
New tuning capacitor? 0 - 120pF-1
Playing With Fusion: AS3935 Lightning Sensor, SEN-39001-R01
beginning boot procedure....



Edited by TassyJim on 28 June 2017 at 3:26am


__________________
It all started with the ZX81....
VK7JH
http://www.c-com.com.au/MMedit.htm
Back to Top View TassyJim's Profile Search for other posts by TassyJim Visit TassyJim's Homepage
 
Justplayin
Senior Member
Senior Member
Avatar

Joined: 31 January 2014
Location: United States
Online Status: Offline
Posts: 248
Posted: 29 June 2017 at 1:39am | IP Logged Quote Justplayin

Jim,

The setcap routine was fun to play with and I can see maybe 88pf value would be better than the 80pf marked on my boards packaging. BUT, your routine doesn't seem to be passing the correct values back to the main program. When I select 88pf the main program sets up for 8pf.

Currently the program is not scrolling with the "Noise level too high" message however when I trigger the lightening emulator it only registers as a "disturber" no matter what level strike I trigger.

Wish I had more to toy around with the code and compare/decode register values, unfortunately it has been a little extra busy at the office.

--Curtis

__________________
I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!!

CGCOLORMAX2, CGMMSTICK1, DUINOMITE-MINI, MICROMITE-150, MICROMITE-170, SKINNYMITE, Explore64
Back to Top View Justplayin's Profile Search for other posts by Justplayin
 
TassyJim
Guru
Guru


Joined: 07 August 2011
Location: Australia
Online Status: Offline
Posts: 2221
Posted: 29 June 2017 at 1:58am | IP Logged Quote TassyJim

It looks like I left the *8 of the end of
Quote:
AS3935_CAPACITANCE = re*8


Once you know the new value, set in the DIM statement so it starts with the new value.
Interestingly, I get better results with a higher value of cap than the supplied one too.
supplied = 96, new value = 112

I measured the frequency with a meter as well as the 'mite.

Jim


__________________
It all started with the ZX81....
VK7JH
http://www.c-com.com.au/MMedit.htm
Back to Top View TassyJim's Profile Search for other posts by TassyJim Visit TassyJim's Homepage
 
Phil23
Guru
Guru


Joined: 27 March 2016
Location: Australia
Online Status: Offline
Posts: 1094
Posted: 29 June 2017 at 10:05am | IP Logged Quote Phil23

Bugger,

Still haven't had a chance to try your code yet Jim, & had a pearler of a storm last night.

Phil.

Back to Top View Phil23's Profile Search for other posts by Phil23
 


<< Prev Page of 5 Next >>
In the news...
 
Post ReplyPost New Topic
Printable version Printable version
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum

Powered by Web Wiz Forums version 7.8
Copyright ©2001-2004 Web Wiz Guide

This page was generated in 0.0938 seconds.
Privacy Policy     Process times : 0, 0, 0, 0.09