'18LF4520 8Mhz HC-12 AK8963 091118 1500 Magmaster Define CONFIG1L = 0x00 Define CONFIG1H = 0x09 'INT OSC EXT CLOCK at RA6 Define CONFIG2L = 0x1e Define CONFIG2H = 0x00 Define CONFIG3L = 0x00 Define CONFIG3H = 0x80 'Set for HVP Define CONFIG4L = 0x80 Define CONFIG4H = 0x00 Define CONFIG5L = 0x0f Define CONFIG5H = 0xc0 Define CONFIG6L = 0x0f Define CONFIG6H = 0xe0 Define CONFIG7L = 0x0f Define CONFIG7H = 0x40 'Define SIMULATION_WAITMS_VALUE = 1 'Comment in for SIM out for PIC Define CLOCK_FREQUENCY = 8 Define SINGLE_DECIMAL_PLACES = 2 Define STRING_MAX_LENGTH = 40 'Needs raising to fit the whole NMEA sentence for transmit>>>>>>>>>> AllDigital 'SET PIN IN/OUT TRISA = %00000001 TRISB = %00000000 TRISC = %10010000 '7=RX=1 4=SDI=1 1=RST=0 TRISD = %00000000 '7=compss c/s=0 5=altmtr c/s=0 4=lcd5110 c/s=0 TRISE = %00000001 '1=Button 'SET BITS ON/OFF PORTA = %00000000 'ON/OFF PORTB = %00000000 PORTC = %00000000 PORTD = %01110000 'ALL C/S HIGH PORTE = %00000000 'POSS MCLR RE3 ADCON0 = 0x03 'AN0-1-2-3 Can be used as Analogue inputs. ADCON1 = 0x0e ADCON2 = %10100100 PIR1 = 0 PIR2 = 0 PIE1 = 0 PIE2 = 0 PIE1.RCIE = 1 'rxd Intr , used for GPS serial input work IPR1 = 0 IPR2 = 0 RCON.IPEN = 1 'This MUST be included when using Interrupts OSCCON = %01110010 'internal 8Mhz clock 'OSCTUNE.PLLEN = 1 '*4 PLL enabled so Fosc = 32MHZ WaitMs 10 '--------------------------------------------------------------------------------------- 'SPI 'Define SPI_CS_REG = PORTD 'For SLAVE use 'Define SPI_CS_BIT = 4 Define SPI_SCK_REG = PORTC Define SPI_SCK_BIT = 3 Define SPI_SDI_REG = PORTC 'not used, but left in so OSH does not burp Define SPI_SDI_BIT = 4 Define SPI_SDO_REG = PORTC Define SPI_SDO_BIT = 5 SPIPrepare 'Define SPICLOCK_STRETCH = 20 '?????????????????????????????????????? ''Symbol lcd_reset = PORTC.1 ''Symbol lcd_mode = PORTC.0 '1=DATA ,0=COMMAND Symbol yled = PORTA.4 'yled Symbol rled = PORTA.5 'rled ''Symbol lcd5110 = PORTD.4 'CS 25110 LCD ''Symbol altmtr = PORTD.5 'CS BAROMETER/TEMP Symbol compss = PORTD.5 'TEMP portd.6 'CS AK8963???????????????????????????????? '--------------------------------------------------------------------------------------- Dim msg1 As String 'any msg data will be passed via this string Dim ascval As String Dim x As Byte '?????????????????????????????????????????????????????????? '--------------------------------------------------------------------- 'COMPASS Dim i As Byte '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Dim data As Byte '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Dim b(24) As Byte '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Dim w1a_raw As Byte 'COMPASS ID Dim w1a As Single 'TEMP result in CALC'###################### Dim str_w1a As String Dim info_raw As Byte Dim info As Single Dim str_info As String Dim st_1_raw As Byte Dim st_1 As Single Dim str_st_1 As String 'Dim id_raw As Byte 'COMPASS ID 'Dim id As Single 'TEMP result in CALC'###################### 'Dim strid As String 'Dim rega_raw As Byte 'COMPASS REGA 'Dim strrega As String 'Dim regb_raw As Byte 'COMPASS REGB 'Dim strregb As String 'READ COMPASS X Dim x_raw As Word Dim x_sing As Single Dim str_x As String 'READ COMPASS Y Dim y_raw As Word Dim y_sing As Single Dim str_y As String 'READ COMPASS Z Dim z_raw As Word Dim z_sing As Single Dim str_z As String Dim st_2_raw As Byte Dim st_2 As Single Dim str_st_2 As String Dim cntl_1_raw As Byte Dim cntl_1 As Single Dim str_cntl_1 As String Dim cntl_2_raw As Byte Dim cntl_2 As Single Dim str_cntl_2 As String Dim astc_raw As Byte Dim astc As Single 'Self test Dim str_astc As String Dim i2c_dis_raw As Byte Dim i2c_dis As Single Dim str_i2cdis As String Dim asax_raw As Byte Dim asax As Single Dim str_asax As String Dim asay_raw As Byte Dim asay As Single Dim str_asay As String Dim asaz_raw As Byte Dim asaz As Single Dim str_asaz As String 'Dim mode_raw As Byte 'COMPASS MODE 'Dim strmode As String 'Dim rdy As Byte 'FOR STATUS RDY 'Dim sttus_raw As Byte 'COMPASS STATUS 'Dim strsttus As String Dim azi As Single 'word Dim strazimuth As String Dim picalc As Single '????????????????????????????????????????????????????????????????? Const pi = 3.14159 picalc = 180 / pi Dim addr As Byte 'ADDRESS FOR COMPASS Dim st2 As Byte 'st2 Overflow TEST Dim strst2 As String '--------------------------------------------------------------------------------- '''''''''''''''''''''''''''''''SET UP MODULES '----------------------------------------------------------------------------------- setup: 'SET HC-12 TX 'TO CHANGE SETTINGS PRESS BUTTON!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Symbol radset = LATD.3 'RADSET 0=COMMAND ON 1=DATA ON '-------------------------------------------------------------------------------------- 'COMPASS INITIALISATION SPICSOn compss = 0 'CHIP SELECT COMPASS ON SPISend 0x0b 'CONFIG CNTL2 RESET SPISend 0x01 'WRITE CONFIG CNTL2 RESET compss = 1 'CHIP SELECT COMPASS OFF compss = 0 'CHIP SELECT COMPASS ON SPISend 0x0f 'CONFIG I2C Disable SPISend 0x00 'WRITE CONFIG I2C OFF compss = 1 'CHIP SELECT COMPASS OFF compss = 0 'CHIP SELECT COMPASS ON SPISend 0x0a 'CONFIG CNTL1 reg SPISend 0x12 'WRITE CONFIG 16BIT, CONT MODE 1 'SPISend 0x16 'WRITE CONFIG 16BIT, CONT MODE 2 compss = 1 'CHIP SELECT COMPASS OFF SPICSOff addr = 0x80 'Register READ start address WaitMs 10 '++++++++++++++++++++++++++++++++++++ '------------------------------------------------------------------------------------ Dim freq As Byte '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Dim strfreq As String '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> freq = 50 strfreq = #freq msg1 = "" Hseropen 9600 Hserout "Ready!", CrLf start: 'HC-12 INITIALISATION 'R1R1R1R1R1R1R1R1R1R1R1R1R1R1R HC-12 1R1R1R1R1R1R1R1R1R1R1R1R1R1R1R1R1R1R1R1R1R1R1R1R1 'PRESS BUTTON (OR RADSET) radset = 0 'SET HC-12 COMMAND ON WaitMs 100 Hserout "AT+C002", CrLf '433.800 WaitMs 100 radset = 1 'SET HC-12 RUN ON WaitMs 100 'R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R2R main: '\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ rled = 1 WaitMs 100 rled = 0 WaitMs 100 '[[[[[[[[[[[[[[[[[[[[[[[[[[[ COMPASS NOTES [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ 'you calculate angle in radians from x, y coordinates. 'Angle in degrees = radians × 180 / Pi ~ radians × 57.3 'oshonsoft has Arctan Function For y / x < 1 'You have To calculate in eight octants ( 45 degrees ) 'Octants can be found from signs of x And y And from 'value of y / x If it is less Or greater than one. 'For example, If North is 0 degrees And clockwise direction: '0 - 45 degrees x / y < 1 And x > 0, y > 0 'angle = Arctan( x / y ) × 57.3 '45 - 90 degrees x/y > 1 x >0 y>0 'angle = 90 - Arctan( y / x ) × 57.3 'etc ... '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 'If x / y < 1 can be written If x < y 'The first example 0-45 degrees. 'x, y And angle are floating point variables '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ '###################################################################################### 'READ COMPASS XYZ REGISTER 'SELF TEST SEQUENCE'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'SPICSOn 'compss = 0 'CHIP SELECT COMPASS ON 'SPISend 0x0a 'CNTL1 BIT 4 = 1 (16BIT), MODE 0000=POWER DOWN, 0001=SINGLE, 0010=CONT1 0110=CONTMODE2, 0100=EXT TRIGGER, 1000=SELF TEST,111=FUSE 'SPISend 0x40 '%0001000 POWER DOWN 'compss = 1 'CHIP SELECT COMPASS OFF 'compss = 0 'CHIP SELECT COMPASS ON 'SPISend 0x0c 'ASTC SELF TEST 'SPISend 0x40 'SELF TEST %01000000 'compss = 1 'CHIP SELECT COMPASS OFF 'compss = 0 'CHIP SELECT COMPASS ON 'SPISend 0x0a 'CNTL1 'SPISend 0x08 '1000=SELF TEST 'compss = 1 'CHIP SELECT COMPASS OFF 'SPICSOff ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '####################### TEST ################################# SPICSOn For i = 0 To 12 '5 'READ XYZ Xx2 Yx2 Zx2 compss = 0 'CHIP SELECT COMPASS ON SPISend addr SPIReceive data b(i) = data compss = 1 'CHIP SELECT COMPASS OFF addr = addr + 1 If addr = 0x8d Then addr = 0x80 '???CHECK??? 0x8c or 0x8d Next i SPICSOff '###########################TEST #################################################### w1a_raw = b(0) info_raw = b(1) st_1_raw = b(2) x_raw.LB = b(3) 'BYTE x_raw.HB = b(4) 'BYTE y_raw.LB = b(5) y_raw.HB = b(6) z_raw.LB = b(7) z_raw.HB = b(8) st_2_raw = b(9) cntl_1_raw = b(10) cntl_2_raw = b(11) astc_raw = b(12) x_sing = x_raw 'WORD y_sing = y_raw z_sing = z_raw i2c_dis_raw = b(13) asax_raw = b(14) asay_raw = b(15) asaz_raw = b(16) 'If ax < ay Then 'If ax > 0 And ay > 0 Then 'azi = arctn(ax, ay) 'Endif 'Endif 'strazimuth = #azi 'FOR 5110 SCREEN str_w1a = #w1a str_info = #info str_st_1 = #st_1 'vvvvvvvvvvvvvvvvvvvvvvvvvvvv 2 'S COMPLIMENT CALC VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV If x_sing > 32767 Then x_sing = 65535 - x_sing x_sing = x_sing + 1 Endif If y_sing > 32767 Then y_sing = 65535 - y_sing y_sing = y_sing + 1 Endif If z_sing > 32767 Then z_sing = 65535 - z_sing z_sing = z_sing + 1 Endif 'AAAAAAAAAAAAAAAAAAAAAAAAAAAA 2'S COMPLIMENT CALC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA str_x = #x_sing 'STRING str_y = #y_sing str_z = #z_sing str_st_2 = #st_2 str_cntl_1 = #cntl_1 str_cntl_2 = #cntl_2 str_astc = #astc str_i2cdis = #i2c_dis str_asax = #asax str_asay = #asay str_asaz = #asaz 'strxword = #ax 'stryword = #ay 'Hserout "DATA ", " ", #str_w1a, " ", #str_info, " ", #str_st_1, " [", #x1, " ", #x2, " ", #y1, " ", #y2, " ", #z1, " ", #z2, "] ", #str_st_2, " ", #str_cntl_1, " ", #str_cntl_2, " ", #str_i2cdis, " ", #str_asax, " ", #str_asay, " ", #str_asaz, CrLf Hserout str_x, ",", str_y, ",", str_z, CrLf 'Magmaster WaitMs 100 'Hserout #x1, ",", #y1, ",", #z1, CrLf Goto main End