Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

SCHOOL OF ELECTRICAL, ELECTRONIC AND COMPUTER ENGINEERING

ENERGY METER
GROUP 8

Document Type : Phase 3 Report, Electrical Design 4 Project

Authors Name : Euveshen Govender 206509534

Shaprin Pillay 205514551

Jared Kisten 206500555

Project Supervisor : Mr. Narushan Pillay

Due Date : October 25, 2010


Electrical Design 4
Energy Meter

Abstract.
This report describes the approach and processes used to develop a final design of a system capable of
measuring real power from the mains. A description of communication between the MCP3909 and
microcontroller is implemented in the report using the method of serial peripheral interface (SPI).The
MCP3909 will be transmitting values of voltage and current to the microcontroller in the form of a 32
bit number and using these values of current and voltage we are able to calculate the power and this is
displayed both on the computer and LCD. The report contains hardware schematics, simulation results
and software.

2
Electrical Design 4
Energy Meter

Contents
Abstract.................................................................................................................................................ii
1. Introduction...................................................................................................................................1
2. Project Description........................................................................................................................2
2.1. Overview...............................................................................................................................2
2.2. Features..................................................................................................................................2
3. Hardware Design...........................................................................................................................3
3.1. PCB – POWER SUPPLY......................................................................................................3
3.2. PCB Design...........................................................................................................................4
4. Software Design............................................................................................................................6
4.1. Interface 3: LCD display configuration and circuitry.............................................................6
Configuring the LCD Display:.......................................................................................................6
Data Transmission.........................................................................................................................7
4.2. Interface 2..............................................................................................................................7
Serial Peripheral Interface (SPI) - Data Transmission...................................................................7
4.3. Interface 4: Communication between Microcontroller and Computer...................................9
RS-232 Communication:...............................................................................................................9
Level conversion:..........................................................................................................................9
Data transmission via RS-232 port:.............................................................................................10
4.4. Choosing a Microcontroller.................................................................................................11
5. Microcontroller pseudo Code:.....................................................................................................12
Program Overview.......................................................................................................................15
Visual basic Software design.........................................................................................................16
6. Testing & Results........................................................................................................................22
7.1. Full schematic & setup.................................................................................................................22
7. Management................................................................................................................................25
Work distribution between group members:....................................................................................25
Current Status..................................................................................................................................25
Timeline..........................................................................................................................................25
8. Conclusion...................................................................................................................................26
References...........................................................................................................................................27
Appendix............................................................................................................................................A1

3
Electrical Design 4
Energy Meter

1. Introduction

A final design of an energy meter is presented in this report. It was required to create a system
interface to a computer on one side, and mains on the other. The circuit is capable of measuring
power; this is done by multiplying the measured values of voltage and current. The power is to be
displayed on an LCD panel, we used the LMB162ABC which is manufactured by - topward - 2 line
16 character. We had the option of transmitting data to the computer via RS-232 or USB, we chose
the RS-232 as we were more familiar with it and it is inexpensive, and it only requires four capacitors
for its connection. The circuit is simple and consists of the MCP3909 measurement unit; we also use a
transformer to step down the high voltages from the mains to a suitable voltage that the MCP3909
could handle. We not only needed to display values of power on the computer but had to create an
application on the computer that reports and logs power reading.

4
Electrical Design 4
Energy Meter

2. Project Description
2.1. Overview
The purpose of this project is to measure power from the mains 230Vrms, 50Hz supply. The
dangerously high mains supply is stepped down appropriately using a transformer in order to make
use of the measuring IC, the MCP3909. Power is obtained through the MCP3909 taking in analogue
input signals for both voltage and current measurements. The voltage and power values are now sent
to the microcontroller via SPI. It is here in the microcontroller were the power value is determined
(voltage*current*gain). From the microcontroller we display the power value on an LCD via GPIO.
The power value is sent to the PC through serial communications. This makes use of the RS232 cable
to send data serially to the computer. A VB interface is used to display the value on the PC.

