Chip type : ATmega8
AVR Core Clock frequency: 8.000000 MHz
Data Stack size : 256
*****************************************************/
#include <mega8.h>
#include <stdio.h>
#include <delay.h>
#include <stdlib.h>
#include <string.h>
#include "port.h"
#include "adc.h"
#include "ext_interupt.h"
#include "timer.h"
#include "usart.h"
eeprom float FLOW_CONS;
eeprom float FLOW_OFFSET;
eeprom float data_akumulasi;
volatile unsigned char data_command;
volatile unsigned char cmd_cons[4];
volatile unsigned char cmd_offset[4];
volatile float cmd_cons_float;
volatile float cmd_offset_float;
volatile float load_eeprom_flow_cons;
volatile float load_eeprom_flow_offset;
volatile unsigned char commandType=0;
volatile unsigned char tempCommandType=0;
volatile float data_real;
volatile float data_flow;
volatile float data_akumulasi_sebelumnya;
volatile float data_baca;
volatile int cnt=0;
int count=0;
unsigned int blink_power=0;
unsigned char simpan=0;
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
TCNT0=0x9F;
blink_power++;
if(adc_data[0] != 0)
{
count++;
simpan=1;
if(count==10000)
{
data_flow = data_flow + data_real;
count = 0;
}
}
if(blink_power==5000)
{
blink_power=0;
LED_POWER=~LED_POWER;
}
}
void main(void)
{
port_init();
usart_init();
timer_init();
adc_init();
load_eeprom_flow_cons = FLOW_CONS;
load_eeprom_flow_offset = FLOW_OFFSET;
while (1)
{
adc_read();
data_real=((float)sensor_flow*load_eeprom_flow_cons)/60;
if(ext_interupt==1)
{
data_akumulasi_sebelumnya = data_akumulasi;
data_akumulasi = data_akumulasi_sebelumnya + data_flow;
LED_ISI_EEPROM=1;
ext_interupt=0;
}
if(kbhit()==1)
{
data_command = getchar();
switch(cnt)
{
case 0:
if(data_command==0x23)
cnt++;
break;
case 1:
if(data_command==0x38)
{
cnt++;
}
else if(data_command==0x23)
{
cnt=1;
}
else
{
cnt=0;
}
break;
case 2:
if(data_command==0x47)
{
cnt++;
}
else if(data_command==0x38)
{
cnt=2;
}
else if(data_command==0x23)
{
cnt=1;
}
else if(data_command==0x02)
{
cmd_cons[0]=getchar();
cmd_cons[1]=getchar();
cmd_cons[2]=getchar();
cmd_cons[3]=getchar();
cmd_cons_float= *((float*)(&cmd_cons));
FLOW_CONS=cmd_cons_float;
TRIGER_MAX_485=1;
printf("cmd_cons_float= %.2f\r",cmd_cons_float);
delay_ms(100);
TRIGER_MAX_485=0;
}
else if(data_command==0x03)
{
cmd_offset[0]=getchar();
cmd_offset[1]=getchar();
cmd_offset[2]=getchar();
cmd_offset[3]=getchar();
cmd_offset_float= *((float*)(&cmd_cons));
FLOW_OFFSET=cmd_offset_float;
TRIGER_MAX_485=1;
printf("cmd_offset_float= %.2f\r",cmd_offset_float);
delay_ms(100);
TRIGER_MAX_485=0;
}
else
{
cnt=0;
}
break;
case 3:
if(data_command==0x45)
{
cnt++;
}
else if(data_command==0x47)
{
cnt=3;
}
else if(data_command==0x38)
{
cnt=2;
}
else if(data_command==0x23)
{
cnt=1;
}
else
{
cnt=0;
}
break;
case 4:
if(data_command==0x54 )
{
cnt++;
tempCommandType=1;
}
else if(data_command==0x45)
{
cnt=4;
}
else if(data_command==0x47)
{
cnt=3;
}
else if(data_command==0x38)
{
cnt=2;
}
else if(data_command==0x23)
{
cnt=1;
}
else
{
cnt=0;
}
break;
case 5:
if(data_command==0x0D && tempCommandType==1)
{
commandType=1;
}
else if(data_command==0x0E)
{
commandType=2;
}
else if(data_command==0x0F)
{
commandType=3;
}
else if(data_command==0x54)
{
cnt=5;
}
else if(data_command==0x45)
{
cnt=4;
}
else if(data_command==0x47)
{
cnt=3;
}
else if(data_command==0x38)
{
cnt=2;
}
else if(data_command==0x23)
{
cnt=1;
}
else
{
cnt=0;
}
break;
}
if(commandType==1)
{ //baca Flow
TRIGER_MAX_485=1;
data_baca = data_akumulasi;
printf("Data_baca= %.3f Data_flow= %.3f\r",data_baca,data_flow);
delay_ms(10);
LED_DATA =~ LED_DATA;
commandType=0;
tempCommandType=0;
cnt=0;
LED_ISI_EEPROM=0;
TRIGER_MAX_485=0;
}
// commandType =2 untuk membuat data_akumulasi=0;
else if(commandType==2)
{
TRIGER_MAX_485=1;
LED_DATA =~ LED_DATA;
commandType=0;
tempCommandType=0;
cnt=0;
data_akumulasi=0;
TRIGER_MAX_485=0;
}
// commandType =3 untuk melihat data eeprom konstanta;
else if(commandType==3)
{
TRIGER_MAX_485=1;
load_eeprom_flow_cons = FLOW_CONS;
load_eeprom_flow_offset = FLOW_OFFSET;
printf("flow_cons= %.3f flow_offset=
%.3f\r",load_eeprom_flow_cons,load_eeprom_flow_offset);
delay_ms(100);
LED_DATA =~ LED_DATA;
commandType=0;
tempCommandType=0;
cnt=0;
LED_ISI_EEPROM=0;
TRIGER_MAX_485=0;
}
}
}
}