Vous êtes sur la page 1sur 40

Senior Proiect

Implementation of PC based Electrocardiogram (ECG)



By

Mirza Atik Ahmed Mehran
ID: 062004

Has been approved
December 2010
by
Dr. Khosru M. Salim
Assistant ProIessor
School oI Engineering and Computer Science
Independent University, Bangladesh




ABSTRACT

One oI the most useIul diagnostic tools Ior heart patients is the electrocardiogram (ECG), which
operates by measuring the tiny electrical signals emitted by the heart through chest electrodes.
The goal oI this proiect was to develop a low cost ECG monitoring device with could be used in
any PC or laptop. The circuit used in my proiect is very low cost & portable and can be used in
any PC or laptop. The main challenge oI the proiect was that the ECG signal has a very low
power and low Irequency: which makes the task oI acquiring signiIicant signal very tough. On
the other hand, the presence oI DC part with the analog signal made the acquisition oI the signal
extremely diIIicult. With 8 months oI research work we were able to get meaningIul ECG signal
displayed in the PC.


.










ACKNOWLEDGMENTS

First oI all, I would like to express gratitude to my supervisor Dr. Khosru M. Salim:who was
always willing to help & motivate me to complete the proiect. Without his able guidance, I
would not have been able to complete this senior proiect in time. It has been a great pleasure to
have him as a supervisor. I would specially like to thank Dr Ali Sheehab Sabbir Ior helping me
with the proiect & motivating to peruse this proiect.

I am also very thankIul to Mr.Gazi Mohammad ShariI and Md.Raihan-Bin-RaIique Ior our
numerous lively discussions and useIul suggestions. I would also like to thank Md. Abdul Malek
Ior helping me with the Analog part.

Finally, I would like to thank all express my outmost gratitude to IUB & all the SECS Iaculties
Ior providing me with such a great environment to do a research work like this.













TABLE OF CONTENTS
Serial No. Contents Page
Chapter 1: Introduction
1.1 Overview 1
1.2 Background 1
1.3 Obiective 1
1.4 Outline oI Proiect 2
Chapter 2: ECG
2.1 Introduction 3
2.2 ECG Measurement 3
2.3 System Functionality 3
2.4 Overview oI ECG 3-5
2.5 Common problems with ECG signal 5
Chapter 3: HARDWARE DESCRIPTION
3.1 Introduction 6
3.2 System Flow Chart Description 7-8
3.3 ECG circuit 8-9
3.4 PC interIacing Circuit 10-12
3.5 Components Explanation
3.5.1 Microcontroller (PIC16F877A) 12-13
3.5.2 Operational AmpliIier (LM741 & AD620) 13
3.5.3 5V battery 13
3.5.4 Positive Voltage Regulator (7805) 14
3.5.5 RS 232 port 14
3.5.6 MAX 232 IC 15
3.5.5 Other Components 15-16
Chapter 4: SOFTWARE DESCRIPTION
4.1 Introduction 17
4.2 Flow chart 17
4.3 Assembly code 18


4.4 C Code 18
4.5 Plot Graph 18-19
Chapter 5: RESULTS AND DISCUSSION
5.1 Result 20-21
5.2 Discussion 21
Chapter 6: Conclusion
6.1 Conclusion 22
6.2 Future scopes 22
-
REFERENCES
23
-
APPENDIX
24-34
Figure List of figure Page
Fig 1.1 Taking ECG data in LAB 2
Fig 2 .1 Typical cardiac cycle oI an ECG 4
Fig 2 .2 The Einthoven Triangle & 3-lead electrode placement 5
Fig 2 .2 ECG signal aIIected by noise 5
Fig 3 .1 Picture oI the System 6
Fig 3.2 System Flow Chart 7
Fig 3.3 ECG Circuit 8
Fig 3.4 ECG output on digital Oscilloscope 9
Fig 3.5 PC interIacing Circuit 10
Fig 3.6 MAX232 circuit 11
Fig 3.7 Pin diagram oI PIC16F877A Microcontroller 12
Fig 3.8 Operational AmpliIier 13
Fig 3.9 Rechargeable battery 13
Fig 3.10 Positive voltage regulator 14
Fig 3.11 RS232 Port 14
Fig 3.12 Max 232 pin diagram 15
Fig 4.1. SoItware part implementation oI PC based ECG machine 18




