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

[ATmega8 i pilot RC5+LCD][C]

killerwo 03 Mar 2009 17:25 2286 1
REKLAMA
  • #1 6231106
    killerwo
    Poziom 10  
    Witam.

    Niestety mam problem z uruchomieniem tego układu korzystam z kodu

    LCD+ATMEGA8+Odbiornik+pilot

    #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;
    
    }


    Program jest pod atmege8 ale z zewnętrznym kwarcem 16MHz którego ja nie posiadam czyli musze zmienic

    #define TIMER_0_CNT 0x22 


    na?

    oraz
    TCCR0 = 0x02;


    na 0x01;

    za bardzo nie wiem jakie czasy musze uzyskac w tych licznikach;/

    drugie pytanie to w głównej pętli:

    
    int main(void)
    {
    	
    	LCD_Initalize();
    	LCD_Clear();
    	
            unsigned char bufor[10];
    	
    	while(1)
    	{
    	
    	
    	        rc5data&=0x003f; 
    
    		LCD_GoTo(0,0);
    		LCD_WriteText("test"); 
    		LCD_GoTo(0,1); 
    		sprintf(bufor,"%u",rc5data); 
    		LCD_WriteText(bufor);
    
    	}
    }


    chodzi o złapanie kodu wysłanego z Pilota(wyświetla sie stałe 0 na LCD) , czy jest cos z min zamieszane?Bardzo proszę o wyrozumiałość wiem ze tematy były wałkowane i używałem przycisku SZUKAJ;]ale nie udało mi sią rozwiązać problemu
  • REKLAMA
  • #2 6234527
    killerwo
    Poziom 10  
    Może ktoś ma napisany program pod TSOP... albo umie sprawdzić co jest nie tak?troche dobrej woli;]
REKLAMA