100% found this document useful (1 vote)
373 views12 pages

Temp-Indicator Using AT89C52

This document describes a microcontroller-based temperature indicator that uses an AT89C52 microcontroller and a DS1621 temperature sensor chip. It outputs temperature readings between -55°C and 125°C digitally to a 16x1 LCD display. The circuit diagram and program written in C are provided. Key components include an AT89C52 microcontroller, DS1621 temperature sensor, LCD display, power supply components, and supporting resistors and capacitors. Temperature readings from the sensor are processed by the microcontroller and sent to the LCD for display.

Uploaded by

rakeshrakesh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
373 views12 pages

Temp-Indicator Using AT89C52

This document describes a microcontroller-based temperature indicator that uses an AT89C52 microcontroller and a DS1621 temperature sensor chip. It outputs temperature readings between -55°C and 125°C digitally to a 16x1 LCD display. The circuit diagram and program written in C are provided. Key components include an AT89C52 microcontroller, DS1621 temperature sensor, LCD display, power supply components, and supporting resistors and capacitors. Temperature readings from the sensor are processed by the microcontroller and sent to the LCD for display.

Uploaded by

rakeshrakesh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

ccoon sntsr ut crt u

i ocnt i o n

Temperature Indicator
Using AT89C52
Aditya Rane

the merits of the Assembly


language program.

eres a microcontroller-based temperature indicator that displays the


temperature in the range of 55C
to 125C. Besides AT89C52 microcontroller,
it uses a temperature sensor chip and an
LCD module. The indicator outputs the
calibrated data in digital form. The program
for the microcontroller is written in C and
not in Assembly language. Since C program
has well-defined syntax, it far outweighs

The circuit

Fig. 1: Block diagram of temperature indicator using AT89C52

Fig. 2: Circuit diagram of temperature indicator using AT89C52


electronics for you

July 2004

Fig. 1 shows the block diagram of the temperature indicator using microcontroller
AT89C52. The power supply
for the circuit is regulated
by IC 7805 and supplied to
different parts of the unit.

construction
Parts List
Semiconductors:
IC1
- 7805 regulator IC
IC2
- AT89C52 microcontroller
IC3
- DS1621 temperature sensor
D1-D4
- 1N4007 rectifier diodes
LED1
- Red LED
Resistors (all -watt, 5% carbon,
unless stated otherwise):
R1
- 1-kilo-ohm
R2
- 47-kilo-ohm
R3
- 10-kilo-ohm
R4, R5
- 4.7-kilo-ohm
VR1
- 1-kilo-ohm preset

Fig. 3: Pin details of IC


DS1621

The thermal
alarm output
(Tout) of IC DS1621
activates when
the temperature
exc e e d s u s e r defined high temperature TH. The
output remains

and temperature readings are all communicated to/from IC DS1621 over a 2-wire
serial cable. The most significant bit (MSB)
of the data is transmitted first and the last
significant bit (LSB) is transmitted last.
Addressing. The chip address of
DS1621 comprises internal preset code nibble 1001 (binary) followed by externally
configurable address pins/bits A2, A1 and

Capacitors:
C1
- 470F, 25V electrolytic
capacitor
C2, C3, C4 - 0.1F ceramic disk
C5
- 10F, 16V electrolytic
capacitor
C6,C7
- 33pF ceramic capacitor
Miscellaneous:
Transformer - 230V AC primary to 0-9V,
250mA secondary
Crystal
- 12 MHz
LCD
- 161 LCD module
S1
- On/Off SPST switch

DS1621 is the temperature sensor chip. The


microcontroller unit (MCU) reads the temperature from the sensor. The temperature
data is compared with certain user-defined
temperature values and processed inside
the MCU as per the program and then sent
to the LCD for display.
Fig. 2 shows the circuit of temperature
indicator using microcontroller AT89C52.
Working of each section of the circuit is
covered in the following paragraphs.
Power supply. The power supply unit
consists of a step-down transformer (230V
AC primary to 0-9V, 250mA secondary),
bridge rectifier and voltage regulator. The
output of the transformer is fed to bridge rectifier diodes D1 through D4 (each 1N4007).
The ripple from the output bridge rectifier is
filtered by capacitor C1 and fed to regulator
IC 7805. The regulated output is given to
the temperature sensor, microcontroller unit
and LCD module, respectively.
When switch S1 is closed, LED1 glows
to indicate the presence of power in the
system.
Temperature sensor. Temperature sensor chip DS1621 (IC3) is an 8-pin DIP IC.
Its pin details are shown in Fig. 3 and the
internal block diagram in Fig. 4. The chip
can measure temperatures from 55C to
+125C in 0.5C increments, which are
read as 9-bit values. It can operate off 2.7V
to 5.5V. Data is read/written via a 2-wire
serial interface. Pins 1 and 2 of the temperature IC are connected to pins 11 and 10 of
the microcontroller, respectively.

Fig. 4: Internal block diagram of IC DS1621

Fig. 5: Pin details of IC AT89C52

active until the temperature drops below


user-defined low temperature TL. User-defined temperature settings are stored in the
non-volatile memory. Temperature settings

A0. The eighth bit of the address byte is


determined by the type of operation (either
read or write) that is to be performed. For
writing to the device the eighth bit is 0
and for reading from the device the eighth
bit is 1. In our case, A2, A1 and A0 pins
are grounded and hence the device address
for writing is 1001000b or 90(hex) and for
reading the device address is 10010001b
or 91(hex).
Configuration/status register. This
register can be accessed for reading or
writing by issuing command byte AC(hex)
from the master (82C52). This register is
particularly required if DS1621 is used for
thermostat control, since it contains flag
bits THF (high-temperature flag) and TLF
(low-temperature flag) which are set to
1 when temperature crosses the respective limits earlier written into TH and
TL registers. It also contains the flag bit
(Done), which is set to 1 when results
of conversion are available after issuing
of start conversion command EE(hex).
The other bits of configuration register are
defined below:
NVB is the non-volatile memory busy
July 2004

electronics for you

construction
flag, 1 is write to an E2 memory cell in
progress, 0 indicates that non-volatile
memory is not busy, POL is non-volatile
output polarity bit (1=active-high and

0=active-low) and 1SHOT is one-shot


mode. A copy to E2 may take up to 10 ms.
If 1SHOT is 1, DS1621 will perform one
temperature conversion upon reception
of the Start Convert T protocol. If 1SHOT
is 0, DS1621 will continuously perform

temperature conversions. This bit is nonvolatile.


Command Set. Complete command
instruction set for accessing various internal
registers as well as for starting and
stopping of conversion process are
given in Table I. For understanding
the exact sequence in which Start
bit, address byte, acknowledgement bit,
command byte(s) and data byte(s) are to
be sent along the I2C bus, please refer to the
datasheet of DS1621, wherein these aspects
have been explained in proper detail. This
will help in understanding the contents of

Fig. 6: Solder-side PCB layout for temperature indicator


using AT8952

Fig. 7: Component layout for the PCB

Table I
DS1621 Command Set
Instruction

Description

Protocol

Read Temperature

Read Counter
Read Slope
Start Convert T
Stop Convert T
Access TH

Access TL

Access Configuration

Reads last converted temperature value from


temperature register.
Reads value of count remaining from counter.
Reads value of the slope accumulator.
Initiates temperature conversion.
Halts temperature conversion.
Reads or writes high temperature limit value into
TH register.
Reads or writes low temperature limit value into
TL register.
Reads or writes configuration data to configuration
register.

Aah

electronics for you

July 2004

A8h
A9h
EEh
22h
A1h
A2h
ACh

the main program.


Microcontroller unit. Microcontroller
AT89C52 (IC2) is a 40-pin IC from Atmel.
Its pin details are shown in Fig. 5. Like
AT89C51, it also belongs to the 8031/8051
family. Microcontroller AT89C52 has a
2568-bit internal random-access memory
(RAM), eight interrupt sources and 8 kB of
flash memory compared to 128x8-bit internal RAM, six interrupt sources and 4 kB of
flash memory in AT89C51. By combining
a versatile 8-bit CPU with flash memory
on a monolithic chip, Atmel AT89C52 is a
powerful, highly flexible and cost-effective
solution to many embedded control applications.
Ports 0 and 2 are 8-bit
bidirectional input/output
(I/O) ports. These ports
havent been used in this
temperature indicator.
Port 1 is an 8-bit bidirectional I/O port with internal pull-ups. Ports 1.0
through 1.7 are connected
to pins 7 through 14 of
the LCD. Port-1 output
buffers can sink/source
four TTL inputs.
Port 3 is an 8-bit bidirectional I/O port with
internal pull-ups. Ports
3.0 and 3.1 of IC2 are
connected to serial clock
line (SCL) and serial data
line (SDA) of IC3, respectively. Ports 3.2 through
3.4 are connected to pins
4 through 6 of the LCD,
respectively. Port-3 output
buffers can sink/source
four TTL inputs.
A 12MHz crystal oscillator is connected to XTAL1
and XTAL2 pins for operation of the microcontroller. A high pulse on RST pin (pin 9)
while the oscillator is running resets the
microcontroller. In this circuit, this pin is
connected to +Vcc through capacitor C5
(10 F, 16V). The external-access enable
pin (EA) is connected to +Vcc for internal
program executions. This pin also receives
the 12V programming-enable voltage (VPP)
during flash programming when 12V programming is selected.

