'18F2431 11Apr2015 'Test program for the 18F2431 'Define SIMULATION_WAITMS_VALUE = 1 'else 0 for PIC Define CLOCK_FREQUENCY = 8 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 str1(8) As Byte Dim char As Byte Dim rxi As Byte Dim char1 As String Dim azimval As Word 'required Head Azimuth Dim elevval As Word '++++ Dim uartrxd As String Dim match1 As Byte Dim rxdta As Byte Dim rx As Byte Dim x 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 = %11001000 'OUT 1,PWM REV 2,PWM FOR 4, TO 18F4520 6, TX 7,RX ''While OSCCON.IOFS = 0 ''Wend 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] WaitUs 100 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 For rxi = 0 To 7 str1(rxi) = 0 Next rxi char = 0 uartrxd = "" gpsazim = "" gpselev = "" gpsrxd = "" azimval = 0 elevval = 0 Enable High 'poscnt/match intr RCSTA.CREN = 1 WaitUs 100 read_azim: If RCSTA.OERR = 1 Then RCSTA.CREN = 0 RCSTA.CREN = 1 char = RCREG char = RCREG PIR1.RCIF = 0 Endif sync1: 'wait for a 'A' start of string If PIR1.RCIF = 0 Then Goto sync1 char = RCREG If char <> 0x41 Then Goto sync1 'A str1(0) = char rxi = 1 get_msg: If PIR1.RCIF = 0 Then Goto get_msg char = RCREG str1(rxi) = char rxi = rxi + 1 If rxi > 7 Then Goto eom Goto get_msg eom: For rxi = 1 To 7 'drop the 'A' char = str1(rxi) char1 = Chr(char) uartrxd = uartrxd + char1 Next rxi Hserout "uartrxd Inp= ", uartrxd, CrLf If MidStr(uartrxd, 4, 1) <> "E" Then Goto clr_azim 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 RCSTA.CREN = 0 azimval = azimval * 10 'times *10 = 0000,0010,0020 thru 3580,3590 b2aval = azimval Gosub bin2asc Hserout "Valid 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 b2aval = poscnt Gosub bin2asc Hserout "Match STOP PWM at: A=", ascbfr3, ascbfr2, ascbfr1, ".", ascbfr0, "deg", CrLf, CrLf PIE3.IC2QEIE = 1 'qei intr enb Resume upcnt: poscnt = poscnt + 1 WaitMs 50 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 50 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 'convert any binary value from 0000h to fffFh to 0000 to 65535 decimal, '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