Fig 4.2 ECG output Irom PC (Live Graph) 19
Fig 5.1 AD620 circuit 20
Fig 5.2 LM741 circuit 20
1

CHAPTER 1
GENERAL INTRODUCTION

1.1.Overview

Microcontroller is one oI the greatest blessings oI the modern era. Nowadays electro cardio gram
(ECG) probes are easily available & with the use oI instrumental AmpliIier AD620, it can be
very easily ampliIied with some very easy circuitry to get analog ECG signal. The second
challenge oI this proiect was to interIace ECG part with the PC.

The microcontroller is used to digitize the analog ECG signal to digital Iormat and send it
through a RS232 cable. Here Tx & Rx oI microcontroller are used Ior sending and receiving
data. The part oI data acquisition Irom Rs232 is done using C and data Irom Tx is dumped in
a Iile. Then using the open source Java application Live Graph the data is plotted. By this way
we have made a very cheap ECG machine using easily available materials. The ECG application
can also be web based to use it Ior telemedicine.

1.2. Background

For a Iew years our engineering school has been trying make a low cost portable ECG machine.
When I came to know the proiect: I was very interested have it as my senior proiect. The main
challenge in this proiect was to trace the analog signal. For the proiect I had to use assembly and
C Ior PIC & PC interIacing. Though the analog circuit was not the much critical but the use oI
AD620 was a big problem. I have been working on this proiect Ior 8 months. Being a part oI the
proiect was a great experience.

1.3. Objective

The main obiective oI this proiect is to get analog ECG signal using ECG probe, converting the
analog signal to digital using microcontroller PIC16F877A and plotting the digital signal in a pc
or a laptop.
2

1.4. Outline of Project

This proiect divided in to Iive chapters. The second chapter describes the ECG signal basics. The
components & hardware part is described in the second chapter. The SoItware part is described
in the Iourth chapter. Results and discussions according to the result are described in part Iive.

Conclusions and Iuture scopes oI the Iollowing proiect are described in the chapter six Ior real
term applications.




Fig 1.1 Taking ECG aata in LAB


3

CHAPTER 2
Electrocardiogram (ECG) Basics
2.1. Introduction
An ECG is a measurement oI the electrical activity oI the heart (cardiac) muscle as obtained
Irom the surIace oI the skin. As the heart perIorms its Iunction oI pumping blood through the
circulatory system, the result oI the action potentials responsible Ior the mechanical events
within the heart is the generation oI a certain sequence oI electrical events.
2.2ECG Measurement

The electrical impulses within the heart act as a source oI voltage, which generates a current Ilow
in the torso and corresponding potentials on the skin. The potential distribution can be modeled
as iI the heart were a time- varying electric dipole II two leads are connected between two points
on the body, electrical voltage observed between the two electrodes is given by the dot product
oI the two vectors.

2.3System Functionality

At Iirst we take signal Irom human body. We know ECG signal is a very low signal so we
have to ampliIy it. By using AD620 & LM741 we ampliIy the signal and we see the signal in
oscilloscope. In digital part we need to PC Ior interIacing. For interIacing we use RS 232 Port
& we send the data in PC & show graph by a Java soItware.
2.4Overview of ECG
Heart Signal
The atria and ventricles work together, alternately contracting and relaxing to pump blood
through your heart. The electrical system oI the heart is the power source that makes this
possible. Heartbeat is triggered by electrical impulses that travel down a special pathway through
your heart. The impulse starts in a small bundle oI specialized cells called the SA node
(senatorial node), located in the right atrium. This node is known as the heart' s natural
4

