**Appendices**

#### **Algorithm Used to Program the Microcontroller Unit.**

The code below was programmed into the micro controlling unit, debugged, and simulated through proteus with prototype executed in detail.

#include <mega8.h> #include <delay.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <io.h> //#include <util/delay.h> //#include <lcd.h> //#include "lib/sim300/sim300.h" //#include <sim300.h> char \*number = "9999999999"; float old\_energy = 0; float reference = 300.0; //LCD #define RS PORTD.6 #define E PORTD.7 char t1,z1; //Global Variables initialization unsigned char buf[10]; //Power facotr values and functions initialization void pf\_func(); unsigned int k=0,x=0,g=0; float P=0; float pf=0; **//\_\_\_\_\_\_\_\_Amperemeter & Voltmeter Functions & Variables start\_\_\_\_\_\_\_\_\_** int adc\_read(int ch); int adc; unsigned char buf[10]; float am=0,energy=0; float vm=0; // initialize adc void adc\_init() { // Internal Reference Voltage 2.56 ADMUX = (1< <REFS0) | (1< <REFS1); // ADC Enable and prescaler of 128 // 8000000/128 = 62500 ADCSRA = (1< <ADEN)|(1< <ADPS2)|(1< <ADPS1)|(1< <ADPS0); } // read adc value int adc\_read(int ch) { // select the corresponding channel 07 // ANDing with '7' will always keep the value // of 'ch' between 0 and 7 ch &= 0b00000111; // AND operation with 7 ADMUX = (ADMUX & 0xF8)|ch; // clears the bottom 3 bits before ORing

```
// start single conversion
   // write '1' to ADSC
   ADCSRA |= (1< <ADSC);
   // wait for the conversion to complete
   // ADSC becomes '0' again
   // till then, run loop continuously
 while(ADCSRA & (1< <ADSC));
   return (ADCW);
}
//_____Voltmeter Functions End__________
//_____________UART Functions__________
void uart_transmit (unsigned char data)
{
   while (!( UCSRA & (1< <UDRE)));
   // wait while register is free
   UDR = data;
   // load data in the register
}
void string_transmit(char *str){
   unsigned char i=0;
   while (str[i]!=0)
   {
 uart_transmit (str[i]);
 i++;
   }
}
//________Power Factor start______
int powerfactor()
{
k=0;
// To complete number of counts
g=g+1;
//To convert into seconds
pf=(float)g/1000000;
//To convert into radians
pf=pf*50*360*(3.14/180);
pf = cos(pf);
k=abs(ceil(pf*100));
return k;
}
//.....................End Power Factor Code
//............................Begin LCD Code......................................................
int lcd_data(char t)
{RS=1;
PORTB=t;
E=1;
delay_ms(1);
E=0;
```

```
delay_ms(1);
t1 = t < < 4;
PORTB=t1;
E=1;
delay_ms(1);
E=0;
delay_ms(1);
return 0;}
int writecmd(char z)
{RS=0;
PORTB=z;
E=1;
delay_ms(1);
E=0;
delay_ms(1);
z1 = z < < 4;
PORTB=z1;
E=1;
delay_ms(1);
E=0;
delay_ms(1);
return 0;}
void lcd_print(char *str)
{unsigned char i=0;
while (str[i]!=0)
{lcd_data(str[i]);
i++;}}
void lcd_init(void)
{writecmd(0x02);
writecmd(0x28);
writecmd(0x0c);
writecmd(0x01);
writecmd(0x06);}
void lcd_gotoxy(unsigned char x, unsigned char y)
{
   unsigned char firstcharadrs[] = {0x80, 0xC0,0x94,0xD4};
writecmd(firstcharadrs[y-1] + x - 1);
delay_us(100);
}
//___________End of LCD code____________
/*
interrupt [USART_RXC] void intrp()
{
data1=string_receive1();
while(1){
if(strncmp(data1,"off",3)==0){
PORTD.2=1;}
```

