8 static struct servo servo_table[] = {
22 #define NB_SERVO (sizeof(servo_table)/sizeof(*servo_table))
24 register uint8_t bypass asm("r2");
25 register uint8_t done asm("r3");
26 register uint8_t portval asm("r4");
27 register uint8_t rxidx asm("r5");
28 #define BYPASS_ENABLE 14
29 #define BYPASS_DISABLE 15
34 * A command is stored on 2 bytes. The first one has its msb to 0, and the
35 * second one to 1. The first received byte contains the command number, and the
36 * msb of the servo value. The second byte contains the lsb of the servo value.
38 * Commands 0 to NB_SERVO are to set the value of servo.
39 * Command 14 is to enable bypass mode.
40 * Command 15 is to disable bypass mode.
59 SIGNAL(TIMER1_COMPA_vect)
62 TIMSK1 &= ~_BV(OCIE1A);
66 static void poll_spi(void)
70 /* reception complete ? */
71 if (!(SPSR & (1<<SPIF)))
75 if ((rxidx == 0) && (c & 0x80)) {
79 if ((rxidx == 1) && ((c & 0x80) == 0)) {
94 if (byte0.cmd_num < NB_SERVO) {
95 val = (uint16_t)byte0.val_msb << 7;
97 servo_table[byte0.cmd_num].command = val;
99 else if (byte0.cmd_num == BYPASS_ENABLE) {
102 else if (byte0.cmd_num == BYPASS_DISABLE) {
110 static void load_timer_at(uint16_t t)
113 TIMSK1 |= _BV(OCIE1A);
116 static void do_one_servo(struct servo *s)
122 //portval = PORTC | (1 << s->bit);
123 portval = (1 << s->bit);
125 load_timer_at(t + 150);
132 //portval = PORTC & (~(1 << s->bit));
133 load_timer_at(t + 150 + 8000 + s->command * 8);
149 /* start timer1 at clk/1 (8Mhz) */
153 /* start timer0 at clk/1024 (~8Khz) */
155 TCCR0B = _BV(CS02) | _BV(CS00);
157 /* enable spi (don't set unused MISO as output) */
165 for (i = 0; i < NB_SERVO; i++) {
166 do_one_servo(&servo_table[i]);
176 while (bypass == 1) {