The program
The C-language program for microcontroller AT89C52 is compiled using crosscompiler C51 Version 7.10 from Keil Soft-

construction
ware. The demo version of this compiler
is available for free on the Website www.
keil.com. It can compile programs up to
2 kB only, which is sufficient for writing
most programs.
For testing the display, the program
Hello.c is given here. This program, when
loaded to AT89C52, displays Hello! How
R U? on the LCD. The Hello.c program
has nothing to do with temperature. It
just guarantees a perfect communication
between the LCD and the microcontroller.
For temperature indication, the program
Temp52.c is used. The programs Hello.c
and Temp52.c, along with the hex files, are
given at the end of this article.
The communication interface between the temperature sensor and the
microcontroller chip follows the I2C (Inter Integrated Circuit) standard, which
is implemented in C here. I 2C is a
simple master/slave type interface.
Simplicity of the IC system is primarily
due to the bidirectional 2-wire (SDA and
SCL) design and the protocol format.
Bidirectional communication is through
2-wire lines (which are either activelow or passive-high). In the program,
the i2c_stop, i2c_start, i2c_write and
i2c_read functions are used for communicating Clock and Data from DS1621 to
P3.0 and P3.1 of AT89C52, respectively.
Such functions as command, ready and
display in the program are used for driving the LCD.
Program compilation for 8051 family
controller. Keil C51 can compile C programs for most of the Atmel family microcontrollers. It also supports other devices.
Unlike other cross-compilers (Hi-Tech, IAR,
SDCC, etc), Keil C51 offers such features as
fast code generation, strong multitasking
environment, real-time operating system
and inbuilt code optimisation. To enjoy
these features, youll need full version of
the compiler.
Keil C51 has options to generate
Assembly code and all the code listing
supported by 8051 family, but Assembly
language generated cannot be recompiled
on any other assembler. As far as code
generation is concerned, it uses minimum RAM and on-chip flash, allowing
faster and optimised program in IntelHex format, which can be loaded to the
microcontroller using any programmer.
Conversion of C program into Intel-Hex
format takes only a few seconds. In fact,
you dont require all that long Assembly
program in order to generate the output
hex file.
electronics for you

July 2004

DOS mode. 1. Installation of Keil C51


automatically generates Keil folder in
your computers C drive.
Pin No. Functions
2. Go to C:\Keil\C51\Bin folder
inside Keil folder.
Pin 1
Ground (Gnd)
3. Copy Hello.c into Bin folder.
Pin 2
+Vcc
4. Copy Regx52.h from C:\Keil\
Pin 3
V0 (display intensity control)
C51\Inc\Atmel folder into C:\Keil\C51\
Pin 4
RS (connected to P3.2 of AT89C52)
Pin 5
R/W (connected to P3.3 of AT89C52) Bin folder.
5. Type c51 Hello.c against the
Pin 6
EN (connected to P3.4 of AT89C52)
prompt and press Enter key.
Pin 7
D0 (connected to P1.0 of AT89C52)
6. Type bl51 Hello.obj. This comPin 8
D1 (connected to P1.1 of AT89C52)
mand is used for linking the Hello.obj
Pin 9
D2 (connected to P1.2 of AT89C52)
file created by Keil C51.
Pin 10
D3 (connected to P1.3 of AT89C52)
7. Type oh51 Hello. This command
Pin 11
D4 (connected to P1.4 of AT89C52)
is used for creating the hex file.
Pin 12
D5 (connected to P1.5 of AT89C52)
Windows mode. 1. Installation of
Pin 13
D6 (connected to P1.6 of AT89C52)
Keil
C51 software automatically creates
Pin 14
D7 (connected to P1.7 of AT89C52)
the icon Keil uVision2 on the desktop.
Pin 15
Backlight +Vcc (not used)
2. Double-click Keil uVision2.
Pin 16
Backlight Gnd (not used)
3. Suppose you have kept Hello.c
under
C:\Windows\Desktop\Hello folder.
LCD
Open Hello.c from the File menu.
For display, a Lampex make 16x1 LCD
4. From the menu bar, select Project/
(model GDM1601A) was used. Pin conNew Project. Name the new project and
nections of this LCD are given in Table II.
save it with extension .uv2.
Pins 15 and 16 havent been used. Pin 3 is
5. Select CPU as Atmel/AT89C52.
connected to the circuit ground through a
6. Choose Yes in the option Copy stan1-kilo-ohm preset that is used to control
dard 8051 code to current project folder.
the light intensity of the LCD. Note that the
7. Choose View/Project Window. A
Hitachi make 161 LCD (HD44780A00)
Project Workspace window appears.
will not work in this project.
8. Double-click Target 1.
9. Right-click Source Group1 and select
Add files to Group Source Group1.
Construction
A window appears.
The circuit of this temperature indicator
10. Add Hello.c and close this winusing microcontroller AT89C52 can be
dow.
assembled on any general-purpose, single11. Double-click Source Group1 on
side PCB. The microcontroller chip and the
the Project Workspace window. Now the
temperature sensor chip are mounted on
file name Hello.c appears.
the respective IC bases. Ensure a proper
12. From Project menu, select Opcontact between pins of the IC bases and
tions for File Hello.c. In Properties,
the solder points on the PCB. Capacitors
choose file type as C source file.
C3 and C4 must be connected near IC2
13. Again from Project menu, select
and IC3, respectively. The actual-size,
Options for Target Target1. A screen
single-side PCB layout for the circuit and
appears.
its component layout are shown in Figs 6
14. Choose Output and tick on Hex
and 7, respectively.
File for generating the hex file. Again
choose Listing option and tick on Conditional and Assembly Code.
Program compilation
15. Open the Project menu and select
After youve installed Keil C51 in your PC,
Build Target or press F7. The compiler
you can compile C program and genershows Hello 0 Error(s), 0 Warning(s)
ate hex file in either DOS or Windows
in the output window just below the projmode. Here, program compilation for the
ect window.
program Hello.c has been explained. The
16. Close the screen and go to the
same procedure is to be followed for the
Hello folder to see the generated hex file
temperature indication program Temp52.
and listing file.
c. For more example programs, refer to the
Load the hex file into the microcondirectory in your hard drive where Keil is
troller chip using a programmer. (Here
installed in the example folder.
weve used Atmel Flash Programmer from
Table II
Pin Connections of the LCD

construction
Frontline Electronics.) Now integrate the
microcontroller chip into the populated
PCB comprising the temperature sensor
and the LCD module.

Troubleshooting
1. Check the COM port on your PC before
programming.

2. In case there is no message even if


all the connections are correct, adjust the
intensity control potentiometer (VR1) for
display.
3. Check whether your hex file
matches with the hex file given below in
the article.
4. If the LCD shows wrong characters,
replace it with another make LCD.

5. If DS1621 is not connected properly


to AT89C52, the display will be completely
blank.
EFY Lab note. All the source
codes and relevant files of this article have been included in this months
EFY-CD.

