Subversion Repositories svnkaklik

Rev

Rev 299 | Go to most recent revision | Blame | Last modification | View Log | Download

//----------------------------------------------------------------------------------
//      Prevodnik RS232 <--> RS485
//      pri startbitu na RS232 zapne okamzite vysilac pokud nejsou detekovana data z RS485.
//----------------------------------------------------------------------------------

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

#define POVOLOVAK_LOW   PORTB &= ~(1<<PB3)
#define POVOLOVAK_HIGH  PORTB |= (1<<PB3)
#define DATA    (PIND & (1<<PD1))

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

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

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

  while(1)
  {
        if(!DATA)   // kdyz je 0 na datech 
        { 
          POVOLOVAK_HIGH;    // zapni vysilani
                  
          for (spozdeni=8;spozdeni>=0;spozdeni --) // odpocitej dobu 8mi bitu 
          {
                last = DATA;
            TCNT1 = 0;                                  // zacni znova merit cas
            while (TCNT1 <= delay)              // bit poznas tak, ze je to nejmensi nalezena delka trvaleho stavu
                {
                  if(DATA != last)
                  {
                    if (TCNT1 < delay) delay = TCNT1;
                        break;
                  }
        }
          }
          while (!DATA);                                // cekani na stop bit

          for (spozdeni=2;spozdeni >= 0;spozdeni --)
          {
            TCNT1 = 0;                                  // zacni znova merit cas zacatku stopbitu
            while (TCNT1 <= delay) if(!DATA) break;
          }
    }
    else  POVOLOVAK_LOW;        // kdyz je 1 prepni na prijem 
        delay = 60000;
  }
  return(0);
}