Const OSCCON = &H70 Const PLLEN = 1 Const T2CON = &H4E Const PR2 = 49 Const TMR2IE = 1 Const T1CON = 0 Const TRISC = &HFC Const PEIE = 1 Const GIE = 1 Const NUM_SERVOS = 10 Const BUFFER = 80 Dim Buff(BUFFER) As Byte, StrCount As Byte, Done As Byte Dim WordTemp As Integer Dim Ms As Long Dim Count As Byte, ServoCount As Byte, I As Byte Dim ServoPos(NUM_SERVOS) As Integer Sub Main() OSCCON = &H70 '8MHz PLLEN = 1 'x4=32MHz 'setup 1mS interrupt = 8,000,000/16 = 500,000/10 = 50,000 set PR2=49 = 50,000/50 = 1000 = 1mS T2CON = &H4E 'pre=16 post=10 PR2 = 49 TMR2IE = 1 'timer 2 interrupts enable T1CON = 0 'timer 1 stopped For I = 0 To NUM_SERVOS - 1 ServoPos(I) = I * 1000 + 8000 '1ms(8000) to 1.875(7/8ths - 15000)ms in 1/8th mS steps Next TRISC = &HFC 'CCP0 & 1 output PEIE = 1 GIE = 1 Do 'adjust servo positions here Loop End Sub Sub Inter() If TMR2IE And TMR2IF Then Ms = Ms + 1 Count = Count + 1 If Count >= 20 Then 'start every 20mS TMR1 = 0 'zero timer 1 T1CON = 1 'start timer 1 Count = 0 CCP1CON = &H8 'CCP1 pin low and high on match - will be first pulse CCPR1L = &HD CCPR2H = &H7 CCP1IE = 1 'enable CCP1 interrupts CCP1IF = 0 'ensure interrupt flag is clear ServoCount = 0 'reset servoCount LATC0 = 1 'connected to data in of shift register will clock in a high when CCP1 goes high End If TMR2IF = 0 End If If CCP1IE And CCP1IF Then LATC0 = 0 'clear the data in pin If ServoCount = 9 Then 'have we done all servos? CCP1IE = 0 'yes so no more CCP1 interrupts End If If CCP1CON = &H8 Then 'have we started the 4000 cycle pulse CCP1CON = &H9 'yes so end the pulse after 0.5mS 'CCPR1=CCPR1+4000; '4000 cycles=0.5mS WordTemp = CCPR1H * 256 + CCPR1L WordTemp = WordTemp + 4000 CCPR1L = WordTemp And 255 CCPR1H = WordTemp / 256 Else CCP1CON = &H8 'No so output the timed gap 'CCPR1=CCPR1+servoPos[servoCount++]-4000; 'will generate an interrupt when servo time is up WordTemp = CCPR1H * 256 + CCPR1L WordTemp = WordTemp - 4000 + ServoPos(ServoCount) CCPR1L = WordTemp And 255 CCPR1H = WordTemp / 256 ServoCount = ServoCount + 1 End If CCP1IF = 0 End If If RCIE & RCIF Then Dim Chr As Byte = RCREG 'get the received character If OERR Or FERR Then 'neither of these should ever occur. CREN = 0 'this is kinda wishful thinking CREN = 1 'as any data received is corrupt StrCount = 0 'however, reset everything Done = 0 'and hope for the best Else 'no errors so use the data If StrCount = 0 And Done = 0 Then 'are we already receiving 'waiting for $ 'no so wait If Chr = "$"C Then 'for $ to appear Buff(StrCount) = Chr 'start receiving StrCount = StrCount + 1 ElseIf Done = 0 Then 'have we collected a full string? Buff(StrCount) = Chr 'no so carry on storing StrCount = StrCount + 1 If Chr = "W"C Then 'have we got the "endOfString" character Done = 1 'yes, so set done true End If End If End If End If End If End Sub