pacemaker. The electrical activity spreads through the walls oI the atria and causes them to
contract. At rest, a normal heart beats around 50 to 99 times a minute. Exercise, emotions, Iever
and some medications can cause your heart to beat Iaster, sometimes to well over 100 beats per
minute. |8||9||10|
Electrocardiogram
ECG is the electrical maniIestation oI the contractile activity oI the heart's myocardium. The P,
QRS and T waves characterize the ECG waveIorm (Figure 1). The most prominent Ieature is the
QRS complex, where R denotes the peak oI QRS complex. Any disturbance in the regular
rhythmic activity oI the heart (amplitude, duration and shape oI rhythms) is known as
arrhythmia. Cardiac arrhythmias may cause the heart to pump less eIIectively, causing
insuIIicient blood to reach the brain another vital organ. When the body's blood Ilow is
inadequate, the person can Iaint or suIIer chest pain. Sometimes, sudden cardiac death can occur.
ThereIore, a continuous cardiac monitoring and online analysis system could detect these rare
episodes oI cardiac arrhythmias as they occur. |8||9||10|

Fig 2 .1 Tvpical caraiac cvcle of an ECG


5

Standard ECG Probe/Lead System
Two electrodes placed over diIIerent areas oI the heart and connected to the galvanometer will
pick up the electrical currents: resulting Irom the potential diIIerence between them The resulting
tracing oI voltage diIIerence at any two sites due to electrical activity oI the heart is called a
"lead". Most oI the ECG machines use 12- lead system, whereas in this proiect a 3- lead system is
used. Both Ieature 3 electrodes placed on the limbs. II the heart' s electrical activity is viewed as a
simple dipole, these three leads record the proiection oI this dipole onto the sides oI the
'Einthoven Triangle'(Iigure 2.), i.e. the equilateral triangle Iormed by the vectors oI the limb
leads. |8||9||10|




Fig 2 .2 The Einthoven Triangle & 3-leaa electroae placement
2.5 Common problems with ECG signal
ECG has amplitude oI only about 1 mV, so to detect it an ampliIier is needed. There is a
problem, though - electrical noise, or electromagnetic interIerence (EMI). EMI is generated by
many common appliances, such as power lines, Iluorescent lights, car ignitions, motors and Ians,
computers, monitors, printers, TVs and television, and cell phones. When the ECG is ampliIied,
the noise is ampliIied too, and oIten swamps the ECG signal. Luckily, the noise is usually oI a
higher Irequency than the ECG. For example, AC hum is 60 Hz, which is above the highest
Irequency in the ECG (about 20 Hz). So the noise can be reduced by low-pass Iiltering. |9|


Fig 2 .2 ECG signal affectea bv noise
6

CHAPTER 3
SYSTEM & HARDWARE DESCRIPTION
3.1. Introduction

The total hardware explanation Ior this proiect is included here. It is included the circuit diagram
and circuit explanation oI this proiect and how this circuits are worked. It is already declared
that, this proiect collect ECG signal Irom ECG probe: analog part is ampliIying the signal
sending the data and the microcontroller processes the data. So the main parts oI this proiect are
ECG signal ampliIication, microcontroller and obviously PC interIacing. All oI these things are
explained details in this chapter.


Fig 3 .1 Picture of the Svstem

7

3.2 System Flow Chart & Description




















Fig 3.2 System Flow Chart
8


From Iigure 3.2, we can see that Iirst signal is acquired using ECG probe (the patient right hand,
leIt hand & leIt leg). As the ECG signal is very low and close to 2 mV: it is then ampliIied using
AD620 200 times and then 11 times post ampliIied using LM741. It can be mentioned that here
AD620 is used as it is a low cost, high accuracy instrumentation ampliIier with superior dc
perIormance and a CMR greater than 100dB to nearly 1 kHz|8|. Then the ampliIied output is sent to
Microcontroller Ior ADC & pc interIacing (which would be discussed in the later parts oI the
Iollowing chapter).

3.3 ECG circuit

Fig 3.3 ECG Circuit

AD620 circuit:
From Iigure 3.3 we can see that diIIerential input ECG input is taken on AD620 |3| pin 3, 2 Irom
probe Irom leIt leg & right arm. The pin 5 is grounded with the patient body and circuit. The pin 4 & 7
are connected to -6V & 6V battery consecutively. Here, battery is used to reduce the 60Hz noise
9

