32
CHAPTER 1 INTRODUCTION 1.1 Introduction As already mentioned in the abstract, power systems are mainly characterized by their voltage and frequency. In general we can broadly classify the power systems across the world into four major groups 1. 100-127V, 50Hz. 2. 100-127V, 60Hz. 3. 220-240V, 50Hz. 4. 220-240V, 60Hz. (The voltages mentioned above are the R.M.S values and the peak values can be obtained by multiplying the corresponding voltages with 1.414) Presently, the frequencies employed mainly are 50Hz (20ms time period) and 60Hz (16.66ms time period). In India the normal system frequency is 50Hz. As electricity cannot be stored, the instantaneous generation must match the demand being taken from the system. If the instantaneous demand is higher than the generation, the system frequency will fall. Conversely, if the instantaneous generation is higher than the demand, the frequency will rise. System frequency will therefore vary around the 50 Hz target and Grid has 1

MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

Embed Size (px)

Citation preview

Page 1: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

CHAPTER 1

INTRODUCTION1.1 Introduction

As already mentioned in the abstract, power systems are mainly characterized by

their voltage and frequency. In general we can broadly classify the power systems

across the world into four major groups

1. 100-127V, 50Hz.

2. 100-127V, 60Hz.

3. 220-240V, 50Hz.

4. 220-240V, 60Hz.

(The voltages mentioned above are the R.M.S values and the peak values can

be obtained by multiplying the corresponding voltages with 1.414)

Presently, the frequencies employed mainly are 50Hz (20ms time period) and

60Hz (16.66ms time period). In India the normal system frequency is 50Hz. As

electricity cannot be stored, the instantaneous generation must match the demand

being taken from the system. If the instantaneous demand is higher than the

generation, the system frequency will fall. Conversely, if the instantaneous

generation is higher than the demand, the frequency will rise. System frequency

will therefore vary around the 50 Hz target and Grid has statutory obligations to

maintain the frequency within +/- 0.5Hz around this level.

So there is a requirement of continuous monitoring of mains frequency in

keeping a check on power fluctuations which is highly important as there are

many frequency sensitive instruments like the pulse dependent clocks which will

run slower or faster dependent on mains frequency. Also some losses acquired in

AC machines are dependent on the supply frequency.

1

Page 2: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

1.2 Objective:

The main objective of the project is to build an inexpensive frequency measuring

device, especially here to measure the supply mains frequency using a PIC

microcontroller. The range of measurement of frequency is from 10Hz to 125KH

(the pre scaler value set to 4).

1.3 Outline of the project:

1. The AC mains supply is first stepped down using a step down transformer ( ), and

is fed to a near zero detector circuit.

2. This output is given to a microcontroller which measures the frequency based on

an algorithm and displays it on an LCD screen.

2

Page 3: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

CHAPTER 2

MODEL DESCRIPTION:

2.1 Block diagram

Fig,1.

2.2 Components Used

1. PIC18F4520 Microcontroller.

2. 16X2 LCD

3. 20MHz Crystal Oscillator

4. Voltage Regulator IC7805

5. BC107 BJT.

6. IN4148 Diode

7. Resistors

8. Capacitors

3

Page 4: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

2.3 Near Zero Detection Circuit

Fig.2.

Fig.3

4

Page 5: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

Firstly the mains supply is stepped down from 230V to 9V by means of a step

down transformer and then it is rectified using a bridge rectifier. The full wave

rectified signal is applied to at the base of transistor whose output is normally

low. As the input signal to the transistor drops to a value below 0.7V the transistor

turns off and the collector voltage rises to +5V supply voltage as shown in the

fig.3. Three pulses of this output constitute one full period of mains frequency.

These pulses are then fed to the PIC microcontroller.

2.4 PIC18F4520 Microcontroller

The output of the near zero detector is fed to the 2nd pin of PORTC of the PIC.

The input to this pin is continuously monitored for a high pulse (by the command

BTFSS)

The PIC microcontroller starts an accurate timer when the first pulse arrives and

the timer stops when the third pulse arrives. Thus the timer count is proportional

to the period and hence the frequency of the waveform. The timer count which is

in hex is converted into ASCII and then to the real frequency by an appropriate

formula and is displayed on an LCD display.

2.6 Cost and Management

S.No Components Quantity Cost(rupees)

1. Transformer (220V/9V) 1 100.00

