Académique Documents
Professionnel Documents
Culture Documents
h"
Unsigned int count;
unsigned int databuf[4];
unsigned int pulseCount,unitConsume,balAmount,tariff,balUnit,totalConsume ;
bit RelyOn = 0;
bit alarm = 0 ;
// Interrupt function for the Pulse sensing
void pulseISR() interrupt 0
{
//unsigned int i;
EX0 = 0;
if(RelyOn)
// If rely is ON
{
if(balAmount >= tariff)
pulseCount++; // Count LED Pulses
if(pulseCount >= ONE_UNIT) // Reaches One Unit Of Electricity
{
pulseCount = 0;
unitConsume++; // count usage of each unit
LEDRED = 0;
delayMore(5);
LEDRED = 1;
//totalConsume++;
if(balAmount >= tariff)balAmount = balAmount - tariff;
else
{
balAmount = 0;}
totalConsume = totalConsume + unitConsume;
// Memorise the current Total Unit used
}
}
EX0 = 1;
}
void Timer0Int() interrupt 1
{
TF0 = 0;
TR0 = 0;
ET0 = 0;
count--;
TH0 = 0x4B; // value for 50msec delay
TL0 = 0xFC;
ET0 = 1;
if(count==0)
{
DisplayDateTime();
count = 1200;}TR0 = 1;
}
void main()
// main function
{
unsigned char i
;BUZ = 0;
LCD_init();
// Initialise
LCDRTC_init();
LCD_command(0x01);
LCD_BCKL = 0;
LCD_command(0x80);
LCD_sendString(" Prepaid Energy");
LCD_command(0xC0);
LCD_sendString(" Metering");
delayMore(400);
TimerInit();
IntInit();
//LCD_BCKL = 1;
LCD_command(0x01);
#define ONBOARD
Device_init(databuf); // Read EEPROM and Store values into RAM
#undef ONBOARD //pulseCount = databuf[0];
// pulse countedtotalConsume = databuf[1];
//if(totalConsume >= 9999)totalConsume = 0;
balAmount = databuf[2];
// balence amountif(balAmount >= 2000)balAmount = 0;
tariff = databuf[3];
if((tariff == 0)||(tariff==0xFF))
tariff = 1;
balUnit = balAmount/tariff;
//#undef ONBOARD
DeviceSetting();
//UpdateUnit();
Validate_Recharge();
// Check for the starting conditionProgram_SmrtCard();
// Check smartcard programming before starting actual process
//while(Insert_SmrtCard());
DisplayDateTime();
LCD_command(0xC0);
LCD_sendString("B:");
IntToString(balAmount);
LCD_sendString(" KWH:");
IntToString(totalConsume);
TR0 = 1;
while(1)
{
#define SMARTCARD
delayMore(5);
Read_Card();
// read SmartCard
Validate Recharge();
Erase_Card();
delayMore(50);
LCD_command(0x80);
LCD_sendString("Remove Smart Crd");
while(Insert_SmrtCard());
LCD_command(0x01);
DisplayDateTime();
#undef SMARTCARDdelayMore(5);
/*#define ONBOARD
Device_init(databuf); // Read EEPROM and Store values into RAM
#undef ONBOARD
balAmount = databuf[2]; */
balUnit = balAmount/tariff;
UpdateUnit();
EX0 = 0;
TR0 = 1;
LCD_sendString(" ");
LCD_command(0xC0);
LCD_sendString("B:");
IntToString(balAmount);
LCD_sendString(" KWH:");
IntToString(totalConsume);
}
if(RelyOn)
// If rely is ON
//totalConsume++;
if(balAmount >= tariff)
balAmount = balAmount - tariff;
else{balAmount = 0;
for(i=0;i<20;i++)
{
BUZ = ~BUZ;delayMore(10);
}
BUZ = 0;
}
if(balAmount > 30)
{
GRNON();
alarm = 0;
}
if((balAmount>=10)&&(balAmount<=30))
{
YELON();
if(alarm==0)
{
BUZ = 1;
for(i=0;i<6;i++)
{
BUZ = ~BUZ;
delayMore(10);
}
alarm = 1;
BUZ = 0;
}
}
if(balAmount<10){REDON();
if(alarm == 1){BUZ = 1;
for(i=0;i<6;i++)
{
BUZ = ~BUZ;
delayMore(10);
}
alarm = 0;BUZ = 0;
}
}
LCD_command(0xC0);LCD_sendString("B:");