Subversion Repositories svnkaklik

Rev

Rev 299 | Rev 302 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
297 kaklik 1
//----------------------------------------------------------------------------------
300 kaklik 2
//	Prevodnik RS232 <--> RS485
299 kaklik 3
//	pri startbitu na RS232 zapne okamzite vysilac pokud nejsou detekovana data z RS485.
297 kaklik 4
//----------------------------------------------------------------------------------
5
 
6
#include <avr/io.h>
7
#include <avr/interrupt.h>
8
#include <stdlib.h>
9
#include <ctype.h>
10
#include <avr/eeprom.h>
11
#include <avr/pgmspace.h>
12
 
300 kaklik 13
#define POVOLOVAK_LOW	PORTB &= ~(1<<PB3)
14
#define POVOLOVAK_HIGH  PORTB |= (1<<PB3)
15
#define DATA	(PIND & (1<<PD1))
297 kaklik 16
 
17
// ------------------------------------------------------------------
18
//	Main
19
// ------------------------------------------------------------------
20
int main(void)
21
{
298 kaklik 22
int delay;
23
int spozdeni;
299 kaklik 24
int last;
297 kaklik 25
 
26
  DDRD |= (1<<DDD5);
300 kaklik 27
  DDRB |= (1<<DDB3);
297 kaklik 28
 
298 kaklik 29
  TCCR1B |= (1 << CS10);  // Set up timer 
297 kaklik 30
 
31
  while(1)
32
  {
300 kaklik 33
	if(!DATA)   // kdyz je 0 na datech 
297 kaklik 34
	{ 
300 kaklik 35
	  POVOLOVAK_HIGH;    // zapni vysilani
36
 
37
	  for (spozdeni=8;spozdeni>=0;spozdeni --) // odpocitej dobu 8mi bitu 
299 kaklik 38
	  {
300 kaklik 39
		last = DATA;
299 kaklik 40
	    TCNT1 = 0;					// zacni znova merit cas
300 kaklik 41
	    while (TCNT1 <= delay)		// bit poznas tak, ze je to nejmensi nalezena delka trvaleho stavu
299 kaklik 42
		{
300 kaklik 43
		  if(DATA != last)
299 kaklik 44
		  {
45
		    if (TCNT1 < delay) delay = TCNT1;
46
			break;
47
		  }
298 kaklik 48
        }
49
	  }
300 kaklik 50
	  while (!DATA);				// cekani na stop bit
51
 
52
	  for (spozdeni=2;spozdeni >= 0;spozdeni --)
53
	  {
54
	    TCNT1 = 0;					// zacni znova merit cas zacatku stopbitu
55
	    while (TCNT1 <= delay) if(!DATA) break;
56
	  }
297 kaklik 57
    }
300 kaklik 58
    else  POVOLOVAK_LOW;  	// kdyz je 1 prepni na prijem 
59
	delay = 60000;
297 kaklik 60
  }
298 kaklik 61
  return(0);
297 kaklik 62
}