2. Diodes (IN4008) 4 2.00

3. Resistors (10K Ohms) 3 1.00

4. PIC 18f4520(sampled) 1 70.00

5. Crystal Oscillator 1 0.50

6. LCD Screen(16X2) 1 150.00

7. Voltage regulator IC (LM7805) 1 5.00

8. PCB, Capacitors and Miscellaneous 50.00

5

Page 6: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

Total cost = 278

NOTE:UART to USB converter(required for data loging in computer) 500rs

CHAPTER-3

OPERATION

3.1 Operation in detail

The output pulses of the near zero detector circuit are counted using 16-bit timer

or counter of the microcontroller (Here timer1 is used). A 20 MHz crystal

oscillator with a counting period is 0.2micro sec and maximum count is 65536. In

perfect 50Hz signal with 20ms period the maximum count will be 25000. The

frequency of the waveform is given by the formula. (Pre-scaler 4)

F= (1.25x10 6 )

Count

By considering that a difference of one count can be measured, the accuracy

measurement is then given by approximately 0.001Hz. For counting 3 pulses we

have roughly eight instructions and the minimum time required by the PIC to

execute these completely is around 8micro s.

6

Page 7: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

3.2 Algorithm

Initiate global program variables

Configure LCD

clear timer/ counter TMR1

Wait until the first low to high transition occurs

start timer/ counter TMR1

wait until second low to high

transition of the pulse

wait until third low to high pulse

transition of the pulse

get timer/ counter value

calculate the frequency

display the frequency on LCD

wait for 5 seconds

END

7

Page 8: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

3.3 Description of the Algorithm

Basically the program runs in an endless loop. In the main loop the following steps

are performed:

1. Initially Timer1 which is a 16-bit register is cleared ( actually two 8 bit registers

TMR1H, TMR1L are cleared).

2. The prescaler value is set so that the frequency is divided by 4 i.e., 20/(4x4) =

1.25.

3. Now the PORTA 2 is configured as i/p port and is continuously monitored and the

timer is started at the starting of the first pulse and stopped at the ending of the

third pulse.

4. Now the value from the TMRIH and TMRIL are brought to two registers NUMH

NUML, i.e the value of the count present in hex format. Then it is converted into

the ASCII format.

5. PORTD and PORTC are configured as o/p ports and PORTD for LCD data (i.e

they are connected to db0 to db7) and PORTC(4,5,6) is for the LCD control.

Then the count value is sent to the LCD (detailed program is appended in

appendix A)

8

Page 9: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

CHAPTER 4

RESULTS AND DISCUSSION

4.1 RESULT

1. Near-Zero detector was set up and soldered on PCB and it was tested for the

required output.

2. The Program written for this project was simulated for results (on PROTEUS

V7.2) and then is flashed on the PIC microcontroller.

3. LCD is interfaced with the microcontroller and the timer 1 count is displayed on

the LCD Screen.

9

Page 10: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

CHAPTER 5

CONCLUSION

5.1 CONCLUSION

An inexpensive frequency measuring device using PIC18f4520 was developed and

constructed on a PCB and tested for results.

5.2 FUTURE SCOPE OF THE PROJECT:

If can be looked into, the project can be extended to data logging i.e., the readings

acquired in these measurements will be logged into a computer by the following

procedure:

1. After the measurement the output of the PIC microcontroller will be given to a

MAX 232 IC Module which converts the PIC TTL to RS232 protocol. The RS-

232 serial communication protocol is a standard protocol used in

asynchronous serial communication. It is the primary protocol used over modem

lines. It is the protocol used by the MICRO STAMP11 when it communicates

with a host PC.

2. Through this serial communication protocol the measurement data is logged into

the computer and this data can be used for various purposes like plotting graphs of

frequency versus time.

10

Page 11: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

Similarly measuring various power system parameters like voltage, current and

power factor at the same time i.e., developing a multi-purpose Meter and logger,

employing a microcontroller or microprocessor modules like RASPBERRY PI,

ARDUINO etc.

References

PIC 18f4520 Datasheet: ww1.microchip.com/downloads/en/devicedoc/39631e.pdf

Rs232 standards: http://www.omega.com/techref/pdf/RS-232.pdf

11

Page 12: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

APPENDIX A

i. CODE

LIST P=PIC18F4520

#INCLUDE P18F4520.INC

