Автоматизация управления флегмовым числом домашней ректификационной колонны

#define F_CPU 16000000UL

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/eeprom.h>

unsigned char number[] = {

0x3f, //0
0x06, //1
0x5b, //2
0x4f, //3
0x66, //4
0x6d, //5
0x7d, //6
0x07, //7
0x7f, //8
0x6f, //9

0x00, // 10
0x40, //- 11
0x08, //6bup_ 12

0x23, //up 13
0x1c, //down 14
0x50, //r 15
0x58, //c 16
0x5e, //d 17
0x74, //h 18
0x63, //oup 19
0x5c, //o 20
0x6b, //Ob 21
0x54, //n 22
0x79, //E 23
0x36 //|| 24

};

char param_dot[] ={ 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0 };
int param_min[] = { 0, 0, 1, 0, 1, 1, 1, 1, 1, 80, 0, 0};
int param_max[] = { 999, 999, 3, 99, 99, 99, 99, 99, 99, 99, 99,99 };

unsigned char data1 = 0, data2 = 0, data3 = 0, fv = 0, fp = 0, fp2 = 0, fl = 0,
status = 0, mtype = 0, is_menu = 0, vcount = 0, fvz = 0, kk = 0, sf = 0, resbutt = 0, count = 0, jj = 0, pi, upState,
downState, OldState, NewState, isok = 0;
int x = 0, curr_temp = 0, temp_temp = 0, stab_temp = 0, tic_h = 0, xtic_voice =
0, xtic_sil = 0, tic_reg = 0, heads_kol = 0, tic_voice = 0, tic_sil = 0,
theads_kol = 0, total_kol = 0, temp_total_kol = 0, tic = 0,
tic_m = 0, kol_stop = 0, quadro = 0, t3pr = 0;
long tic_w = 0,vkoeff = 0, param[12], tparam[12],reg_k=1000;

unsigned int fparam[12] EEMEM;

#define W1_PORT PORTC
#define W1_DDR DDRC
#define W1_PIN PINC
#define W1_BIT 3

ISR( TIMER1_OVF_vect ) {
cli();

TCNT1H = 0xF9;
TCNT1L = 0xE4; //16mhz

if ((PINB & 0b00000001) == 1) {
resbutt++;
if (resbutt > 30) {
PORTC &= ~(1 << PC5);
vcount = 1;
vkoeff = 0;
tic_w=0;
fvz = 0;
reg_k=1000;
pi = 2;
}
} else {
resbutt = 0;
}

if (quadro>0&&param[2] == 2&&pi > 14 && pi < 17&&reg_k > param[10]*10&& stab_temp + param[4] < curr_temp)
{
if (quadro>0)
{
quadro--;
}
if (quadro==0)
{
PORTC &= ~(1 << PC5);
vcount = 1;
vkoeff = 0;
pi = 18;
}
}

if (isok > 0) {
isok--;
} else {
isok = 0;
}

if (fv == 1 && x > 0 && param[11] < 99) {
if (tic_voice > 0) {
PORTC |= (1 << PC4);
tic_voice--;
} else {
PORTC &= ~(1 << PC4);
fv = 0;
tic_sil = 10;
}
}
if (fv == 0 && x > 0) {
if (tic_sil > 0) {
tic_sil--;
} else {
x--;
tic_voice = xtic_voice;
tic_sil = xtic_sil;
fv = 1;
}
}

if (x == 0) {
kk = 0;
fv = 0;
}

if (pi > 10) {
tic++;
}
if (pi == 13) {
tic_w++;
}
if (pi == 17) {
tic_reg++;
}

if (pi == 16&&param[2]<3) {
tic_h++;
if (tic_h > param[5] * 60) {
if (param[2] == 1) {
tic_h = 0;
pi = 18;
}
if (param[2] == 2) {
reg_k = reg_k — param[8]*10;
pi = 17;
x = 1;
xtic_voice = 30;
xtic_sil = 5;
tic_voice = xtic_voice;
tic_sil = xtic_sil;
fv = 1;

if (reg_k <= param[10]*10) {
tic_h = 0;
pi = 18;
}
}

}

}
if (pi == 0 || pi == 1) {
tic_m++;
if (tic_m > 40) {
tic_m = 0;
pi = pi + 14;
}
}

if ((vcount == 0 && pi == 14) | (vcount == 0 && pi == 0)) {
vkoeff++;
if (vkoeff < param[6]) {
PORTC |= (1 << 5);
} else {
vcount = 1;
vkoeff = 0;
}

}
if ((vcount == 0 && pi > 14 && pi < 17) | (vcount == 0 && pi == 1)) {
vkoeff++;
if (vkoeff < param[7]) {
PORTC |= (1 << 5);
} else {
vcount = 1;
vkoeff = 0;
}

}

if ((vcount == 1 && pi == 14) | (vcount == 1 && pi == 0)) {
vkoeff++;
if (vkoeff < param[0] * 10) {
PORTC &= ~(1 << 5);
} else {
vcount = 0;
vkoeff = 0;
heads_kol++;
}

}
if ((vcount == 1 && pi == 15) | (vcount == 1 && pi == 1)) {
vkoeff++;

if (fvz == 0) {
if (param[2]==3)
{
reg_k=1000-t3pr;
}
if (vkoeff < param[1] * 1000 / reg_k) {
PORTC &= ~(1 << 5);
} else {
vcount = 0;
vkoeff = 0;
}

} else {
if (vkoeff < param[5] * 30) {
PORTC &= ~(1 << 5);
} else {
vcount = 0;
vkoeff = 0;
fvz = 0;
}
}
}

if (pi == 19) {
PORTC |= (1 << 5);
}
if ((pi > 1 && pi < 14) || (pi > 16 && pi < 19)) {
PORTC &= ~(1 << 5);
}

sei();

}

