Vous êtes sur la page 1sur 3


Pulse Counter using at89C4051

HimansHu sHarma
mar Sunil Ku

Parts List
Semiconductors: IC1 IC2 IC3 D1-D4 LED1, LED2 LCD AT89C4051 microcontroller MAX232 RS-232 driver 7805, 5V regulator 1N4007 rectifier diode 5mm LED 16-character 2-line

ulse counters are widely used in our day-to-day life. Almost all museums and theatres have a visitor counter installed at entry/exit to measure the visitor traffic. In industries, counting is done for production control. Market tests too are performed by counting the sold goods. A pulse counter could be roughly divided into three parts: a pulse source, an electronic device that counts, stores and prepares outputs, and a display to show the accumulated count. This pulse counter is based on Atmel AT89C4051 microcontroller. TTL-logic-compatible pulses generated by the source are fed to the counter for counting. The AT89C4051 is a low-voltage, high-performance, 8-bit microcontroller with 4 kB of Flash programmable and erasable read-only

memory, 128 bytes of RAM, 15 input/ output (I/O) lines, two 16-bit timers/ counters, a five-vector, two-level interrupt architecture, a full-duplex serial port, a precision analogue comparator, on-chip oscillator and clock circuitry. System clock plays a significant role in operation of the microcontroller. An 11.0592MHz quartz crystal provides basic clock to the microcontroller (IC1) at its pins 4 and 5. Power-on reset is provided by electrolytic capacitor C3 and resistor R1. Switch S1 is used for manual reset. Port pin P3.5 receives the input pulse and the count is displayed on the LCD as well as hyper terminal of the PC. Pulse reception is indicated by LED2, which is connected to port pin

Resistors (all -watt, 5% carbon): R1 - 10-kilo-ohm R2, R3 - 330-ohm R4 - 33-ohm Capacitors: C1 C2 C3 C4, C5 C6-C10 1000F, 25V electrolytic 0.1F ceramic disk 10F, 16V electrolytic 33pF ceramic disk 1F, 16V electrolytic

Miscellaneous: X1 - 230V AC primary to 9V, 500mA secondary transformer S1 - Push-to-on switch - 11.0592MHz crystal XTAL - 9-pin D-type female COM port connector - Heat-sink

Fig. 1: Circuit of microcontroller-based pulse counter 1 0 8 O c tO b e r 2 0 1 0 e l e c t ro n i c s f o r yo u w w w. e f y m ag . cO m

pin (TXD) is specified at port pin P3.1. The microcontroller is connected to T IN (pin 11) of MAX232. TOUT (pin 14) of IC2 is connected to pin 2 of the COM port connector. The signals provided on these pins are TTL-level and must be boosted and inverted through a MAX232 converter Fig. 2: An actual-size, single-side PCB for microcontroller-based pulse counter to comply with RS232 standard. The MAX232 has two internal charge pumps that convert +5V into 10V (unloaded) for RS-232 driver operation. The first converter uses capacitor C8 to double the +5V input to +10V on capacitor C10 at pin 2. The second converter uses capacitor C7 to invert +10V to -10V on capacitor C6 Fig. 3: Component layout for the PCB at pin 6. All the serial communication is controlled through P3.7 of the microcontroller. Port pins special-function register SCON. This P1.0 through P1.7 of the microcontroller register contains mode-selection bits, are connected to data pins D0 through ninth data bit for transmit and receive D7 of the LCD, respectively. Port pins (TB8 and RB8), and serial-port interrupt P3.4, P3.3 and P3.2 are connected to bits (TI and RI). Serial communication register-select RS, read-write R/W and requires standard baud rate. Timer-1 enable E of the LCD, respectively. is configured in auto-reload mode to The data is sent to the LCD in generate baud rate. The baud rate is ASCII format for display. Only the determined as: commands are sent in hex form to the LCD. Register-select RS signal is used (2SMOD Oscillator frequency) to distinguish between data (RS=1) Baud rate = (3212(256 - TH1)) and command (RS=0). Using preset VR1, you can control the contrast of The 230V, 50Hz AC mains is the LCD. Resistor R4 limits the current stepped down by transformer X1 to through backlight of the LCD. deliver a secondary output of 9V, 500 Port pin P3.1 of the microconmA. The transformer output is rectitroller is used to interface with the fied by a full-wave rectifier comprisPCs hyper terminal through RS-232 ing diodes D1 through D4, filtered by interface MAX232 IC (IC2). The microcapacitor C1 and regulated by IC 7805 controller provides a transmit channel (IC3). Capacitor C2 bypasses the ripfor serial data transfer. Transmit data ples present in the regulated supply.
w w w. e f y m ag . cO m

Fig. 4: Hyperterminal window for name and icon

Fig. 5: Hyperterminal window for COM port selection

LED1 acts as the power indicator and R2 limits the current through LED1.

Construction and testing

An actual-size, single-side PCB for the microcontroller-based pulse counter is shown in Fig. 2 and its component layout in Fig. 3. Assemble the circuit on a PCB as it minimises time and assembly errors. Carefully assemble the components and double-check for any overlooked error. Connect the assembled circuit to the COM port of the computer. The accumulated pulse count transferred is sent to the PC through the COM port using the HyperTerminal program. To open the HyperTerminal program, go to Start Programs Accessories Communications HyperTerminal. You will see a window as shown in Fig. 4. Type the desired name and click ok. Select a COM port (refer Fig. 5) while ignoring the other options and click ok.