CONFIG OSC=HS, IESO=OFF

CONFIG WDT=OFF

CONFIG BORV=3, PWRT=ON, BOREN=ON

_ui_TenK EQU 0x20

_ui_Thou EQU 0x21

_ui_Hund EQU 0x22

_ui_Tens EQU 0x23

_ui_Ones EQU 0x24

_ui_NumH EQU 0x25

_ui_NumL EQU 0x26

LCD_DATA EQU PORTD

LCD_CTRL EQU PORTC

RS EQU RC5

RW EQU RC6

EN EQU RC7

12

Page 13: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

ORG 0H

Z4 MOVLW 0X0F

MOVWF ADCON1

B7 MOVLW 0X20

MOVWF T1CON

CLRF TRISD

CLRF TRISC

BSF TRISA,2

MOVLW 0H

MOVWF TMR1H

MOVWF TMR1L

CALL LDELAY

B1 BTFSS PORTA,2

BRA B1

B2 BTFSC PORTA,2

BRA B2 ; we left first pulse to make sure we dont start counting in the middle of

the wave

BSF T1CON,0

B3 BTFSS PORTA,2

BRA B3

B4 BTFSC PORTA,2

BRA B4

B5 BTFSS PORTA,2

BRA B5

B6 BTFSC PORTA,2

BRA B6 ;here we get time for three pulses

BCF T1CON,0

13

Page 14: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

MOVFF TMR1H,_ui_NumH ; we start converting 16bit hex value we got in

above timer to ascii values to be displayed on lcd

MOVFF TMR1L, _ui_NumL

MOVLW 0x00

SWAPF _ui_NumH,w

ANDLW 0x0F

ADDLW 0xF0

MOVWF _ui_Thou

ADDWF _ui_Thou,f

ADDLW 0xE2

MOVWF _ui_Hund

ADDLW 0x32

MOVWF _ui_Ones

MOVF _ui_NumH,w

ANDLW 0x0F

ADDWF _ui_Hund,f

ADDWF _ui_Hund,f

ADDWF _ui_Ones,f

ADDLW 0xE9

MOVWF _ui_Tens

ADDWF _ui_Tens,f

ADDWF _ui_Tens,f

SWAPF _ui_NumL,w

ANDLW 0x0F

ADDWF _ui_Tens,f

ADDWF _ui_Ones,f

RLCF _ui_Tens,f

14

Page 15: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

RLCF _ui_Ones,f

COMF _ui_Ones,f

RLCF _ui_Ones,f

MOVF _ui_NumL,w

ANDLW 0x0F

ADDWF _ui_Ones,f

RLCF _ui_Thou,f

MOVLW 0x07

MOVWF _ui_TenK

MOVLW 0x0A

Lb1:

;addwf _ui_Ones,f

DECF _ui_Tens,f

ADDWF _ui_Ones,f

BTFSS STATUS,C ;

GOTO Lb1

Lb2:

;addwf _ui_Tens,f

DECF _ui_Hund,f

ADDWF _ui_Tens,f

BTFSS STATUS,C

GOTO Lb2

Lb3:

;addwf _ui_Hund,f

DECF _ui_Thou,f

ADDWF _ui_Hund,f

BTFSS STATUS,C

15

Page 16: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

GOTO Lb3

Lb4:

;addwf _ui_Thou,f

DECF _ui_TenK,f

ADDWF _ui_Thou,f

BTFSS STATUS,C

GOTO Lb4

; Convert to ASCII

MOVLW 0x30

ADDWF _ui_Ones,F

ADDWF _ui_Tens,F

ADDWF _ui_Hund,F

ADDWF _ui_Thou,F

ADDWF _ui_TenK,F

CLRF TRISD ; at this point we start sending ascii values to lcd

CLRF TRISC

BCF LCD_CTRL,EN

CALL LDELAY

CALL READY

MOVLW 0X38

CALL COMMAND

CALL LDELAY

CALL READY

MOVLW 0X0E

CALL COMMAND

CALL LDELAY

CALL READY

MOVLW 0X01

CALL COMMAND

16

Page 17: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

CALL LDELAY

CALL READY

MOVLW 0X06

CALL COMMAND

CALL LDELAY

CALL READY

MOVLW 0X86

CALL COMMAND

CALL LDELAY

CALL READY

MOVF _ui_TenK,W

CALL DATA_DISPLAY