ISR( TIMER0_OVF_vect ) {
cli();

TCNT0 = 0xbc; //16mhz

PORTC &= ~((1 << 2) | (1 << 1) | (1 << 0));

if (count == 0) {
PORTD = number[data3];
PORTC |= (1 << 0);
}
if (count == 1) {
PORTD = number[data2];
PORTC |= (1 << 1);
if (param_dot[pi] == 1) {
PORTD |= (1 << 7);
}
if (param_dot[pi] == 0) {
PORTD &= ~(1 << 7);
}
}
if (count == 2) {
PORTD = number[data1];
PORTC |= (1 << 2);
if (pi == 14) {
PORTD |= (1 << 7);
} else {
PORTD &= ~(1 << 7);
}
}
count++;
if (count == 3)
count = 0;

}

ISR( TIMER2_OVF_vect ) {
cli();

if ((PINB & 0b00000001) == 1 && isok == 0) //
{

if (pi >= 2 && pi <= 11) {
pi++;
}

if (pi == 12) {

for (unsigned char pj = 2; pj < 12; pj++) {
if (param[pj] > tparam[pj] || param[pj] < tparam[pj]) {
eeprom_write_word(&fparam[pj], param[pj]);
}
}
}

jj = 0;
if (pi == 15 || pi == 16) {
PORTC &= ~(1 << PC5);
vcount = 1;
vkoeff = 0;
pi = 14;
jj = 1;
}

if (pi == 14 && jj == 0) {
stab_temp = curr_temp;
pi = 15;
sf = 0;
PORTC &= ~(1 << PC5);
vcount = 1;
vkoeff = 0;
}

if (pi == 19) {
kol_stop = 0;
pi = 2;
}

if (pi == 18) {
pi = 19;
}

isok = 5;

}

NewState = PINB & 0b00000110;
if (NewState != OldState) {
switch (OldState) {
case 4: {
if (NewState == 6)
upState++;
if (NewState == 0)
downState++;
break;
}

case 0: {
if (NewState == 4)
upState++;
if (NewState == 2)
downState++;
break;
}
case 2: {
if (NewState == 0)
upState++;
if (NewState == 6)
downState++;
break;
}
case 6: {
if (NewState == 2)
upState++;
if (NewState == 4)
downState++;
break;
}
}
OldState = NewState;
}

if (upState >= 4) {
tic_m = 0;

if (pi <= 11) {
param[pi]++;
if (param[pi] > param_max[pi]) {
param[pi] = param_min[pi];
}
}

if (pi > 13 && pi < 16) {
tic_m = 0;
param[pi — 14]++;
if (param[pi — 14] > param_max[pi — 14]) {
param[pi — 14] = param_min[pi — 14];
}

pi = pi — 14;
}

upState = 0;
}

if (downState >= 4) {
tic_m = 0;

if (pi <= 11) {
if (param[pi] == param_min[pi]) {
param[pi] = param_max[pi];
} else {
param[pi]--;
}

}

if (pi > 13 && pi < 16) {
tic_m = 0;
if (param[pi — 14] == param_min[pi — 14]) {
param[pi — 14] = param_max[pi — 14];
} else {
param[pi — 14]--;
}

pi = pi — 14;
}

downState = 0;
}

sei();

}