e l e c t ro n i c s f o r yo u O c tO b e r 2 0 1 0 1 0 9

Now select the baud rate as 9600, data bits as 8, parity as none, stop bits as 1, flow control as Xon/Xoff and click ok (refer Fig. 6). The microcontroller counts the received pulses and sends the pulse count to the hyper terminal and LCD. You will see a message on the screen as shown in Fig. 8. Simultaneously, the pulse count appears on the LCD. to give the correct speed (9600 bits per second). SCON gives the necessary UART signal and your data is sent serially. When port pin P3.5 (T1) goes low, the internal counFig. 7: Count on ter increases by hyperterminal screen one. This count is displayed on the LCD as well as hyper terminal of the PC. EFY note. The source code of this project has been included in this months EFY-CD and is also available on www.efymag.com website.

The software for this project is given at the end of this article. It is written in C language and compiled using Keil Vision4 compiler. Burn the generated hex code into the microcontroller using a suitable programmer. In the software, first enable timer

Fig. 6: Hyperterminal window for selection of baud rate, data bits, parity, stop bits and flow control

and serial interrupts. Then configure the timer by mode and reload value

#include<reg51.h> #define LCD P1 unsigned int Count,d1,d2,d3,d4,x1,x2,x3,x4; sbit Pulse = P3^5; sbit LED = P3^7; sbit rs = P3^4; sbit rw = P3^3; sbit en = P3^2; void LCDInit (); void lcdcmd (unsigned char); void lcdwrt (unsigned char); void Delay (unsigned int); void Convert (unsigned int); void SerialTx (unsigned char); void main(void) { unsigned char z,Display; unsigned char code str1[]= PULSE COUNTER ; unsigned char code str2[]= Design By ; unsigned char code str3[]= HIMANSHU ; unsigned char code str4[]= SHARMA ; unsigned char code str5[]=PULSE: 0000 ; P0=0xFF; P1=0xFF; P2=0xFF; P3=0xFF; IE=0x00; Count=0x00; ///////SERIALIZATION DATA/////// TMOD = 0x20; TH1 = 0xFD; SCON = 0x50; TR1 = 1; LED = 0x00; LCDInit(); for(z=0;z<16;z++) { Display=str1[z]; lcdwrt(Display); Delay(1); SerialTx(Display); Delay(1); } SerialTx(\r); SerialTx(\n); lcdcmd(0xC0); for(z=0;z<16;z++) { Display=str2[z]; lcdwrt(Display); Delay(1); SerialTx(Display); Delay(1); } Delay(200); SerialTx(\r); SerialTx(\n); lcdcmd(0x01);

Delay(1); for(z=0;z<16;z++) {

Display=str3[z]; lcdwrt(Display); Delay(1); SerialTx(Display); Delay(1);

} SerialTx(\r); SerialTx(\n); lcdcmd(0xC0); for(z=0;z<16;z++) { Display=str4[z]; lcdwrt(Display); Delay(1); SerialTx(Display); Delay(1); } Delay(200); SerialTx(\r); SerialTx(\n); lcdcmd(0x01); Delay(1); lcdcmd(0xC0); for(z=0;z<16;z++) { Display=str5[z]; lcdwrt(Display); Delay(1); SerialTx(Display); Delay(1); } Delay(70); SerialTx(\r); SerialTx(\n); while(1) { LED=1; Delay(30); if(Pulse==0) { Count=Count+1; Convert(Count); if(Count==9999) } }


} void Convert(unsigned int value) { x1 = value / 10; d1 = value % 10; x2 = x1 / 10; d2 = x1 % 10; x3 = x2 / 10; d3 = x2 % 10; d4 = x3 % 10; d1=d1|0x30; d2=d2|0x30; d3=d3|0x30;

} void LCDInit() { lcdcmd(0x38); Delay(10); lcdcmd(0x0E); Delay(10); lcdcmd(0x01); Delay(10); lcdcmd(0x0C); Delay(10); lcdcmd(0x80); Delay(10); return; } void lcdcmd(unsigned char value) { LCD=value; rs=0; rw=0; en=1; Delay(1); en=0; return; } void lcdwrt(unsigned char value) { LCD=value; rs=1; rw=0; en=1; Delay(1); en=0; return; } void SerialTx(unsigned char value) { SBUF = value; while(TI==0); TI = 0; } void Delay(unsigned int x) { unsigned int i,j; for (i=0;i<=x;i++) for (j=0;j<=500;j++); }

d4=d4|0x30; lcdcmd(0xC7); lcdwrt(d4); lcdwrt(d3); lcdwrt(d2); lcdwrt(d1); Delay(1); SerialTx(d4); SerialTx(d3); SerialTx(d2); SerialTx(d1); Delay(1); SerialTx(\r); SerialTx(\n); LED=0; Delay(30);

1 1 0 O c tO b e r 2 0 1 0 e l e c t ro n i c s f o r yo u

w w w. e f y m ag . cO m