Subversion Repositories svnkaklik

Rev

Rev 299 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

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