'18F2431 11Apr2015 'Test program for the 18F2431 '''Define SIMULATION_WAITMS_VALUE = 1 'else 0 for PIC Define CONFIG1L = 0x00 Define CONFIG1H = 0x08 Define CONFIG2L = 0x0c Define CONFIG2H = 0x20 Define CONFIG3L = 0x04 Define CONFIG3H = 0x80 Define CONFIG4L = 0x80 'LVP= 0 = Set for HVP '''''Define CONFIG4L = 0x84 'LVP= 1 = Set for LVP Define CONFIG4H = 0x00 Define CONFIG5L = 0x0f Define CONFIG5H = 0xc0 Define CONFIG6L = 0x0f Define CONFIG6H = 0xe0 Define CONFIG7L = 0x0f Define CONFIG7H = 0x40 AllDigital Dim ascbfr4 As Byte @ 0x070 Dim ascbfr3 As Byte @ 0x071 Dim ascbfr2 As Byte @ 0x072 Dim ascbfr1 As Byte @ 0x073 Dim ascbfr0 As Byte @ 0x074 Dim b2avall As Byte @ 0x075 Dim b2avalm As Byte @ 0x076 Dim no_bits As Byte @ 0x077 Dim digit_cnt As Byte @ 0x078 Dim b2aval As Word Dim temp3 As Word Dim poscnt As Word 'CAP2BUFL/H Position counter register Dim oldpos As Word Dim hdr1 As String 'Remote Azimuth from 18F4520 Dim uartrxd As String Dim gpsazim As String 'String shows as Dim gpselev As String '++++ Dim gpsrxd As String Dim azimval As Word 'required Head Azimuth Dim elevval As Word '++++ Dim uartrxd As String Dim match1 As Byte Dim rx As Byte Dim azimcnt As Byte ADCON0 = %00000011 'A/D Control Register 0 ADCON1 = %00000000 ADCON2 = %10100001 ADCON3 = %00000000 ADCHS = %00000000 'A/D Channels select ANSEL0 = %00000001 'Analogue select Register 0 OSCCON = %01110010 'internal 8Mhz clock TRISA = %00011100 'IN 2,IND 3,QEA 4,QEA TRISB = %00000000 'OUT 4,LED TRISC = %10001000 'OUT 1,PWM REV 2,PWM FOR 4, TO 18F4520 6, TX 7,RX QEICON = %00010111 IPR3.IC2QEIP = 1 'high pri intr QEI Interrupt priority bit PIE3.IC2QEIE = 1 'qei intr enb QEI Interrupt flag eneble bit PIR3.IC2QEIF = 0 'clr in s/w, max cnt or indx QEI Interrupt flag bit PIR3.IC3DRIF = 0 'clr in s/w REG file motion feedback filter Disable High CAP2BUFL = 0x00 'POSCNT from quad signals, preload L0000 CAP2BUFH = 0x00 'POSCNT from quad signals, preload H0000 CAP3BUFL = 0x0f 'MAXCNT, preload with 3599 degrees [actual 359deg] CAP3BUFH = 0x0e 'MAXCNT, preload with ???? degrees [actual ???deg] poscnt = 0 Hseropen 9600 WaitMs 100 poscnt = 0 'change Start Poscount to suit your testing verify_uart: b2aval = poscnt Gosub bin2asc Hserout "Scanner at Poscount= ", ascbfr3, ascbfr2, ascbfr1, ".", ascbfr0, " deg", CrLf Hserout "Uart Ready for AxxxExxx input", CrLf clr_azim: 'clear all buffers hdr1 = "" uartrxd = "" gpsazim = "" gpselev = "" gpsrxd = "" oldpos = 0 azimval = 0 elevval = 0 Enable High read_azim: Hserget hdr1 'remote Azimuth from 18F4520 PIC, it expects AxxxExxx in ASCII format If hdr1 <> "A" Then Goto clr_azim 'wait for 'A' sync character For rx = 0 To 6 'get all 6 characters Hserin gpsrxd uartrxd = uartrxd + gpsrxd Next rx If MidStr(uartrxd, 4, 1) <> "E" Then Goto clr_azim skip1: gpsazim = LeftStr(uartrxd, 3) gpselev = RightStr(uartrxd, 3) '++++ for Elev azimval = StrValW(gpsazim) 'convert ascii to numeric and elevval = StrValW(gpselev) '++++ If azimval > 359 Then Goto clr_azim 'invalid so await next azim input Endif azimval = azimval * 10 'times *10 = 0000,0010,0020 thru 3580,3590 ''''Hserout "New Azim Inp=", #azimval, " Elev=", #elevval, CrLf, CrLf 'test ONLY b2aval = azimval Gosub bin2asc Hserout "UART Azim Inp= ", ascbfr3, ascbfr2, ascbfr1, ".", ascbfr0, "deg", CrLf get_cnt: If poscnt = azimval Then PIR3.IC2QEIF = 1 'raise Intr Goto clr_azim Endif If poscnt < azimval Then If azimval - poscnt >= 1800 Then Gosub dwncnt Else Gosub upcnt Endif Endif If poscnt > azimval Then '270 > 90 If poscnt - azimval >= 1800 Then Gosub upcnt Else Gosub dwncnt Endif Endif Goto get_cnt End On High Interrupt Save System PIR3.IC2QEIF = 0 Hserout "Match STOP PWM at: A=", #azimval, " P=", #poscnt, CrLf, CrLf PIE3.IC2QEIE = 1 'qei intr enb Resume upcnt: poscnt = poscnt + 1 WaitMs 100 If poscnt > 3599 Then poscnt = 0 CAP2BUFL = poscnt.LB CAP2BUFH = poscnt.HB '''Hserout "PWM CW= ", #poscnt, CrLf b2aval = poscnt Gosub bin2asc Hserout "pwm CW = ", ascbfr3, ascbfr2, ascbfr1, ".", ascbfr0, "deg", CrLf Return dwncnt: poscnt = poscnt - 1 WaitMs 100 If poscnt = 65535 Then poscnt = 3599 CAP2BUFL = poscnt.LB CAP2BUFH = poscnt.HB ''Hserout "PWM CCW= ", #poscnt, CrLf b2aval = poscnt Gosub bin2asc Hserout "pwm CWW = ", ascbfr3, ascbfr2, ascbfr1, ".", ascbfr0, "deg", CrLf Return 'you can convert any binary value from 0000h to fffFh to 0000 to 65535 decimal 'just name the binary word as b2aval and call this subr and the ASCII 'result will be in ascbfr4,3,2,1,0, ready for your LCD or UART 'just pop the DP in the output to the LCD [as shown above] bin2asc: ascbfr4 = b2aval / 10000 temp3 = b2aval Mod 10000 ascbfr3 = temp3 / 1000 temp3 = b2aval Mod 1000 ascbfr2 = temp3 / 100 temp3 = temp3 Mod 100 ascbfr1 = temp3 / 10 ascbfr0 = temp3 Mod 10 'results are BCD so 'convert To ascii For lcd Or uart ascbfr4 = ascbfr4 Or 0x30 ascbfr3 = ascbfr3 Or 0x30 ascbfr2 = ascbfr2 Or 0x30 ascbfr1 = ascbfr1 Or 0x30 ascbfr0 = ascbfr0 Or 0x30 Return