Dobry den, pokousim se precist signal z vystupu modelarskeho prijimace. Zjistuji kdy prisla hrana a pote merim jak dlouho je pin na 1, tuto hodnotu hned posilam do funkce pro ovladani serva. Externi preruseni pouzit nechci protoze chci cist vice nez 3 vystupy.
#include <avr/io.h> // this contains all the IO port definitions
#include <compat/ina90.h> // among other things, contains _NOP()
#include <avr/interrupt.h>
#define F_CPU 16E6
#include <util/delay.h>
#include <inttypes.h>
volatile unsigned int counter0;
volatile unsigned int counter1;
volatile unsigned int s1a_pos = 1550;
volatile unsigned char rc_state;
volatile unsigned char rc_state_old;
volatile unsigned int rc_start_time;
volatile unsigned int rc_end_time;
volatile unsigned int rc_total_time;
#define timer0_top 65200 //8bit = 1s
#define timer1_top 50 //in Hz
void servo_pos( unsigned int s1a)
{
if (s1a <= 700) s1a = 700;
if (s1a > 2500) s1a = 2500;
//hitec c577
//s1a_pos=700;//s1a_pos=1500;//s1a_pos=2500;
//hitec 65hb
//s1a_pos=600;//s1a_pos=1450;//s1a_pos=2300;
if ( TCNT1 <= s1a) // 0 - n
PORTD = 1<<PD7;
else
PORTD = 0<<PD7;
}
ISR (TIMER0_OVF_vect)
{
++counter0;
}
ISR (TIMER1_OVF_vect)
{
++counter1;
}
int main()
{
DDRD = 0b11111111;
DDRA = (0<<PA7);
PORTA |= (1<<PA7); //pull up
/* Initial TIMER1 */
TCCR1B = (1<<WGM13) | (1<<CS11) ;//set counter1 to mode 8,with no prescaling//phase and freq correct PWM
ICR1 = 20000;//ICR1 to produce 50Hz //(MCU_FREQ = 1600000/(2 x n=1 x 20000)) = 50Hz n=prescaler
TIMSK |= (1<<TOIE1);
/* Initial TIMER0 */
TCCR0 = (1<<CS00); // Use maximum prescale: Clk
TIMSK |= (1<<TOIE0); // Enable Counter Overflow Interrupt
sei(); // Enable Global Interrupt
rc_state_old = rc_state = 1;
rc_total_time = rc_end_time = rc_start_time = 1500;
while (1)
{
//global(poloha serva) - resim w loop0, odskoceni kontrola TCNT1 pripadne zmena
servo_pos(s1a_pos);
//ted je hrana
if ( rc_state != rc_state_old) {
if ( rc_state == 1 ) {
rc_start_time = TCNT1;
}
else {
rc_end_time = TCNT1;
if( rc_end_time > rc_start_time )
rc_total_time = rc_end_time - rc_start_time;
//prectene pwm hned poslu na servo
if ( (rc_total_time > 700) && ( rc_total_time <2500) )
s1a_pos = rc_total_time;
}
//zmena hrany je oblouzena
rc_state_old = rc_state;
}
if ( (PINA & (1<<PA7)) == (1<<PA7) ) //ted neni pin zapojenej - bez gnd
//stav je TRUE
rc_state = 1;
else
rc_state = 0;
}
}
Muze nekdo poradit? Diky