2.2. Features
 Power supplied via the mains(230 stepped down to 5V)
 LCD screen user interface – LCD screen used to display the power
 Hardware circuitry
 IC- MCP3909 (energy meter)
 PC interface

Interface 4
Interface 1 Interface 2

Mains MCP3909 Micro

Interface 3

Power
LCD
Supply
(5V)

Figure 1: Overview Block Diagram

5
Electrical Design 4
Energy Meter

3. Hardware Design
3.1. PCB – POWER SUPPLY

Figure 2: Track layout with relevant pin connections

6
Electrical Design 4
Energy Meter

Figure 3: Top 3-D view of power supply

3.2. PCB Design

Figure4 : Component layout on ultiboard

7
Electrical Design 4
Energy Meter

Figure 5: Track layout with relevant pin connections

Figure6 : Top 3-D view of PCB

8
Electrical Design 4
Energy Meter

Figure 7: Bottom 3-D view showing surface mounting of the MCP3909

Two additional connections were necessary to make the PCB work. Pin 9(Master
Clear pin) of the MCP3909 is connected to Port B pin 3 of the Atmega 16. This is
used in order to enable the SPI for communication. Also, Pin 14 (Chip Select) of the
MCP3909 is connected to ground as this pin needs to be a low when serial
communication takes place.

4. Software Design
4.1. Interface 3: LCD display configuration and circuitry

Configuring the LCD Display:


The LCD display has two primary modes of operation, 8-bit mode and 4-bit mode. 8-bit mode is used
when speed is important and this mode requires ten I/O pins from the microcontroller. 4-bit mode
requires only 6 I/O pins and is used when trying to minimize the amount I/O pins required in a design.
To minimize I/O pin requirements 4-bit mode will be used for this design. Since 4-bit mode only uses
four data pins, data is transferred four bits at a time. Only the last four data bits of the LCD display are
used.

Table 2: Electrical characteristics of LCD Display [5]

Parameter Symbol Rating


Operating voltage VDD 4.7 – 5.3V
Input voltage VI 0.8VDD – VDD
Operating current IDD 1.3 – 3.0mA

9
Electrical Design 4
Energy Meter

Figure 8: LCD Display hardware configuration

Figure 15 shows the necessary connections for the LCD module. Pins 1 and 2 are the power supply
pins. The R/W pin (Pin 5) requires a LOW signal for writing to the LCD display and a HIGH signal to
read from it. Pin 4 is the register select pin used to select whether display data or instruction data is
being sent and pin 6 is the enable pin. Pins 7 to 14 are the data pins with pin 14 being the most
significant bit.

Data Transmission
All data transmitted to the LCD needs to be characters for transmission to work. Using the predefined
function on C,” lcd_putf()”, this displays the value on the LCD display. Since we are using 4 bit
mode, the first 4 bits from the MSB side (i.e. bit 7 to4) will first be transmitted then the next 4.

4.2. Interface 2

Serial Peripheral Interface (SPI) - Data Transmission

Shift Direction
MSB LSB

MOSI 1 0 0 1 1 0 1 1 MISO

Clock Generator MASTER


SCK SS

SCK CS

10
Electrical Design 4
Energy Meter

SO SI
0 1 0 0 1 1 0 0
MSB LSB
Shift Direction

SLAVE

Figure9: Master and Slave interconnection

The SPI master and slave interconnection diagram shown above explains how the shift register is used
to transfer and receive the data.The master transfers0b10011011 (0×9B) to the slave and at the same
time the slave device also want to transfer the 0b01001100 (0×4C) data to the master.By activating
the CS (chip select) pin on the slave device, now the slave is ready to receive the data.On the first
clock cycle both master and slave shift register will shift their registers content one bit to the left, the
SPI slave will receive the first bit from the master on its LSB register while at the same time the SPI
master will receive its first data from slave on its LSB register.

Master Slave
MSB LSB MSB LSB

1 0 0 1 1 0 1 1 0 1 0 0 1 1 0 0

MSB LSB MSB LSB

1st Clock 0 0 1 1 0 1 1 0 1 0 0 1 1 0 0 1

