'18F2431 AZ BRAKE 311215 1201 'Define SIMULATION_WAITMS_VALUE = 1 'else 0 for PIC Define CLOCK_FREQUENCY = 8 Define CONFIG1L = 0x00 Define CONFIG1H = 0x08 'INT OSC Define CONFIG2L = 0x0c Define CONFIG2H = 0x20 Define CONFIG3L = 0x04 Define CONFIG3H = 0x80 Define CONFIG4L = 0x80 'Set for HVP Define CONFIG4H = 0x00 Define CONFIG5L = 0x0f Define CONFIG5H = 0xc0 Define CONFIG6L = 0x0f Define CONFIG6H = 0xe0 Define CONFIG7L = 0x0f Define CONFIG7H = 0x40 Dim poscnt As Word 'CAP2BUFL/H Position TIME register Dim gpsazim As String 'String shows as Dim gpselev As String '++++EL 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 '++++EL Dim uartrxd As String Dim forrev As Bit 'Forward and reverse switch 0=REV 1=FOR Dim time As Byte 'interrupt test AllDigital ADCON0 = %00000000 'A/D Control Register 0 ADCON1 = %00000000 ADCON2 = %10100001 ADCON3 = %00000000 ADCHS = %00000000 'A/D Channels select ANSEL0 = %00000000 'Analogue select Register 0 OSCCON = %01110010 'internal 8Mhz clock TRISA = %00011100 'IN 4QEA 3QEA 2IND TRISB = %00000000 'OUT 4,LED TRISC = %10000000 'IN 7RX OUT-6TX-5//-4//-3//-2FOR-1REV 'TRISA = %00000001 From LOOP timer>>>>>>>>>>>>>>>>>>>>> 'TRISB = %00000000 From LOOP timer>>>>>>>>>>>>>>>>>>>>>>>> '#'PORTA.0 => AFN Forward Nfet '#'PORTA.1 => ARN Reverse Nfet '#'PORTA.2 =< INDX BUTTON '#'PORTA.3 =< QEA '#'PORTA.4 =< QEB '#'PORTA.5 n/u '#'PORTA.6 =< AR Reverse button '#'PORTA.7 =< AF Forward button '#'PORTB.4 => LED '#'PORTC.0 n/u '#'PORTC.1 => PWM CCP2 Pfet Reverse '#'PORTC.2 => PWM CCP1 Pfet Forward '#'PORTC.3 n/u '#'PORTC.4 n/u '#'PORTC.5 N/u '#'PORTC.6 => TX '#'PORTC.7 =< RX '#'PORTE.3 =< rst button Symbol led = PORTB.4 led = 1 WaitMs 1000 led = 0 WaitMs 1000 ''While OSCCON.IOFS = 0'indicates when the internal oscillator block has stabilized, ''Wend '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 'set up timer 1 T0CON.TMR0ON = 1 'Bit7 = enables timer0 T0CON.T016BIT = 0 'Bit6 = timer0 is configured As A 16-Bit timer/TIME T0CON.T0CS = 0 'Bit5 = Internal clock (FOSC/4) T0CON.T0SE = 0 'Bit4 = 0 = Increment on low-to-high transition on T0CKI pin T0CON.PSA = 0 'Bit3 = Timer0 prescaler is assigned.from prescaler output T0CON.T0PS2 = 0 'Bit2 = 1:2 Prescale value T0CON.T0PS1 = 0 'Bit1 = 1:2 Prescale value T0CON.T0PS0 = 0 'Bit0 = 1:2 Prescale value '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Symbol forn = PORTA.0 'FORWARD NFET ENABLE Symbol revn = PORTA.1 'REVERSE NFET ENABLE Symbol forp = PORTC.2 'FORWARD PFET ENABLE Symbol revp = PORTC.1 'REVERSE PFET ENABLE 'assign pwm pins 'PWMon 1, 8 'RC1 CCP1, Mode at 8MHz , to change this look at the Manual tables 'PWMon 2, 8 'RC2 CCP2 '#'QEICON.VELM = 0 'Bit7 0 = Velocity mode enabled '#'QEICON.QERR = 0 'Bit6 Must be in INDX MODE '#'QEICON.UP/DOWN = 0 'Bit5 Direction of Rotation Status bit 0REV 1FOR '#'QEICON.QEIM2 = 1 'Bit4 '#'QEICON.QEIM1 = 1 'Bit3 110 = QEI enabled in 4x Update mode; position TIME is reset on period match (POSCNT = MAXCNT) '#'QEICON.QEIM0 = 0 'Bit2 '#'QEICON.PDEC1 = 1 'Bit1 REG file '#'QEICON.PDEC0 = 1 'Bit0 REG file QEICON = %00011011 'QEI enabled in 4x Update mode; position TIME is reset on period match (POSCNT = MAXCNT IPR3.IC2QEIP = 1 'high pri intr QEI Interrupt priority bit PIE3.IC2QEIE = 1 'qei intr enb QEI Interrupt flag eneble bit PIR3.IC2QEIF = 0 'Has reached the MAXCNT value, INT QEI MODULE Interrupt flag bit PIR3.IC3DRIF = 0 'clr in s/w REG file motion feedback filter(NOT USED)??????????? 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(((((((((((((((((((( 'PWM and NFETS Off forn = 0 'FORWARD NFET OFF revn = 0 'REVERSE NFET OFF forn = 0 'FORWARD NFET = 0 revn = 0 'REVERSE NFET = 0 verify_uart: 'START<><><><><><><><><><><>< 'Hserout "poscnt = ", #poscnt, ".", "test", CrLf Hserout "Uart Ready for AxxxExxx input", CrLf '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1 clr_azim: 'clear all buffers For rxi = 0 To 7 str1(rxi) = 0 Next rxi char = 0 uartrxd = "" gpsazim = "" gpselev = "" 'azimval = 0 'TEST WITH NO RESET elevval = 0 Enable High 'poscnt/match intr RCSTA.CREN = 1 WaitUs 100 '//////////////////////////////////////////////////// read_azim: 'no Gosub If RCSTA.OERR = 1 Then RCSTA.CREN = 0 RCSTA.CREN = 1 'CREN: continuous receive Enable Bit 'Asynchronous mode: '1 = Enables receiver '0 = Disables receiver 'Synchronous mode: '1 = Enables continuous receive until Enable Bit, CREN, is cleared (CREN overrides SREN) '0 = disables continuous receive 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 '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! get_cnt: 'No gosub If poscnt = azimval Then '0000=0000??TRACKER is pointing at DEGREES INPUT (AZIMVAL) PIR3.IC2QEIF = 1 'raise Intr, Has reached the MAXCNT value, MAXCNT INT QEI MODULE Interrupt flag bit 'Goto clr_azim'~?~?~?~?~?~?~?~?~?~?~?~TESTTESTTESTTEST Endif TMR0H = 0 'Watch TMR0 HIGH BYTE not TMR0H>>>>>>>>>>>>>>>>>>>>>> TMR0L = 0 get_count: poscnt.LB = CAP2BUFL 'Read QUAD cntr, range 0000 through 3599 'update 18F2431 poscnt.HB = CAP2BUFH ''compare routine for Azimuth required and the Head angle 'the Head always takes the shortest path towards the required Azimuth. '################################################################ If INTCON.TMR0IF = 1 Then 'Switches at TMR0 rollover time = 1 INTCON.TMR0IF = 0 TMR0H = 0 'Watch TMR0 HIGH BYTE not TMR0H>>>>>>>>>>>>>>>>> TMR0L = 0 Endif If poscnt = azimval Then If time = 0 Then '> In case of skip??? Gosub brake Goto get_count 'keep reading cap2buf [poscnt] Endif Endif If poscnt = azimval Then If time = 1 Then time = 0 Goto clr_azim 'START Clears buffers, get ready for next uart rxd Endif Endif If poscnt < azimval Then If azimval - poscnt >= 1800 Then Gosub ccwise 'REV passes thru 0000 TO 3599 index time = 0 Else Gosub cwise time = 0 Endif Endif If poscnt > azimval Then '270 to 90 If poscnt - azimval >= 1800 Then Gosub cwise 'FOR passes thru 3599 TO 0000 index time = 0 Else Gosub ccwise time = 0 Endif Endif '############################################################ 'If QEICON.5 = 0 Then 'rev 'If QEICON.5 = 1 Then 'fwd Goto get_count 'Added in case of misfire?? End On High Interrupt Save System PIR3.IC2QEIF = 0 'poscnt=maxcnt PORTA.4 = 0 PORTA.4 = 1 PIE3.IC2QEIE = 1 'qei intr enb Resume cwise: 'FWD revn = 0 'REVERSE NFET OFF revp = 0 'REVERSE PFET OFF forn = 1 'FORWARD PFET ON forp = 1 'FORWARD PFET ON Return ccwise: 'REV forn = 0 'FORWARD NFET OFF forp = 0 'FORWARD NFET OFF revn = 1 'REVERSE NFET ON revp = 1 'REVERSE PFET = 1 Return brake: revn = 0 'REVERSE NFET OFF forn = 0 'FORWARD NFET OFF forn = 0 'FORWARD NFET OFF revn = 0 'REVERSE NFET OFF forp = 1 'FORWARD NFET ON revp = 1 'REVERSE PFET ON Return