뉴티씨



  • HOME
  • 고객지원
  • 질문답변


 
ATmega2560 코딩 관련하여 질문 올립니다.
 글쓴이 : 지근호
작성일 : 18-02-06 20:09
조회 : 72  
#include <mega2560.h>
#include <stdio.h>
#include <stdlib.h>
#include <delay.h>
#include <string.h>
#include <math.h>
                                                                           
typedef unsigned char byte;

volatile long int cnt_rising = 0;
volatile long int cnt_falling = 0;
volatile long int cnt_elev = 0;
volatile long int cnt_aile = 0;
volatile long int cnt_thro = 0;
volatile long int cnt_rudd = 0;

//Receiver Port Setting
void Port_init()
{
    DDRD = 0b00000000;        //입력 설정
    PORTD = 0b00001100;        //INT2, 3
   
    DDRE = 0b00000000;        //입력 설정
    PORTE = 0b11000000;        //INT6, 7
   
    EIMSK = 0b11001100;        //INT2, 3, 6, 7
    EICRA = 0b11110000;        //INT2, 3 rising edge
    EICRB = 0b11110000;        //INT6, 7 rising edge
}

interrupt [EXT_INT2] void ext_int2_isr(void)    //ELEV Channel
{
    if(EICRA == 0b11110000){
        cnt_rising = (unsigned long)((unsigned long)TCNT1H << 8 | (unsigned long)TCNT1L);
        EICRA = 0b11100000;
    }
    else{
        cnt_falling = (unsigned long)((unsigned long)TCNT1H << 8 | (unsigned long)TCNT1L);
        cnt_elev = (40000 - cnt_rising + cnt_falling) %40000;
        EICRA = 0b11110000;
    }
}

interrupt [EXT_INT3] void ext_int3_isr(void)    //AILE Channel
{
    if(EICRA == 0b11110000){
        cnt_rising = (unsigned long)((unsigned long)TCNT1H << 8 | (unsigned long)TCNT1L);
        EICRA = 0b10110000;
    }
    else{
        cnt_falling = (unsigned long)((unsigned long)TCNT1H << 8 | (unsigned long)TCNT1L);
        cnt_aile = (40000 - cnt_rising + cnt_falling) %40000;
        EICRA = 0b11110000;
    }
}

interrupt [EXT_INT6] void ext_int6_isr(void)    //THRO Channel
{
    if(EICRB == 0b11110000){
        cnt_rising = (unsigned long)((unsigned long)TCNT1H << 8 | (unsigned long)TCNT1L);
        EICRB = 0b11100000;
    }
    else{
        cnt_falling = (unsigned long)((unsigned long)TCNT1H << 8 | (unsigned long)TCNT1L);
        cnt_thro = (40000 - cnt_rising + cnt_falling) %40000;
        EICRB = 0b11110000;
    }
}

interrupt [EXT_INT7] void ext_int7_isr(void)    //RUDD Channel
{
    if(EICRB == 0b11110000){
        cnt_rising = (unsigned long)((unsigned long)TCNT1H << 8 | (unsigned long)TCNT1L);
        EICRB = 0b10110000;
    }
    else{
        cnt_falling = (unsigned long)((unsigned long)TCNT1H << 8 | (unsigned long)TCNT1L);
        cnt_rudd = (40000 - cnt_rising + cnt_falling) %40000;
        EICRB = 0b11110000;
    }
}

//Timer
void Timer1_init()
{
    DDRB |= 0b11111111;       
    TCCR1A = 0b10100010;
    TCCR1B = 0b00011001;
    ICR1H = 39999 >> 8;
    ICR1L = 39999 & 0xFF;
}
void Timer3_init()
{
    DDRE |= 0b00110000;
    TCCR3A = 0b10100010;
    TCCR3B = 0b00011001;
    ICR3H = 39999 >> 8;
    ICR3L = 39999 & 0xFF;
}

void main(void)
{
    Port_init();
    Timer1_init();
    Timer3_init();
   
    DDRE |= 0x02;
   
    UCSR0A |= 0x00;
   
    UCSR0B |= 0x18;
   
    UCSR0C |= 0x06;
   
    UBRR0H = 0x00;
   
    UBRR0L = 0x67;
   
    UDR0 = 0x00;
   
    SREG |= 0x80;
   
    while(1)
    {
    printf("ELEV : %6ld / AILE : %6ld / THRO : %6ld / RUDD : %6ld\n", cnt_elev, cnt_aile, cnt_thro, cnt_rudd);
}
}

조종기 : DEVO7, 수신기 RX701을 ATmega2560에 연결하여

수신기로 부터 받아오는 값을 UART를 통해 확인하고자 합니다.

헌데 TCNT1H와 TCNT1L를 합치는 부분에서 문제가 발생하는 것인지

출력되는 값이 안정되지 않고 지속적으로 변동됩니다.

혹시 코딩에 수정할 부분이 있는지, 어떻게 수정을 하면 될지

알 수 있을까요?