MSB LSB MSB LSB

2nd Clock 0 1 1 0 1 1 0 1 0 0 1 1 0 0 1 0

MSB LSB MSB LSB

3rd Clock 1 1 0 1 1 0 1 0 0 1 1 0 0 1 0 0

MSB LSB MSB LSB

4th Clock 1 0 1 1 0 1 0 0 1 1 0 0 1 0 0 1

MSB LSB MSB LSB

5th Clock 0 1 1 0 1 0 0 1 1 0 0 1 0 0 1 1

11
Electrical Design 4
Energy Meter

MSB LSB MSB LSB

6th Clock 1 1 0 1 0 0 1 1 0 0 1 0 0 1 1 0

MSB LSB MSB LSB

7th Clock 1 0 1 0 0 1 1 0 0 1 0 0 1 1 0 1

MSB LSB MSB LSB

8th Clock 0 1 0 0 1 1 0 0 1 0 0 1 1 0 1 1

Figure 10: Master and Slave Data Transfer Diagram

Using the same principal for each bit, the complete data transfer between master and slave will be
done in 8 clock cycles. Once the complete transfer is done, the contents of the shift register are sent to
the buffer register. Data can now be read from the buffer register. It is very important to read the
incoming data as it will be lost in the next transfer of data. For the purpose of this design, only data
transfer from the master to slave (MCP3909 to MCU) will take place as we only need the current and
voltage values from the MCP3909.

4.3. Interface 4: Communication between Microcontroller and Computer

RS-232 Communication:
The RS-232 is an asynchronous serial communications port. Data is transmitted by the RS-232 port,
one bit at a time (serially transmitted). The RS-232 contains two data lines, the TX and RX lines. The
TX is a wire which allows the transmission of data to the next device, and the RX is the wire which
receives the data from the other device. For the purpose of the design, we will only be using the TX
pin, as data will only be sent to the computer.

Figure 11: Serial data transmission between two devices

The logic levels of the RS-232 port is defined as follows, HIGH = -12V and LOW = 12V, while on a
microcontroller the logic levels are defined as HIGH = 5V and LOW = 0V. Therefore a level
converter is required to interface the computer to the microcontroller to obtain the correct logic level.

12
Electrical Design 4
Energy Meter

Level conversion:
To obtain the desired voltage levels (+5V high, 0V low), the MAX232 chip is used. This device
connects to the USART terminals of the microcontroller and directly to the RS232 port. The MAX232
requires 4 capacitors to ensure that it operates correctly.

Fig 12: Pin connection of MAX 232 interfaced with both the Microcontroller and DSUB9F, connector for the RS-232
cable.

Data transmission via RS-232 port:


Data is transferred one bit at a time (serial communication). Before data transmissions can occur,
some properties needs to be specified. Such properties are; baud rate, number of data bits, number of
start bits, number of stop bits, and type of parity is to be used (even, odd, or no parity).

In idle mode the RX pin of the RS-232 port remains low, to start transmitting data a low (12 V for
RS-232) start bit is sent on the pin the PC detects this change in state, to test if this is a start of data
transmission the PC checks the state of the pin at half the time it takes to transfer one bit, if the state is
still low, then the PC consider it as a start bit and does not neglect it as noise. Thereafter the first bit is
transmitted, after all bits have a parity bit is sent (if parity is in use), followed by one or two stop bits
(stop bits denoted by a high state, -12 V for RS-232).

13
Electrical Design 4
Energy Meter

Figure 13: Example of Data Transmission (8 bits, no parity, and 1 stop bit)

4.4. Choosing a Microcontroller


The microcontroller used must be able to firstly accommodate all of the required inputs and outputs.
The following figure shows the required number of inputs and outputs.
Enable

Register Select
LCD Display Read/ write
SCK Inputs (SPI)
D4
SD0 D5
D6
D7
Microcontroller

Computer Display (USART)