Irom source. The pin 8 & 1 are used Ior gain. The higher the resistance between pin 1 & 8 higher the
gain. The pin 6 is the output pin. In the output a high pass Iilter is used to reduce noise.
In order to achieve good signal to noise ratio, diIIerential ampliIiers are used. Since the output is
proportional to the diIIerence between the two voltages, this circuit has a Iairly good common mode
reiection. UnIortunately, the diIIerential ampliIier's perIormance is limited due to low input
impedance. This problem has been avoided by incorporating a dual Instrumentation ampliIier in the
place oI a diIIerential ampliIier. The Instrumentation ampliIier chosen Ior Iront end design was
(AD620). The AD620 is a low cost, high accuracy instrumentation ampliIier with superior dc
perIormance and a CMR greater than 100dB to nearly 1 kHz. It also has a 50uV max oIIset voltage,
low input bias current (1nA max), and low input voltage noise (0.28uV Irom 0.1 Hz to 20Hz).
LM741 circuit:
From the Iigure 3.3 LM741 |5| is used Ior post ampliIy the signal Irom AD620. Here, non inverting
input pin 3 is used Ior inputting signal Irom AD620. The circuit works as a non-inverting ampliIier.
The output Irom the LM741 (Pin 6) is Iurther Iiltered using another stage oI high pass Iilter. This stage
oI ampliIier is used because microcontroller accepts 5V signal.






Fig 3.4 ECG output on digital Oscilloscope
10

3.4 PC interfacing Circuit

Fig 3.5PC interIacing Circuit


From Iigure 3.5we can see that pic16I877a microcontroller is used, where analog ECG signal
would be inputted. This is converted to digital signal by ADC in the microcontroller. This
converted data is then send to the TXREG register oI the microcontroller, Irom where the signal
is then send to the COM1 port on the PC. For the side oI the PC we used the RS232 pin.

For Analog input we used port A oI microcontroller: and used ADC module ADCON0 and
ADCON1. The analog signal is send to the microcontroller through port a pin number 2. Then it
is sending to output pin in port C, the output pin Ior transmitting is TX/RC6.
We used the RS232 to transmit to the PC. The RS232 pin conIiguration is given below. We had
to use only two 3 pins out oI them, The RX, TX and the ground. The RS232 pins are connected
to the MAX232.



11
















Fig 3.6 MAX232 circuit[4{

From the Iigure 3.6 we can see that max232 connection and the pin conIiguration. For Max we
needed 6 capacitors oI 1 micro-Iarad. TX/RC6 is connected to pin 11 & RX/RC7 to pin 12 oI
MAX232|4|. The circuit basically converts the Iive volts signal to PC usable 15V to -15V
signals.

In Serial Communication the data`s are sent or received serially i.e. a bit is send at a time.
ThereIore upon sending eight bit, a byte is send. For communication there are three modes oI
sending or receiving data:
1) Simplex
2) HalI-Duplex
3) Full- Duplex
Simplex is a one way communication only, i.e. only one can send data another will receive. In
HalI-Duplex it`s a two-way communication but only one side can either transmit or receive at a
moment.
12

In case oI the microcontroller, we are using pic16I877a. This microcontroller has two module Ior
serial communication, Master Synchronous Serial Port (MSSP) module and The Universal
Synchronous Asynchronous Receiver Transmitter (USART) module. In our proiect we are using
USART module.
From PC`s side we had two port options USB and RS232. For both ports we need another IC Ior
the signal voltage conversion.
3.5 Components Explanation
3.5.1 Microcontroller (PIC16F877A)
Microchip produced PIC16F877A microcontroller is used here. Actually this is the heart oI the
system. It takes the data Iorm temperature sensor, relative humidity sensor and processes the data and
sends it by the cell phone.







Fig 3.7 Pin aiagram of PIC16F877A Microcontroller [2{

The main Ieatures oI PIC16F877A and on-chip peripherals are given as Iollows |2| :
Operating Frequency is DC 20 MHz.
Flash Program Memory is 8K.
Data Memory is 368 bytes.
Data EEPROM Memory is 256 bytes.
15 Interrupt sources.
13

Pots A, B, C, D, and E are I/O ports.
3 Timers.
2 Capture/Compare modules.
8 input channels Ior 10 bit Analog-to-Digital module
2 analog comparators
35 instructions sets
Packages are 40 pins PDIP, 44 pins PLCC, 44 pins TQFP, 44 pins QFN
3.5.2. Operational Amplifier (LM741 & AD620)
AmpliIy the ECG signal AD620 is used & Ior post ampliIication LM741 is used. This ampliIier can
operate with a Battery with positive & negative supply.

Fig 3.8Operational AmpliIier
3.5.3. 5V battery
A rechargeable battery is used as backup power supply. The battery can provide 5 volts and the
capacity is 300mA/hour.





Fig 3.9 Rechargeable battery
14

3.5.4. Positive Voltage Regulator (7805)
This is a regulator Ior 5V output. It can operate Irom 5-30volts range. This can supply steady 5 volt
to the system.

Fig 3.10Positive voltage regulator
3.5.5. RS 232 port
RS232 was used to transmit to the PC. The RS232 pin conIiguration is given below. We had to
use only two 3 pins out oI them, The RX, TX and the ground. The RS232 pins are connected to
the MAX232.








Fig 3.11RS232 Ports





15

3.5.6. MAX 232 IC
The max232 connection and the pin conIiguration are given below.

Fig 3.12 Max 232 pin diagram
3.5.5 Other Components
Except the above components some other components were used. The purpose oI those components
with Iigure is given below,
Component name Purpose Image
ECG Probe It gets the electrical current or
pulse produced Irom heart




Diode Diode is used Ior current control
Ior Battery Charging

16

IC-Socket IC socket is used Ior Op-Amps
& microcontroller. Cause this
are constantly Changed

Capacitor Used Ior Iiltering in the analog
Circuit & also used in the PC
interIacing Circuit

Resistor DiIIerent types oI resistors are
used

Wire DiIIerent types oI wire are used
Printed Circuit Board A designed circuit board is used


















17

CHAPTER 4
SOFTWARE DESCRIPTION
4.1 Introduction
The proiect has two soItware parts. The Iirst part is assembly code using the microcontroller. In
the microcontroller part the analog ECG signal is converted into digital signal through the ADC
Iunction oI the microcontroller and sent through the Tx part oI the to max232 & through Rs232
to PC/Laptop. The second soItware part is implemented through the windows.h Iunction oI the
C.In this part the COM port is used to Ietch the ADC value Irom Tx and is dumped into a
Dump Iile. Then the open source Java application Live Graph is used to plot data.
4.2 Flow chart
In the Iollowing part an over view oI the soItware part oI the system is given









Fig 4.1SoItware part implementation oI PC based ECG machine


18

4.3Assembly code
The main task done by the assembly code is to do the ADC and send the value oI the ADC to the
asynchronous transmission mode to the Tx transmitter to RS232.
At Iirst we initialized the input and output port. Then we setup the ADCON0 and ADCON1
register Ior ADC conversion. AIter that we set the baud rate and did the interrupt enable. Then
ADC will be started. AIter that, program will check Ior ADC value. II the program does not get
any value then it will make a loop and check the ADC value again and again. But whenever it
gets value then transmit the value through RC6/TX pin and when TXREG buIIer will empty then
service interrupt will be enabled and make 2 microsecond delay. AIter that the program will do
the things again Irom 'check Ior ADC value step.
4.4 C++ Code
The C code uses the Windows.h header Iile to open the common port. By using the COM port
we can use USB, serial port etc to get data Irom RS232. One the port is opened we can
continuously read data Irom comport. Here an very important part is the time out parameters. I
have used the trial and error method to get the right amount oI timing Ior reading data Irom
microcontroller. AIter reading the ADC value it is dumped in a dump Iile using the Iile operation
Iunction. The dump Iile acts like a buIIer. For having a nice continuous graphing I have done the
dumping Iile in such a way that it reIreshes every 100 ADC value.
4.5Plot Graph
Plotting oI the Dumped ADC value is done by the Live Graph (Open source Java Application). It
is a very easy to use Plotting application. The Dump Iile is loaded using the load interIace .The
timing interval can be changed. So the chance oI missing data is very rare. This is very important
Ior ECG Signal.

19


Fig 4.2 ECG output Irom PC (Live Graph)

20

CHAPTER 5
Result & Discussion
Result

Fig 5.1 AD620 circuit
Theoretical Gain equation Ior AD620,
G 49.4 K/Rg 1
In the circuit our Rg is 200
So, Gain is 248.

Fig 5.2 LM741 circuit
Theoretical Gain equation Ior LM741,
G 1 RI/ Ri
So, gain is 11
Total gain 248*112728
21

Practical Gain:
The input signal voltage 2 mV
Output signal voltage 5v
So, Gain Pout/Pin
5V/2*10`3
2500
So, Error 1-(Gtheory-Gpractical)/ (GpractialGtheory)}
1-(2728-2500)/(27282500)}
1- 0.583
0.165
16.5
Discussion
From the above calculation oI the system we can see that the system has Error oI 16.5. It is
due to the internal circuit resistance, capacitance and other Iactors. The main problem with the
Iollowing system is that we are dealing with a very low voltage & Irequency signal. So, to isolate
the signal Irom noise is very tough. Another problem Iound Irom 8 months oI recherch is that
there is some DC part in the analog circuit which cannot be reduced Irom our experimental
circuit. So, there is a need Ior using some extra circuitry Ior reducing the DC Part. The PC
interIacing part works without any problem. It can be said with some improvement oI the analog
circuit the portable ECG machine can be a very eIIicient & reliable system.





22

CHAPTER 6
CONCLUSION
6.1. Conclusion
The proiect is a very Iunctional Ior telemedicine & low cost medication. The PC interIacing &
displaying Iunction on the PC makes the circuit usable in any PC or Laptop. It can easily be
converted into a web based system where any doctor can track a patient ECG Irom any part oI
the world. The EGC can be stored & kept Ior record. The proiect really has some great Iuture
scopes with some modiIication.
6.2. Future Scopes
Wireless ECG monitoring system
Application oI mobile communication: The ECG captured in memory can be transmitted by the
use oI GSM module, which can be incorporated in the transmitter part.
Internet communication in telemedicine: The ECG can also be transmitted to any part oI the
world via Internet. The doctors or team oI specialists can thus analysis the data and take the
decisions accordingly.
SelI explanatory remedies by the device: Additional soItware & circuitry can be added, so as to
enable the device to analysis the acquired waveIorm on itselI & provide the best possible remedy
immediately.






23

REFERENCES
|1| MICROCONTROLLER (FROM WIKIPEDIA)
|2| DATASHEET (PIC16F877A)
|3| DATASHEET (AD630)
|4| DATASHEET (Max232)
|5| DATASHEET (LM741)
HelpIul websites:
[7] http://www.scribd.com/doc/36416013/Investigation-oI-Ecg-Signal-Using- Dsp-Processor-and-
Wavelets
[8] http://cnx.org/content/m33166/latest/
[9] http://www.Ireepatentsonline.com/7058444.html
[10] http://liIeasamedicalstudent.blogspot.com/20090801archive.html
[11] http://liIeasamedicalstudent.blogspot.com/20090801archive.html








24

APPENDIX
Assembly code:
list p16I877
include p16I877.inc~

FIXDELAY equ 0x23
c1 equ 0x24

org 0x00:
goto start
org 0x04
goto serviceinterrupt
start

movlw .0
movwI c1

call initialPORTS
call initialADC1
call initialSPAC : initial serial ports in asynchronous communication
call initialTRANS
25

loop1
goto loop1
return

initialPORTS
bsI STATUS, RP0
movlw b'11111111'
movwI TRISA : Analog INPUT
movlw b'10000000'
movwI TRISC : RX as input and TX as output
bcI STATUS, RP0
return

initialADC1
movlw b'00000001' : AN0 input and POWER on module
movwI ADCON0
bsI STATUS, RP0 : Enter Bank 1
clrI ADCON1 : Set all input as analog
bcI STATUS, RP0 : Exit Bank 1
return

26

initialADC2
bsI ADCON0,2 : Start ADC module
waittimer
btIsc ADCON0,2 : Wait Till ADC conversion is done
goto waittimer
return

initialSPAC

bsI INTCON,GIE
bsI INTCON,PEIE

bsI STATUS, RP0
bsI PIE1,TXIE : Trasmit interrupt enable
: bsI PIE1,RCIE : Receive interrupt enable

movlw .25
movwI SPBRG : Baud rate 9600 bps
movlw b'00100100'
movwI TXSTA : 8-bit,Transmit enabel,asynchronous, highspeed

