AkcelerometerTopoliVirag.c: Rozdiel medzi revíziami
Zo stránky SensorWiki
Vytvorená stránka „#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 ...“ |
Bez shrnutí editace |
||
(2 medziľahlé úpravy od rovnakého používateľa nie sú zobrazené.) | |||
Riadok 1: | Riadok 1: | ||
<source lang="c"> | |||
#include <stdlib.h> | #include <stdlib.h> | ||
#include <stdio.h> | #include <stdio.h> | ||
Riadok 9: | Riadok 10: | ||
volatile unsigned int Xzaciatok = 0; | volatile unsigned int Xzaciatok = 0; | ||
volatile | volatile long int XPWM=0; | ||
volatile unsigned int Xkoniec=0; | volatile unsigned int Xkoniec=0; | ||
volatile unsigned int Xaktualne=0; | volatile unsigned int Xaktualne=0; | ||
Riadok 17: | Riadok 18: | ||
// PWM Y | // PWM Y | ||
volatile unsigned int Yzaciatok = 0; | volatile unsigned int Yzaciatok = 0; | ||
volatile | volatile long int YPWM=0; | ||
volatile unsigned int Ykoniec=0; | volatile unsigned int Ykoniec=0; | ||
volatile unsigned int Yaktualne=0; | volatile unsigned int Yaktualne=0; | ||
Riadok 26: | Riadok 27: | ||
FILE mystdout = FDEV_SETUP_STREAM(lcdDataWrite, NULL, _FDEV_SETUP_WRITE); // nová funkcia pre jeden znak | FILE mystdout = FDEV_SETUP_STREAM(lcdDataWrite, NULL, _FDEV_SETUP_WRITE); // nová funkcia pre jeden znak | ||
unsigned char pole[8][16]={" | unsigned char pole[8][16]={"1x65456798776549","2xMOIJNKZHGFRTWD","3xjkreiooasjkrpl","4x@#$%^&*()_+}|:","5x65456798776549","6xMOIJNKZHGFRTWD","7xjkreiooasjkrpl","8x@#$%^&*()_+}|:"}; | ||
ISR(PCINT0_vect) // X prerušenie | ISR(PCINT0_vect) // X prerušenie | ||
Riadok 39: | Riadok 40: | ||
dobeznaX=Xaktualne; | dobeznaX=Xaktualne; | ||
XPWM=dobeznaX-Xzaciatok; | XPWM=dobeznaX-Xzaciatok; | ||
if (XPWM<=0) | |||
{ | |||
XPWM=dobeznaX+65536-Xzaciatok; | |||
} | |||
} | } | ||
else | else | ||
Riadok 47: | Riadok 52: | ||
Xzaciatok=Xaktualne; | Xzaciatok=Xaktualne; | ||
rozdielX=Xzaciatok-Xkoniec; | rozdielX=Xzaciatok-Xkoniec; | ||
if (rozdielX <=0) | |||
{ | |||
rozdielX=Xzaciatok+65536-Xkoniec; | |||
} | |||
} | } | ||
} | } | ||
Riadok 62: | Riadok 71: | ||
Yzaciatok=Yaktualne; | Yzaciatok=Yaktualne; | ||
rozdielY=Yzaciatok-Ykoniec; | rozdielY=Yzaciatok-Ykoniec; | ||
if (rozdielY <=0) | |||
{ | |||
rozdielY=Yzaciatok+65536-Ykoniec; | |||
} | |||
} | } | ||
else | else | ||
Riadok 68: | Riadok 81: | ||
dobeznaY=Yaktualne; | dobeznaY=Yaktualne; | ||
YPWM=dobeznaY-Yzaciatok; | YPWM=dobeznaY-Yzaciatok; | ||
if (YPWM<=0) | |||
{ | |||
YPWM=dobeznaY+65536-Yzaciatok; | |||
} | |||
} | } | ||
} | } | ||
Riadok 92: | Riadok 109: | ||
} | } | ||
} | } | ||
Riadok 149: | Riadok 123: | ||
TCCR1B = 0b11000010; // T1 clk = F_CPU : 1024, falling edge pin ICP1, | 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 !! | TCCR1A = 0b00000000; // T1 in timer mode !! Note: if You omit this, TCNT1 will be only 8-bit !! | ||
TCNT1 = 0x0000; // inicializácia | TCNT1 = 0x0000; // inicializácia poèítadla (16-bit! Low+High bytes) | ||
lcdInit4(); | lcdInit4(); | ||
stdout = &mystdout; // Odteraz funguje printf(); | stdout = &mystdout; // Odteraz funguje printf(); | ||
Riadok 155: | Riadok 129: | ||
int i=0,j=0; | int i=0,j=0; | ||
int cislo=0; | |||
int k1=0; | |||
for(;;){ | 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 < 9500){ | |||
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); | |||
delay_ms( | 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 < 9500){ | |||
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<=-5) | |||
{ | |||
k1=3; | |||
} | |||
} | |||
} | |||
} | } | ||
</source> |
Aktuálna revízia z 14:36, 16. január 2013
#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 long 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 long 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;
if (XPWM<=0)
{
XPWM=dobeznaX+65536-Xzaciatok;
}
}
else
// sirka impulzu nabezna
{
Xkoniec=Xzaciatok;
Xzaciatok=Xaktualne;
rozdielX=Xzaciatok-Xkoniec;
if (rozdielX <=0)
{
rozdielX=Xzaciatok+65536-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;
if (rozdielY <=0)
{
rozdielY=Yzaciatok+65536-Ykoniec;
}
}
else
// sirka impulzu dobezna
{
dobeznaY=Yaktualne;
YPWM=dobeznaY-Yzaciatok;
if (YPWM<=0)
{
YPWM=dobeznaY+65536-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 < 9500){
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 < 9500){
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<=-5)
{
k1=3;
}
}
}
}