Factored to support configurable amount of pwm's, fixes

This commit is contained in:
Marcus Priesch 2014-03-12 12:34:23 +01:00 committed by harald
parent bf6f388691
commit 992e54fe25
8 changed files with 363 additions and 518 deletions

View file

@ -3,7 +3,7 @@ all: er-example-server
# variable for this Makefile # variable for this Makefile
# configure CoAP implementation (3|7|12|13) (er-coap-07 also supports CoAP draft 08) # configure CoAP implementation (3|7|12|13) (er-coap-07 also supports CoAP draft 08)
WITH_COAP=13 WITH_COAP=7
# for some platforms # for some platforms
UIP_CONF_IPV6=1 UIP_CONF_IPV6=1

View file

@ -1,2 +1,2 @@
#!/bin/bash #!/bin/bash
avrdude -V -pm128rfa1 -c arduino -P/dev/ttyUSB0 -b57600 -e -U flash:w:er-example-server.osd-merkur.hex:a -U eeprom:w:er-example-server.osd-merkur.eep:a avrdude -pm128rfa1 -c arduino -P/dev/ttyUSB0 -b57600 -e -U flash:w:er-example-server.osd-merkur.hex:a -U eeprom:w:er-example-server.osd-merkur.eep:a

View file

@ -35,14 +35,14 @@
static servo_channel_type servo_channels [] = static servo_channel_type servo_channels [] =
{ /* PORTx, DDRx, PINxn, PWM */ { /* PORTx, DDRx, PINxn, PWM */
{0x05, 0x04, PINB7, 0} // r {0x05, 0x04, 1 << PINB7, 0} // r1
, {0x0E, 0x0D, PINE2, 0} // g , {0x0E, 0x0D, 1 << PINE2, 0} // g1
, {0x05, 0x04, PINB6, 0} // b , {0x05, 0x04, 1 << PINB6, 0} // b1
, {0x05, 0x04, PINB5, 0} // w , {0x05, 0x04, 1 << PINB5, 0} // w1
, {0x05, 0x04, PINB1, 0} // r , {0x05, 0x04, 1 << PINB1, 0} // r2
, {0x05, 0x04, PINB0, 0} // g , {0x05, 0x04, 1 << PINB0, 0} // g2
, {0x05, 0x04, PINB2, 0} // b , {0x05, 0x04, 1 << PINB2, 0} // b2
, {0x05, 0x04, PINB3, 0} // w , {0x05, 0x04, 1 << PINB3, 0} // w2
}; };
#endif #endif

View file

@ -1,78 +0,0 @@
//os_fname_map// t4-servo-config:h:h
// Copyright (C) 2014 Marcus Priesch, All rights reserved
// In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at
// ****************************************************************************
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
// ****************************************************************************
//
//++
// Name
// t4-servo-config
//
// Purpose
// configuration for t4-servo
//
// Revision Dates
// 10-Mar-2014 (MPR) Creation
// ««revision-date»»···
//--
#include <avr/io.h>
#ifndef __T4_SERVO_CONFIG_H__
#define __T4_SERVO_CONFIG_H__ 1
// timing: (X:pwm, _:zero)
//
// X_______________XXXXXXXXXXXXX_______________XXXXXXXXXXXXX
//
// |<------------->|<--------->|<------------->|<--------->| ...
// SERVO_OFFSET SERVO_MAX SERVO_OFFSET SERVO_MAX
//
// within SERVO_MAX you get the PWM set with servo_set (id, pwm)
// if you set SERVO_OFFSET to zero, you get a pwm from 0 to 100% duty cycle
//
// the period of the smallest tick is derived from the timer4 prescaler
// plus the ISR overhead. however, the latter one should be constant
// regardless of the pwm signal generated
#define SERVO_OFFSET 0
#define SERVO_MIN 0
#define SERVO_MAX 255
// Counter value for timer 4 without any prescaler for a single tick
#define PWMFREQ 500
#define T4_VALUE F_CPU/SERVO_MAX/PWMFREQ
#define SERVO_COUNT 8
#define T4_SENSOR_NAME "Dual RGBW Led"
#ifndef servo_channels_defined
#define servo_channels_defined
static servo_channel_type servo_channels [SERVO_COUNT] =
{ {0x0E, 0x0D, PINE2, 0}
, {0x0B, 0x0D, PINB7, 0}
, {0x0B, 0x0D, PINB6, 0}
, {0x0B, 0x0D, PINB5, 0}
, {0x0B, 0x0D, PINB0, 0}
, {0x0B, 0x0D, PINB1, 0}
, {0x0B, 0x0D, PINB2, 0}
, {0x0B, 0x0D, PINB3, 0}
};
#endif
#endif

