[ATMEGA8] Cyfrowy termometr i kalkulator


* Version 0.6B *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
***************************************************************************/




#include
#include
#include
/*
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
*/

#define sbi(port, bit) (port) |= (1 << (bit))
#define cbi(port, bit) (port) &= ~(1 << (bit))

//PC5 - tranzystor od DIS1 (lewy wyświetlacz)
//PORTD - sterowanie wyświetlaczami
//PB0 - tranzystor od DIS2 (prawy wyświetlacz)
//PB1 - PB2 - diody kontrolne
//PC1 - PC4 - przyciski
//PC3, PC1, PC4, PC2

//-------------------
unsigned int L[10];
unsigned int P[10];

unsigned int lw;

//uint16_t nap;


//unsigned int buf;
unsigned int tryb;
unsigned int znak;
unsigned int krok;
unsigned int LDP, PDP;
unsigned long int a, b, c, num; //moduł calc

unsigned long int nap;



void czekaj(int czas)
{
unsigned int i = 0;
unsigned int j = 0;
unsigned int k = 0;

for(i =0; i < 255; i++)
{
for(j =0; j < 10; j++)
{
for(k = 0; k < czas; k++)
{
asm("nop");
}
}
}
}

void czekaj_m(unsigned int czas)
{
unsigned int i = 0;
unsigned int j = 0;
unsigned int k = 0;

for(i =0; i < 10; i++)
{
for(j =0; j < 10; j++)
{
for(k = 0; k < czas; k++)
{
asm("nop");
}
}
}
}

void init (void)
{
sbi(DDRC, PC5);

DDRD = 0xFF;

sbi(DDRB, PB0);

sbi(DDRB, PB1);
sbi(DDRB, PB2);

//---------------------------------

sbi(PORTC, PC1);
sbi(PORTC, PC2);
sbi(PORTC, PC3);
sbi(PORTC, PC4);

cbi(PORTC, PC5);

PORTD = 0xFF;

cbi(PORTB, PB0);

sbi(PORTB, PB1);
sbi(PORTB, PB2);
asm("nop");

//----------------------------------

L[0] = 0b10000001;
L[1] = 0b11110011;
L[2] = 0b01001001;
L[3] = 0b01100001;
L[4] = 0b00110011;
L[5] = 0b00100101;
L[6] = 0b00000101;
L[7] = 0b11110001;
L[8] = 0b00000001;
L[9] = 0b00100001;

P[0] = 0b00100100;
P[1] = 0b01101111;
P[2] = 0b00110010;
P[3] = 0b00100011;
P[4] = 0b01101001;
P[5] = 0b10100001;
P[6] = 0b10100000;
P[7] = 0b00101111;
P[8] = 0b00100000;
P[9] = 0b00100001;

lw = 0;

//---------------------------

cbi(TCCR0, CS02); //clkdiv64
sbi(TCCR0, CS00);
sbi(TCCR0, CS01);

sbi(TIMSK, TOIE0);

//---------------------------

sbi(ADMUX, REFS0);
sbi(ADCSRA, ADEN);
sbi(ADCSRA, ADIE);
sbi(ADCSRA, ADPS0); //dla 1Mhz
sbi(ADCSRA, ADPS1);
//sbi(ADCSRA, ADFR);



lw = 99;
num = 0;
tryb = 0;
znak = 0;
LDP = 0;
PDP = 0;

czekaj_m(10);
cbi(PORTB, PB2);
cbi(PORTB, PB1);
sei();

}


void test (unsigned int czas)
{
cbi(TIMSK, TOIE0);
PORTD = 0xFF;

cbi(PORTD, PD0);
czekaj_m(czas);
sbi(PORTD, PD0);
cbi(PORTD, PD1);
czekaj_m(czas);
sbi(PORTD, PD1);
cbi(PORTD, PD2);
czekaj_m(czas);
sbi(PORTD, PD2);
cbi(PORTD, PD3);
czekaj_m(czas);
sbi(PORTD, PD3);
cbi(PORTD, PD4);
czekaj_m(czas);
sbi(PORTD, PD4);
cbi(PORTD, PD5);
czekaj_m(czas);
sbi(PORTD, PD5);
cbi(PORTD, PD6);
czekaj_m(czas);
sbi(PORTD, PD6);
cbi(PORTD, PD7);
czekaj_m(czas);
sbi(PORTD, PD7);

sbi(TIMSK, TOIE0);

}

void test2 (unsigned int czas)
{
cbi(TIMSK, TOIE0);
PORTD = 0xFF;


unsigned int i = 0;

sbi(PORTB, PB0);
cbi(PORTC, PC5);

for (i=0; i < 10; i++)
{
PORTD = L[i];
czekaj_m(czas);
}

cbi(PORTB, PB0);
sbi(PORTC, PC5);


for (i=0; i < 10; i++)
{
PORTD = P[i];
czekaj_m(czas);
}
sbi(TIMSK, TOIE0);
}

