summaryrefslogtreecommitdiff
path: root/n/asserv/src/pwm.c
blob: 4ab8722bd5f5a8a1e521dbf197b7d8963f63d094 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* pwm.c */
/* asserv - Position & speed motor control on a ATmega128. {{{
 *
 * Copyright (C) 2004 Nicolas Schodet
 *
 * Robot APB Team/Efrei 2005.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */

/** Define the PWM output used for left motor. */
#define PWM_LEFT_OCR OCR1C
/** Define the PWM output used for right motor. */
#define PWM_RIGHT_OCR OCR1B
/** Define the direction output for left motor. */
#define PWM_LEFT_DIR 3
/** Define the direction output for right motor. */
#define PWM_RIGHT_DIR 2

/** PWM values. */
int16_t pwm_left, pwm_right;
/** PWM reverse direction. */
uint8_t pwm_dir = _BV (PWM_LEFT_DIR);

/* +AutoDec */

/** Initialise PWM generator. */
static inline void
pwm_init (void);

/** Preprocess a PWM value before sending it to hardware. */
static inline uint8_t
pwm_preproc (uint16_t v);

/** Update the hardware PWM values. */
static inline void
pwm_update (void);

/* -AutoDec */

/** Initialise PWM generator. */
static inline void
pwm_init (void)
{
    /* Phase correct PWM, TOP = 0xff, OC1B & OC1C with positive logic.
       f_IO without prescaler.
       Fpwm = f_IO / (2 * prescaler * TOP) = 28912 Hz. */
    TCCR1A =
	regv (COM1A1, COM1A0, COM1B1, COM1B0, COM1C1, COM1C0, WGM11, WGM10,
		   0,      0,      1,      0,      1,      0,     0,     1);
    TCCR1B = regv (ICNC1, ICES1, 5, WGM13, WGM12, CS12, CS11, CS10,
		       0,     0, 0,     0,     0,    0,    0,    1);
    /* Enable pwm and direction outputs in DDRB. */
    DDRB |= _BV (7) | _BV (6) | _BV (PWM_LEFT_DIR) | _BV (PWM_RIGHT_DIR);
}

/** Preprocess a PWM value before sending it to hardware. */
static inline uint8_t
pwm_preproc (uint16_t v)
{
    if (v > 255)
	return 255;
    else
	return v;
}

/** Update the hardware PWM values. */
static inline void
pwm_update (void)
{
    uint16_t left, right;
    uint8_t dir;
    dir = PORTB & ~(_BV (PWM_LEFT_DIR) | _BV (PWM_RIGHT_DIR));
    /* Set left PWM. */
    if (pwm_left == 0)
      {
	left = 0;
      }
    else if (pwm_left < 0)
      {
	left = pwm_preproc (-pwm_left);
      }
    else
      {
	dir |= _BV (PWM_LEFT_DIR);
	left = pwm_preproc (pwm_left);
      }
    /* Set right PWM. */
    if (pwm_right == 0)
      {
	right = 0;
      }
    else if (pwm_right < 0)
      {
	right = pwm_preproc (-pwm_right);
      }
    else
      {
	dir |= _BV (PWM_RIGHT_DIR);
	right = pwm_preproc (pwm_right);
      }
    /* Setup registers. */
    dir ^= pwm_dir;
    PORTB = dir;
    PWM_LEFT_OCR = left;
    PWM_RIGHT_OCR = right;
}