#include <stdlib.h>
#include <stdio.h>
#include <avr/io.h>
#include "lcd.h"
#include <avr/interrupt.h>
// PWM X
volatile unsigned int Xzaciatok = 0;
volatile unsigned int XPWM=0;
volatile unsigned int Xkoniec=0;
volatile unsigned int Xaktualne=0;
volatile long rozdielX;
volatile unsigned int dobeznaX=0;
// PWM Y
volatile unsigned int Yzaciatok = 0;
volatile unsigned int YPWM=0;
volatile unsigned int Ykoniec=0;
volatile unsigned int Yaktualne=0;
volatile long rozdielY;
volatile unsigned int dobeznaY=0;
FILE mystdout = FDEV_SETUP_STREAM(lcdDataWrite, NULL, _FDEV_SETUP_WRITE); // nová funkcia pre jeden znak
unsigned char pole[8][16]={"1x65456798776549","2xMOIJNKZHGFRTWD","3xjkreiooasjkrpl","4x@#$%^&*()_+}|:","5x65456798776549","6xMOIJNKZHGFRTWD","7xjkreiooasjkrpl","8x@#$%^&*()_+}|:"};
ISR(PCINT0_vect) // X prerušenie
{
//hodnota countra pri prerušení
Xaktualne=TCNT1;
// dobežná hrana
if (!(PINB & 0b00100000))
// sirka impulzu dobezna
{
dobeznaX=Xaktualne;
XPWM=dobeznaX-Xzaciatok;
}
else
// sirka impulzu nabezna
{
Xkoniec=Xzaciatok;
Xzaciatok=Xaktualne;
rozdielX=Xzaciatok-Xkoniec;
}
}
ISR(PCINT1_vect) // Y prerušenie
{
//hodnota countra pri prerušení
Yaktualne=TCNT1;
// nábežná hrana
if (PINC & 0b00000001)
// sirka impulzu nabezna
{
Ykoniec=Yzaciatok;
Yzaciatok=Yaktualne;
rozdielY=Yzaciatok-Ykoniec;
}
else
// sirka impulzu dobezna
{
dobeznaY=Yaktualne;
YPWM=dobeznaY-Yzaciatok;
}
}
//oneskorenie, milisekundy
void delay_ms(unsigned int ms)
{
unsigned int index;
while (ms)
{
index = F_CPU / 19040;
while (index)
{
asm volatile ("nop");
index--;
}
ms--;
}
}
int main(void)
{
DDRB = 0b00000000; // nastav ICR - Port B, pin0 ako INPUT
DDRC = 0b00000000; // nastav ICR - Port C, pin5 ako INPUT
PCICR=0b00000011; // nastavenie externého prerušenia
PCIFR=0b00000011;
PCMSK1=0b00000001;
PCMSK0=0b00100000;
TCCR1B = 0b11000010; // T1 clk = F_CPU : 1024, falling edge pin ICP1,
TCCR1A = 0b00000000; // T1 in timer mode !! Note: if You omit this, TCNT1 will be only 8-bit !!
TCNT1 = 0x0000; // inicializácia poèítadla (16-bit! Low+High bytes)
lcdInit4();
stdout = &mystdout; // Odteraz funguje printf();
sei(); // povolenie všetkých prerušení
int i=0,j=0;
int cislo=0;
int k1=0;
for(;;){
//dozadu
if (YPWM > 10000){
cislo=12000-((YPWM-rozdielY)+20000);
if (cislo<=0)
cislo=0;
lcdControlWrite(1<<LCD_CLR);
lcdControlWrite(0x80);
lcdControlWrite(0x80+4+k1);
for(i=0;i<10;i++){
lcdDataWrite(pole[j][i]);
}
lcdControlWrite(0x80+0x40+4+k1);
for(i=0;i<10;i++){
lcdDataWrite(pole[j+1][i]);
}
delay_ms(cislo);
j++;
if(j==7)
j=0;
}
//dopredu
if (YPWM < 9000){
cislo=12000-(rozdielY-YPWM);
if (cislo<=0)
cislo=0;
lcdControlWrite(1<<LCD_CLR);
lcdControlWrite(0x80);
lcdControlWrite(0x80+4+k1);
for(i=0;i<10;i++){
lcdDataWrite(pole[j][i]);
}
lcdControlWrite(0x80+0x40+4+k1);
for(i=0;i<10;i++){
lcdDataWrite(pole[j+1][i]);
}
delay_ms(cislo);
j--;
if(j<0)
j=6;
}
//doprava
if (XPWM > 10000){
cislo=12000-((XPWM-rozdielX)+20000);
if (cislo<=0)
cislo=0;
lcdControlWrite(1<<LCD_CLR);
lcdControlWrite(0x80);
lcdControlWrite(0x80+4+k1);
for(i=0;i<10;i++){
lcdDataWrite(pole[j][i]);
}
lcdControlWrite(0x80+0x40+4+k1);
for(i=0;i<10;i++){
lcdDataWrite(pole[j+1][i]);
}
delay_ms(cislo);
k1++;
if(k1==4)
{
k1=-4;
}
}
//dolava
if (XPWM < 9000){
cislo=12000-(rozdielX-XPWM);
if (cislo<=0)
cislo=0;
lcdControlWrite(1<<LCD_CLR);
lcdControlWrite(0x80);
lcdControlWrite(0x80+4+k1);
for(i=0;i<10;i++){
lcdDataWrite(pole[j][i]);
}
lcdControlWrite(0x80+0x40+4+k1);
for(i=0;i<10;i++){
lcdDataWrite(pole[j+1][i]);
}
delay_ms(cislo);
k1--;
if(k1<=-4)
{
k1=3;
}
}
}
}