CALL LDELAY

CALL READY

MOVF _ui_Thou,W

CALL DATA_DISPLAY

CALL LDELAY

CALL READY

MOVF _ui_Hund,W

CALL DATA_DISPLAY

CALL LDELAY

CALL READY

MOVF _ui_Tens,W

CALL DATA_DISPLAY

CALL LDELAY

CALL READY

MOVF _ui_Ones,W

CALL DATA_DISPLAY

CALL LDELAY

CALL READY

17

Page 18: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

HERE BRA HERE

COMMAND MOVWF LCD_DATA ; syntax for commands to lcd

BCF LCD_CTRL,RS

BCF LCD_CTRL,RW

BSF LCD_CTRL,EN

CALL SDELAY

BCF LCD_CTRL,EN

RETURN

DATA_DISPLAY MOVWF LCD_DATA ; syntax for data display

BSF LCD_CTRL,RS

BCF LCD_CTRL,RW

BSF LCD_CTRL,EN

CALL SDELAY

BCF LCD_CTRL,EN

RETURN

READY SETF TRISD

BCF LCD_CTRL,RS

BSF LCD_CTRL,RW

BACK BSF LCD_CTRL,EN

CALL SDELAY

BCF LCD_CTRL,EN

BTFSC LCD_DATA,7

BRA BACK

CLRF TRISD

RETURN

SDELAY MOVLW D'255'

B10 DECF WREG

BNZ B10

RETURN

LDELAY MOVLW 0X08 ; code for causing delay of 13.5seconds

18

Page 19: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

MOVWF T0CON

MOVLW 0XCF

MOVWF TMR0H

MOVLW 0X2C

MOVWF TMR0L

BSF T0CON,TMR0ON

AGAIN BTFSS INTCON,TMR0IF

BRA AGAIN

BCF T0CON,TMR0ON

BCF INTCON,TMR0IF

RETURN

MOVLW 0X0F

MOVWF T0CON

CLRF TMR0H

CLRF TMR0L

BSF T0CON,TMR0ON

Z2 BTFSS

BRA Z2

BCF INTCON,TMR0IF

GOTO Z4

END

19

Page 20: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

20

Page 21: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

DETAILED CKT DIAGRAM

21

Page 22: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

APPENDIX B

RS232 PROTOCOL

Fig.4

The above figure shows the relationship between the various components in a

serial ink. These components are the UART, the serial channel, and the interface

logic. An interface chip known as the universal asynchronous receiver/transmitter

or UART is used to implement serial data transmission. The UART sits between

the host computer and the serial channel. The serial channel is the collection of

wires over which the bits are transmitted. The output from the UART is a

standard TTL/CMOS logic level of 0 or 5 volts. In order to improve bandwidth,

remove noise, and increase range, this TTL logical level is converted to an RS-

232 logic level before being sent out on the serial channel. This conversion is

done by the interface logic shown in the above figure. Usually in the systems the

interface logic is implemented by the comm stamp.

A frame is a complete and nondivisible packet of bits. A frame includes both

information (e.g., data and characters) and overhead (e.g., start bit, error checking

and stop bits). In asynchronous serial protocols such as RS-232, the frame

consists of one start bit, seven or eight data bits, parity bits, and stop bits. A

timing diagram for an RS-232 frame consisting of one start bit, 7 data bits, one

parity bits and two stop bits. Note that the exact structure of the frame must be

22

Page 23: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

agreed upon by both transmitter and receiver before the comm-link must be

opened.

Most of the bits in a frame are self-explanatory. The start bit is used to signal the

beginning of a frame and the stop bit is used to signal the end of a frame. The

only bit that probably needs a bit of explanation is the parity bit. Parity is used to

detect transmission errors. For even parity checking, the number of 1's in the data

plus the parity bit must equal an even number. For odd parity, this sum must be an

odd number. Parity bits are used to detect errors in transmitted data. Before

sending out a frame, the transmitter sets the parity bit so that the frame has either

even or odd parity. The receiver and transmitter have already agreed upon which

type of parity check (even or odd) is being used. When the frame is received, then

the receiver checks the parity of the received frame. If the parity is wrong, then

the receiver knows an error occurred in transmission and the receiver can request

that the transmitter re-send the frame.

23

Page 24: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

APPENDIX C

SIMULATION REPORTS

24

Page 25: MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50

25