TX
9((USART

14
Electrical Design 4
Energy Meter

Figure 14: Microcontroller I/O Pins Required

From this diagram it can be seen that 10 I/O pins are required. Other microcontroller features required
by this design are shown in the table below.

Table 3: Microcontroller Requirements

Feature Requirement
Timer Yes
ADC No
SPI Yes
USART Yes
Memory No

It was therefore decided that a 32 pin microcontroller would be sufficient for this project. The
ATmega 16 or the ATmega32can be used for this project. We will be using the ATmega 16. It was
chosen because it can accommodate all the inputs and outputs required and it is fairly cheap compared
to other 32 pin microcontrollers.

5. Microcontroller pseudo Code:


The current and voltage from the mains are measured using the appropriate analogue circuitry and the
MCP3909 IC. It is this chip that sends the obtained voltage and current values to the microcontroller
via a SPI (Serial Peripheral Interface) connection. The microcontroller handles the incoming data as
described by the following algorithms:

Main Loop:

 The microcontroller waits for the data from the MCP3909 IC.
 If the data received counter equals one then the microcontroller exits the main loop and
breaks to the Calculation subroutine.
 Else if the data counter is not equal to one then the microcontroller code continues to loop in
the main program

SPI Data Received Interrupt:

 Move receive data to temp register


 Set data receive count to one

15
Electrical Design 4
Energy Meter

 Exit interrupt

Calculation Subroutine:

 Clear the data received counter


 Set voltage register equals the higher 16-bits of the temp register
 Set current register equals the lower 16-bits of the temp register
 Multiply the current value with the stored value in the voltage register, and store the result in
the power register
 Multiply the contents of the power register by the gain factor (factor by which the voltage and
current was scaled), the accurately determine the power. Store this result into the power
register.
 Call the ASCII Conversion subroutine.
 Call the LCD Display Subroutine
 Return back to main program

ASCII Conversion Subroutine:

The following algorithm is designed to handle values up to 9999, but can be adapted to handle higher
values

 Take voltage value


 Loop while value is greater than a thousand, subtract one thousand and increment thousand
register. Break to next step if value less than thousand.
 Loop while value is greater than a hundred, subtract one hundred and increment hundred
register. Break to next step if value less than hundred.
 Loop while value is greater than ten, subtract ten and increment ten register. Break to next
step if value less than ten.
 Loop while value is greater than zero, subtract one and increment unit register. Break to next
step if value is equal to zero.
 Add offset value to the thousand, hundred, ten and unit register
 Call transmit subroutine
 Clear thousand, hundred, ten and unit register
 Repeat above steps for current value
 After both values are sent, exit subroutine

Transmit Subroutine:

 Send the thousand register value via the RS232 port


 Wait for transmit complete interrupt flag to be set
 Send the hundred register value via the RS232 port
 Wait for transmit complete interrupt flag to be set
 Send the ten register value via the RS232 port
 Wait for transmit complete interrupt flag to be set
 Send the unit register value via the RS232 port
 Wait for transmit complete interrupt flag to be set

16
Electrical Design 4
Energy Meter

 Return from subroutine

LCD Display subroutine:

 Clear LCD
 Using standard library available in all C compilers, it is possible to convert the values we are
receiving into a character string which can be displayed on an LCD
 Display string on LCD, using predefined function “lcd_putsf()”

Visual Basic pseudo Code:

Visual Basic interface is used to display the value of power, voltage, current and error in measured
value. The resulting values are also stored in a text document for future access. The algorithm for the
Visual Basic Pro is described below.

Visual Basic Code:

Main program:

 Wait for received data


 When data received, break to Received Data subroutine
 If both values are received( complete counter is one), jump to Calculate subroutine

Received Data:

 Check Variable select (Variable select is a counter used to specify which variable is received
i.e. Voltage or current.)
 If Variable select is equal to one (all values received will be stored in the voltage variable)
o Check Received Counter
o Case1: Received Counter is equal to one, multiply the received data by a thousand
and add it to the voltage variable
o Case2: received counter is equal to two, then multiply the received data by hundred
and add it to the voltage variable
o Case3: received counter is equal to three, then multiply the received data by ten and
add it to the voltage variable
o Case4: received counter is equal to four; add received data to the voltage variable.
o Clear received counter and increment the variable select
 Else If variable select equals two
o Check Received Counter

17
Electrical Design 4
Energy Meter

o Case1: Received Counter is equal to one, multiply the received data by a thousand
and add it to the current variable.
o Case2: received counter is equal to two, then multiply the received data by hundred
and add it to the current variable.
o Case3: received counter is equal to three, then multiply the received data by ten and
add it to the current variable.
o Case4: received counter is equal to four; add received data to the current variable.
o Clear received counter and set variable select to one. Set complete counter to one.
 Exit subroutine.

Calculate subroutine:

 Multiply the current, voltage and gain (value which were used to scale voltage and current
variables) and store result in power variable.
 Display voltage, current and power, using text boxes.
 Store voltage, current and power in a text file
 Return to main loop.

Program Overview

The main program loops continuously until the data is received via the SPI which triggers a,receive
complete interrupt. The data comes in as a 32 bit value in the interrupt service routine the value is
stored in a temporary register. The data received register indicates weather you receive the value. If
the data received is equal to one it will break away from the main program into the calculation
subroutine. In the calculate subroutine we set the higher 16bits of the temp register as voltage and the
lower 16bits as current. The power is calculated by multiplying the current, voltage and gain. Once
current and voltage values have been obtained, we use our ASCII subroutine to convert the values into
equivalent ASCII values. Transmit the resultant current and voltage values via RS232. Display the
calculated power value on the LCD. In order to do this we need to convert the power value into a
string, then only can we display the values using predefined functions” lcd_putsf ()”.

18
Electrical Design 4
Energy Meter

After the data is sent the visual basic program stores the received voltage and current values as
described in procedures above. The power value is calculated. Display the voltage, current and power
values using the graphical user interface shown below, and also stores these values in a text file.

Visual basic Software design


The first question that needs to be asked when approaching the software design is what we want the
software to do. In this design the software has two important functions. The first function is to send
and receive data using serial communication. The second and most important function is to capture
the data and store in a database so that it can be analyzed by the user. These functions will be broken
down into further detail following the report.

The basic software functions that are required from Visual Basic can be seen in figure16. Once the
user opens the form on visual basic, the software starts working automatically. This means that the
serial port is opened and then waits until data is received. When data is received, the ASCII values are
automatically converted by visual basic. These decimal number and stored in a temporary variables.
These decimal numbers are then added up to form a decimal number of two decimal points which is
then ready to be sent to Microsoft Access. An example of how the numbers are received and
converted is illustrated in the following table.

19
Electrical Design 4
Energy Meter

Visual basic program:

Firstly, variables needed to be defined as strings, integers or bytes respectively. We then needed to
create a data base in Microsoft Access and then export it visual basic using DataGridView. Two
buttons were created on visual basic to allow the user to perform various functions. An exit button
was created to allow user to exit from interface and a refresh button was used to receive current,
voltage and power values and are displayed with the aid of textboxes. The received values from
serialport1 were ASCII values which were transmitted from the microcontroller and we used the
ReadChar() function to handle this. When programing this button, the serial port 1 was set up, time
and date, using DateTimePicker1.Text as well as logging data into the database created using
Microsoft access.

Error handling:

It was necessary to create an error handler in the textboxes which only allowed numeric values to be
displayed in textboxes, such as “0123456789”.

20
Electrical Design 4
Energy Meter

Fig.15 Flow chart of visual basic code

21
Electrical Design 4
Energy Meter

Figure 16: Graphical user interface using visual basic

Visual basic code:

Flow diagrams Public Class Form1


Dim hr As Integer
Dim power As Integer
Dim current As Integer
Dim voltage As Integer
Dim min As String
Dim time As String
Dim time_1 As String
Dim millisecond As String
Dim num As Byte
Dim CharsAllowed As String
Dim MyDataRow As DataRow

Private Sub form1_load(ByVal sender As System.Object, ByVal e As System


.EventArgs)handles MyBase.Load(http://dotnetperls.com/datagridview-vbnet)
Me.Design4TableAdapter.Fill(Me.Database1DataSet.Design4)
Dim con As New OleDb.OleDbConnection
con.ConnectionString =
"provider=provider=microsoft.ACE.OLEDB.12.0;data"
Design4BindingSource = |DataDirectory|\design_4.accdb"
Me.Design4TableAdapter.Fill(Me.Database1DataSet.Design4)

22
Electrical Design 4
Energy Meter

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
End
End Sub

Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
CharsAllowed = "0123456789"
If CharsAllowed.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End Sub

Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles TextBox4.TextChanged

End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
SerialPort1.PortName = "COM1"
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.Open()
power = SerialPort1.ReadChar() + 48
SerialPort1.Close()
SerialPort1.Open()
voltage = SerialPort1.ReadChar() + 48
SerialPort1.Close()
SerialPort1.Open()
current = SerialPort1.ReadChar() + 48
SerialPort1.Close()
Dim MyDataRow As DataRow
MyDataRow = Database1DataSet.Tables("design_4").NewRow()
MyDataRow("power") = TextBox4.Text
MyDataRow("voltage") = TextBox3.Text
MyDataRow("current") = TextBox2.Text
MyDataRow("Date") = DateTimePicker1.Text
hr = DateTimePicker1.Value.Hour.ToString()
min = DateTimePicker1.Value.Minute.ToString()
time = hr & ":" & min
MyDataRow("time") = time
TextBox1.Text = time
Database1DataSet.Tables("design_4").Rows.Add(MyDataRow)
Me.Validate()
Me.Design4BindingSource.EndEdit()
Me.Design4TableAdapter.Update(Me.Database1DataSet.Design4)

23
Electrical Design 4
Energy Meter

TextBox4.Text = power
TextBox3.Text = voltage
TextBox2.Text = current
End Sub

Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
CharsAllowed = "0123456789"
If CharsAllowed.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles TextBox2.TextChanged

End Sub

Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
CharsAllowed = "0123456789"
If CharsAllowed.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles TextBox3.TextChanged

End Sub

Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Label2.Click

End Sub

24
Electrical Design 4
Energy Meter

6. Testing & Results

7.1. Full schematic & setup

Now that all the paper design is complete the actual building and testing of both the hardware and
software designs had to be implemented.

After testing this circuit, there was an error that occurred. At first there was no communication
taking place between the PC and the microcontroller. The first step in debugging was to look at the
device that allows communication to take place. This was the MAX232. After looking at all the
connections for the MAX232 it was found that no errors were present. This left the microcontroller.
After researching it was noted that the microcontroller runs of an internal clock of 1MHz by default if
not changed. This frequency is too low for serial communication to work. Now that the problem was
recognized the solution was to change the internal clock of the microcontroller to 4 MHz by fusing
the bits using CodeVisionAVR C Compiler. After this was done the circuit worked and all that was left
to do was to design a PCB (printed circuit board) for the final prototype. This PCB was designed using
Ultiboard from the Electronics Workbench software package ( as seen in section 3 hardware design).
The actual PCB’s as well as the final system overview is illustrated below :-

25
Electrical Design 4
Energy Meter

26
Electrical Design 4
Energy Meter

The step down transformer steps down the high mains voltage from 230 V AC to 8 V AC. We then
created a power supply using simple analogue circuitry. The 8 V AC going into the supply is
converted to DC voltage using a full wave bridge rectifier & making use of a smoothing capacitor to
give a smooth DC output with very little ripple. The inexpensive Lm7805 voltage regulator is used to
give a constant 5V DC output at 1 Amp.

The 5V DC is sent to the main PCB circuit to power the MCP3909, the Atmega 16 and the LCD. A
function generator is used to provide inputs to pins 5, 6, 7 & 8 of the MCP3909.

27
Electrical Design 4
Energy Meter

7. Management

Work distribution between group members:

Jared Kisten – power supply, SPI

Euveshen Govender – microcontroller, LCD display, SPI, serial communications

Shaprin Pillay –visual basic interface.

Current Status
At the time of writing, the following progress has been made:

 Final specifications
 Complete Design layout
 Complete software structure

Timeline
The following figure shows a projected timeline.

Aug 2010 Sep 2010 Oct 2010


ID Task Name Start Finish Duration

1 Research 7/28/2010 8/5/2010 9d

2 Phase 1 Report 8/4/2010 8/11/2010 8d

3 Research Analog Circuitry 8/12/2010 8/13/2010 2d

4 Working on Code 8/14/2010 8/15/2010 2d

5 Hardware Design & Simulation 8/16/2010 8/23/2010 8d

6 Phase 2 Report 8/24/2010 8/31/2010 8d

7 Phase 2 Presentation 8/29/2010 8/31/2010 3d

8 Building and Testing 9/1/2010 9/29/2010 29d

9 Debugging 9/18/2010 10/4/2010 17d

10 Final Report 10/4/2010 10/15/2010 11d

11 Final Presentation 10/9/2010 10/18/2010 9d

28
Electrical Design 4
Energy Meter

8. Conclusion

The aim of this design was to develop an Energy Meter which allowed a user to monitor data from the
mains and also log these values into a data base. This takes out the possibility of human error. The
background theory was properly researched and a time schedule was drawn up to make this design a
success within the time period given. Problems presented themselves during this design but research
and innovative thinking made this project possible. The full paper design of this project was presented
in this report, describing the features and functionality of the energy meter. The hardware schematics
were designed and simulated using Simulink and Ultiboard. The software described in this report is
implemented and simulated and then integrated with the hardware for testing.

29
Electrical Design 4
Energy Meter

References

[1] Mobicon Electronic Components. 2002. Ultrasonic Sensor. (Transmitter)


http://www.mantech.co.za/Datasheets/Products/UT1612MPR.pdf, accessed on March 5, 2010.

[2] http://www.kpsec.freeuk.com/powersup.htm#trsr , accessed on August 20, 2010

[3] Wikipedia, “Sound Pressure”, http://en.wikipedia.org/wiki/Sound_pressure. [Online]. [Accessed:


March 5, 2010]

[4] National Semiconductor. 2003. Tone Decoder. http://www.national.com/ds/LM/LM567.pdf,


accessed on March 12, 2010.

[5] ShenzenTopway Technology Co Ltd. 2005. LCD Module User Manual.


http://www.mantech.co.za/Datasheets/Products/LMB162ABC.pdf, accessed on March 24, 2010.

30
Electrical Design 4
Energy Meter

Appendix
Microcontroller code:

/*****************************************************

This program was produced by the

CodeWizardAVR V1.25.3 Evaluation

Automatic Program Generator

© Copyright 1998-2007 Pavel Haiduc, HP InfoTech s.r.l.

http://www.hpinfotech.com

Project :

Version :

Date : 10/25/2010

Author : Freeware, for evaluation and non-commercial use only

Company :

Comments:

Chip type : ATmega16

Program type : Application

Clock frequency : 4.000000 MHz

Memory model : Small

External SRAM size : 0

Data Stack size : 256

*****************************************************/

#include <mega16.h>

31
Electrical Design 4
Energy Meter

#include <stdio.h>

#include <stdlib.h>

#include <delay.h>

#include <string.h>

#include <math.h>

// Alphanumeric LCD Module functions

#asm

.equ __lcd_port=0x15 ;PORTC

#endasm

#include <lcd.h>

// Standard Input/Output functions

#include <stdio.h>

// SPI functions

#include <spi.h>

// Declare your global variables here

int temp ;

char LCD_txt[16];//Buffer for LCD to display text

char Display_LCD[16];//Buffer to display on the LCDvoid main(void)

// Declare your local variables here

// Input/Output Ports initialization

// Port A initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In

32
Electrical Design 4
Energy Meter

// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T

PORTA=0x00;

DDRA=0x00;

// Port B initialization

// Func7=Out Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In

// State7=0 State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T

PORTB=0x00;

DDRB=0xB0;

// Port C initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In

// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T

PORTC=0x00;

DDRC=0x00;

// Port D initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In

// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T

PORTD=0x00;

DDRD=0x00;

// Timer/Counter 0 initialization

// Clock source: System Clock

// Clock value: Timer 0 Stopped

// Mode: Normal top=FFh

// OC0 output: Disconnected

33
Electrical Design 4
Energy Meter

TCCR0=0x00;

TCNT0=0x00;

OCR0=0x00;

// Timer/Counter 1 initialization

// Clock source: System Clock

// Clock value: Timer 1 Stopped

// Mode: Normal top=FFFFh

// OC1A output: Discon.

// OC1B output: Discon.

// Noise Canceler: Off

// Input Capture on Falling Edge

// Timer 1 Overflow Interrupt: Off

// Input Capture Interrupt: Off

// Compare A Match Interrupt: Off

// Compare B Match Interrupt: Off

TCCR1A=0x00;

TCCR1B=0x00;

TCNT1H=0x00;

TCNT1L=0x00;

ICR1H=0x00;

ICR1L=0x00;

OCR1AH=0x00;

OCR1AL=0x00;

OCR1BH=0x00;

OCR1BL=0x00;

34
Electrical Design 4
Energy Meter

// Timer/Counter 2 initialization

// Clock source: System Clock

// Clock value: Timer 2 Stopped

// Mode: Normal top=FFh

// OC2 output: Disconnected

ASSR=0x00;

TCCR2=0x00;

TCNT2=0x00;

OCR2=0x00;

// External Interrupt(s) initialization

// INT0: Off

// INT1: Off

// INT2: Off

MCUCR=0x00;

MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization

TIMSK=0x00;

// USART initialization

// Communication Parameters: 8 Data, 1 Stop, No Parity

// USART Receiver: Off

// USART Transmitter: On

// USART Mode: Asynchronous

// USART Baud rate: 9600

UCSRA=0x00;

35
Electrical Design 4
Energy Meter

UCSRB=0x08;

UCSRC=0x86;

UBRRH=0x00;

UBRRL=0x19;

// Analog Comparator initialization

// Analog Comparator: Off

// Analog Comparator Input Capture by Timer/Counter 1: Off

ACSR=0x80;

SFIOR=0x00;

// SPI initialization

// SPI Type: Master

// SPI Clock Rate: 1000.000 kHz

// SPI Clock Phase: Cycle Half

// SPI Clock Polarity: Low

// SPI Data Order: MSB First

SPCR=0x50;

SPSR=0x00;

// LCD module initialization

lcd_init(16);

while (1)

ftoa(Temp,2,Display_LCD);

36
Electrical Design 4
Energy Meter

sprintf(LCD_txt,"Power = %s",Display_LCD);

lcd_gotoxy(0,0);

lcd_puts(LCD_txt);

delay_ms(1000);

lcd_clear();

};

void USART_Init( unsigned int baud )

UBRRH = (unsigned char)(baud>>8); // set baud rate

UBRRL = (unsigned char)baud; // enable receive

UCSRC = (1<<URSEL)|(3<<UCSZ0); // set frame format

void USART_Transmit( unsigned char data )

while ( !( UCSRA & (1<<5)) ); // Wait for empty transmit buffer

UDR = DATA; // Put data into buffer, sends the data

void SPI_MasterInit(void)

37
Electrical Design 4
Energy Meter

DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK); //Set MOSI and SCK output, all others input

SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0); //Enable SPI, Master, set clock rate fck/16

void SPI_MasterTransmit(char cData)

/* Start transmission */

SPDR = cData;

/* Wait for transmission complete */

while(!(SPSR & (1<<SPIF)))

void SPI_SlaveInit(void)

/* Set MISO output, all others input */

DDR_SPI = (1<<DD_MISO);

/* Enable SPI */

SPCR = (1<<SPE);

char SPI_SlaveReceive(void)

/* Wait for reception complete */

while(!(SPSR & (1<<SPIF)))

/* Return data register */

return SPDR;

38
Electrical Design 4
Energy Meter

39

You might also like