View file

@ -1,113 +1,101 @@
/* /*
** Copyright (C) 2013 Marcus Priesch, All rights reserved ** Copyright (C) 2013-2014 Marcus Priesch, All rights reserved
** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at ** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at
** **
** Redistribution and use in source and binary forms, with or without ** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions ** modification, are permitted provided that the following conditions
** are met: ** are met:
** 1. Redistributions of source code must retain the above copyright ** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer. ** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright ** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the ** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution. ** documentation and/or other materials provided with the distribution.
** 3. Neither the name of the Institute nor the names of its contributors ** 3. Neither the name of the Institute nor the names of its contributors
** may be used to endorse or promote products derived from this software ** may be used to endorse or promote products derived from this software
** without specific prior written permission. ** without specific prior written permission.
** **
** THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ** THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE ** ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE. ** SUCH DAMAGE.
** **
**++ **++
** Name ** Name
** t4-servo-sensor ** t4-servo-sensor
** **
** Purpose ** Purpose
** Implements a sensor around t4servo.c ** Implements a sensor around t4servo.c
** **
** **
** Revision Dates ** Revision Dates
** 31-Mar-2013 (MPR) Creation ** 31-Mar-2013 (MPR) Creation
** ««revision-date»»··· ** 12-Mar-2014 (MPR) Factored to support configurable amount of pwm's
**-- ** ««revision-date»»···
*/ **--
*/
#include "contiki.h"
#include "dev/t4-servo.h" #include "contiki.h"
#include "dev/t4-servo-sensor.h" #include "dev/t4-servo.h"
#include "dev/t4-servo-sensor.h"
const struct sensors_sensor t4_servo_sensor;
static int status(int type); const struct sensors_sensor t4_servo_sensor;
static int enabled = 0;
static int status(int type);
static int value (int channel) static int enabled = 0;
{
if (channel >= SERVO_COUNT) static int value (int channel)
return -1; {
else if (channel < 0) if (channel >= SERVO_COUNT)
return -2; return -1;
else else if (channel < 0)
return t4_servo_get (channel); return -2;
} else
return t4_servo_get (channel);
static int configure (int type, int c) // type, c: SENSORS_ACTIVE, 1 -> act. }
// type, c: SENSORS_ACTIVE, 0 -> deact.
{ static int configure (int type, int c) // type, c: SENSORS_ACTIVE, 1 -> act.
switch (type) // type, c: SENSORS_ACTIVE, 0 -> deact.
{ {
case SENSORS_ACTIVE : switch (type)
if (c == 0) {
{ case SENSORS_ACTIVE :
t4_servo_off (); if (c == 0)
} {
else if (c == 1) t4_servo_off ();
{ }
t4_servo_init (); else if (c == 1)
t4_servo_set_io (0, T4_SENSOR_1_PORT, T4_SENSOR_1_DDR, T4_SENSOR_1_PIN); {
t4_servo_set_io (1, T4_SENSOR_2_PORT, T4_SENSOR_2_DDR, T4_SENSOR_2_PIN); t4_servo_init ();
t4_servo_set_io (2, T4_SENSOR_3_PORT, T4_SENSOR_3_DDR, T4_SENSOR_3_PIN); }
t4_servo_set_io (3, T4_SENSOR_4_PORT, T4_SENSOR_4_DDR, T4_SENSOR_4_PIN); break;
}
break; default :
if (type >= SERVO_COUNT)
case 0 : return -1;
t4_servo_set (0, c); else
break; t4_servo_set (type, c);
break;
case 1 :
t4_servo_set (1, c); }
break; return 0;
}
case 2 :
t4_servo_set (2, c); static int status(int type)
break; {
switch (type)
case 3 : {
t4_servo_set (3, c); case SENSORS_ACTIVE:
case SENSORS_READY:
default: return enabled;
break; }
} return 0;
return 0; }
}
SENSORS_SENSOR(t4_servo_sensor, T4_SENSOR_NAME, value, configure, status);
static int status(int type)
{
switch (type)
{
case SENSORS_ACTIVE:
case SENSORS_READY:
return enabled;
}
return 0;
}
SENSORS_SENSOR(t4_servo_sensor, T4_SENSOR_NAME, value, configure, status);

View file

@ -1,71 +1,52 @@
/* /*
** Copyright (C) 2013 Marcus Priesch, All rights reserved ** Copyright (C) 2013-2014 Marcus Priesch, All rights reserved
** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at ** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at
** **
** Redistribution and use in source and binary forms, with or without ** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions ** modification, are permitted provided that the following conditions
** are met: ** are met:
** 1. Redistributions of source code must retain the above copyright ** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer. ** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright ** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the ** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution. ** documentation and/or other materials provided with the distribution.
** 3. Neither the name of the Institute nor the names of its contributors ** 3. Neither the name of the Institute nor the names of its contributors
** may be used to endorse or promote products derived from this software ** may be used to endorse or promote products derived from this software
** without specific prior written permission. ** without specific prior written permission.
** **
** THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ** THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE ** ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE. ** SUCH DAMAGE.
** **
**++ **++
** Name ** Name
** t4-servo-sensor ** t4-servo-sensor
** **
** Purpose ** Purpose
** Implements a sensor around t4servo.c ** Implements a sensor around t4servo.c
** **
** **
** Revision Dates ** Revision Dates
** 31-Mar-2013 (MPR) Creation ** 31-Mar-2013 (MPR) Creation
** ««revision-date»»··· ** 12-Mar-2014 (MPR) Factored to support configurable amount of pwm's
**-- ««revision-date»»···
*/ **--
*/
#ifndef __T4_SERVO_SENSOR_H__
#define __T4_SERVO_SENSOR_H__ #ifndef __T4_SERVO_SENSOR_H__
#define __T4_SERVO_SENSOR_H__
#include "lib/sensors.h"
#include "lib/sensors.h"
extern const struct sensors_sensor t4_servo_sensor; #include "t4-servo-config.h"
#define T4_SENSOR_NAME "T4-Sensor" extern const struct sensors_sensor t4_servo_sensor;
#define T4_SENSOR_1_PORT 0x0E #endif /* __T4_SERVO_SENSOR_H__ */
#define T4_SENSOR_1_DDR 0x0D
#define T4_SENSOR_1_PIN PINE4
#define T4_SENSOR_2_PORT 0x0E
#define T4_SENSOR_2_DDR 0x0D
#define T4_SENSOR_2_PIN PINE3
#define T4_SENSOR_3_PORT 0x0E
#define T4_SENSOR_3_DDR 0x0D
#define T4_SENSOR_3_PIN PINE5
#define T4_SENSOR_4_PORT 0x0E
#define T4_SENSOR_4_DDR 0x0D
#define T4_SENSOR_4_PIN PINE6
#endif /* __T4_SERVO_SENSOR_H__ */

View file

@ -1,150 +1,139 @@
/* /*
** Copyright (C) 2013 Marcus Priesch, All rights reserved ** Copyright (C) 2013-2014 Marcus Priesch, All rights reserved
** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at ** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at
** **
** Redistribution and use in source and binary forms, with or without ** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions ** modification, are permitted provided that the following conditions
** are met: ** are met:
** 1. Redistributions of source code must retain the above copyright ** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer. ** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright ** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the ** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution. ** documentation and/or other materials provided with the distribution.
** 3. Neither the name of the Institute nor the names of its contributors ** 3. Neither the name of the Institute nor the names of its contributors
** may be used to endorse or promote products derived from this software ** may be used to endorse or promote products derived from this software
** without specific prior written permission. ** without specific prior written permission.
** **
** THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ** THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE ** ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE. ** SUCH DAMAGE.
** **
**++ **++
** Name ** Name
** t4-servo ** t4-servo
** **
** Purpose ** Purpose
** Implements software pwm on any portpins via timer 4 ** Implements software pwm on any portpins via timer 4
** **
** **
** Revision Dates ** Revision Dates
** 31-Mar-2013 (MPR) Creation ** 31-Mar-2013 (MPR) Creation
** <EFBFBD><EFBFBD>revision-date<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ** 12-Mar-2014 (MPR) Factored to support configurable amount of pwm's
**-- ** ««revision-date»»···
*/ **--
*/
#include "t4-servo.h"
#include <avr/io.h> #include "t4-servo.h"
#include <avr/interrupt.h> #include "t4-servo-config.h"
#include "led.h" #include "t4-servo-hardware.h"
#include <avr/io.h>
static servo_channel_type servo_channels [SERVO_COUNT]; #include <avr/interrupt.h>
// timer 4: CTC OCR4A // static servo_channel_type servo_channels [SERVO_COUNT];
#define WGM4 0x4
// timer 4: CTC OCR4A
void t4_servo_init (void) #define WGM4 0x4
{
unsigned char channel; void t4_servo_init (void)
{
for (channel = 0; channel < SERVO_COUNT; channel ++) unsigned char channel;
{
servo_channels [channel].duty = SERVO_MAX/2; for (channel = 0; channel < SERVO_COUNT; channel ++)
t4_servo_set_io (channel, DEFAULT_PORT, DEFAULT_DDR, DEFAULT_PIN); {
} _SFR_IO8 (servo_channels [channel].ddr ) |= servo_channels [channel].pin;
_SFR_IO8 (servo_channels [channel].port) &= ~(servo_channels [channel].pin);
cli (); }
TCCR4A = 0x00;
TCCR4A_struct.wgm4 = WGM4 & 0x3; cli ();
TCCR4B_struct.wgm4 = (WGM4 & 0xc) >> 2; TCCR4A = 0x00;
TCCR4B_struct.cs4 = 0x1; // No prescaler TCCR4A_struct.wgm4 = WGM4 & 0x3;
TCCR4C = 0x00; TCCR4B_struct.wgm4 = (WGM4 & 0xc) >> 2;
OCR4A = T4_VALUE; TCCR4B_struct.cs4 = 0x1; // No prescaler
TIMSK4_struct.ocie4a = 1; TCCR4C = 0x00;
sei(); OCR4A = (T4_VALUE);
} TIMSK4_struct.ocie4a = 1;
TIMSK4_struct.toie4 = 1;
void t4_servo_off (void) sei();
{ }
TIMSK4_struct.ocie4a = 0;
} void t4_servo_off (void)
{
int t4_servo_set_io TIMSK4_struct.toie4 = 0;
( unsigned char channel TIMSK4_struct.ocie4a = 0;
, unsigned char port }
, unsigned char ddr
, unsigned char pin int t4_servo_get (unsigned int channel)
) {
{ if (channel >= SERVO_COUNT)
if (channel >= SERVO_COUNT) return -1;
return -1; //printf ("t4_servo_get: %d, %d\n", channel, servo_channels [channel].duty);
return servo_channels [channel].duty;
servo_channels [channel].port = port; }
servo_channels [channel].ddr = ddr;
servo_channels [channel].pin = 1 << pin; int t4_servo_set (unsigned int channel, unsigned char duty)
{
_SFR_IO8 (ddr) |= pin; //printf ("t4_servo_set: %d, %d\n", channel, duty);
return 0; if (channel >= SERVO_COUNT)
} return -1;
int t4_servo_get (unsigned int channel) if (duty > SERVO_MAX)
{ return -2;
if (channel >= SERVO_COUNT)
return -1; if (duty < SERVO_MIN)
return -3;
return servo_channels [channel].duty;
} servo_channels [channel].duty = duty;
return 0;
int t4_servo_set (unsigned int channel, unsigned int duty) }
{
if (channel >= SERVO_COUNT) ISR (TIMER4_COMPA_vect, ISR_NOBLOCK)
return -1; {
unsigned char channel;
if (duty > SERVO_MAX) static unsigned int tick_count = 0;
return -2;
cli ();
if (duty < SERVO_MIN) for (channel = 0; channel < SERVO_COUNT; channel ++)
return -3; {
if (tick_count < servo_channels [channel].duty)
servo_channels [channel].duty = duty; {
return 0; // turn on
} // _SFR_IO8 (servo_channels [channel].ddr ) |= servo_channels [channel].pin;
_SFR_IO8 (servo_channels [channel].port) |= servo_channels [channel].pin;
ISR (TIMER4_COMPA_vect, ISR_NOBLOCK) }
{ else
unsigned char channel; {
static unsigned int tick_count = 0; // turn off
_SFR_IO8 (servo_channels [channel].port) &= ~(servo_channels [channel].pin);
cli (); // _SFR_IO8 (servo_channels [channel].ddr ) |= servo_channels [channel].pin;
for (channel = 0; channel < SERVO_COUNT; channel ++) }
{ }
if (tick_count < servo_channels [channel].duty)
{ tick_count ++;
// turn on
_SFR_IO8 (servo_channels [channel].ddr ) |= servo_channels [channel].pin; if (tick_count >= (SERVO_MAX + SERVO_OFFSET))
_SFR_IO8 (servo_channels [channel].port) |= servo_channels [channel].pin; {
} tick_count = 0;
else }
{ sei();
// turn off
_SFR_IO8 (servo_channels [channel].port) &= ~(servo_channels [channel].pin); }
_SFR_IO8 (servo_channels [channel].ddr ) |= servo_channels [channel].pin;
}
}
tick_count ++;
if (tick_count >= (SERVO_MAX + SERVO_OFFSET))
{
tick_count = 0;
}
sei();
}

View file

@ -1,96 +1,61 @@
/* /*
** Copyright (C) 2013 Marcus Priesch, All rights reserved ** Copyright (C) 2013-2014 Marcus Priesch, All rights reserved
** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at ** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at
** **
** Redistribution and use in source and binary forms, with or without ** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions ** modification, are permitted provided that the following conditions
** are met: ** are met:
** 1. Redistributions of source code must retain the above copyright ** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer. ** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright ** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the ** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution. ** documentation and/or other materials provided with the distribution.
** 3. Neither the name of the Institute nor the names of its contributors ** 3. Neither the name of the Institute nor the names of its contributors
** may be used to endorse or promote products derived from this software ** may be used to endorse or promote products derived from this software
** without specific prior written permission. ** without specific prior written permission.
** **
** THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ** THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE ** ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE. ** SUCH DAMAGE.
** **
**++ **++
** Name ** Name
** t4-servo ** t4-servo
** **
** Purpose ** Purpose
** Implements software pwm on any portpins via timer 4 ** Implements software pwm on any portpins via timer 4
** **
** **
** Revision Dates ** Revision Dates
** 31-Mar-2013 (MPR) Creation ** 31-Mar-2013 (MPR) Creation
** <EFBFBD><EFBFBD>revision-date<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ** 12-Mar-2014 (MPR) Factored to support configurable amount of pwm's
**-- ««revision-date»»···
*/ **--
*/
#ifndef __T4_SERVO_H__
#define __T4_SERVO_H__ #ifndef __T4_SERVO_H__
#define __T4_SERVO_H__
// timing: (X:pwm, _:zero)
// typedef struct struct_servo_channel
// X_______________XXXXXXXXXXXXX_______________XXXXXXXXXXXXX {
// unsigned char port;
// |<------------->|<--------->|<------------->|<--------->| ... unsigned char ddr;
// SERVO_OFFSET SERVO_MAX SERVO_OFFSET SERVO_MAX unsigned char pin;
// unsigned char duty;
// within SERVO_MAX you get the PWM set with servo_set (id, pwm) } servo_channel_type;
// if you set SERVO_OFFSET to zero, you get a pwm from 0 to 100% duty cycle
// void t4_servo_init(void);
// the period of the smallest tick is derived from the timer4 prescaler void t4_servo_off(void);
// plus the ISR overhead. however, the latter one should be constant
// regardless of the pwm signal generated int t4_servo_get(unsigned int channel);
int t4_servo_set(unsigned int channel, unsigned char duty);
#define SERVO_OFFSET 256 #endif /* __T4_SERVO_H__ */
#define SERVO_MIN 16
#define SERVO_MAX 32
// Counter value for timer 4 without any prescaler for a single tick
#define PWMFREQ 500
#define T4_VALUE F_CPU/SERVO_MAX/PWMFREQ
#define SERVO_COUNT 4
#define DEFAULT_PORT 0x0E
#define DEFAULT_PIN 1
#define DEFAULT_DDR 0x0D
typedef struct struct_servo_channel
{
unsigned char port;
unsigned char pin;
unsigned char ddr;
unsigned char duty;
} servo_channel_type;
void t4_servo_init(void);
void t4_servo_off(void);
int t4_servo_get(unsigned int channel);
int t4_servo_set(unsigned int channel, unsigned int duty);
int t4_servo_set_io
( unsigned char channel
, unsigned char port
, unsigned char ddr
, unsigned char pin
);
#endif /* __T4_SERVO_H__ */