Unit - 2
IO Port Interfacing-I
The pin diagram of 8051 microcontroller consists of 40 pins as shown below. A total of 32 pins are set away into four Ports such as P0, P1, P2 and P3. Where, each port contains 8 pins. Therefore, the microcontroller 8051 pin diagram and explanation are given below.
Figure 1 PIN Diagram of 8051
- Port1 (Pin1 to Pin8): Port1 includes pin1.0 to pin1.7 and these pins can be configured as input or output pins.
- Pin 9 (RST): Reset pin is used to Reset 8051 Microcontroller by giving a positive pulse to this Pin.
- Port3 (Pin 10 to 17): The Port3 Pins are similar to port1 pins and can be used as universal Input or output pins. These pins dual-function Pins and the function of each Pin is given as:
- Pin 10 (RXD): RXD pin is a Serial Asynchronous Communication Input or Serial synchronous Communication Output.
- Pin 11 (TXD): Serial Asynchronous Communication Output or Serial Synchronous Communication clock Output.
- Pin 12 (INT0): Input of Interrupt 0
- Pin 13 (INT1): Input of Interrupt 1
- Pin 14 (T0): Input of Counter 0 clock
- Pin 15 (T1): Input of Counter 1 clock
- Pin 16 (WR): Writing Signal to write content on external RAM.
- Pin 17 (RD): Reading Signal to read contents of external RAM.
- Pin 18 and 19 (XTAL2, XTAL1): X2 and X1 pins are input output pins for the oscillator. These pins are used to connect an internal oscillator to the microcontroller.
- Pin 20 (GND): Pin 20 is a ground pin.
- Port2 (Pin 21 to Pin28): Port2 includes pin21 to pin28 which can be configured as Input Output Pins. But this is only possible when we don’t use any external memory. If we use external memory, then these pins will work as high order address bus (A8 to A15).
- Pin 29 (PSEN): This pin is used to enable external program memory. If we use an external ROM for storing the program, then logic 0 appears on it, which indicates Micro controller to read data from the memory.
- Pin 30 (ALE): Address Latch Enable pin is an active high-output signal. If we use multiple memory chips, then this pin is used to distinguish between them. This Pin also gives program pulse input during programming of EPROM.
- Pin 31 (EA): If we have to use multiple memories then the application of logic 1 to this pin instructs the Microcontroller to read data from both memories: first internal and then external.
- Port 0 (Pin 32 to 39): Similar to the port 2 and 3 pins, these pins can be used as input output pins when we don’t use any external memory. When ALE or Pin 30 is at 1, then this port is used as data bus: when the ALE pin is at 0, then this port is used as a lower order address bus (A0 to A7)
- Pin40 (VCC): This VCC pin is used for power supply.
Key takeaway
The 8051 has four 8-bit I/O ports: P0, P1, P2, and P3. We can access either the entire 8 bits or any single bit without altering the rest. When accessing a port in a single-bit manner, we use the syntax "SETB X. y" where X is the port number 0, 1,2, or 3, and Y is the desired bit number from 0 to 7 for data bits D0 to 07. See Figure 5-2. For example, "SETB Pl. 5" sets high bit 5 of port 1. Remember that D0 is the LSB and D7 is the MSB.
Every SFR register is assigned a byte address and ports PO - P3 are part of the SFR. For example, PO is assigned byte address 80H, and PI has address of 90H as shown in Figure below. While all of the SFR registers are byte-addressable some of them are also bit-addressable. The P0- P3 are among this category of SFR registers. From Figure below we see that the bit addresses for P0 are 80H to 87H, and for P I arc 90H to 97H, and so on.
Fig 2 SFR RAM address
When code such as "SETB Pl.0" is assembled, it becomes "SETB 90H"since P 1.0 has the RAM address of 90H. The bit addresses 00 - 7FH belong to RAM byte addresses 20 - 2FH, and bit addresses 80 - F7H belong to SFR of P0, TCON, P1, SCON, P2, etc. The bit addresses for P0 - P3 are below
Fig 3 Bit address of all ports
Light Emitting Diodes are considered as semi- conductor light sources. The commonly used LEDs have a cut-off voltage of 1.7V and current of 10mA. When applied with its required voltage and current it glows with full intensity.
The Light Emitting Diode works similar to normal PN- diode but it emits energy in the form of light. The colour of the light depends on the semiconductors band gap.
Figure 4. LED
LED is connected to AT89C51 microcontroller with the current limiting resistor. The value of this resistor is calculated using the following formula.
R= (V-1.7)/10mA, where V is the input voltage.
The maximum output voltage of microcontrollers is 5V. Hence the calculated value of resistor is 330 Ohms. The resistor can be connected to cathode or the anode of LED.
- Initially, burn the code into the microcontroller.
- Connect the LEDs to the Port0 of the microcontroller.
- Switch on the circuit.
- The LEDs start glowing.
- Switch off the circuit.
Figure 5. LED Interfacing
- In the circuit, push button switch S1, capacitor C3 and resistor R3 forms the reset circuitry.
- When S1 is pressed, voltage at the reset pin (pin9) goes high and this resets the chip. C1, C2 and X1 are related to the on -chip oscillator to produces the required clock frequency.
- P1.0 (pin1) is acts as the output pin. When P1.0turns high transistor Q1 is forward biased and LED gets ON. When P1.0 turns low, the transistor gets cut off and the LED extinguishes.
- The transistor driver circuit for the LED can be avoided and the LED can be connected directly to the P1.0 pin along with the series current limiting resistor(~1K).
- The time for which P1.0turns high and low (time period of the LED) is determined by the program.
Program:
START: CPL P1.0
ACALL WAIT
SJMP START
WAIT: MOV R4, #05H
WAIT1: MOV R3, #00H
WAIT2: MOV R2, #00H
WAIT3: DJNZ R2, WAIT3
DJNZ R3, WAIT2
DJNZ R4, WAIT1
RET
There are set of PUSH buttons in matrix keypads which are interconnected. For 4x4 matrix there are 4 PUSH buttons in each of the 4 rows. One terminal of all 4 PUSH buttons is connected in row 1. The other terminal represents the 4 columns. Hence, there are 8 terminals we need to connect with a microcontroller.
Fig 6 Interfacing of Keypad
Before interfacing keypad, it is important to interface LCD with the microcontroller. The 8 terminals of the keypads need to be connected to any of the 8 pins (ports) of the microcontroller. We need to know the number of row and column whenever any button is pressed. After tracking location, we can print the character accordingly.
Now the question is how to get the location of the pressed button. This is explained below:
1. First we have made all the Rows to Logic level 0 and all the columns to Logic level 1.
2. Whenever we press a button, column and row corresponding to that button gets shorted and makes the corresponding column to logic level 0. Because that column becomes connected (shorted) to the row, which is at Logic level 0. So, we get the column no. See main () function
Figure 7 Finding Column Number
3. Now we need to find the Row no., so we have created four functions corresponding to each column. Like if any button of column one is pressed, we call function row_finder1(), to find the row no.
4. In row_finder1() function, we reversed the logic levels, means now all the Rows are 1 and columns are 0. Now Row of the pressed button should be 0 because it has become connected (shorted) to the column whose button is pressed, and all the columns are at 0 logic. So, we have scanned all rows for 0.
Fig 8 Finding Row number
5. So whenever we find the Row at logic 0, means that is the row of pressed button. So now we have column no (got in step 2) and row no., and we can print no. Of that button using lcd_data function.
Program
#include<reg51.h>
#define display_port P2 //Data pins connected to port 2 on microcontroller
sbit rs = P3^2; //RS pin connected to pin 2 of port 3
sbit rw = P3^3; // RW pin connected to pin 3 of port 3
sbit e = P3^4; //E pin connected to pin 4 of port 3
Sbit C4 = P1^0; // Connecting keypad to Port 1
sbit C3 = P1^1;
sbit C2 = P1^2;
sbit C1 = P1^3;
sbit R4 = P1^4;
sbit R3 = P1^5;
sbit R2 = P1^6;
sbit R1 = P1^7;
Void msdelay(unsigned int time) // Function for creating delay in milliseconds.
{
unsigned i,j ;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char command) //Function to send command instruction to LCD
{
display_port = command;
rs= 0;
rw=0;
e=1;
msdelay(1);
e=0;
}
Void lcd_data(unsigned char disp_data) //Function to send display data to LCD
{
display_port = disp_data;
rs= 1;
rw=0;
e=1;
msdelay(1);
e=0;
}
Void lcd_init() //Function to prepare the LCD and get it ready
{
lcd_cmd(0x38); // for using 2 lines and 5X7 matrix of LCD
msdelay(10);
lcd_cmd(0x0F); // turn display ON, cursor blinking
msdelay(10);
lcd_cmd(0x01); //clear screen
msdelay(10);
lcd_cmd(0x81); // bring cursor to position 1 of line 1
msdelay(10);
}
Void row_finder1() //Function for finding the row for column 1
{
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
If(R1==0)
lcd_data('1');
if(R2==0)
lcd_data('4');
if(R3==0)
lcd_data('7');
if(R4==0)
lcd_data('*');
}
Void row_finder2() //Function for finding the row for column 2
{
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
If(R1==0)
lcd_data('2');
if(R2==0)
lcd_data('5');
if(R3==0)
lcd_data('8');
if(R4==0)
lcd_data('0');
}
Void row_finder3() //Function for finding the row for column 3
{
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
If(R1==0)
lcd_data('3');
if(R2==0)
lcd_data('6');
if(R3==0)
lcd_data('9');
if(R4==0)
lcd_data('#');
}
Void row_finder4() //Function for finding the row for column 4
{
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
If(R1==0)
lcd_data('A');
if(R2==0)
lcd_data('B');
if(R3==0)
lcd_data('C');
if(R4==0)
lcd_data('D');
}
Void main()
{
lcd_init();
while(1)
{
msdelay(30);
C1=C2=C3=C4=1;
R1=R2=R3=R4=0;
if(C1==0)
row_finder1();
else if(C2==0)
row_finder2();
else if(C3==0)
row_finder3();
else if(C4==0)
row_finder4();
}
}
Key takeaway
Before interfacing keypad, it is important to interface LCD with the microcontroller. The 8 terminals of the keypads need to be connected to any of the 8 pins (ports) of the microcontroller. We need to know the number of row and column whenever any button is pressed.
Figure 9. Seven Segment Display Interfacing with 8051.
Working:
- The 8051 has 4 I/O ports P0 to P3 each with 8 I/O pins, P0.0 to P0.7, P1.0 to P1.7, P2.0 to P2.7, P3.0 to P3.7. Port P1 acts as an I/P port for microcontroller and port P0 as an O/P port, Port P2 is used for displaying the number of pressed key.
- Make all rows of port P0 high so that it gives high signal when key is pressed.
- Check if any key is pressed by scanning port P1 by checking all the columns for non- zero condition.
- If any key is pressed, identify which key is pressed make one row high at a time.
- Initiate a counter to hold the count so that each key is counted.
- Check port P1 for nonzero condition. If any nonzero number is there in [accumulator], start column scanning by following step 9.
- Otherwise make next row high in port P1.
- Add a count of 08h to the counter to move to the next row by repeating steps from step 6.
- If any key pressed is found, the [accumulator] content is rotated right through the carry until carry bit sets, while doing this increment count in the counter till carry is found.
- Move the content in the counter to display in data field or to memory location
- To repeat the procedures, go to step 2.
Program:
Start of main program:
To check that whether any key is pressed
Start: mov a, #00h
Mov p1, a; making all rows of port p1 zero
Mov a, #0fh
Mov p1, a; making all rows of port p1 high
Press:mov a, p2
Jz press; check until any key is pressed
After making sure that any key is pressed
Mov a, #01h; make one row high at a time
Mov r4, a
Mov r3, #00h; initiating counter
Next: mov a, r4
Mov p1, a; making one row high at a time
Mov a, p2; taking input from port A
Jnzcolscan; after getting the row jump to check column
Mov a, r4
Rl a; rotate left to check next row
Mov r4, a
Mov a, r3
Add a, #08h; increment counter by 08 count
Mov r3, a
Sjmp next; jump to check next row
After identifying the row to check the column following steps are followed
Colscan: mov r5, #00h
In: rrc a; rotate right with carry until get the carry
Jc out; jump on getting carry
Inc r3; increment one count
Jmp in
Out: mov a, r3
Da a; decimal adjust the contents of counter
Before display
Mov p2, a
Jmp start; repeat for check next key.
In these systems the microcontroller generates output in digital form, The controlling system requires analog signal because they don't accept digital data. Hence, there is a need to use DAC which converts digital data into its equivalent analog voltage.
In the figure shown, we use 8-bit DAC 0808. This IC converts digital data into its equivalent analog current. The I to V converter converts the current into its equivalent voltage.
According to theory of DAC Equivalent analog output is given as:
Ex:
1. IF data =00H [00000000] and
Vref = 10V
Therefore, V0= 0 Volts.
2. If data is 80H [10000000],
And Vref = 10V
Therefore, V0= 5 Volts.
Different Analog output voltages for different Digital signal is given as:
DATA | OUTPUT VOLTAGE |
00H | 0V |
80H | 5V |
FFH | 10V |
Interfacing Diagram
Figure 10. Interfacing 8051 with DAC 0808
Program:
ORG 000H
MOV P1, #00H
Call squarwave
Call triwave
Call stairwave
Jmp repeat
Squarwave: MOV P1, #FFH
Call delay2sec
MOV P1, #00H
Call delay2sec
Ret
Triwave: MOV R7, #00H
Triwave1: MOV P1, R7
Inc R7
Cjne R7, #FFH, triwave1
MOV R7, #FFh
Triwave2: MOV P1, R7
Djnz R7, triwave2
RET
Stairwave: MOV P1, #00H
Call delay2sec
MOV P1, #20H
Call delay2sec
MOV P1, #40H
Call delay2sec
RET
Delay1sec: MOV R0, #10
Del2 : MOV R1, #250
Del1: MOV R2, #250
DJNZ R2, $
DJNZ R1, del2
DJNZ R0, del1
RET
Delay2sec: MOV R0, #20
Del2 : MOV R1, #250
Del1: MOV R2, #250
DJNZ R2, $
DJNZ R1, del21
DJNZ R0, del22
RET
END
Key takeaway
In these systems the microcontroller generates output in digital form, The controlling system requires analog signal because they don't accept digital data. Hence, there is a need to use DAC which converts digital data into its equivalent analog voltage.
ADC 0808/0809
ADC0808/0809 is a monolithic CMOS device which consists of 28 pin and gives 8-bit value in output for 8- channel ADC input pins (IN0-IN7).
It has resolution of 8 therefore it can encode the analog data into one of the 256 levels (28).
It has three channel address line namely: ADDA, ADDB and ADDC to select channel.
Pin Diagram for ADC0808
Figure 11. ADC 0808 Pin
ADC0808/0809 needs clock pulse for conversion which can be provided by using oscillator or microcontroller.
Select the input channel using address lines, like the input line IN0 by keeping all three address lines (ADDA, ADDB, ADDC) low.
Suppose IN2 is selected the keep ADDA and ADDB low and ADDC high.
Figure 12. Channel selection
SOC [Start of conversion]:
When High to low signal is appears on the pin of ADC, and ADC starts conversion
Figure 13. SOC
EOC [End of conversion]:
To indicate completion of conversion ADC sends high EOC signal to microcontroller.
OE [Output Enable]:
When a high signal is applied to this pin, the output latch of ADC is enabled and the converted data is made available to Microcontroller.
The reference voltage determines the range of analog input voltage.
For example: - If reference voltage is 5V then analog voltage ranges from 0V-5V. If the reference voltage is 2.56V then the range is from 0V-2.56V.
The frequency of the applied clock signal determines conversion speed.
Working:
- Initially, microcontroller provides a 500 KHz clock signal to ADC0808 because the Timer 0 interrupt requires clock signal to operate to operate as ADC.
- Then the microcontroller sends a LOW to HIGH level signal to ALE pin (active-high pin) of ADC0808 to enable the latch in the address.
- By applying HIGH to LOW Level signal to SC (Start Conversion), ADC starts analog to digital conversion. And then wait for the EOC (End of Conversion) pin to go LOW.
- When EOC goes LOW, it means analog to digital conversion has been completed and data is ready to use.
- After this, microcontroller enables the output line by applying a HIGH to LOW signal to OE pin of ADC0808.
- ADC0808 gives ratio metric conversion output at its output pins.
Radiometric conversion is given by
Vin/ (Vfs – Vz) = Dx / (Dmax – Dmin)
Where:
Vin is input voltage for conversion
Vfs is full scale voltage
Vz is zero voltage
Dx is data point being measured
Dmax is maximum data limit
Dmin is minimum data limit.
Interfacing Diagram:
Figure 14. Interfacing 8051 with ADC0808
Program:
SOC EQU P2.6
EOC EQU P2.5
OE EQU P2.4
ALE EQU P2.3
ADDR_C EQU P2.2
ADDR_B EQU P2.1
ADDR_A EQU P2.0
ORG 0000H
MOV P1, #0FFH
SETB P2.5
CLR SOC
CLR OE
CLR ALE
AGAIN: CLR ADDR_C
CLR ADDR_B
CLR ADDR_A
SETB SOC
SET ALE
ACALL DELAY
CLR SOC
CLR ALE
BACK: JB EOC BACK
HERE: JNB EOC HERE
SETB OE
MOV A, P1
ACALL CONVERSION
ACALL DISPLAY
AGAIN: SJMP AGAIN
Key takeaway
ADC0808/0809 needs clock pulse for conversion which can be provided by using oscillator or microcontroller.
SOC- When High to low signal is appears on the pin of ADC, and ADC starts conversion
EOC- To indicate completion of conversion ADC sends high EOC signal to microcontroller.
- Stepper motors is mainly used to translate electrical pulses into mechanical movements.
- They are used in some disk drives, dot matrix printers. The key advantage of using stepper motor is its position control.
- Stepper motors generally have a permanent magnet shaft (rotor), and it is surrounded by a stator.
Figure 15: Stepper Motor
Parameters of stepper motors:
- Step Angle - The step angle is the angle at which the rotor moves when one pulse is applied as an input to the stator. This parameter determines the position of stepper motor.
- Steps per Revolution - This is the number of step angles required for a complete revolution. The formula is 360° /Step Angle.
- Steps per Second - This parameter is used to measure number of steps covered in each second.
RPM - The RPM is the Revolution Per Minute. It measures the frequency of rotation. We can measure the number of rotations in one minute.
The relation between RPM steps per revolution and steps per second is
Steps per second = rpm x steps per revolution / 60
Interfacing
Port P0 of 8051 is used for connecting the stepper motor. HereULN2003 is used. This is basically a high voltage, high current Darlington transistor array. Each ULN2003 has seven NPN Darlington pairs. It can provide high voltage output with common cathode clamp diodes for switching inductive loads.
The Unipolar stepper motor works in three modes.
Wave Drive Mode: In this mode, one coil is energized at a time. So, all four coils are energized one after another. This mode produces less torque than full step drive mode.
Steps | Winding A | Winding B | Winding C | Winding D |
1 | 1 | 0 | 0 | 0 |
2 | 0 | 1 | 0 | 0 |
3 | 0 | 0 | 1 | 0 |
4 | 0 | 0 | 0 | 1 |
Full Drive Mode: In this mode, two coils are energized at the same time. This mode produces more torque. Here the power consumption is also high.
Steps | Winding A | Winding B | Winding C | Winding D |
1 | 1 | 1 | 0 | 0 |
2 | 0 | 1 | 1 | 0 |
3 | 0 | 0 | 1 | 1 |
4 | 1 | 0 | 0 | 1 |
Half Drive Mode: In this mode, one and two coils are energized alternately. At first, one coil is energized then two coils are energized. This is basically a combination of wave and full drive mode. It increases the angular rotation of the motor.
Steps | Winding A | Winding B | Winding C | Winding D |
1 | 1 | 0 | 0 | 0 |
2 | 1 | 1 | 0 | 0 |
3 | 0 | 1 | 0 | 0 |
4 | 0 | 1 | 1 | 0 |
5 | 0 | 0 | 1 | 0 |
6 | 0 | 0 | 1 | 1 |
7 | 0 | 0 | 0 | 1 |
8 | 1 | 0 | 0 | 1 |
Figure 16. Interfacing Stepper Motor with 8051
Program:
Main: MOV A, # 0FF H; Initialization of Port 1
MOV P1, A;
MOV A, #77 H; Code for the Phase 1
MOV P1, A; ACALL DELAY; Delay subroutine
MOV A, # BB H; Code for the Phase II
MOV P1, A;
ACALL DELAY; Delay subroutine.
MOV A, # DD H; Code for the Phase III
MOV P1, A; ACALL DELAY;
MOV A, # EE H; Code for the Phase 1
MOV P1, A;
ACALL DELAY; Delay subroutine
SJMP MAIN; Keep the motor rotating continuously.
DELAY Subroutine
MOV R4, #0FF H; Load R4 with FF
MOV R5, # 0FF; Load R5 with FF
LOOP1: DJNZ R4, LOOP1; Decrement R4 until zero, wait
LOOP2: DJNZ R5, LOOP2; Decrement R5 until zero, wait
RET; Return to main program
Key takeaway
- Stepper motors is mainly used to translate electrical pulses into mechanical movements.
- They are used in some disk drives, dot matrix printers. The key advantage of using stepper motor is its position control.
Relays are mainly used for two basic operations.
- One is a low voltage application. For low voltage applications preference is given to reduce the noise of the whole circuit.
- Second, for high voltage applications, they are designed to reduce a phenomenon called arcing.
Interfacing
Here the transistor is wired as a switch for interfacing relay with 8051. This transistor drives the relay. The transistor will be in OFF state when the pin P3.0 is in the LOW state. When 1 is written to P3.0 current will flow to the base of the transistor and the relay energizes.
Figure 17. Interfacing with Relays.
Program:
Relay equ P0.0
Swequ P1.0
;*************************************************
Org 0000h
Main:clr relay ;Configure inp and outp
Setb sw
Up: jnbsw,on ;wait for switch to be pressed
Clr relay
Acall delay
Sjmp up
On: setb relay; Turn ON relay
Acall delay
Acall delay
Here:jb sw, here; wait for switch to be released
Clr relay; Turn OFF relay
Acall delay
Acall delay
Sjmp up; Loop
;*************************************************
Delay:mov r7, #0ffh; delay subroutine
Again:mov r6, #0ffh
Djnz r6, $
Djnz r7, again
Ret
;*************************************************
End
Buzzer is an electronic device that converts the electronic signal into buzzing noise, that is applied to it.
Interfacing:
Figure 18. Interfacing with buzzer
- Pin 40 and 31 is connected to Vcc(5v). Port 1.5 is connected with an inverter and then to the buzzer. Pin 18 and 19 are connected to a crystal oscillator with ceramic 0.01uf capacitor. The buzzer gets turned on when port 2.3 is high.
- For resetting the buzzer pin 31 and 3 are connected together with 1uf capacitor and then connected to ground through 10K resistor.
Program:
Org 00H
HERE: JNB P2.3, HERE; keep monitoring for high
SET B P1.5; set bit P1.5
CLR P1.5; make P1.5 low
SJMP HERE; keep repeating
END
Key takeaway
Buzzer is an electronic device that converts the electronic signal into buzzing noise, that is applied to it.
- Opto-isolator called as opto-coupler is used to provide isolation between two electrical circuits. It is electrical component which is used to transmit input signal by using light energy signals.
- It provides electrical coupling between input and output through light waves.
- Its main purpose is to avoid changing in voltages at output side to appearing at input side.
- Higher voltage fluctuations may damage input side electrical components.
Working
Optocoupler consists of a LED which converts input electrical signal into light energy signal. Photo transistor at the output side detects light signal and turned on when LED light falls on it. In this way its output becomes low and high according to input signal and LED lights as shown in figure below:
Figure 19. Optoisolator
Interfacing
Figure 20. Interfacing with Opto-isolator
Program:
ORG 00H
MOV P3, #00H; Configure PORT3 as output
again: setb P3.5; relay ON, Buzzer ON
Acall Delay
clr P3.5; relay Off, Buzzer off
Acall Delay
sjmp again
Delay: Mov R3, #08H
Up2: Mov R2, #0FFH
Up1: Mov R1, #0FFHH
Here: DJNZ R1, Here
DJNZ R2, Up1
DJNZ R3, Up2
RET
END
Key takeaway
Optocoupler consists of a LED which converts input electrical signal into light energy signal. Photo transistor at the output side detects light signal and turned on when LED light falls on it. In this way its output becomes low and high according to input signal and LED lights
Data acquisition system is the process of measuring an electrical or physical phenomenon such as voltage, current, temperature, pressure or sound with computer.
- It measures the physical parameter or takes specific action based on the data received.
Figure 21. DAS system
- The DAS system consists of sensors, DAS measurement hardware and computer with programmable software.
- First the sensor measures the physical quantity or electrical quantity like temperature, pressure, voltage or current.
- The device converts the measured analog signal into digital signal. It is interfaced with PC.
- We can control the driver circuit by computer Through DAS.
Figure 22. DAS interfacing
Figure 23. Pin Diagram
Program:
Start: MOV P1, #00H
LCALL initserial
LCALL latch
LCALL start_conv
LCALL wait
LCALL get
MOV A, P2
MOV R6, A
LCALL send_read
SJMP start
Delay: MOV R4, #80H
Here: MOV R5, #30H
Again: DJNZ R5, again
DJNZ R4, here
RET
Initserial: MOV TMOD, #20H
MOV TH1, #FDH
MOV SCON, #50H
SETB TR1
SETB EA
SETB ES
RET
Start_conv: LCALL delay
CLR P3.3
LCALL delay
SETB P3.3
RET
Wait: JB P3.4, wait
RET
Get: LCALL delay
CLR P3.5
LCALL delay
SETB P3.5
RET
Send_char: MOV SBUF, R6
Next: JNB TI, next
CLR TI
RET
Send_read: LCALL send_char
MOV A, #”.”
LCALL send_char
RET
Latch: CLR P3.6
LCALL delay
SETB P3.6
RET
Key takeaway
- The DAS system consists of sensors, DAS measurement hardware and computer with programmable software.
- First the sensor measures the physical quantity or electrical quantity like temperature, pressure, voltage or current.
- The device converts the measured analog signal into digital signal. It is interfaced with PC.
- We can control the driver circuit by computer Through DAS.
Write a C program for the 8051 to transfer the letter "A" serially at 4800 baud continuously. Use 8-bIt data and 1 stop bit.
#include <regSl.h>
Void main(void)
{
TMOD=Ox20; //use Timer 1,8-BIT auto-reload
TH1=OXFA; //4800 baUd rate
SCON=OxSO;
TR1=l;
While(l)
{
SBUF=' A'; //place value in buffer
While (TI==O);
TI=O;
}
}
Write an 8051 C program to transfer the message "YES" serially at 9600 baud 8-bit data, 1 stop bit, Do this continuously.
#include <regSl.h>
Void SerTx(unsigned char);
Void main (void)
{
TMOD=Ox20; //use Timer 1,8-BIT auto-reload
THl=OxFD; //9600 baUd rate
SCON=OxSO; //start timer
TRl=l;
While(l)
{
SerTx('Y');
SerTx (,E ');
SerTx (,S ');
}
}
Void SerTx(unsigned char x)
{
SBUF=x; //place value in buffer
While (TI==O); //wait until transmitted
TI=O;
}
Program the 8051 in C to receive bytes of data serially and put them in Pl. Set the baud rate at 4800, 8-bit data, and I stop bit.
#include <regSl.h>
Void main (void)
{
Unsigned char mybyte; //use Timer 1,8-BIT auto-reload
TMOD=Ox20; //4800 baud rate
THl=OxFA;
SCON=OxSO;
TRl=l; //start timer
While (1) //repeat forever
{
,
While (RI==O); //wait to receive
Mybyte=SBUF; //save value
Pl=mybyte; //write value to port
RI=O;
}
}
Write an 8051 C program to send two different strings to the serial port. Assuming that SW is connected to pin P2.0, monitor its status and make a decision as follows:
SW = 0: send your first name
SW = I: send your last name
Assume XTAL= 11.0592MHz, baud rate of 9600, 8-bit data, I stop bit.
#include <reg51.h>
Sbit MYSW=P2AO; //input switch
Void main (void)
{
Unsigned char z,
Unsigned char fname []="ALI";
Unsigned char lname []="SMITH";
TMOD=Ox20; //use Timer l, a-BIT auto-reload
THl=OxFD; //9600 baud rate
SCON=Ox50;
TRI=l; //start timer
If (MYSW==O) //check switch
{
For(z=O;z<3;z++) //write name
{
SBUF=fname[z]; //place value in buffer
While (TI==O); //wait for transmit
TI=O;
}
}
Else
{
For(z=o;z<s;z++) //write name
{
SBUF=lname[z]; //place value in buffer
While (TI==O); //wait for transmit
TI=O;
}
}
}
Write an 8051 C program to send the two messages "Normal Speed" and "High Speed" to the serial port. Assuming that SW is connected to pin P2.0, monitor its status and set the baud rate as follows:
SW = 0 28,800 baud rate,
SW = 1 56K baud rate
Assume that XTAL = 11.0592 MHz for both cases.
#include <reg51.h>
Sbit MYSW=P2AO; //input switch
Void main(void)
{
Unsigned char. Z;
Unsigned char Messl []= "Normal Speed";
Unsigned char Mess2 [] ="High Speed";
TMOD=Ox20; //use Timer 1,8-BIT auto-reload
THI=OxFF; //28,800 for normal speed
SCON=Ox50;
TRl=l; //start timer
If (MYSW==O)
{
For(z=0;z<12;z++)
{
SBUF=Messl[z]; //place value in buffer
While (TI==O); //wait for transmit
TI=O;
}
}
Else
{
PCON=PCONIOx80;// for high speed of 56K
For(z=O;z<IO;z++)
{
SBUF=Mess2[z]; //place value in buffer
While (TI==O); //wait for transmit
TI=O;
}
}
}
Write a C program for the DS89C4xO to transfer letter "A" serially at 4800 baud continuously. Use the second serial port with 8-bit data and I stop bit. We can only use Timer 1 to set the baud rate.
#include <regsl.h>
Sfr SBUFhOxCl;
Sfr SCON1=OxCO;
Sbi t TIl=OXC1;
Void main (void)
(
TMOD=Ox20; //use Timer 1 for 2nd serial port
TH1=OxFA; //4800 baud rate
SCON1=OxSO; //use 2nd serial port SCONl register
TR1=1; //start timer
While (l)
{
SBUFl='A';//us~ 2nd serial port SBUFl register
While (TI1~=O); //walt for transmit
TI1=O;
}
}
Program the DS89C4xO in C to receive bytes of data serially via the serial port and put them in P1. Set the baud rate at 9600, 8-bit data and 1-stop bit. Use Timer 1 for baud rate generation.
#include <regS1.h>
Sfr SBUF1=OxC1'
Sfr SCON1=OXCO:
Sbit RI1=OxCO" ,
Void main (void)
{
Unsigned char mybyte;
TMOD=0x20; //use Timer1,8-BIT auto-reload
TH1=0xFD;//9600
SCON1=0xS0;//use SCON1 of 2nd serial port
TR1=1;
While (1)
{
While (RIl==O); //monitor RI1 of 2nd serial port
Mybyte=SBUF1'; //use SBUF1 of 2nd serial port
P2=mybyte; //place value on port
RIl=O;
}
}
References:
1. Kenneth J. Ayala, ‘The 8051 Microcontroller Architecture, Programming and Applications’, Cengage Learning, 3rd Edition
2. Ajay Deshmukh, “Microcontrollers Theory and Applications”, TATA McGraw Hill, 4th Edition
3. Peatman, John B, “Design with PIC Microcontroller”, Pearson Education PTE, 1st Edition
4. Data Sheet of PIC 18Fxxxx series