void show_seg(int data, int literal) {
if (literal == 0) {
data3 = data / 100;
data2 = (data — data3 * 100) / 10;
data1 = (data — data3 * 100) % 10;
} else {
if (literal == 12) {
data3 = literal;
data2 = literal;
data1 = literal;
} else {
data3 = literal;
data2 = data / 10;
data1 = data % 10;
}
}

}

unsigned char w1_find() {
unsigned char device;
W1_DDR |= 1 << W1_BIT;
_delay_us(485);
W1_DDR &= ~(1 << W1_BIT);
_delay_us(65);
if ((W1_PIN & (1 << W1_BIT)) == 0x00)
device = 1;
else
device = 0;
_delay_us(420);
return device;
}

void w1_sendcmd(unsigned char cmd) {
for (unsigned char i = 0; i < 8; i++) {
if ((cmd & (1 << i)) == 1 << i) {
W1_DDR |= 1 << W1_BIT;
_delay_us(2);
W1_DDR &= ~(1 << W1_BIT);
_delay_us(65);
} else {
W1_DDR |= 1 << W1_BIT;
_delay_us(65);
W1_DDR &= ~(1 << W1_BIT);
_delay_us(5);
}
}
}

unsigned char w1_receive_byte() {
unsigned char data = 0;
for (unsigned char i = 0; i < 8; i++) {
W1_DDR |= 1 << W1_BIT;
_delay_us(2);
W1_DDR &= ~(1 << W1_BIT);
_delay_us(7);
if ((W1_PIN & (1 << W1_BIT)) == 0x00)
data &= ~(1 << i);
else
data |= 1 << i;
_delay_us(50);
}
return data;
}

int temp_18b20() {
unsigned char data[2];
int temp = 0;
if (w1_find() == 1) {
w1_sendcmd(0xcc);
w1_sendcmd(0x44);
_delay_ms(750);
w1_find();
w1_sendcmd(0xcc);
w1_sendcmd(0xbe);
data[0] = w1_receive_byte();
data[1] = w1_receive_byte();
temp = data[1];
temp = temp << 8;
temp |= data[0];
temp = temp * 0.625;
if (temp > 0 && temp < 999) {
temp_temp = temp;
} else {
temp = temp_temp;
}
}
return temp;
}

