logo elektroda
logo elektroda
X
logo elektroda
REKLAMA
REKLAMA
Adblock/uBlockOrigin/AdGuard mogą powodować znikanie niektórych postów z powodu nowej reguły.

Atmega16[C] problem z uruchomieniem rc5

xysio_ 14 Mar 2009 11:05 2603 3
REKLAMA
  • #1 6279572
    xysio_
    Poziom 18  
    znalazłem na forum bibliotekę c służącą do odbioru rc5(co prawa była pisana na At8 ale myślę że na At16 nie powinno być problemów)
    ale jednak nie mogę uruchomić programu, w ogóle nie ma reakcji na pilot(uniwersalny ustawiony na RC5)
    w pętli głównej programu wpisałem
    	while(1)
    	{
    		if(rc5data)
    		{
    			PORTB=0x00;
    		}
    	}

    w bibliotece zmieniłem ustawienia Timera0 wg opisu dla kwarcu 4Mhz
    oto ta biblioteka
    #include <avr/io.h>
    #include <avr/interrupt.h>
    #include <avr/signal.h>
    
    
    #define RC5BitHigh()    (bit_is_set(PIND,PD3))
    #define RC5BitLow()     (bit_is_clear(PIND,PD3))
    #define WAITFORTIMER()  { while ( timerflag == 0); timerflag = 0; }
    
    #define TIMER_0_CNT 0x22     //  111us with CLK/8 prescale kwarc 16mhz 0x22 /4mhz 0xc9
    #define RC5BITREF1  6 
    #define RC5BITREF2  11
    #define RC5BITREF3  14
    #define IR_int_enable GIFR|=(1<<INT1);GICR|=(1<<INT1)
    #define IR_int_disable GICR&=~(1<<INT1)
    
    
    volatile uint8_t timerflag;  //must be volatile because modified by interrupt handler
    volatile unsigned int    rc5data;
    
    
    void rc5_init()
    {
        DDRD  &= ~(1<<PD3);           // konfiguracja PD3 jako wejście
        PORTD |= (1<<PD3);           // enable internal pull-up resistors
     
       MCUCR|=_BV(ISC11); // ext. int. activated by falling edge
       MCUCR&=~_BV(ISC10);
       GIFR = (1<<INTF1); // clear ext. int. flag
       GICR|=_BV(INT1); // enable ext. int.
    }
    
    
    SIGNAL(SIG_OVERFLOW0)  //przepelneinie timera2
    {
        timerflag = 1;               // set global variable
        TCNT0 = TIMER_0_CNT;         // reset counter to get this interrupt again
    }
    
    SIGNAL(SIG_INTERRUPT1)
    {
    IR_int_disable;      //wylaczenie przerwan zewnetrznych
    sei();
        unsigned char   timer, i;
        // init timer/Counter2   
        TCCR0 = 0x02;                    // use CLK/8 prescale
        TCNT0 = TIMER_0_CNT;             // set timer T/16 = 111us
        TIMSK |= (1<<TOIE0);             // enable TCNT0 overflow interrupt
        TIMSK &=~(1<<TOIE2);             // disable TCNT2 overflow interrupt
       
        // measure startbit
        timerflag = 0; timer = 0;
        while ( RC5BitLow() && (timer < RC5BITREF2) ) {
            WAITFORTIMER();
            timer++;
        }
        if ( (timer > RC5BITREF1) && (timer < RC5BITREF2) ) {
            // startbit ok, decode
    
            // wait T/4: synchronize in the middle of first half of second bit
            while ( timer < RC5BITREF3 ) {
                WAITFORTIMER();
                timer++;
            }
           
            // read the remaining bits
            rc5data = 1;
            for (i=0; i<13; i++) {
                rc5data <<= 1; 
                if ( RC5BitHigh() ) {
                    rc5data |= 0x0001;
                    // wait max T/2 for H->L transition (middle of next bit)
                    timer = 0;
                    while ( RC5BitHigh() && (timer < 16) ) {
                        WAITFORTIMER();
                        timer++;
                    }
                }else{
                    rc5data &= ~0x00001;
                    // wait max T/2 for L->H transition (middle of next bit)
                    timer = 0;
                    while ( RC5BitLow() && (timer < 16) ) {
                        WAITFORTIMER();
                        timer++;
                    }
                }                   
                if ( timer == 16 ) {
                    rc5data = 0x0000;   // error, next bit not found
                    return;
                }
               
                // wait 3/4 T: await next bit
                for ( timer=0; timer < 12 ; timer++) WAITFORTIMER();
            }
    
        }else {
            rc5data = 0x0000;  // error, invalid RC-5 code
        }
        TCCR0=0;
        TIMSK &=~(1<<TOIE0);              // disable TCNT0 overflow interrupt
        TIMSK |= (1<<TOIE2);              // enable TCNT2 overflow interrupt
    IR_int_enable;
    
    }//rc5decode 
  • REKLAMA
  • Pomocny post
    #2 6280115
    hotdog
    Poziom 26  
    generalnie nie wczytując się w kod za bardzo, to nie podałeś funkcji main i nie wiadomo co tak w ogóle tam jest. Ogólnie kod działa na przerwaniach - zewnętrznych czujnika i timerów.

    Co mogę napisać nie widząc funkcji main, to:
    - podłącz czujnik podczerwieni pod IN1 (PD3),
    - wywołaj w main rc5_init();
    - później włącz przerwania przez sei();

    ogólnie następna uwaga to PORTB=0x00; tak naprawdę nic nie zrobi, bo domyślnie w PORTB jest właśnie 0x00, no chyba że jakoś to zainicjowałeś inaczej.

    pozdrawiam
  • REKLAMA
  • #3 6280209
    xysio_
    Poziom 18  
    główny plik wygląda tak
    #include<avr/io.h>
    #include<ścieżka do rc5.h>
    
    int main()
    {
    DDRD=0x00;;//Porty D jako wejścia
    DDRB=0xFF;//PORTYB jako wyjścia
    PORTB=0xFF;//portb w stan wysoki
    rc5_init();
    sei();
    while(1)
    {
    	if(rc5data)
    	{
    		PORTB=0x00;
    	}
    }
    }


    Dodano po 13 [minuty]:

    włożyłem wszystko do jednego pliku i zastosowałem sie do twoich rad i ruszyło
    #include <avr/io.h>
    #include <avr/interrupt.h>
    #include <avr/signal.h>
    
    
    #define RC5BitHigh()    (bit_is_set(PIND,PD3))
    #define RC5BitLow()     (bit_is_clear(PIND,PD3))
    #define WAITFORTIMER()  { while ( timerflag == 0); timerflag = 0; }
    
    #define TIMER_0_CNT 0xC9     //  111us with CLK/8 prescale kwarc 16mhz 0x22 /4mhz 0xc9
    #define RC5BITREF1  6
    #define RC5BITREF2  11
    #define RC5BITREF3  14
    #define IR_int_enable GIFR|=(1<<INT1);GICR|=(1<<INT1)
    #define IR_int_disable GICR&=~(1<<INT1)
    
    
    volatile uint8_t timerflag;  //must be volatile because modified by interrupt handler
    volatile unsigned int    rc5data;
    
    
    void rc5_init()
    {
        DDRD  &= ~(1<<PD3);           // konfiguracja PD3 jako wejście
        PORTD |= (1<<PD3);           // enable internal pull-up resistors
     
       MCUCR|=_BV(ISC11); // ext. int. activated by falling edge
       MCUCR&=~_BV(ISC10);
       GIFR = (1<<INTF1); // clear ext. int. flag
       GICR|=_BV(INT1); // enable ext. int.
    }
    
    
    SIGNAL(SIG_OVERFLOW0)  //przepelneinie timera2
    {
        timerflag = 1;               // set global variable
        TCNT0 = TIMER_0_CNT;         // reset counter to get this interrupt again
    }
    
    SIGNAL(SIG_INTERRUPT1)
    {
    IR_int_disable;      //wylaczenie przerwan zewnetrznych
    sei();
        unsigned char   timer, i;
        // init timer/Counter2   
        TCCR0 = 0x02;                    // use CLK/8 prescale
        TCNT0 = TIMER_0_CNT;             // set timer T/16 = 111us
        TIMSK |= (1<<TOIE0);             // enable TCNT0 overflow interrupt
        TIMSK &=~(1<<TOIE2);             // disable TCNT2 overflow interrupt
       
        // measure startbit
        timerflag = 0; timer = 0;
        while ( RC5BitLow() && (timer < RC5BITREF2) ) {
            WAITFORTIMER();
            timer++;
        }
        if ( (timer > RC5BITREF1) && (timer < RC5BITREF2) ) {
            // startbit ok, decode
    
            // wait T/4: synchronize in the middle of first half of second bit
            while ( timer < RC5BITREF3 ) {
                WAITFORTIMER();
                timer++;
            }
           
            // read the remaining bits
            rc5data = 1;
            for (i=0; i<13; i++) {
                rc5data <<= 1;
                if ( RC5BitHigh() ) {
                    rc5data |= 0x0001;
                    // wait max T/2 for H->L transition (middle of next bit)
                    timer = 0;
                    while ( RC5BitHigh() && (timer < 16) ) {
                        WAITFORTIMER();
                        timer++;
                    }
                }else{
                    rc5data &= ~0x00001;
                    // wait max T/2 for L->H transition (middle of next bit)
                    timer = 0;
                    while ( RC5BitLow() && (timer < 16) ) {
                        WAITFORTIMER();
                        timer++;
                    }
                }                   
                if ( timer == 16 ) {
                    rc5data = 0x0000;   // error, next bit not found
                    return;
                }
               
                // wait 3/4 T: await next bit
                for ( timer=0; timer < 12 ; timer++) WAITFORTIMER();
            }
    
        }else {
            rc5data = 0x0000;  // error, invalid RC-5 code
        }
        TCCR0=0;
        TIMSK &=~(1<<TOIE0);              // disable TCNT0 overflow interrupt
        TIMSK |= (1<<TOIE2);              // enable TCNT2 overflow interrupt
    IR_int_enable;
    
    }//rc5decode 
    
    int main()
    {
    DDRD=0x00;;//Porty D jako wejścia
    DDRB=0xFF;//PORTYB jako wyjścia
    PORTB=0xFF;//portb w stan wysoki
    rc5_init();
    sei();
    while(1)
    {
    	if(rc5data)
    	{
    		PORTB=0x00;
    	}
    }
    }
    
  • #4 6285411
    hotdog
    Poziom 26  
    czyli rozumiem że nie podłączyłeś czujnika pod PD3?
    Powodzenia w dalszej walce...
REKLAMA