Hello Jalians,

I found this http://www.mikroe.com/forum/viewtopic.php?t=13607 post
with a great program for a 8 channel servo controller! The program is
in mikroBasic, I have translated it to JAL. It works very well, and
there is plenty of time in the main loop for doing other things.

I would like to share this code with you, maybe it will be usefull for
the library.

Best regards,

Sander


-- jal 2.4m
include 16f876a

pragma target OSC   HS                  -- HS crystal or resonator
pragma target clock 4_000_000   -- oscillator frequency
pragma target WDT   disabled
pragma target LVP   disabled
pragma eedata       "xorservo"

-- don't need analog
enable_digital_io()

alias blink is pin_A5
pin_A5_direction = output

-- servo values range from 1000 to 2000, 1500 is the center position
var word servo_value[8] = {1000,1142,1284,1426,1568,1710,1852,2000}

PORTC_direction = all_output            -- 8 servos on port C
T1CON                   = 0b_0000_0000          -- prescaler = 1 = 1us ticks ; 
timer1 off
TMR1                    = 0x0000                        -- Timer1 starts at 0
CCP1CON                 = 0b_0000_1011          -- Compare mode, trigger 
special event
CCP2CON                 = 0b_0000_1010          -- Compare mode, generate 
software interrupt
on match
CCPR1                   = 2_500                         -- special event 
interrupt every 2500us
INTCON                  = 0b_1100_0000          -- enable GIE and PEIE
PIE1                    = 0b_0000_0100          -- CCP1 Interrupt Enable bit on
PIE2                    = 0b_0000_0001          -- CCP2 Interrupt Enable bit on
PIR1_CCP1IF     = 0                                     -- CCP1 Interrupt Flag 
bit off
PIR2_CCP2IF     = 0                                     -- CCP2 Interrupt Flag 
bit off
var bit*3 sn    = 0b_000                        -- servo number rolls over from 
7 to 0

-- configure ADC
const bit  ADC_HIGH_RESOLUTION = high
const byte ADC_NCHANNEL = 1
const byte ADC_NVREF = 0
include adc
adc_init()
var word measure

procedure servo_control() is
   pragma interrupt
   if PIR1_CCP1IF == 1 then             -- CCP1 resets TMR1;
      PORTC = (1 << sn)                 -- Active Servo High
      CCPR2 = servo_value[sn]   -- Active Servo Value loaded in CCPR2
      PIR1_CCP1IF = 0
   else                                                 -- CCP2 Normal Compare 
Event Interrupt
      PORTC_low = 0                             -- Turn Off Active Servo
      sn = sn + 1                               -- Get Ready for next Servo
      PIR2_CCP2IF = 0
   end if
end procedure

T1CON_TMR1ON = 1                                -- Timer1 On

forever loop

   -- set the first servo value with adc
   measure = adc_read(0)
   servo_value[0] = (1000 + measure)

   -- do usefull stuf here
   -- update servo_value[n] with adc, serial, ...

   blink = !blink
   delay_100ms(1)

end loop

-- 
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/jallib?hl=en.

Reply via email to