-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.c
98 lines (89 loc) · 1.63 KB
/
app.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include "XqGpio.h"
#include <avr/interrupt.h>
typedef struct Servo {
unsigned char pin;
unsigned int duty_clicks;
unsigned int cycle_clicks;
unsigned int clicks;
}Servo;
/* 18 pins for servos: 2-13, A0-A5 */
Servo M[18];
static void on_100us();
ISR(TIMER0_OVF_vect)
{
TCNT0 = 231; /*256 - 160 */
on_100us();
}
static void start_timer()
{
TCNT0 = 231;
TCCR0A = 0;
TIMSK0 = (1<<TOIE0);
TCCR0B = 0x03; /* prescal = 1 */
sei();
}
#define US_PER_CLICK 100
static void init_servos()
{
int i=0;
unsigned char pins[] = {2,3,4,5,6,7,8,9,10,11,12,13,A0,A1,A2,A3,A4,A5};
for(i=0; i<18; i++){
M[i].pin = pins[i];
M[i].duty_clicks = 500 / US_PER_CLICK;
M[i].cycle_clicks = 2000 / US_PER_CLICK;
M[i].clicks = 0;
xqGpioSetMode(M[i].pin, OUT);
}
}
static void on_100us()
{
int i;
for(i=0; i<17; i++){
if(M[i].clicks == 0){
xqGpioWrite(M[i].pin, HIGH);
}
if(M[i].clicks == M[i].duty_clicks){
xqGpioWrite(M[i].pin, LOW);
}
M[i].clicks ++;
if(M[i].clicks == M[i].cycle_clicks){
M[i].clicks = 0;
}
}
}
int main()
{
init_servos();
start_timer();
/* start_listen */
int i=0;
for(i=0;i<18;i++){
M[i].duty_clicks = 5;
}
while(1)
{
/* start_listen */
int i=0;
for(i=0;i<18;i++){
M[i].duty_clicks = 500/US_PER_CLICK;
}
xqGpioWrite(A5, HIGH);
_delay_ms(1000);
for(i=0;i<18;i++){
M[i].duty_clicks = 1400/US_PER_CLICK;
}
xqGpioWrite(A5, LOW);
_delay_ms(1000);
for(i=0;i<18;i++){
M[i].duty_clicks = 24/US_PER_CLICK;
}
xqGpioWrite(A5, HIGH);
_delay_ms(1000);
xqGpioWrite(A5, LOW);
_delay_ms(1000);
}
return 0;
}