```
lcd_print("House Disconnected");
data1=string_receive1();
if(strncmp(data1,"on",2)==0){
PORTD.2=0;
break;}
 }
}
  */
void Tx_data(char *str)
{
string_transmit("AT+CMGS=");
uart_transmit('"');
string_transmit(number);
 uart_transmit('"');
uart_transmit('\r');
 while(*str)
{
uart_transmit(*str);
str++;
delay_ms(0);
}
 uart_transmit('\r');
uart_transmit(0x1a);
}
void main(void)
{
//Local Variables
int adc_int[41];
int max=0;
int i=0;
int a = 0;
float max_power = 4000;
// Input/Output Ports initialization
DDRB = 0xff;
DDRC = 0x00;
DDRD = 0b11001100;
//____________UART Initializaion_____________
UBRRH=0x00;
UBRRL=12;
UCSRA=(0< <RXC) | (0< <TXC) | (0< <UDRE) | (0< <FE) | (0< <DOR) |
(0< <UPE) | (1< <U2X) | (0< <MPCM);
UCSRB=(1< <RXCIE) | (0< <TXCIE) | (0< <UDRIE) | (1< <RXEN) |
(1< <TXEN) | (0< <UCSZ2) | (0< <RXB8) | (0< <TXB8);
UCSRC=(1< <URSEL) | (0< <UMSEL) | (0< <UPM1) | (0< <UPM0) |
(0< <USBS) | (1< <UCSZ1) | (1< <UCSZ0) | (0< <UCPOL);
#asm("sei")
//__LCD Initialization____
lcd_init();
```

```
lcd_gotoxy(1,1);
```

```
lcd_print("SE METER");
string_transmit("SE METER\r\n");
while(1)
{
 if (a == 0){ PORTD.3 = 1; a = 1;} // Pin n goes high
else{ PORTD.3 = 0; a = 0;} // Pin n goes low; // (PORTD.3 == 1
UCSRB=(1< <RXCIE) | (0< <TXCIE) | (0< <UDRIE) | (1< <RXEN) |
(1< <TXEN) | (0< <UCSZ2) | (0< <RXB8) | (0< <TXB8);
delay_ms(1500);
//_____POWER FACTOR____
pf_func();
x = powerfactor();
P=x;
delay_us(20);
lcd_init();
itoa (x,buf);
lcd_print(buf);
lcd_data('%');
lcd_print("PF");
lcd_data(',');
lcd_data(' ');
//______Ammeter_____
// Initialize ADC
adc_init();
for(i=0; i<=40; i++)
     {
 adc_int[i] = adc_read(1); // read adc value at PORTC.1
     }
     max=adc_int[0];
for(i=0; i<=40; i++)
     {
     if(max<adc_int[i])
     max=adc_int[i];
     }
adc=max - 240;
itoa(max,buf);
//am = (float)(adc*0.006849);// 7/1024
am = (float)(adc*0.0416709 *0.7071);// 32.67/(1024 - 240)
ftoa(am,3, buf);
lcd_print(buf);
lcd_data('A');
lcd_data(',');
//______________Voltmeter___________
 adc_init();
for( i=0; i<=40; i++)
     {
```
adc\_int[i] = adc\_read(0); // read adc value at PORTC.0

```
}
     max=adc_int[0];
for( i=0; i<=40; i++)
     {
     if(max<adc_int[i])
     max=adc_int[i];
     }
adc=max;
itoa(max,buf);
vm = adc*0.30585 * 0.707; //313/1024
ftoa(vm,2, buf);
 lcd_gotoxy(1,2);
lcd_print(buf);
lcd_data('V');
delay_ms(700);
lcd_init();
lcd_print("***POWER***");
lcd_gotoxy(1,2);
P=P/100;
am=am*vm*P;
if (am/P > max_power){
PORTD.2=1;
}
if (am/P < max_power){
PORTD.2=0;
}
ftoa(am,2, buf);
    //string_transmit(buf);
    // string_transmit("\n\r");
lcd_gotoxy(1,2);
lcd_print(buf);
lcd_data('W');
delay_ms(700);
    am=3.4*am;
    am=am/3600;
    energy=am+energy;
ftoa(energy,2, buf);
    //string_transmit(buf);
    //string_transmit("\n\r");
lcd_init();
lcd_print("***ENERGY***");
lcd_gotoxy(1,2);
lcd_print(buf);
lcd_print("Wh");
    if ((int)energy > (old_energy + reference)){
old_energy = (int)energy;
ftoa(old_energy,2, buf);
Tx_data(buf);
Tx_data("KWH\n\r"); }
}
```

```
}
//_________POWER FACTOR FUNCTIONS_____________
void pf_func()
{
while(1)
{
if ( PINC.2==1 )
{
TCNT1=0;
TCCR1B = 0x01; // Start timer at Fcpu/1
break;
}
else
 {
 continue;
}
}
while(1)
{
if ( PINC.3 == 1 )
{
TCCR1B = 0x00;
g=TCNT1;
break;
}
else
 {
 continue;
}
}
}
```
*Smart Metering Technologies*
