I am trying to make a ESC using the baby-o 328p. The 3pirc example is very similar to what i would like to achieve.
I have been able to get the interrupts working for both PD0 and PC5, however i am not able to use the pulse lengths to set the motor speeds. Below is the code i have working changing the brightness of the LED. My TX gives me pulse times of between 360 and 600.
/*
#include <pololu/orangutan.h>
#include <avr/interrupt.h>
#define F_CPU 20000000UL // Baby Orangutan frequency (20MHz)
const int minPulseTime = 156; // 0.5 ms
const int neutralPulseTime = 469; // 1.5 ms
const int maxPulseTime = 782; // 2.5ms
volatile unsigned int prevTime;
volatile unsigned int highDur;
volatile unsigned char newPulse;
unsigned int pulse;
ISR( PCINT2_vect ) /* Code to execute when external interrupt on PCINT23 .. 16 is triggered by a logic change*/
{
// Save a snapshot of PIND at the current time
unsigned char pind = PIND;
unsigned int time = TCNT1;
if (pind & (1 << PORTD0))
{
// PD0 has changed to high so record the start of pulse
prevTime = time;
}
else
{
// PD0 has changed to low so record the high pulse's duration
highDur = time - prevTime;
newPulse = 1; // The high pulse just finished so we can process it now
}
}
int main(void)
{
// Setup for External Interrupt PCINT16 (uses pin 2 or PD0)
SREG = ( 1<<SREG_I ); // Enables global interrupts
PCMSK2 = (1 << PORTD0); // Set pin-change interrupt mask for pin PD0
PCICR = ( 1<<PCIE2 ); // Enables vector interrupts on PCINT23 .. 16
TCCR1B = 0x03; // Timer 1 ticks at 20MHz/64 = 312.5kHz (1 tick per 3.2us)
red_led(0);
//set_motors(0,0);
while (1) // Loop forever
{
long m1 = 0;
if (neutralPulseTime > highDur)
{
m1 = (neutralPulseTime - highDur);
}
else
{
m1 = (highDur - neutralPulseTime);
}
m1 = m1 * 2;
if (m1 <4)
m1 = 0;
if (m1 >= 256)
m1 = 255;
//set_motors(m1,m1);
if (m1 < 0)
m1 = -m1;
if (m1 ==0 )
{
red_led(0);
delay_us(255);
//set_motors(0,0);
}
else
{
red_led(1);
delay_us(m1);
red_led(0);
delay_us(255-m1);
//set_motors(m1,m1);
}
}
}
I have a video of this code running on youtube.
http://www.youtube.com/watch?v=UF6i3ue-ksk
My problem is whenever i try to set the motor speeds the motors do not run and the LED does not run as before. Is this a problem with the timers of the pwm affecting the delay timer?