Rev 302 | Rev 304 | Go to most recent revision | Blame | Compare with Previous | 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))
volatile unsigned int preteceni;
unsigned int bit;
unsigned int bitdelay;
unsigned int last;
unsigned int cas;
ISR(TIMER0_OVF_vect) // interrupt od preteceni casovace
{
preteceni++; // kdyz pretece, poznamenej to.
}
// ------------------------------------------------------------------
// Main
// ------------------------------------------------------------------
int main(void)
{
DDRD |= (1<<DDD5);
DDRB |= (1<<DDB3);
TIMSK |= (1 << TOIE0); // Enable timer overflow interrupt
TCCR0B |= (1 << CS10); // Set up timer
sei(); // enable interrupts
while(1)
{
if(!DATA) // kdyz je 0 na datech
{
POVOLOVAK_HIGH; // zapni vysilani
cas = 0xFFFF;
bitdelay = 0;
for (bit=0;bit<=7;bit++) // odpocitej dobu 8mi bitu
{
last = DATA;
TCNT0 = 0; // vynuluj citac pred tim, ne smazes preteceni, protoze jinak by se mohlo stat, ze se nastavi preteceni driv, nez ma
preteceni = 0;
while ((TCNT0+preteceni*0x0100) <= cas) // bit poznas tak, ze je to nejmensi nalezena delka trvaleho stavu
{
bitdelay=TCNT0+preteceni*0x0100;
if(DATA != last) // zkontroluj, jestli se nezmenil stav dat.
{
if (bitdelay < cas) cas = bitdelay; //kdyz se zmenil a trvalo to kratsi dobu nez pred tim poznemenej, jak dlouho to trvalo
break;
}
}
}
// while (!DATA); // cekani na stop bit
/* for (bit=2;bit >= 0;bit--) // odpocitej dva stopbity
{
TCNT0 = 0; // zacni znova merit cas zacatku stopbitu
preteceni=0;
while ((TCNT0+preteceni*0x0100) <= cas)
{
if(!DATA) break;
}
}*/
}
else POVOLOVAK_LOW; // kdyz je 1 prepni na prijem
}
return(0);
}