The MAIN routine:
Code: Select all
Device = 18F16Q41
Clock = 64
Config
FEXTOSC = OFF, // Oscillator not enabled
RSTOSC = HFINTOSC_64MHZ,// HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1
CLKOUTEN = OFF, // CLKOUT function is disabled
PR1WAY = ON, // PRLOCKED bit can be cleared and set only once
CSWEN = ON, // Writing to NOSC and NDIV is allowed
FCMEN = OFF, // Fail-Safe Clock Monitor disabled
FCMENP = OFF, // Fail-Safe Clock Monitor disabled
FCMENS = OFF, // Fail-Safe Clock Monitor Disabled
MCLRE = EXTMCLR, // If LVP=0, MCLR pin is MCLR; If LVP=1, RE3 pin function is MCLR
PWRTS = PWRT_64, // PWRT set at 64ms
MVECEN = OFF, // Interrupt contoller does not use vector table to prioritze interrupts
IVT1WAY = ON, // IVTLOCKED bit can be cleared and set only once
LPBOREN = OFF, // Low-Power BOR disabled
BOREN = SBORDIS, // Brown-out Reset enabled , SBOREN bit is ignored
BORV = VBOR_2P85, // Brown-out Reset Voltage (VBOR) set to 2.8V
ZCD = OFF, // ZCD module is disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
PPS1WAY = OFF, // PPSLOCKED bit can be set and cleared repeatedly (subject to the unlock sequence)
STVREN = ON, // Stack full/underflow will not cause Reset
LVP = ON, // HV on MCLR/VPP must be used for programming
XINST = OFF, // Extended Instruction Set and Indexed Addressing Mode disabled
WDTCPS = WDTCPS_0, // Divider ratio 1:32
WDTE = OFF, // WDT Disabled; SWDTEN is ignored
WDTCWS = WDTCWS_7, // window delay = 50%; no software control; keyed access required
WDTCCS = SOSC, // WDT reference clock is SOSC
BBSIZE = BBSIZE_512,// Boot Block size is 512 words
BBEN = OFF, // Boot block disabled
SAFEN = OFF, // SAF disabled
DEBUG = OFF, // Background Debugger disabled
WRTB = OFF, // Boot Block not Write protected
WRTC = OFF, // Configuration registers not Write protected
WRTD = OFF, // Data EEPROM not Write protected
WRTSAF = OFF, // SAF not Write Protected
WRTAPP = OFF, // Application Block not write protected
CP = OFF // PFM and Data EEPROM code protection disabled
#option USART_TX = PORTA.4 '
#option USART_RX = PORTA.5 '
#option USART2_TX = PORTC.1 '
#option USART2_RX = PORTB.5 '
#option ACC_SPI_CS = PORTC.6 ' Accel chip slelect
#option MEM_SPI_CS = PORTB.6 ' Accel chip slelect
#option SPI_SCK = PORTC.7 ' SPI clock
#option SPI_SDI = PORTC.3 ' SPI data in
#option SPI_SDO = PORTC.4 ' SPI data out
// import modules...
Include "intosc.bas"
Include "convert.bas"
Include "system.bas"
Include "USART.BAS"
Include "USART2.bas"
Include "setdigitalio.bas"
Include "string.bas"
Include "EEPROM.bas"
Include "pps.bas"
Include "SPIxv.bas"
Include "M85RS256.BAS"
Include "IIS328.bas"
'Include "SV_SETUP.bas"
Private Dim
red As PORTC.0, ' High to turn on
blue As PORTA.2,
re As PORTC.2, ' Low to recieve
de As PORTB.4, ' High to send
int_1 As PORTC.5, ' Accel int 1
int_2 As PORTB.7, ' Accel int 2
a_cs As PORTC.6, ' Accel chip select
m_cs As PORTB.6, ' Memory chip select
rdy As PORTC.5 ' Accelorometer data ready
Private Dim
id As Byte,
b1 As Byte,
XYZ_int As int_XYZ ' Structure from IIS328
'****************************************************************************
Sub BLINK_LED()
High(red)
DelayMS(80)
Low(red)
High(blue)
DelayMS(80)
Low(blue)
End Sub
'****************************************************************************
Private Sub SET_TERM_SERIAL()
pps.unlock()
' USART1
Input(PORTA.5) ' Make RX1 pin an input
U1RXPPS = $05 ' RA5 > RX1
Output(PORTA.4) ' Make TX1 pin an output
RA4PPS = $10 ' TX1 > RA4
pps.lock()
USART.SetBaudrate(br9600) ' Baud rate
USART.ReadTerminator = 13 ' CR
End Sub
'****************************************************************************
Private Sub SET_485_SERIAL()
pps.unlock()
' USART2
Output(PORTC.1) ' Make TX2 pin an output
RC1PPS = $13 ' TX2 > RC1
Input(PORTB.5) ' Make RX2 pin an input
U2RXPPS = $15 ' RX2 > RB5
pps.lock()
USART2.SetBaudrate(br9600) ' Baud rate
USART2.ReadTerminator = 13
End Sub
'****************************************************************************
Public Sub SET_SPI()
pps.unlock()
' SPI
Input(PORTC.3) 'Make C3 pin an input
SPI1SCKPPS = $17 'RC7 > SCK2
RC7PPS = $1B 'SCK1 > RC7 (bidir)
SPI1SDIPPS = $13 'RC3 > SDI1
RC4PPS = $1C 'SDO1 > RC4
pps.lock()
End Sub
'****************************************************************************
SetAllDigital()
PMD0 = $3D ' Clock stuff enabled
PMD1 = $F0 ' Timers enabled
PMD2 = $E7 ' Clock stuff enabled, DAC ADC disabled
PMD3 = $2F ' USART1,2 AND SPI1 enabled
PMD4 = $FF ' USART3 disabled
PMD5 = $FF '
Low(blue)
Low(red)
Input(re)
Input(de)
Input(int_1)
Input(int_2)
Input(rdy)
High(a_cs)
High(m_cs)
BLINK_LED
SET_SPI
SET_TERM_SERIAL
USART.ClearOverrun
USART.Write(0,0,"I'M ALIVE!",10,13)
'USART.ClearOverrun
'If USART.DataAvailabletimeout(1000) = true Then
' b1 = USART.ReadByte
' SV_SETUP.SET_ID
' DelayMS(3)
'EndIf
'id = EE.ReadByte($00)
'USART.Write("ID = ",DecToStr(id),10,13)
'DelayMS(3)
IIS328.WHO
IIS328.INIT
'****************************************************************
MAIN:
XYZ_int = IIS328.READ_XYZ
' IIS328.READ_XYZ ' Just to make sure the routine runs
USART.Write(DecToStr(XYZ_int.x)," ",DecToStr(XYZ_int.y)," ",DecToStr(XYZ_int.z),10,13)
End
Code: Select all
Module IIS328
Include "USART.bas"
Include "convert.bas"
Include "setdigitalio.bas"
Include "M85RS256.BAS"
Include "SPIxv.bas"
Include "utils.bas"
Dim
a_cs As ACC_SPI_CS.ACC_SPI_CS@
' SPI
Private Dim
red As PORTC.0, ' High to turn on
blue As PORTA.2,
rdy As PORTC.5 ' Accelorometer data ready
Private Dim
lp1 As Word,
lp2 As Word,
b1 As Byte,
reg_dat(56) As Word,
w1 As Word,
w2 As Word,
w3 As Word,
w4 As Word,
w5 As Word,
wx(50) As Word,
wy(50) As Word,
wz(50) As Word,
ix(51) As Integer,
iy(51) As Integer,
iz(51) As Integer
' diff_x As Integer,
' diff_y As Integer,
' diff_z As Integer
Private Const
who_cmd = $8F, ' Who Am I
reg1_r = $A0, ' Read Reg 1, single read
reg1_r_m = $E0, ' Read Reg 1, multiple read
reg1_w = $20, ' Write Reg 1, single write
reg1_w_m = $60, ' Write Reg 1, multiple write
xyz_off = $20, ' Reg 1 - power down
xyz_50 = $27, ' XYZ anabled at 50 Hz - Reg 1
pwr_dn = $00, ' Power downt the accell - Reg 1
' reg_2 = $14,
reg_2 = $00, ' No internal filter
reg_3 = $82, ' Int 1 is Data Ready, active low
' reg_3 = $80, ' Int 1 is Data Ready, active low
reg_4 = $C0, ' Block update, big endinan, 2g, self test off, 4-wire SPI
' reg_5 = $03, ' Awake
reg_5 = $00, ' Sleep to wake
filter_rst_r = $A5, ' HP FILTER RESET single read
clear_int1 = $B1, ' Clear interupt 1
int_1_dur = $01, ' Interupt 1 duration
r_xyz = $E8, ' Multiple read XYZ registers starting at $28
acc_off = $07 ' Turn off accel at REG1
Public Structure int_XYZ
x As Integer
y As Integer
z As Integer
End Structure
'******************************************************************************
Public Sub ACCEL_SPI()
SPI.Open(SPI_MODE_3,spiOscDiv16,spiSampleMiddle) '
Low(a_cs)
End Sub
'******************************************************************************
Private Sub LED()
High(red)
DelayMS(60)
Low(red)
High(blue)
DelayMS(60)
Low(blue)
End Sub
'******************************************************************************
Public Function WHO() As Byte
result = 0
ACCEL_SPI
SPI.Transfer(who_cmd)
result = SPI.Transfer
High(a_cs)
USART.Write(" WHO AM I - ",HexToStr(result,2),10,13)
End Function
'******************************************************************************
Public Sub HP_FILTER_RESET()
ACCEL_SPI
SPI.Transfer(filter_rst_r)
b1 = SPI.Transfer
High(a_cs)
End Sub
'******************************************************************************
Public Sub INIT()
ACCEL_SPI
SPI.Transfer(reg1_w_m)
SPI.Transfer(xyz_50)
SPI.Transfer(reg_2)
SPI.Transfer(reg_3)
SPI.Transfer(reg_4)
SPI.Transfer(reg_5)
High(a_cs)
' test
' Low(a_cs)
' SPI.Transfer($72) ' Multiple write to INT1 THS - DURATION
' SPI.Transfer($05)
' SPI.Transfer($01)
' High(a_cs)
HP_FILTER_RESET
' Low(a_cs)
' SPI.Transfer($30) ' Single write to INT1 CFG
' SPI.Transfer($3F)
' High(a_cs)
{ Low(a_cs)
SPI.Transfer(reg1_r_m)
w1 = SPI.Transfer
w2 = SPI.Transfer
w3 = SPI.Transfer
w4 = SPI.Transfer
w5 = SPI.Transfer
High(a_cs)
USART.Write("REG 1 = ",HexToStr(w1,2),10,13)
USART.Write("REG 2 = ",HexToStr(w2,2),10,13)
USART.Write("REG 3 = ",HexToStr(w3,2),10,13)
USART.Write("REG 4 = ",HexToStr(w4,2),10,13)
USART.Write("REG 5 = ",HexToStr(w5,2),10,13)}
End Sub
{'****************************************************************
Private Sub DELAY_232()
While USART.TRMT = 0
Wend
DelayUS(10)
End Sub}
'******************************************************************************
Public Sub TURN_ON()
ACCEL_SPI
SPI.Transfer(reg1_w)
SPI.Transfer(xyz_50)
High(a_cs)
USART.Write("SET REG_1 TO h27",10,13)
End Sub
'******************************************************************************
Public Sub ACCEL_OFF()
ACCEL_SPI
SPI.Transfer(reg1_w)
SPI.Transfer(pwr_dn)
High(a_cs)
End Sub
'******************************************************************************
Public Sub CLEAR_INT_1()
ACCEL_SPI
SPI.Transfer(clear_int1)
b1 = SPI.Transfer
High(a_cs)
' USART.Write("INT1_SRC = ",HexToStr(b1),10,13)
End Sub
'******************************************************************************
Public Sub READ_ALL_REG()
b1 = 0
ACCEL_SPI
SPI.Transfer($E0)
For lp1 = $20 To $37
reg_dat(lp1) = SPI.Transfer
Next
High(a_cs)
' For lp1 = $20 To $37
' USART.Write("REG h",HexToStr(lp1)," = ",HexToStr(reg_dat(lp1),2),10,13)
' Next
End Sub
'******************************************************************************
Private Function TWOS_COMP(acc_data As Word) As Integer
' USART.write(0,0,DecToStr(acc_data)," ")
' DelayMS(3)
If acc_data > 32767 Then
result = acc_data - 65536
Else
result = acc_data
EndIf
End Function
'******************************************************************************
Public Sub XYZ() ' Dummy read -
While rdy = 1
Wend
Low(a_cs)
SPI.Transfer(r_xyz)
w1.byte1 = SPI.Transfer
w1.byte0 = SPI.Transfer
w2.byte1 = SPI.Transfer
w2.byte0 = SPI.Transfer
w3.byte1 = SPI.Transfer
w3.byte0 = SPI.Transfer
High(a_cs)
CLEAR_INT_1
End Sub
'******************************************************************************
Public Function READ_XYZ() As int_XYZ
ix(50) = 0
iy(50) = 0
iz(50) = 0
High(blue)
DelayMS(500)
For lp1 = 0 To 9
XYZ
Next
For lp1 = 0 To 49
While rdy = 1
Wend
Low(a_cs)
SPI.Transfer(r_xyz)
wx(lp1).byte1 = SPI.Transfer
wx(lp1).byte0 = SPI.Transfer
wy(lp1).byte1 = SPI.Transfer
wy(lp1).byte0 = SPI.Transfer
wz(lp1).byte1 = SPI.Transfer
wz(lp1).byte0 = SPI.Transfer
High(a_cs)
Next
Low(blue)
For lp1 = 0 To 49
' USART.Write("x ",DecToStr(wx(lp1),5)," y ",DecToStr(wy(lp1),5)," z ",DecToStr(wz(lp1),5),10,13)
ix(lp1) = TWOS_COMP(wx(lp1))
iy(lp1) = TWOS_COMP(wy(lp1))
iz(lp1) = TWOS_COMP(wz(lp1))
' USART.Write(DecToStr(lp1),"- x ",DecToStr(ix(lp1),6)," y ",DecToStr(iy(lp1),6)," z ",DecToStr(iz(lp1),6),10,13)
Next
' BUBBLE SORT
For lp1 = 0 To (49)
For lp2 = 1 To (50)
If ix(lp1) > ix(lp2) Then ' Compare this and next x data
Swap(ix(lp1),ix(lp2))
EndIf
If iy(lp1) > iy(lp2) Then ' Compare this and next x data
Swap(iy(lp1),iy(lp2))
EndIf
If iz(lp1) > iz(lp2) Then ' Compare this and next x data
Swap(iz(lp1),iz(lp2))
EndIf
Next
Next
' For lp1 = 1 To 49
' USART.Write(DecToStr(lp1),"- ",DecToStr(ix(lp1),6)," ",DecToStr(iy(lp1),6)," ",DecToStr(iz(lp1),6),10,13)
' Next
If ix(49) < 0 Then
result.x = (ix(49) * -1) + ix(1)
Else
result.x = ix(1) - ix(49)
EndIf
If iy(49) < 0 Then
result.y = (iy(49) * -1.0) + iy(1)
Else
result.y = iy(1) - iy(49)
EndIf
If iz(49) < 0 Then
result.z = (iz(49) * -1.0) + iz(1)
Else
result.z = iz(1) - iz(49)
EndIf
USART.Write(DecToStr(result.x)," ",DecToStr(result.y)," ",DecToStr(result.z),10,13)
End Function
'******************************************************************************
Input(rdy)
High(a_cs)