temp52.c
/* Written By: Aditya Rane
T.E Computer Engg, Lokmanya Tilak College of Engineering, New Bombay, Vashi
E-mail: aditya@orionengg.com
Program for temperature indicator compiled under keil
'C' */
#include<stdio.h>
#include<string.h>
#include<Regx52.h>
//-----------------------------------------------------------------------//Global Variable
//------------------------------------------------------------------------int temperature;
#define
HIGH 0x01 // Active High Signal
#define
LOW 0x00 // Active Low Signal
#define
TRUE 0x01 // Active High State
#define
FALSE 0x00 // Active Low State
//------------------------------------------------------------------------// Functions Prototyping
//------------------------------------------------------------------------void ready (void);
void command (int);
void display (char *);
void i2c_stop (void);
void i2c_start (void);
void i2c_write (unsigned char);
unsigned char i2c_read (void);
void convert (unsigned char);
//------------------------------------------------------------------------// Port Defination
//------------------------------------------------------------------------#define
DATA P3_1
// Serial data
#define
CLOCK P3_0 // Serial clock
//Begining of Main Program
void main (void)
{
int tmp;
char str[16];
bit flag = FALSE;
unsigned char ch;
void command (int);
void display (char *);
command(0x3c);
command(0x0c);
command(0x06);
while(1)
{

i2c_start();

i2c_write(0x90);

i2c_write(0xEE);

i2c_stop();

i2c_start();

i2c_write(0x90);

i2c_write(0xAA);

i2c_start();

i2c_write(0x91);

ch = i2c_read();

i2c_stop();

temperature = 0;

convert(ch);

if(flag == FALSE)

{

flag = TRUE;


}

else

{

tmp = temperature;

if(tmp != temperature)
{


tmp = temperature;

sprintf(str,"%d%s
",temperature," Centigrade");

command(0x01);

command(0x80);

display(str);

}

}
}
}
//Delay Servive Routine
void delay_time (void)
{
unsigned int i;
for(i=0;i<100;i++);
}
//I2C Start Function
void i2c_start (void)
{
DATA = HIGH;
delay_time();
CLOCK = HIGH;
delay_time();
DATA = LOW;
CLOCK = LOW;
}
//I2C Stop Function
void i2c_stop (void)
{
unsigned char i;
CLOCK = LOW;
DATA = LOW;
CLOCK = HIGH;
delay_time();
DATA = HIGH;
i = DATA;
}
//I2C Data Write Function
void i2c_write (unsigned char j)
{
unsigned char i;
for(i=0;i<8;i++)
{

DATA = ((j & 0x80) ? 1 : 0);

j <<= 1;

CLOCK = HIGH;

delay_time();

CLOCK = LOW;

}
i = DATA;
CLOCK = HIGH;
delay_time();
CLOCK = LOW;

}
//I2C Data Read Function
unsigned char i2c_read (void)
{
unsigned char i,j;
j = 0;
i = DATA;
for(i=0;i<8;i++)
{

j <<= 1;

CLOCK = HIGH;

j |= DATA;

delay_time();

CLOCK = LOW;

}

return j;
}
//Binary to Decimal Conversion Function
void convert (unsigned char ch)
{
char x;
unsigned char arr[8]={128,64,32,16,8,4,2,1};
if(((ch & 0x80) ? 1 : 0)==0)
{

for(x=0;x<8;++x)

{

if(((ch & 0x80) ? 1 : 0))


temperature = temperature +
arr[x] * ((ch & 0x80) ? 1 : 0);

ch <<= 1;

}
}
else
{

ch=~ch;

ch=ch+1;

for(x=0;x<8;++x)

{

if(((ch & 0x80) ? 1 : 0))

temperature = temperature +
arr[x] * ((ch & 0x80) ? 1 : 0);

ch <<= 1;

}

temperature=-temperature;
}
}
//Display Ready Check Function
void ready (void)
{
P3_4 = 0x00;
P1 = 0xff;
P3_2 = 0x00;
P3_3 = 0x01;
while(P1_7)
{

P3_4 = 0x00;

P3_4 = 0x01;
}
P3_4 = 0x00;
}
//Display Command Function void command (int a)
{
ready();
P1 = a;
P3_2 = 0x00;
P3_3 = 0x00;
P3_4 = 0x01;
P3_4 = 0x00;
}
//Display Write Function
void display (char *str)
{
unsigned int i;
for(i=0;i<=strlen(str)-1;++i)
{

if(i == 8)

command(0xc0);

if(i == 16)

command(0x80);

ready();

P1 = str[i];

P3_2 = 0x01;

P3_3 = 0x00;

P3_4 = 0x01;

P3_4 = 0x00;
}
}
July 2004

electronics for you

construction

temp52.hex
:100F270025642573002043656E7469677261646583
:090F3700008040201008040201B2
:100DA800C2007F3C7E00120F9A7F0C7E00120F9AC1
:100DB8007F067E00120F9A120F8B7F90120EB87F5B
:100DC800EE120EB8120F6A120F8B7F90120EB87FB8
:100DD800AA120EB8120F8B7F91120EB8120F098F3C
:100DE80034120F6AE4F508F509AF34120CF220004A
:100DF8000AD20085082285092380BCE5236509708D
:100E080004E522650860B08508228509237538FF46
:100E180075390F753A2785083B85093C753DFF757F
:100E28003E0F753F2C7B007A00792412085C7F0105
:100E38007E00120F9A7F807E00120F9A7B007A0044
:080E48007924120E50020DBFC7
:0E0F7C00E4FFFE0FBF00010EEF64644E70F53F
:010F8A002244
:0F0F8B00D2B1120F7CD2B0120F7CC2B1C2B02211
:100F6A00C2B0C2B1D2B0120F7CD2B1A2B1E433F591
:010F7A003541
:010F7B002253
:020EB800AD0784
:100EBA00E4FCED30E703D38001C392B1ED25E0FDF8
:100ECA00D2B0120F7CC2B00CBC08E7A2B1E433FC6A
:070EDA00D2B0120F7CC2B080
:010EE10022EE
:100F0900E4FDA2B1E4FCED25E0FDD2B0A2B1E433E9
:0D0F19004205120F7CC2B00CBC08EBAF0506
:010F260022A8
:020CF2008F353C
:100CF40078377C007D007BFF7A0F79387E007F088F
:100D0400120C2CE53530E7047F0180027F00EF7080
:100D140041F536E53530E7047F0180027F00EF605E
:100D24002374372536F8E6FD7C00E5357E0030E790
:100D3400047F0180027F00120C7FEF2509F509EE84
:100D44003508F508E53525E0F5350536E536B4080A
:100D5400C2226335FF0535E4F536E53530E7047F17
:100D64000180027F00EF602374372536F8E6FD7CAE
:100D740000E5357E0030E7047F0180027F00120C1D
:100D84007FEF2509F509EE3508F508E53525E0F589
:100D9400350536E536B408C2C3E49509F509E4958A
:030DA40008F50847
:010DA7002229
:100F4000C2B47590FFC2B2D2B3309706C2B4D2B465
:050F500080F7C2B4228D
:0E0F9A00120F408F90C2B2C2B3D2B4C2B422C2
:060E50008B358A3689375C
:100E5600E4F538F539AB35AA36A937120F55EF2424
:100E6600FFFFEE34FFFED3E5399FE5389E5042E59D
:100E7600396408453870067FC0FE120F9AE539645A
:100E860010453870067F80FE120F9A120F40AB3560

:100E9600AA36A937853982853883120C52F590D245
:100EA600B2C2B3D2B4C2B40539E53970A8053880E8
:010EB600A497
:010EB7002218
:03000000020FA844
:0C0FA800787FE4F6D8FD758148020DA8A2
:100B5C00E709F608DFFA8046E709F208DFFA803E7B
:100B6C0088828C83E709F0A3DFFA8032E309F60868
:100B7C00DFFA8078E309F208DFFA807088828C83D0
:100B8C00E309F0A3DFFA806489828A83E0A3F60884
:100B9C00DFFA805889828A83E0A3F208DFFA804C5E
:100BAC0080D280FA80C680D4806980F28033801035
:100BBC0080A680EA809A80A880DA80E280CA80339E
:100BCC0089828A83ECFAE493A3C8C582C8CCC58316
:100BDC00CCF0A3C8C582C8CCC583CCDFE9DEE780E6
:100BEC000D89828A83E493A3F608DFF9ECFAA9F065
:100BFC00EDFB2289828A83ECFAE0A3C8C582C8CCBB
:100C0C00C583CCF0A3C8C582C8CCC583CCDFEADED3
:100C1C00E880DB89828A83E493A3F208DFF980CC35
:100C2C0088F0EF60010E4E60C388F0ED2402B4042E
:100C3C000050B9F582EB2402B4040050AF232345D5
:060C4C008223900BAC7343
:100C5200BB010CE58229F582E5833AF583E0225057
:100C620006E92582F8E622BBFE06E92582F8E222A1
:0D0C7200E58229F582E5833AF583E49322BB
:100C7F00EF8DF0A4A8F0CF8CF0A428CE8DF0A42E89
:020C8F00FE2243
:10080000E5442438F8E60544227835300802783883
:10081000E475F001120CBC020C912001EB7F2ED28A
:10082000018018EF540F2490D43440D4FF30050BCE
:10083000EF24BFB41A0050032461FFE545600215A0
:10084000450548E5487002054730080D7835E475E0
:10085000F001120CBCEF020CAA020EE27403D208E3
:100860008003E4C208F5448B358A368937E4F545C0
:10087000F547F548E54560077F2012083B80F57590
:1008800046FFC202C201C203C204C206C207C209B5
:10089000120809FF700D3008057F0012084CAF48A0
:1008A000AE4722B4255FC2D5C205120809FF24D085
:1008B000B40A00501A75F00A784530D50508B6FF1D
:1008C0000106C6A426F620D5047002D20480D924DD
:1008D000CFB41A00EF5004C2E5D205020A4CD2028E
:1008E00080C6D20180C0D20380BCD2D580BAD206E5
:1008F00080B47F2012083B2003077401B5450040F7
:10090000F1120800FF12083B020874D209D20780D6
:1009100095120800FB120800FA120800F94A4B7001
:1009200006791D7A0B7BFF20032EE545602A7E00A9
:100930008E82758300120C5260060EEE654670F0D2
:10094000C2D5EBC0E0EAC0E0E9C0E0EE120A93D005

:10095000E0F9D0E0FAD0E0FB120C91FF60AAEBC006
:10096000E0EAC0E0E9C0E012083BD0E02401F9D0A1
:10097000E03400FAD0E0FBE5460460DCD546D980DF
:10098000877BFF7A0A798FD203809C791080027965
:1009900008C207C2098008D2D5790A8004790AC240
:1009A000D5E546047002F546E4FAFDFEFF120800A4
:1009B000FC7B08200213120800FD7B1030010A1294
:1009C0000800FE120800FF7B20EC3382D592D55040
:1009D00013C3E43001069FFFE49EFEE42002039D62
:1009E000FDE49CFCE4CBF8C202EC700CCFCECDCC85
:1009F000E824F8F870F38017C3EF33FFEE33FEED11
:100A000033FDEC33FCEB33FB994002FB0FD8E9EBF1
:100A1000300205F8D0E0C448B202C0E00AEC4D4E06
:100A20004F78207B0070C2EAB5460040BCC0E0129F
:100A30000A95D0F0D0E0200204C4C0E0C4B202C0E5
:100A4000F0120824D0F0D5F0EB020874120CCC0997
:100A50001153098B5808E24C08DE42098F4F099761
:0F0A60004409974908F743099D550981460981C3
:100A6F00450981470B3D5008E62D08EA2E090D2B4D
:100A7F0008EE23090B200B262A08A64800000905BB
:100A8F003F3F3F00790AA2D5200414300609B91060
:100A9F00020404B9080104A2D52007025001042062
:100AAF0003689203B545005034C0E07F203004192D
:100ABF007F30A20372077206500F120AECC203C2F4
:100ACF0007C206C2097F30800F300603E9C0E0126B
:100ADF00083B300603D0E0F9D0E0B545CC3006171F
:100AEF007F30B9100C12083B7F583005077F788094
:100AFF0003B9080312083B3003057F2D02083B7F23
:100B0F00202009F87F2B2007F322920380CF286E35
:100B1F00756C6C2900D2021208003002F8C20278FC
:100B2F004530D50108F60208A62D50434958120842
:100B3F00002403B405004001E4900B389312082CF5
:0D0B4F00743A12082CD20475450402098B7B
:100F5500E4FFFE120C91600C0FEF70010E09E970B1
:050F6500F20A80EF22FA
:100C9100BB010689828A83E0225002E722BBFE0261
:090CA100E32289828A83E4932294
:100CAA00BB010689828A83F0225002F722BBFE0129
:020CBA00F32223
:100CBC00FAE6FB0808E6F925F0F618E6CA3AF62239
:100CCC00D083D082F8E4937012740193700DA3A3B7
:100CDC0093F8740193F5828883E4737402936860CB
:060CEC00EFA3A3A380DFCB
:100EE200EFB40A07740D120EED740A309811A89926
:100EF200B8130CC2983098FDA899C298B811F63070
:070F020099FDC299F5992247
:00000001FF

Hello.c
#include<stdio.h>
#include<string.h>
#include<Regx52.h>

ready();
P1=a;
P3_2=0x00;
P3_3=0x00;
P3_4=0x01;
P3_4=0x00;
}