//--------odświeżanie wyświetlacza--------

void odswiezL (void)
{
unsigned int c = lw / 10;
PORTD = L[c];
if (LDP == 1) cbi(PORTD, PD0);
sbi(PORTB, PB0);
cbi(PORTC, PC5);
}

void odswiezP (void)
{
unsigned int c = lw % 10;
PORTD = P[c];
if (PDP == 1) cbi(PORTD, PD5);
cbi(PORTB, PB0);
sbi(PORTC, PC5);
}

ISR(TIMER0_OVF_vect)
{
odswiezL();
czekaj_m(1);
odswiezP();
czekaj_m(1);
}

//------------------------------------------

ISR(ADC_vect) //wczytanie pomiaru ADC do pamięci
{
nap = ADCW;
//nap = ADCL;
//buf = ADCH;
}

void wyswietl(unsigned int czas, unsigned long int liczba)
{
PDP = 0;
lw = (unsigned int) (liczba / 100);
czekaj_m(czas);
PDP = 1;
lw = (unsigned int) ( (liczba / 10) % 100);
czekaj_m(czas);
PDP = 1; LDP = 1;
lw = (unsigned int) (liczba % 100);
czekaj_m(czas);
PDP = 0; LDP = 0;
}

void prezentuj(void)
{
sbi(ADCSRA, ADSC);
czekaj_m(20);
if (tryb == 1) wyswietl(50, nap); else lw = (nap % 100);
}



void migaj(unsigned int czas)
{
sbi(PORTB, PB2);
czekaj_m(czas);
cbi(PORTB, PB2);
czekaj_m(czas);
sbi(PORTB, PB2);
czekaj_m(czas);
cbi(PORTB, PB2);
czekaj_m(czas);
}

void calc(void)
{
lw = num;
if (znak == 1)
{
if (krok == 0) {a = num; migaj(1); czekaj(1); }
if (krok == 1) {b = num; migaj(1); czekaj(1); }
krok++;
if (krok == 2) {c = (a + b); krok = 0; migaj(5); if (c >= 100) wyswietl(70, c); else {lw = c % 100; num = c % 100;} }
znak = 0;
}
}

void zmien_tryb(void)
{
sbi(PORTB, PB2);

while ( bit_is_clear(PINC, PC1) )
{
tryb++;
if (tryb == 3) tryb = 0;
lw = (tryb*10) + tryb;
czekaj(1);
}
cbi(PORTB, PB2);
}

void debug(void)
{
if ( ( bit_is_clear(PINC, PC3) ) & ( bit_is_clear(PINC, PC1) ) )
{
lw = 88;
czekaj(1);
test(30);
test2(30);
unsigned int i = 0; while (i != 99) {i++; lw = i; czekaj_m(5);}
}
}


//----------------------------------------------

int main (void)
{
init();

while (1)
{
if (tryb == 0) prezentuj();
if (tryb == 1) prezentuj();
if (tryb == 2) calc();

if ( bit_is_clear(PINC, PC3) ) { debug(); znak = 1;}
if ( bit_is_clear(PINC, PC1) ) { zmien_tryb(); }
if ( bit_is_clear(PINC, PC4) ) { if (num < 99) num++; else num = 0; czekaj_m(5); }
if ( bit_is_clear(PINC, PC2) ) { if (num > 0) num--; czekaj_m(5); }
}
while (1) { }
while (1) { asm("nop"); }
while (0) { }

return(0);
}

Wyszukiwarka

Podobne podstrony:
Cyfrowy termometr do PC Lampomittari
NE035 TERMOMETR CYFROWY 20°C÷ 99°C LEDICLq07
Kalkulator 3 cyfrowy
4 Termometr cyfrowy – dioda półprzewodnikowa ochładzanie w azocie, śr 205,883
4 Termometr cyfrowy – dioda półprzewodnikowa kalib 0
czterokanałowy termometr cyfrowy
Termometr cyfrowy z wyświetlaczem LCD
Termometr cyfrowy z PIC16C84
4 kanałowy termometr cyfrowy
Czterokanalowy termometr cyfrowy AVT2389
4 Termometr cyfrowy – dioda półprzewodnikowa kalib 100
kalkulator cyfrowy
4 Termometr cyfrowy – dioda półprzewodnikowa temperatura pokojowa P N
Termometr cyfrowy 50 150°C
4 Termometr cyfrowy – dioda półprzewodnikowa ocieplanie 5k cd śr 17 045
4 Termometr cyfrowy – dioda półprzewodnikowa ocieplanie 5k śr 22,055

więcej podobnych podstron