summaryrefslogtreecommitdiff
path: root/n/line-follower/src/pwm.c
diff options
context:
space:
mode:
Diffstat (limited to 'n/line-follower/src/pwm.c')
-rw-r--r--n/line-follower/src/pwm.c108
1 files changed, 108 insertions, 0 deletions
diff --git a/n/line-follower/src/pwm.c b/n/line-follower/src/pwm.c
new file mode 100644
index 0000000..883201f
--- /dev/null
+++ b/n/line-follower/src/pwm.c
@@ -0,0 +1,108 @@
+/* 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.
+ *
+ * }}} */
+
+/* PWM values. */
+int16_t pwm_left, pwm_right;
+
+/* +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 (3) | _BV (2);
+}
+
+/** Preprocess a PWM value before sending it to hardware. */
+static inline uint8_t
+pwm_preproc (uint16_t v)
+{
+ // This is a try to correct the optocoupler problem...
+ //v += 0x10;
+ if (v > 255)
+ return 255;
+ else
+ return v;
+}
+
+/** Update the hardware PWM values. */
+static inline void
+pwm_update (void)
+{
+ /* Set left PWM. */
+ if (pwm_left == 0)
+ {
+ OCR1B = 0;
+ }
+ else if (pwm_left < 0)
+ {
+ PORTB &= ~_BV (2);
+ OCR1B = pwm_preproc (-pwm_left);
+ }
+ else
+ {
+ PORTB |= _BV (2);
+ OCR1B = pwm_preproc (pwm_left);
+ }
+ /* Set right PWM. */
+ if (pwm_right == 0)
+ {
+ OCR1C = 0;
+ }
+ else if (pwm_right < 0)
+ {
+ PORTB |= _BV (3);
+ OCR1C = pwm_preproc (-pwm_right);
+ }
+ else
+ {
+ PORTB &= ~_BV (3);
+ OCR1C = pwm_preproc (pwm_right);
+ }
+}
+