void ready(void);
void command(int);
void display(char *);
void main (void)
{
command(0x3c);
command(0x0c);
command(0x06);
command(0x01);
command(0x80);
display("Hello! How R U ?");
while(1);
}



}
}

void display(char *str)


{
unsigned int i;
for(i=0;i<=strlen(str)-1;++i)
{

if(i == 8)

command(0xc0);

if(i == 16)

command(0x80);

ready();

P1 = str[i];

P3_2 = 0x01;

P3_3 = 0x00;

void command(int a)
{
void ready(void);

P3_4 = 0x01;
P3_4 = 0x00;

void ready(void)
{
P3_4=0x00;
P1=0xff;
P3_2=0x00;
P3_3=0x01;
while(P1_7)
{

P3_4=0x00;

P3_4=0x01;
}
P3_4=0x00;
}

Hello.hex
:0300000002092AC8
:0C092A00787FE4F6D8FD75810E0208AE5F
:1009190048656C6C6F2120486F7720522055203F25
:0109290000CD
:1008AE007F3C7E001209067F0C7E001209067F0631
:1008BE007E001209067F017E001209067F807E00EF
:0E08CE001209067BFF7A09791912080080FED4
:100906008E0D8F0E1208DC850E90C2B2C2B3D2B421
:03091600C2B42246
:060800008B088A09890A39
electronics for you

July 2004

:10080600E4F50BF50CAB08AA09A90A1208F1EF24C6
:10081600FFFFEE34FFFED3E50C9FE50B9E5042E54D
:100826000C6408450B70067FC0FE120906E50C64D1
:1008360010450B70067F80FE1209061208DCAB0815
:10084600AA09A90A850C82850B83120868F590D23D
:10085600B2C2B3D2B4C2B4050CE50C70A8050B80C5
:01086600A4ED
:01086700226E
:1008DC00C2B47590FFC2B2D2B3309706C2B4D2B4D0
:0508EC0080F7C2B422F8

:10086800BB010CE58229F582E5833AF583E0225045
:1008780006E92582F8E622BBFE06E92582F8E2228F
:0D088800E58229F582E5833AF583E49322A9
:1008F100E4FFFE120895600C0FEF70010E09E9701C
:05090100F20A80EF2264
:10089500BB010689828A83E0225002E722BBFE0261
:0908A500E32289828A83E4932294
:00000001FF

construction

SOURCE CODE FOR PROGRAMMABLE LOGIC CONTROLLER (PC BASED)


#include <conio.h>
#include <bios.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fstream.h>
#include <iostream.h>
#include <math.h>
#include <process.h>
#include <string.h>
#include <timer.h>
//Global Sub routines
void runprog();//Run program
void viewprog();//Display Program
void viewstat();//Display status
void forceout();//Force outputs
void helpu(); //Help
void exiter();//Exit to Dos
void openingmenu();//Opening menu
void paintscreen(int scrclr);//Paint screen
void border(); //Draw borders
void displaystat();//Display status
void displayhelp(char helpfilename[10]);//Display a
Help File
//Logic commands
void ser(int inputno);//Series contact
void par(int inputno);//Parallel Contacts
void ope(int outputno);//Output
void opl(int outputno);//Output latch
void opu(int outputno);//Output unlatch
int tmr(int tno,int timeset);//Timer
int ctu(int cno,int countset);//Counter Up
int ctd(int cno,int countset);//Counter Down
int rto(int hourr,int minn);//Real Time Output
//Read Inputs And Energise Outputs
void inputread();//Read input port
void setoutputs();//Set outputs
//Global variables
//Inputs and Outputs Image table
int I1,I2,I3,I4,I5,O1,O2,O3,O4,O5,O6,O7,O8;
int NI1,NI2,NI3,NI4,NI5,NO1,NO2,NO3,NO4,NO5,
NO6,NO7,NO8;
//Counters Up
int C1,C2,C3,C4,C5;
int CACC1,CACC2,CACC3,CACC4,CACC5;
int CDN1,CDN2,CDN3,CDN4,CDN5;
int risingedge;
//Counters Down
int CO1,CO2,CO3,CO4,CO5;
int COACC1,COACC2,COACC3,COACC4,COACC5;
int CODN1,CODN2,CODN3,CODN4,CODN5;
int cd1,cd2,cd3,cd4,cd5;
//Timers
int T1,T2,T3,T4,T5;
int TACC1,TACC2,TACC3,TACC4,TACC5;
int TEN1,TEN2,TEN3,TEN4,TEN5;
int TDN1,TDN2,TDN3,TDN4,TDN5;
int tmrscan1,tmrscan2,tmrscan3,tmrscan4,tmrscan5;
//Results Register
int res;
//Real Time Output
int RTDN;
struct time t;
class TTimer r;
//For the scan timer
double te,se;
time_t first1,second1,first2,second2,first3,second3,first4,
second4,first5,second5;
//Command And Parameter Register
int commno[1000];
int para1[1000],para2[1000];
//Start of main
void main()
{
openingmenu();
}
//Opening menu
electronics for you

July 2004

void openingmenu()
{
window(1,1,80,25);
clrscr();
paintscreen(9);
border();
textcolor(YELLOW);
textbackground(LIGHTBLUE);
gotoxy(25,3);
cprintf(==PC Based Logic Controller==);
gotoxy(30,4);
cprintf(Ver 1.0 (5 I/p,8 O/p));
textcolor(LIGHTGREEN);
gotoxy(25,6);
cprintf(== M A I N M E N U ==);
gotoxy(25,7);
cprintf(**********************);
textcolor(LIGHTRED);
gotoxy(25,9);
cprintf(F1 >- Run a Logic Program);
gotoxy(25,11);
cprintf(F2 >- View/Edit a Logic Program);
gotoxy(25,13);
cprintf(F3 >- View Input/Output Status);
gotoxy(25,15);
cprintf(F4 >- Force Outputs);
gotoxy(25,17);
cprintf(F5 >- Online Help);
gotoxy(25,19);
cprintf(F6 >- Exit);
USERCHOICE:
while(!kbhit())
{}
char userchoice=getch();
switch(userchoice)
{
case (char(59)):runprog();break;
case (char(60)):viewprog();break;
case (char(61)):viewstat();break;
case (char(62)):forceout();break;
case (char(63)):helpu();break;
case (char(64)):exiter();break;
default:goto USERCHOICE;
}
}
//paints the screen with specified colour
void paintscreen(int scrclr)
{
textbackground(scrclr);
for(int x=0;x<82;++x)
{
for(int y=0;y<26;++y)
{
gotoxy(x,y);
cprintf( );
}
}
}
//Draws borders
void border()
{
int i;
textcolor(LIGHTRED);
for(i=3;i<80;i++)//top line
{
gotoxy(i,1);
cprintf(\xcd);
}
gotoxy(79,1);//top left curve
cprintf(\xbb);
for(i=2;i<25;i++)//Right line
{
gotoxy(79,i);
cprintf(\xba);
}
gotoxy(79,25);
cprintf(\xbc); //bottom right curve
for(i=2;i<25;i++)//left line

{
gotoxy(2,i);
cprintf(\xba);
}
gotoxy(2,25);
cprintf(\xc8);//left bottom curve
for(i=3;i<79;i++)
{
gotoxy(i,25);
cprintf(\xcd);
}
gotoxy(2,1);
cprintf(\xc9);//left bottom curve
}
//Run a Logic program
void runprog()
{
char logicfile[15];
char comm[3],in;
int count;
char commn[3];
int par1,par2,par3;
int n,inpu;
int f;
fstream infile;
START:
window(1,1,80,25);
clrscr();
textcolor(YELLOW);
textbackground(LIGHTBLUE);
gotoxy(25,3);
cprintf(==LOGIC CONTROLLER==);
gotoxy(25,4);
cprintf(====================);
gotoxy(25,6);
cprintf(RUN A LOGIC PROGRAM FILE PAGE);
window(10,8,75,8);
textcolor(YELLOW);
clrscr();
textcolor(LIGHTCYAN);
cprintf( Enter the Logic File Name: (****.lgx) - );
scanf(%15s,&logicfile);
infile.open(logicfile,ios::in);
if(infile.fail())
{
window(10,8,70,9);
textcolor(LIGHTGREEN+BLINK);
clrscr();
cprintf(..Error Opening File or File Does not Exist../n/
rPress F3 to Exit,F4 to Renter);
ERROR:
while(!kbhit())
{}
char choice;
choice=getch();
switch(choice)
{
case(char (62)):goto START;break;
case(char(61)):openingmenu();break;
default:goto ERROR;
}
}
window(1,1,80,25);
clrscr();
paintscreen(4);
border();
textcolor(YELLOW);
textbackground(4);
gotoxy(5,3);
cprintf( Run Logic Controller File );
gotoxy(5,4);
cprintf(====================);
textcolor(LIGHTRED);
gotoxy(45,2);
cprintf(F1=StartESC- Exit);
gotoxy(10,6);
textcolor(LIGHTRED);
cprintf(Controller Inputs:);

construction
gotoxy(10,7);
cprintf(==================);
gotoxy(10,8);
cprintf(Input # 1(I1):);
gotoxy(10,9);
cprintf(Input # 2(I2):);
gotoxy(10,10);
cprintf(Input # 3(I3):);
gotoxy(10,11);
cprintf(Input # 4(I4):);
gotoxy(10,12);
cprintf(Input # 5(I5):);
gotoxy(10,13);
textcolor(CYAN);
cprintf(Controller Outputs);
gotoxy(10,14);
cprintf(==================);
gotoxy(10,15);
cprintf(Output # 1(O1):);
gotoxy(10,16);
cprintf(Output # 2(O2):);
gotoxy(10,17);
cprintf(Output # 3(O3):);
gotoxy(10,18);
cprintf(Output # 4(O4):);
gotoxy(10,19);
cprintf(Output # 5(O5):);
gotoxy(10,20);
cprintf(Output # 6(O6):);
gotoxy(10,21);
cprintf(Output # 7(O7):);
gotoxy(10,22);
cprintf(Output # 8(O8):);
textcolor(YELLOW);
gotoxy(40,3);
cprintf(Counter(UP) Preset ACC Done);
gotoxy(40,4);
cprintf(====================
=);
gotoxy(40,5);
cprintf(Counter #1);
gotoxy(40,6);
cprintf(Counter #2);
gotoxy(40,7);
cprintf(Counter #3);
gotoxy(40,8);
cprintf(Counter #4);
gotoxy(40,9);
cprintf(Counter #5);
textcolor(GREEN);
gotoxy(40,10);
cprintf(Counter(DOWN) Preset ACC Done);
gotoxy(40,11);
cprintf(====================
==);
gotoxy(40,12);
cprintf(Counter #1);
gotoxy(40,13);
cprintf(Counter #2);
gotoxy(40,14);
cprintf(Counter #3);
gotoxy(40,15);
cprintf(Counter #4);
gotoxy(40,16);
cprintf(Counter #5);
gotoxy(40,17);
textcolor(LIGHTRED);
cprintf(Timer Enable Preset ACC Done);
gotoxy(40,18);
cprintf(====================
==);
gotoxy(40,19);
cprintf(Timer #1);
gotoxy(40,20);
cprintf(Timer #2);
gotoxy(40,21);
cprintf(Timer #3);
gotoxy(40,22);
cprintf(Timer #4);
gotoxy(40,23);
cprintf(Timer #5);
gotoxy(40,24);

cprintf(RTO Timer Done:);


gotoxy(10,23);
textcolor(YELLOW);
cprintf(Last Scan(msec):);
gotoxy(10,24);
textcolor(LIGHTBLUE);
cprintf(File Name:);
gotoxy(21,24);
textcolor(LIGHTBLUE);
cprintf(%15s,logicfile);
//Starting To Read The File
count=0;
while(!infile.eof())
{
infile>>comm;
if(stricmp(comm,ser)==0){commno[count]=1;infile
>> para1[count];}
if(stricmp(comm,par)==0){commno[count]=2;infil
e>> para1[count];}
if(stricmp(comm,ope)==0){commno[count]=3;infil
e>> para1[count];}
if(stricmp(comm,opl)==0){commno[count]=4;infil
e>> para1[count];}
if(stricmp(comm,opu)==0){commno[count]=5;infil
e>> para1[count];}
if(stricmp(comm,tmr)==0){commno[count]=6;infile
>> para1[count];infile>>para2[count];}
if(stricmp(comm,ctu)==0){commno[count]=7;infile
>> para1[count];infile>>para2[count];}
if(stricmp(comm,ctd)==0){commno[count]=8;infile
>> para1[count];infile>>para2[count];}
if(stricmp(comm,rto)==0){commno[count]=9;infile
>> para1[count];infile>>para2[count];}
count=count+1;
}
infile.close();
//Running The Program
while(!kbhit())
{}
char choice;
choice=getch();
switch(choice)
{
case(char(59)):goto RUN;break;
case(char(27)):openingmenu();break;
}
RUN:
textcolor(YELLOW+BLINK);
_setcursortype(_NOCURSOR);
for(;;)
{
res=1;
inputread();
for(n=0;n<=count-1;n++)
{
r.Start();
switch(commno[n])
{
case 1:ser(para1[n]);break;
case 2:par(para1[n]);break;
case 3:ope(para1[n]);break;
case 4:opl(para1[n]);break;
case 5:opu(para1[n]);break;
case 6:tmr(para1[n],para2[n]);break;
case 7:ctu(para1[n],para2[n]);break;
case 8:ctd(para1[n],para2[n]);break;
case 9:rto(para1[n],para2[n]);break;
}
}
gotoxy(45,2);
cprintf(Press ESC TO Stop...);
gotoxy(70,2);
cprintf(Prog= RUN);
setoutputs();
r.Stop();
f=f+1;
if(f>=100)
{
f=0;
te=r.Time();

}
r.Reset();
displaystat();
if(kbhit())
{
if ((in = getch())==\x1B)break;
}
}
gotoxy(45,2);
cprintf(ESC =Stop and EXIT);
gotoxy(70,2);
cprintf(Prog=HALT);
while(!kbhit())
{}
choice=getch();
switch(choice)
{
case(char(59)):goto RUN;break;
case(char(27)):openingmenu();break;
default:openingmenu();
}
}
//View/Edit Program
void viewprog()
{
char lfile[15];
char* comm;
char tot[25];
comm=Edit ;
window(1,1,80,25);
clrscr();
paintscreen(4);
border();
textcolor(YELLOW);
textbackground(4);
gotoxy(20,4);
cprintf( View /Edit Controller File );
gotoxy(20,5);
cprintf(====================
==);
window(3,8,75,8);
textcolor(YELLOW);
clrscr();
textcolor(CYAN);
cprintf(Enter the Logic File Name (New or Existing):(????.
lgx) - );
scanf(%15s,&lfile);
strcat(tot,comm);
strcat(tot,lfile);
system(tot);
openingmenu();
}
//View I/P,O/P Status
void viewstat()
{
char in;
int inpu;
int inpuo;
window(1,1,80,25);
clrscr();
paintscreen(4);
border();
textcolor(YELLOW);
textbackground(4);
gotoxy(20,4);
cprintf( Logic Controller Input Output Status );
gotoxy(20,5);
cprintf(====================
==);
gotoxy(25,6);
cprintf(Press ESC TO Exit Status Screen);
gotoxy(20,8);
textcolor(LIGHTRED);
cprintf(Controller Inputs:);
gotoxy(20,9);
cprintf(==================);
gotoxy(20,11);
cprintf(Input # 1(I1):);
gotoxy(20,12);
cprintf(Input # 2(I2):);
gotoxy(20,13);
July 2004

electronics for you

construction
cprintf(Input # 3(I3):);
gotoxy(20,14);
cprintf(Input # 4(I4):);
gotoxy(20,15);
cprintf(Input # 5(I5):);
gotoxy(20,17);
textcolor(CYAN);
cprintf(Controller Outputs);
gotoxy(20,18);
cprintf(==================);
gotoxy(20,20);
cprintf(Output # 1(O1):);
gotoxy(20,21);
cprintf(Output # 2(O2):);
gotoxy(20,22);
cprintf(Output # 3(O3):);
gotoxy(20,23);
cprintf(Output # 4(O4):);
gotoxy(40,11);
cprintf(Output # 5(O5):);
gotoxy(40,12);
cprintf(Output # 6(O6):);
gotoxy(40,13);
cprintf(Output # 7(O7):);
gotoxy(40,14);
cprintf(Output # 8(O8):);
_setcursortype(_NOCURSOR);
for(;;)
{
//scan inputs
inpu=inp(0x379);
if ((inpu & 8)==0){gotoxy(35,11);textcolor(GREEN);
cprintf(ON );}
if ((inpu & 8)==8){gotoxy(35,11);textcolor(YELLOW);
cprintf(OFF);}
if ((inpu & 16)==0){gotoxy(35,12);textcolor(GREEN);
cprintf(ON );}
if ((inpu & 16)==16){gotoxy(35,12);textcolor(YELLOW);
cprintf(OFF);}
if ((inpu & 32)==0){gotoxy(35,13);textcolor(GREEN);
cprintf(ON );}
if ((inpu & 32)==32){gotoxy(35,13);textcolor(YELLOW);
cprintf(OFF);}
if ((inpu & 64)==0){gotoxy(35,14);textcolor(GREEN);
cprintf(ON );}
if ((inpu & 64)==64){gotoxy(35,14);textcolor(YELLOW);
cprintf(OFF);}
if ((inpu & 128)==128){gotoxy(35,15);textcolor(GREEN);
cprintf(ON );}
if ((inpu & 128)==0){gotoxy(35,15);textcolor(YELLOW);
cprintf(OFF);}
//scan outputs
inpuo=inp(0x378);
if ((inpuo & 1)==1){gotoxy(36,20);textcolor(GREEN);
cprintf(ON );}
if ((inpuo & 1)==0){gotoxy(36,20);textcolor(YELLOW);
cprintf(OFF);}
if ((inpuo & 2)==2){gotoxy(36,21);textcolor(GREEN);
cprintf(ON );}
if ((inpuo & 2)==0){gotoxy(36,21);textcolor(YELLOW);
cprintf(OFF);}
if ((inpuo & 4)==4){gotoxy(36,22);textcolor(GREEN);
cprintf(ON );}
if ((inpuo & 4)==0){gotoxy(36,22);textcolor(YELLOW);
cprintf(OFF);}
if ((inpuo & 8)==8){gotoxy(36,23);textcolor(GREEN);
cprintf(ON );}
if ((inpuo & 8)==0){gotoxy(36,23);textcolor(YELLOW);
cprintf(OFF);}
if ((inpuo & 16)==16){gotoxy(56,11);textcolor(GREEN);
cprintf(ON );}
if ((inpuo & 16)==0){gotoxy(56,11);textcolor(YELLOW);
cprintf(OFF);}
if ((inpuo & 32)==32){gotoxy(56,12);textcolor(GREEN);
cprintf(ON );}
if ((inpuo & 32)==0){gotoxy(56,12);textcolor(YELLOW);
cprintf(OFF);}
if ((inpuo & 64)==64){gotoxy(56,13);textcolor(GREEN);
cprintf(ON );}
if ((inpuo & 64)==0){gotoxy(56,13);textcolor(YELLOW);
cprintf(OFF);}
if ((inpuo & 128)==128){gotoxy(56,14);textcolor(GREEN);
electronics for you

July 2004

cprintf(ON );}
if ((inpuo & 128)==0){gotoxy(56,14);textcolor(YELLOW);
cprintf(OFF);}
if(kbhit())
{
if ((in = getch())==\x1B)break;
}
}
openingmenu();
}
//Force Outputs
void forceout()
{
char cho;
char in;
window(1,1,80,25);
clrscr();
paintscreen(4);
border();
textcolor(YELLOW);
textbackground(4);
gotoxy(25,3);
cprintf( Force Controller Outputs );
gotoxy(25,4);
cprintf(====================
==);
gotoxy(10,8);
cprintf(Output # 1- F1);
gotoxy(10,9);
cprintf(Output # 2- F2);
gotoxy(10,10);
cprintf(Output # 3- F3);
gotoxy(10,11);
cprintf(Output # 4- F4);
gotoxy(10,12);
cprintf(Output # 5- F5);
gotoxy(10,13);
cprintf(Output # 6- F6);
gotoxy(10,14);
cprintf(Output # 7- F7);
gotoxy(10,15);
cprintf(Output # 8- F8);
gotoxy(30,7);
cprintf(Output Status>>);
gotoxy(30,18);
cprintf(ESC=Exit to Main..);
_setcursortype(_NOCURSOR);
for(;;)
{
while(!kbhit())
{
textcolor(LIGHTRED);
outp(0x378,0);
gotoxy(35,8);
cprintf(OFF);
gotoxy(35,9);
cprintf(OFF);
gotoxy(35,10);
cprintf(OFF);
gotoxy(35,11);
cprintf(OFF);
gotoxy(35,12);
cprintf(OFF);
gotoxy(35,13);
cprintf(OFF);
gotoxy(35,14);
cprintf(OFF);
gotoxy(35,15);
cprintf(OFF);
}
cho=getch();
textcolor(GREEN);
if(cho==(char(59))){outp(0x378,1);gotoxy(35,8);cp
rintf( ON);};
if(cho==(char(60))){outp(0x378,2);gotoxy(35,9);cp
rintf( ON);};
if(cho==(char(61))){outp(0x378,4);gotoxy(35,10);cp
rintf( ON);};
if(cho==(char(62))){outp(0x378,8);gotoxy(35,11);cp
rintf( ON);};
if(cho==(char(63))){outp(0x378,16);gotoxy(35,12);c

printf( ON);};
if(cho==(char(64))){outp(0x378,32);gotoxy(35,13);c
printf( ON);};
if(cho==(char(65))){outp(0x378,64);gotoxy(35,14);c
printf( ON);};
if(cho==(char(66))){outp(0x378,128);gotoxy(35,15);
cprintf( ON);};
if(cho==(char(27))){openingmenu();};
delay(100);
}
}
//Help Utility
void helpu()
{
window(1,1,80,25);
clrscr();
paintscreen(9);
border();
textbackground(LIGHTBLUE);
textcolor(YELLOW);
gotoxy(25,5);
cprintf(==M A I N H E L P M E N U==);
gotoxy(25,6);
cprintf(++++++++++++++++++++
+);
textcolor(LIGHTRED);
gotoxy(25,8);
cprintf(F1 >- Help on Running a Logic Program);
gotoxy(25,10);
cprintf(F2 >- Help on Viewing/Editing a Logic Program);
gotoxy(25,12);
cprintf(F3 >- Help on Viewing Input/Output Status);
gotoxy(25,14);
cprintf(F4 >- Help on Force Outputs);
gotoxy(25,16);
cprintf(F5 >- About This Program);
gotoxy(25,18);
cprintf(ESC >- Return to Main);
HELPCHOICE:
while(!kbhit())
{}
char userchoiceh=getch();
switch(userchoiceh)
{
case (char(59)):{textcolor(LIGHTRED);displayhelp(
HELPS.PG1);}break;
case (char(60)):{textcolor(LIGHTGREEN);displayhelp
(HELPS.PG2);}break;
case (char(61)):{textcolor(LIGHTCYAN);displayhelp
(HELPS.PG3);}break;
case (char(62)):{textcolor(YELLOW);displayhelp (HELPS.
PG4);}break;
case (char(63)):{textcolor(LIGHTMAGENTA);displayhelp(
HELPS.PG5);}break;
case (char(27)):openingmenu();break;
default:goto HELPCHOICE;
}
getch();
}
//Exit To DOS
void exiter()
{
window(1,1,80,25);
clrscr();
paintscreen(9);
border();
textcolor(YELLOW+BLINK);
textbackground(9);
gotoxy(25,10);
cprintf(Exiting to DOS.....);
delay(2000);
textcolor(YELLOW);
clrscr();
exit(1);
}
//Logic Controller Commands
//Series Contacts
void ser(int inputno)
{

construction
int addr;
switch(inputno)
{
case 1:addr=I1;break;
case 2:addr=I2;break;
case 3:addr=I3;break;
case 4:addr=I4;break;
case 5:addr=I5;break;
case 6:addr=NI1;break;
case 7:addr=NI2;break;
case 8:addr=NI3;break;
case 9:addr=NI4;break;
case 10:addr=NI5;break;
case 11:addr=O1;break;
case 12:addr=O2;break;
case 13:addr=O3;break;
case 14:addr=O4;break;
case 15:addr=O5;break;
case 16:addr=O6;break;
case 17:addr=O7;break;
case 18:addr=O8;break;
case 19:addr=NO1;break;
case 20:addr=NO2;break;
case 21:addr=NO3;break;
case 22:addr=NO4;break;
case 23:addr=NO5;break;
case 24:addr=NO6;break;
case 25:addr=NO7;break;
case 26:addr=NO8;break;
case 27:addr=CDN1;break;
case 28:addr=CDN2;break;
case 29:addr=CDN3;break;
case 30:addr=CDN4;break;
case 31:addr=CDN5;break;
case 32:addr=~CDN1;break;
case 33:addr=~CDN2;break;
case 34:addr=~CDN3;break;
case 35:addr=~CDN4;break;
case 36:addr=~CDN5;break;
case 37:addr=CODN1;break;
case 38:addr=CODN2;break;
case 39:addr=CODN3;break;
case 40:addr=CODN4;break;
case 41:addr=CODN5;break;
case 42:addr=~CODN1;break;
case 43:addr=~CODN2;break;
case 44:addr=~CODN3;break;
case 45:addr=~CODN4;break;
case 46:addr=~CODN5;break;
case 47:addr=TEN1;break;
case 48:addr=TEN2;break;
case 49:addr=TEN3;break;
case 50:addr=TEN4;break;
case 51:addr=TEN5;break;
case 52:addr=~TEN1;break;
case 53:addr=~TEN2;break;
case 54:addr=~TEN3;break;
case 55:addr=~TEN4;break;
case 56:addr=~TEN5;break;
case 57:addr=TDN1;break;
case 58:addr=TDN2;break;
case 59:addr=TDN3;break;
case 60:addr=TDN4;break;
case 61:addr=TDN5;break;
case 62:addr=~TDN1;break;
case 63:addr=~TDN2;break;
case 64:addr=~TDN3;break;
case 65:addr=~TDN4;break;
case 66:addr=~TDN5;break;
case 67:addr=RTDN;break;
case 68:addr=~RTDN;break;
}
res=res&addr;
}
//Parallel Contacts
void par(int inputno)
{
int addr;
switch(inputno)
{
case 1:addr=I1;break;
case 2:addr=I2;break;

case 3:addr=I3;break;
case 4:addr=I4;break;
case 5:addr=I5;break;
case 6:addr=NI1;break;
case 7:addr=NI2;break;
case 8:addr=NI3;break;
case 9:addr=NI4;break;
case 10:addr=NI5;break;
case 11:addr=O1;break;
case 12:addr=O2;break;
case 13:addr=O3;break;
case 14:addr=O4;break;
case 15:addr=O5;break;
case 16:addr=O6;break;
case 17:addr=O7;break;
case 18:addr=O8;break;
case 19:addr=NO1;break;
case 20:addr=NO2;break;
case 21:addr=NO3;break;
case 22:addr=NO4;break;
case 23:addr=NO5;break;
case 24:addr=NO6;break;
case 25:addr=NO7;break;
case 26:addr=NO8;break;
case 27:addr=CDN1;break;
case 28:addr=CDN2;break;
case 29:addr=CDN3;break;
case 30:addr=CDN4;break;
case 31:addr=CDN5;break;
case 32:addr=~CDN1;break;
case 33:addr=~CDN2;break;
case 34:addr=~CDN3;break;
case 35:addr=~CDN4;break;
case 36:addr=~CDN5;break;
case 37:addr=CODN1;break;
case 38:addr=CODN2;break;
case 39:addr=CODN3;break;
case 40:addr=CODN4;break;
case 41:addr=CODN5;break;
case 42:addr=~CODN1;break;
case 43:addr=~CODN2;break;
case 44:addr=~CODN3;break;
case 45:addr=~CODN4;break;
case 46:addr=~CODN5;break;
case 47:addr=TEN1;break;
case 48:addr=TEN2;break;
case 49:addr=TEN3;break;
case 50:addr=TEN4;break;
case 51:addr=TEN5;break;
case 52:addr=~TEN1;break;
case 53:addr=~TEN2;break;
case 54:addr=~TEN3;break;
case 55:addr=~TEN4;break;
case 56:addr=~TEN5;break;
case 57:addr=TDN1;break;
case 58:addr=TDN2;break;
case 59:addr=TDN3;break;
case 60:addr=TDN4;break;
case 61:addr=TDN5;break;
case 62:addr=~TDN1;break;
case 63:addr=~TDN2;break;
case 64:addr=~TDN3;break;
case 65:addr=~TDN4;break;
case 66:addr=~TDN5;break;
case 67:addr=RTDN;break;
case 68:addr=~RTDN;break;
}
res=res|addr;
}
// Outputs
void ope(int outputno)
{
if(res==1)
{
switch(outputno)
{
case 1:O1=1;break;
case 2:O2=1;break;
case 3:O3=1;break;
case 4:O4=1;break;
case 5:O5=1;break;
case 6:O6=1;break;

case 7:O7=1;break;
case 8:O8=1;break;
}
}
if(res==0)
{
switch(outputno)
{
case 1:O1=0;break;
case 2:O2=0;break;
case 3:O3=0;break;
case 4:O4=0;break;
case 5:O5=0;break;
case 6:O6=0;break;
case 7:O7=0;break;
case 8:O8=0;break;
}
}
res=1;
}
//latched output
void opl(int outputno)
{
if(res==1)
{
switch(outputno)
{
case 1:O1=1;break;
case 2:O2=1;break;
case 3:O3=1;break;
case 4:O4=1;break;
case 5:O5=1;break;
case 6:O6=1;break;
case 7:O7=1;break;
case 8:O8=1;break;
}
}
res=1;
}
//Output Unlatch
void opu(int outputno)
{
if(res==1)
{
switch(outputno)
{
case 1:O1=0;break;
case 2:O2=0;break;
case 3:O3=0;break;
case 4:O4=0;break;
case 5:O5=0;break;
case 6:O6=0;break;
case 7:O7=0;break;
case 8:O8=0;break;
}
}
res=1;
}
//Timer Function
int tmr(int tno,int timeset)
{
switch(tno)
{
case(1):T1=timeset;break;
case(2):T2=timeset;break;
case(3):T3=timeset;break;
case(4):T4=timeset;break;
case(5):T5=timeset;break;
}
if(res==1)
{
switch(tno)
{
case(1):if(tmrscan1==0){first1=time(NULL);tmrscan1
=1;};break;
case(2):if(tmrscan2==0){first2=time(NULL);tmrscan2
=1;};break;
case(3):if(tmrscan3==0){first3=time(NULL);tmrscan3
=1;};break;
case(4):if(tmrscan4==0){first4=time(NULL);tmrscan4
=1;};break;
case(5):if(tmrscan1==0){first1=time(NULL);tmrscan1
July 2004

electronics for you

construction
=1;};break;
}
switch(tno)
{
case(1):{second1=time(NULL);};break;
case(2):{second2=time(NULL);};break;
case(3):{second3=time(NULL);};break;
case(4):{second4=time(NULL);};break;
case(5):{second5=time(NULL);};break;
}
switch(tno)
{
case(1):if(TDN1!=1){TEN1=1,TACC1=second1first1;};break;
case(2):if(TDN2!=1){TEN2=1;TACC2=second2first2;};break;
case(3):if(TDN3!=1){TEN3=1;TACC3=second3first3;};break;
case(4):if(TDN4!=1){TEN4=1;TACC4=second4first4;};break;
case(5):if(TDN5!=1){TEN5=1;TACC5=second5first5;};break;
}
switch(tno)
{
case(1):if(difftime(second1,first1)==T1){TDN1=1;};
break;
case(2):if(difftime(second2,first2)==T2){TDN2=1;};
break;
case(3):if(difftime(second3,first3)==T3){TDN3=1;};
break;
case(4):if(difftime(second4,first4)==T4){TDN4=1;};
break;
case(5):if(difftime(second5,first5)==T5){TDN5=1;};
break;
}
}
if(res==0)
{
switch(tno)
{
case(1):{TEN1=0;TACC1=0;tmrscan1=0;TDN1=
0;};break;
case(2):{TEN2=0;TACC2=0;tmrscan2=0;TDN2=
0;};break;
case(3):{TEN3=0;TACC3=0;tmrscan3=0;TDN3=
0;};break;
case(4):{TEN4=0;TACC4=0;tmrscan4=0;TDN4=
0;};break;
case(5):{TEN5=0;TACC5=0;tmrscan5=0;TDN5=
0;};break;
}
}
}
//Up Counter
int ctu(int cno,int countset)
{
switch(cno)
{
case(1):C1=countset;break;
case(2):C2=countset;break;
case(3):C3=countset;break;
case(4):C4=countset;break;
case(5):C5=countset;break;
}
if(res==1&&risingedge==0)
{
switch(cno)
{
case(1):if(CDN1!=1){CACC1=CACC1+1;};break;
case(2):if(CDN2!=1){CACC2=CACC2+1;};break;
case(3):if(CDN3!=1){CACC3=CACC3+1;};break;
case(4):if(CDN4!=1){CACC4=CACC4+1;};break;
case(5):if(CDN1!=1){CACC5=CACC5+1;};break;
}
risingedge=1;
electronics for you

July 2004

}
if(res==0&&risingedge==1)risingedge=0;
switch(cno)
{
case(1):if(CACC1==C1)CDN1=1;break;
case(2):if(CACC2==C2)CDN2=1;break;
case(3):if(CACC3==C3)CDN3=1;break;
case(4):if(CACC4==C4)CDN4=1;break;
case(5):if(CACC5==C5)CDN5=1;;break;
}
if(countset==0&&res==1)
{
switch(cno)
{
case(1):{CACC1=0;CDN1=0;};break;
case(2):{CACC2=0;CDN2=0;};break;
case(3):{CACC3=0;CDN3=0;};break;
case(4):{CACC4=0;CDN4=0;};break;
case(5):{CACC5=0;CDN5=0;};break;
}
}
}
//Down Counter
int ctd(int cno,int countset)
{
switch(cno)
{
case(1):{CO1=countset;};break;
case(2):{CO2=countset;};break;
case(3):{CO3=countset;};break;
case(4):{CO4=countset;};break;
case(5):{CO5=countset;};break;
}
if(res==1&&risingedge==0)
{
switch(cno)
{
case(1):if(CODN1!=1){cd1=cd1+1;COACC1=CO1cd1;};break;
case(2):if(CODN2!=1){cd2=cd2+1;COACC2=CO2cd2;};break;
case(3):if(CODN3!=1){cd3=cd3+1;COACC3=CO3cd3;};break;
case(4):if(CODN4!=1){cd4=cd4+1;COACC4=CO4cd4;};break;
case(5):if(CODN3!=1){cd5=cd5+1;COACC5=CO5cd5;};break;
}
risingedge=1;
}
if(res==0&&risingedge==1)risingedge=0;
switch(cno)
{
case(1):if(CO1==cd1)CODN1=1;break;
case(2):if(CO2==cd2)CODN2=1;break;
case(3):if(CO3==cd3)CODN3=1;break;
case(4):if(CO4==cd4)CODN4=1;break;
case(5):if(CO5==cd5)CODN5=1;;break;
}
if(countset==0&&res==1)
{
switch(cno)
{
case(1):{COACC1=0;CODN1=0;};break;
case(2):{COACC2=0;CODN2=0;};break;
case(3):{COACC3=0;CODN3=0;};break;
case(4):{COACC4=0;CODN4=0;};break;
case(5):{COACC5=0;CODN5=0;};break;
}
}
}
//Real Time Output
int rto(int hourr,int minn)
{
int hr,mn;
if(res==1)
{
gettime(&t);

if(hourr==t.ti_hour && minn==t.ti_min)


{
RTDN=1;
}
if(hourr==0 && minn==0)
{
RTDN=0;
}
}
}
//Reading From And Sending Outputs
void inputread()
{
int inpval;
inpval=inp(0x379);
if((inpval&8)==0)I1=1;
if((inpval&8)==8)I1=0;
if((inpval&16)==0)I2=1;
if((inpval&16)==16)I2=0;
if((inpval&32)==0)I3=1;
if((inpval&32)==32)I3=0;
if((inpval&64)==0)I4=1;
if((inpval&64)==64)I4=0;
if((inpval&128)==0)I5=0;
if((inpval&128)==128)I4=1;
NI1=~I1;
NI2=~I2;
NI3=~I3;
NI4=~I4;
NI5=~I5;
NO1=~O1;
NO2=~O2;
NO3=~O3;
NO4=~O4;
NO5=~O5;
NO6=~O6;
NO7=~O7;
NO8=~O8;
}
//Sending Output To The Port
void setoutputs()
{
int outval;
outval=1*O1+2*O2+4*O3+8*O4+16*O5+32*O6+6
4*O7+128*O8;
outp(0x378,outval);
}
//Display Status On Screen
void displaystat()
{
textcolor(YELLOW);
gotoxy(25,8);
cprintf(%d,I1);
gotoxy(25,9);
cprintf(%d,I2);
gotoxy(25,10);
cprintf(%d,I3);
gotoxy(25,11);
cprintf(%d,I4);
gotoxy(25,12);
cprintf(%d,I5);
gotoxy(26,15);
cprintf(%d,O1);
gotoxy(26,16);
cprintf(%d,O2);
gotoxy(26,17);
cprintf(%d,O3);
gotoxy(26,18);
cprintf(%d,O4);
gotoxy(26,19);
cprintf(%d,O5);
gotoxy(26,20);
cprintf(%d,O6);
gotoxy(26,21);
cprintf(%d,O7);
gotoxy(26,22);
cprintf(%d,O8);
gotoxy(55,5);
cprintf(%3d,C1);

construction
gotoxy(55,6);
cprintf(%3d,C2);
gotoxy(55,7);
cprintf(%3d,C3);
gotoxy(55,8);
cprintf(%3d,C4);
gotoxy(55,9);
cprintf(%3d,C5);
gotoxy(62,5);
cprintf(%3d,CACC1);
gotoxy(62,6);
cprintf(%3d,CACC2);
gotoxy(62,7);
cprintf(%3d,CACC3);
gotoxy(62,8);
cprintf(%3d,CACC4);
gotoxy(62,9);
cprintf(%3d,CACC5);
gotoxy(68,5);
cprintf(%d,CDN1);
gotoxy(68,6);
cprintf(%d,CDN2);
gotoxy(68,7);
cprintf(%d,CDN3);
gotoxy(68,8);
cprintf(%d,CDN4);
gotoxy(68,9);
cprintf(%d,CDN5);
gotoxy(55,12);
cprintf(%3d,CO1);
gotoxy(55,13);
cprintf(%3d,CO2);
gotoxy(55,14);
cprintf(%3d,CO3);
gotoxy(55,15);
cprintf(%3d,CO4);
gotoxy(55,16);
cprintf(%3d,CO5);
gotoxy(62,12);
cprintf(%3d,COACC1);
gotoxy(62,13);
cprintf(%3d,COACC2);
gotoxy(62,14);
cprintf(%3d,COACC3);
gotoxy(62,15);

cprintf(%3d,COACC4);
gotoxy(62,16);
cprintf(%3d,COACC5);
gotoxy(68,12);
cprintf(%d,CODN1);
gotoxy(68,13);
cprintf(%d,CODN2);
gotoxy(68,14);
cprintf(%d,CODN3);
gotoxy(68,15);
cprintf(%d,CODN4);
gotoxy(68,16);
cprintf(%d,CODN5);
gotoxy(49,19);
cprintf(%d,TEN1);
gotoxy(49,20);
cprintf(%d,TEN2);
gotoxy(49,21);
cprintf(%d,TEN3);
gotoxy(49,22);
cprintf(%d,TEN4);
gotoxy(49,23);
cprintf(%d,TEN5);
gotoxy(57,19);
cprintf(%3d,T1);
gotoxy(57,20);
cprintf(%3d,T2);
gotoxy(57,21);
cprintf(%3d,T3);
gotoxy(57,22);
cprintf(%3d,T4);
gotoxy(57,23);
cprintf(%3d,T5);
gotoxy(64,19);
cprintf(%3d,TACC1);
gotoxy(64,20);
cprintf(%3d,TACC2);
gotoxy(64,21);
cprintf(%3d,TACC3);
gotoxy(64,22);
cprintf(%3d,TACC4);
gotoxy(64,23);
cprintf(%3d,TACC5);
gotoxy(70,19);

cprintf(%d,TDN1);
gotoxy(70,20);
cprintf(%d,TDN2);
gotoxy(70,21);
cprintf(%d,TDN3);
gotoxy(70,22);
cprintf(%d,TDN4);
gotoxy(70,23);
cprintf(%d,TDN5);
gotoxy(56,24);
cprintf(%d,RTDN);
gotoxy(27,23);
cprintf(%.2f,te*1000);
}
//Help File Display
void displayhelp(char helpfilename[10])
{
fstream infile;
textbackground(BLACK);
window(1,1,80,25);
const int max=80;
char buffer[max];
clrscr();
infile.open(helpfilename,ios::in);
if(infile.fail())
{
window(10,8,70,9);
textcolor(YELLOW+BLINK);
clrscr();
cprintf(.....Help not Available or Error Opening File ...\n\
r..Press any Key to Return to Main.....);
getch();
openingmenu();
}
while(!infile.eof())
{
infile.getline(buffer,max);
cout<<buffer;
cout<<endl;
}
infile.close();
getch();
helpu();
} q

July 2004

electronics for you

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy