Subversion Repositories svnkaklik

Rev

Rev 297 | Rev 299 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

//----------------------------------------------------------------------------------
//      Prevodnik RS232 <--> RS485 s prodlevou pri prevodu smerem do RS485
//      pri startbitu na RS232 zapne okamzite vysilac. ceka nez prestanou prichazet data a vysilac vypne.
//  je implementovany jednoduchy algoritmus na rozpoznani rychlosti, ktery vsak nefunkuje na veskery mozny obsah bajtu.
//----------------------------------------------------------------------------------

#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <ctype.h>
#include <avr/eeprom.h>
#include <avr/pgmspace.h>


#define F_CPU 3686400UL
#include <util/delay.h>

void delay_ms(unsigned int ms)
{
  while(ms)
  {
    _delay_ms(0.96);
        ms--;
  }
}


// ------------------------------------------------------------------
//      Main
// ------------------------------------------------------------------
int main(void)
{
int delay;
int bit;
int spozdeni;

  DDRD |= (1<<DDD5);
//  DDRB |= (1<<DDB3);

  TCCR1B |= (1 << CS10);  // Set up timer 

//  PORTB |= (1<<PB3);

  while(1)
  {
        if(!(PIND & (1<<PD1)))   // kdyz je 0 na datech 
        { 
          PORTD &= ~(1<<PD5);    // zapni vysilani
          TCNT1 = 0;                            // zacni merit cas 

      while (!(PIND & (1<<PD1)));  // cekej na 1                        

          delay = TCNT1;                                // hned jak narazis na 1, poznamenej si jak dlouho to trvalo
          TCNT1 = 0;                                    // zacni znova merit cas
          bit = (delay >> 3);                   // cas ktery jsme pred tim zmerili videl 8mi aby jsme se dostali maximalne na delku bitu
          
          
          while (TCNT1 <= delay);  // pockej zmerenou dobu 
          if((PIND & (1<<PD1)))   // kdyz je 1 na datech, pockej jeste
          { 
            for (spozdeni=0;spozdeni<=16;spozdeni ++)
            {
              TCNT1 = 0;                                        // zacni znova merit cas
                  if((PIND & (1<<PD1))) while (TCNT1 <= bit);                   // kdyz je porad 1 tak pockej jeste delku bitu  
                  else break;
        }
          }
    }
    else  PORTD |= (1<<PD5);    // kdyz je 1 prepni na prijem 
  }
  return(0);
}