int main() {

DDRC |= (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0);
DDRB |= (1 << 5) | (1 << 4);

DDRD = 0xff;
PORTD = 0xff;

TCCR0 = (1 << CS02) | (0 << CS01) | (1 << CS00);
TCCR1B = (1 << CS02) | (0 << CS01) | (1 << CS00);
TCCR2 = (1 << CS02) | (0 << CS01) | (1 << CS00);

TIMSK |= (1 << TOIE0);
TIMSK |= (1 << TOIE2);
TIMSK |= (1 << TOIE1);

TCNT1H = 0xF9;
TCNT1L = 0xE4; //16mhz

sei();

fv = 1;

pi = 2;
mtype = 1;

for (unsigned char pj = 0; pj < 12; pj++) {
param[pj] = eeprom_read_word(&fparam[pj]);
tparam[pj] = param[pj];
}

PORTC &= ~(1 << PC5);

while (1) {

if (pi == 0||pi == 1) {
show_seg(param[pi], 0);
}
if (pi < 12&&pi>1) {
show_seg(param[pi], pi + 13);
}
if (pi == 12 || pi == 14) {
show_seg(curr_temp, 0);
}
if (pi == 13) {
show_seg(param[3] * 6 — tic_w / 600, 0);
}
if (pi == 15&&param[2]<3) {
if (curr_temp >= stab_temp) {
if (curr_temp — stab_temp > 99) {
show_seg(99, 10);
} else {
show_seg(curr_temp — stab_temp, 10);
}

}
if (curr_temp < stab_temp) {
if (stab_temp — curr_temp > 99) {
show_seg(99, 11);
} else {
show_seg(stab_temp — curr_temp, 11);
}

}
}
if (pi == 15&&param[2]==3) {
show_seg(curr_temp, 0);
}
if (pi == 16) {
show_seg(param[5] * 6 — tic_h / 10, 0);
}
if (pi == 17) {
show_seg(reg_k/10, 0);
}
if (pi == 18) {
show_seg(kol_stop, 0);
}
if (pi == 19) {
show_seg(888, 0);
}

if (tic > 10 && pi > 11) {
curr_temp = temp_18b20();
tic = 0;
}

if ((param[2]<3&&curr_temp > 500 && pi == 12)||(param[2]==3&&curr_temp > 800 && pi == 12)) {
x = 5;
xtic_voice = 10;
xtic_sil = 10;
tic_voice = xtic_voice;
tic_sil = xtic_sil;
fv = 1;
pi = 13;
heads_kol = 0;
theads_kol = 0;
kk = 0;
PORTB |= (1 << PB4);
}
if (param[3] * 6 < tic_w / 600 && pi == 13) {
x = 2;
xtic_voice = 5;
xtic_sil = 10;
tic_voice = xtic_voice;
tic_sil = xtic_sil;
fv = 1;
pi = 14;
}

if (pi == 14) {
if (heads_kol % (param[11] * 10) == 0
&& heads_kol > (param[11] * 10 — 1) && kk == 0
&& heads_kol > theads_kol&& param[0]>0) {
x = heads_kol / (param[11] * 10);
xtic_voice = 5;
xtic_sil = 10;
tic_voice = xtic_voice;
tic_sil = xtic_sil;
fv = 1;
kk = 1;
theads_kol = heads_kol;
}

}

if (pi == 15 && stab_temp + param[4] < curr_temp&&param[2]<3) {
PORTC &= ~(1 << PC5);
tic_h = 0;
vcount = 1;
vkoeff = 0;
fvz = 1;
if (sf == 0 && (param[2] == 2 || param[2] == 1)) {
pi = 16;
kol_stop++;
quadro = param[5]*4*60;
}
sf = 1;
}
if (pi > 14 && pi < 17 && stab_temp + param[4] >= curr_temp&&param[2]<3) {
sf = 0;
pi = 15;
}

if (pi == 17 && tic_reg > 30) {
tic_reg = 0;
pi = 15;
}

if (param[2]==2&&pi > 14 && pi < 18 && stab_temp + param[4] >= curr_temp) {
quadro=0;
}

if (param[2]==3&&pi==15)
{
if (curr_temp<param[9]*10)
{
t3pr=0;
}
if (curr_temp>=param[9]*10&&curr_temp<=param[10]*10)
{
t3pr=param[8]*(curr_temp-param[9]*10);
}
if (t3pr>1000||curr_temp>param[10]*10||param[9]>param[10])
{
pi=18;
PORTC &= ~(1 << PC5);
vcount = 1;
vkoeff = 0;
}
}

if (pi == 18 && kk == 0) {
x = 1;
xtic_voice = 20;
xtic_sil = 5;
tic_voice = xtic_voice;
tic_sil = xtic_sil;
fv = 1;
kk = 1;
reg_k = 1000;
tic_w = 0;
fvz = 0;
PORTB |= (1 << PB5);

for (unsigned char pj = 0; pj < 2; pj++) {
if (param[pj] > tparam[pj] || param[pj] < tparam[pj]) {
eeprom_write_word(&fparam[pj], param[pj]);
}
}
}

if (pi == 19) {
PORTB &= ~(1 << PB5);
}

}
return 0;

}

© Geektimes