27

bcI STATUS, RP0
movlw b'10010000' : assume no Iramming and overrun error
movwI RCSTA
return

initialTRANS
call initialADC2 : Get ADC Value
movI ADRESH,0
movwI TXREG
return

serviceinterrupt
call delayagain
call initialTRANS
retIie
DELAY200
MOVLW 0x42 : 66 LOOPS
MOVWF FIXDELAY : 200us Iixed delay
NOTDONE200 DECFSZ FIXDELAY, 1 : Decrement oI FIXDELAY
GOTO NOTDONE200 : II 200us isn't up go back to
NOTDONE200
28

RETURN : II 200us is up then return to
instruction.

delayagain
movI c1,0
addlw .1
movwI c1
call DELAY200
movI c1,0
xorlw .125
btIss STATUS, Z
goto delayagain
bcI STATUS, Z
return

end





29

C++ code:
#includeiostream~
#includeIstream~
#includestring~
#includewindows.h~
#includecmath~

using namespace std:
int convert(int):

int main()
HANDLE hSerial:

//Openning oI serial port:
/*string stComPort:
string iPortNo:
cout"Enter serial port number: ":
cin~~iPortNo:
stComPort"COM"iPortNo:
coutstComPortendl:

30


hSerialCreateFile(stComPort.cstr(),GENERICREAD'GENERICWRITE,

0,0,OPENEXISTING,FILEATTRIBUTENORMAL,0):*/

hSerialCreateFile("\\\\.\\COM10",GENERICREAD'GENERICWRITE,

0,0,OPENEXISTING,FILEATTRIBUTENORMAL,0):
iI(hSerialINVALIDHANDLEVALUE)
iI(GetLastError()ERRORFILENOTFOUND)
cout"Serial port doesn't exist\n":
}
else
cout"Port exists but some other error occured\n":
}
}

//Setting parameters:
DCB dcbSerialParams 0}:
dcbSerialParams.DCBlengthsizeoI(dcbSerialParams):

iI(!GetCommState(hSerial, &dcbSerialParams))
31

cout"Error getting state\n":
}

dcbSerialParams.BaudRateCBR9600:
dcbSerialParams.ByteSize8:
dcbSerialParams.StopBitsONESTOPBIT:
dcbSerialParams.ParityNOPARITY:

iI(!SetCommState(hSerial, &dcbSerialParams))
cout"Error setting up the serial port state\n":
}

COMMTIMEOUTS timeouts0}:

timeouts.ReadIntervalTimeout10:
timeouts.ReadTotalTimeoutConstant10:
timeouts.ReadTotalTimeoutMultiplier40:
timeouts.WriteTotalTimeoutConstant20:
timeouts.WriteTotalTimeoutMultiplier20:

iI(!SetCommTimeouts(hSerial, &timeouts))
32

cout"Error setting timeout prameters\n":
}

const int n25:
char szBuII|n1|0}:

DWORD dwBytesRead0, dwBytesWritten0:

oIstream outIile:
//outIile.open("output.txt",ios::trunc):
//outIile"99"endl:

outIile.open("output.txt",ios::app):
oIstream outIile1("output.txt",ios::trunc):
outIile"99"endl:

int count0:
outIile"0"endl:
while(1)
count:
memset(szBuII,0,n1):
33

dwBytesRead0:
iI(!ReadFile(hSerial,szBuII,n,&dwBytesRead,NULL))
cout"Read Irom serial port Iailed\n":
}
//coutszBuII|0|endl:
int xszBuII|0|:
coutxendl:
iI(count100)
oIstream outIile("output.txt",ios::trunc):
count0:
outIile"999"endl:
}
iI(x0)
x256x:
//coutconvert((int)szBuII|0|)endl:
coutxendl:
}
outIilexendl:

}

34

CloseHandle(hSerial):
return 0:
}

int convert(int x)
int y-x-1:
char bin|8|:
Ior(int i0:i8:i)
bin|i|y2:
iI(bin|i|1)bin|i|0:
else bin|i|1:
yy/2:
}
y0:
Ior(i0:i8:i)
ybin|i|*pow(2,i):
}
return y:
}

Vous aimerez peut-être aussi