Download as pdf
Download as pdf
You are on page 1of 80
MODULE_2:1/O INTERFACING TECHNIQUES Agenda 2 (eee eee oO Memory Interfacing a A/D and D/A Converters a Timers and Counters a Watch-Dog Timer (WDT) o Encoder and Decoder o UART a Sensors and Actuators Timers and Counters =a a 8051 microcontrollers 15 have two timers /counters which work on the clock frequency 1 is a clock that contiols the sequence of an event while counting in fixed intervals of time 18. Timer/counter can be used for = time delay generation = counting external events Xtal Oscillator To 1 repeat or iniiate an action wines 5 Clock Every Timer needs a clock to work = 8051 provides it from an external crystal which is the main clock source for Timer. | The internal circuitry in the 8051 microcontrollers provides a clock source to the timers which Is 1/12th of the frequency of crystal attached to the microcontroller, also called Machine cycle frequency How to calculate the timer frequency, if the crystal frequency(Xtal) is given? = Timer clock frequency= (Xtal Oscillator frequency)/12 = (11.0592 MHz)/12 = 921.6 KHz = Period T= 1/Timer clock frequency = 1/(921.6 kHz}=1.085 US Timers and Counters + Timers in a controller are in-built chips that are controlled by special function registers (SFRs) assigned for Timer operations. SFRs used to configure Timers in different modes of operations + Two ways to generate Time Delays Software — using loops and are very precise + Timers — precise and accurate + Two Timers — Timer 0 and Timer 1 Timer 0 is controlled by two 8 bit SFRs: THO(Higher) & TLO(Lower) =8+8=16 bits Timer 1 is controlled by two 8 bit SFRs: TH1(Higher) & TL1(Lower)=8+8=16 bits value of a Timer register increases by one every time a timer counts + maximum number of times ai timer can count without repeating is 2'¢, i.e., 65536 + maximum allowed counts in value of Timer registers can be from OOOOH to FFFFH Working of Timers 8 0 The registers of Timers are loaded with some initial value O The value of a Timer register increases by one after every machine cycle. 0 If no value is loaded into the Timer, it starts counting from OOOOH. 0 When the Timer reaches FFFFH, it reloads to OOOOH. O This roll over is communicated to the controller by raisina. flag corresponding to that Timer “7 0 a ow Timer SFRs 2 (eee eee 0 Timer Control Reaisters ; F : 7 \ 7 oa TCON tm | me | ro | tro | cet m ‘0 ro | Toon = Used to start/stop timers = Contains bits to indicate the overflow of timers om TMOD a Timer / Counter kegisters oT a TLO = ealealeien[eafal = via T= Tel alain Tal oTH1 x " TCON Register Bit7 - TFT: Timer Overflow Flag 115 Timer! overflow occurred (1. Timer goes to its max and roll over back to zero}; 0 = Timer overflow not occurred. 1 cleared through software. Inthe Timer! overflow interrupt service routine, © Bit6-TRI:Timer? Run Control Bit 2 1=Timerl start; 0 = Timer! stop. It is sat ond cleared by software. 2 Bit 5—TFO:Timer0 Overflow Flag it will get cleared automatically while exiting from ISR. 2 1 = Timer0 overflow occurred (Le. TimerO goes to Its max end roll over back to ze10},;0 = Timer overflow not occurred. 8 cleared through software. n the Timer0 overflow interrupt service routine, ths bit will get cleared automatically while exiting from ISR. 2 Bit 4—TRO: Timer Run Control Bit 2 1=Timer0 start. 0=Timer0 stop. is set and cleared by software, © Bit3 -IE1: External interrupt] Edge Flag 5 1 = Externalinterrupt] occurred. 0 = Externel interrupt] Processed. It is set and cleared by hardware. © Bit 2- ITI: External interrupt] Trigger Type Select Bit a 1 = Interrupt occurs on falling edge ot INTI pin. = Interrupt occur on a low level atthe INT? pin © Bit 1 ~IEO: External Interrupt0 Edge Flag External interrupt0 occurred. 0 = Externallinterrupt0 Processed. [tis sat and cleared by hardware, © Bit O—ITO, External Interrupt0 Trigger Type Select Bit 1=errep! scr on fang edge st N10 pin = Ierypt ones ov @ HOW level t ITO ph TMOD Register =a a TMODis an 8- 1D hs lower 4 bits are used for Timer and the upper 4 bits are used for Timer! a Bit7,3 - GATE 5 1 = Enable Timer/Counter only when the INTO/INT1 pin is high and TRO/TR1 is set. 5 0 = Enable Timer/Counter when TRO/TR1 is set. co Bit 6,2 - C/ (Counter/Timer): Timer or Counter select bit ose] TB 0011 3:binsimermocle) | 13-bitimer/counter, Obit of THX & 5:bit oF Tix 11 (16-bit timer mode) 16-bit timer /counter, THx cascaded with TLx it register used to set timer mode of timerO and timer] O 1 = Use as Counter 5 0 = Use as Timer B.bit timer /counter (avto-reload mods), TLx reload oO 0 Bit 5:4 & 1:0- MIMO a 0 2(B-birawto-reloadmede) “i ge ej held by seach Tox onetion Timer /Counter mode select bit Split the 16-bit timer into two 8-bit mers ie. THx ond Dee Th lke two 8-bitimer MODE 0 (13 bit timer mode) 2 Mode 0 is a 13-bit timer mode for which 8-bit of THx and 5-bit of TLx (as Prescaler) are used. It is mostly used for interfacing possible with old MCS-48 family microcontrollers 1D. 8-bit of THx and lower 5-bit of TLx used to form a total 13-bit timer. Higher 3-bits of TLx should be written as zero while using timer mode0, or it will affect the result. 13-bittimer D7 Do | D7 I I ] Do Problem: EI msec 3} Generate a square wave of 2mSec period using an AT89C51 microcontroller with timerO in modeO ‘on the P1.0 pin of port. Assume xtal oscillator frequency of 11.0592 MHz. eelos [om MODE 0 (13 bit timer mode) * As the Xtal oscillator frequency is 11.0592 MHz we have a machine cycle of 1.085uSec * Hence, the required count to generate a delay of 1mSec. is, + Count =(1*10%-3)/(1.085% 104-6) = 921 * The maximum count of Moded is 2°13 (0 - 8191) and * the Timer0 count will increment from 0 = 8191. * So we need to load value which is 921 less from its maximum count ie. 8191. Also, here in the below program, we need an additional 13 MC (machine cycles) from call fo return of delay function. Hence value needed to be loaded is, . Value=(8191-Count)+Function_MCycles+1 =7284= 0x1C74 * So we need to load 0x1C74 value in TimerO. + 1C74 = 0001 1100 0111 0100 b, now load lower 5-bit in TLO and next 8-bit in THO * so here we get, + TLO = 0001 0100 = 0x14 and THO = 1110 0011 = OxE3 MODE 0 (13 bit timer mode) =e Oo Programming steps for delay function © Load Tmod register value = TMOD = 0x00 for Timer0/1 modeO (13 bit timer mode). © Load calculated THx value i.e. here THO = OxE3. © Load calculated TLx value i.e. here TLO = Ox14. O Start the timer by setting a TRx bit. i.e. here TRO = 1. © Poll TFx flag till it does not get set. © Stop the timer by clearing TRx bit. ie. here TRO = 0. © Clear timer flag TFx bit i.e. here TFO = 0. G Repeat from step 1 to 7 for the delay again. MODE 0 (13 bit timer mode) #include sbit test = P10; void timer_delay() { Oxe3; 145 TRO = 1; TMOD = 0x00; while(1) { test = ~test; timer_delay(}; /* Include x51 header file */ /* set test pin O of port! */ /* Timer0 delay function */ /* Load 8-bit in THO (here Timer0 used) */ /* Load 5-bit in TLO */ /* Start timerO */ /* Wait until timer0 flag set */ /* Stop timer0 */ /* Clear timer0 flag */ /* Timer0/1 mode0 (13-bit timer mode) */ /* Toggle test pin */ /* Call timer0 delay */ MODE 1 — 16 bit timer mode a (SSS ES oO Mode 1 is a 16-bit timer mode used to generate a delay Ouses 8-bit of THx and 8-bit of TLx to form a total 16-bit register Tex a ee Tx p15 bo Problem: Generate a square wave of 2mSec time period using an AT89C51 microcontroller with timerO in mode] on the P1.0 pin of port1. Assume Xtal oscillator frequency of 11.0592 MHz. ke I msec 4 MODE 1 (16 bit timer mode) As Xtal is 11.0592 MHz we have a machine cycle of 1.085uSec. Hence, the required count to generate a delay of ImSec. is, Count =(1<104(-3)} / (1.085x10%(-6) ) = 921 And model has a max count is 2°16 (0 - 65535) and it increments from 0 10 65535 so we need fo load value which is 921 less from its max. count i.e. 65535. Also, here in the below program, we need an additional 13 MC (machine cycles) from call to return of delay function. Hence value needed to be loaded is, Value=(65535-Count)+Function_MCycles+1 =64615= (FC74)Hex So we need to load FC74 Hex value higher byte in THO and lower byte in TLO as, THO = OxFC & TLO = 0x74 MODE 1 (16 bit timer) a 1. Load Tmod register value i.e. TMOD = 0x01 for TimerO mode] (16-bit timer mode). Load calculated THx value i.e. here THO = OxFC. Load calculated TLx value i.e. here TLO = 0x74. Start the timer by setting a TRx bit. i.e. here TRO = 1. Poll TFx flag till it does not get set. Stop the timer by clearing TRx bit. ie. here TRO = 0. Clear timer flag TFx bit i.e. here TFO = 0. Repeat from step 1 to 7 for the delay again. OPNOaARON MODE 1 (16 bit timer mode) Hinclude /* Include x51 header file */ sbit test = PINO; /* set test pin0 of portl */ void timer_delay() /* Timer0 delay function */ { /* Load higher 8-bit i /* Load lower 8-bit in TO */ TRO = 1; /* Start timer0 */ while(TFO /* Wait until timer0 flag set */ TRO = 0; /* Stop timer0 */ TFO = 0; /* Clear timer0 flag */ } void maing { TMOD = 0x01; /* TimerO model (16-bit timer mode) */ while(1) { test = ~test; /* Toggle test pin */ timer_delay(); /* Call timer0 delay */ } MODE 2 (8 bit auto reload mode) 0 Mode 2 is an 8-bit auto-reload timer mode. | 5 n this mode, we have to load the THx-8 bit value only. © when the Timer gets started, the THx value gets automatically loaded into the TLx and TLx starts counting from that value. o After the value of TLx overflows from the OxFF to 0x0, © the TFx flag gets set and again value from the THx gets automatically loaded into the TLx register. © That's why this is called the auto-reload mode. a Problem: generating a square wave on PORT1.0 with 200uSec. time period using Timer] in mode2. We will use 11.0592 MUM Neel x ral natn eieradav cenit cal MODE 2 (8 bit auto reload mode) As Xtal is 11.0592 MHz we have a machine cycle of 1.085uSec. Hence, the required count to generate a delay of ImSec. is, Count =(100104(-6)) / (1.085% 10%(-6) }=92 And mode2 has a max count is 28 (0 - 255) and it increment from 0 — 255 so we need to load value which is 92 less from its max. count i.e. 255. Hence value need to be load is, Value=(255-Count)+1 =164= OxAd So we need to load A4 Hex value in a higher byte as, THI = 0xA4 MODE 2 (8 bit auto reload mode) 2 (eee eee co Programming steps for delay function G Load Tmod register value i.e. TMOD = 0x20 for Timer] mode2 (8-bit timer auto reload mode). © Load calculated THx value i.e. here TH] = OxA4. © Load same value for TLx i.e. here TL] = OxA4. @ Start the timer by setting a TRx bit. i.e. here TRI = 1. @ Poll TFx flag till it does not get set. © Clear timer flag TFx bit i.e. here TF1 = 0. 0 Repeat from step 5 and 6 for the delay again. MODE 2 (8 bit auto reload mode) #include sbit test = P10; void main() { TMOD = 0x20; THI = 0x44; TLI = OxA4; TRI=1; while(1) { test = “test; while(TF] == 0}; TF1=0; } /* Include x51 header file */ /* set test pinO of port! */ /* Timer! mode? (8-bit auto reload timer mode) */ /* Load 8-bit in THI */ /* Load 8-bit in TL1 once */ /* Start timer */ /* Toggle test pin */ /* Wait until timer! flag set */ /* Clear timer! flag */ Timer interrupt in 8051 2 8051 has two timer interrupts assigned with different vector address. 3 When Timer count rolls over from its max valve to 0, it sets the timer flag ee) 0008H TFx. ooreH This will interrupt the 8051 to serve ISR (interrupt ser global and timer interrupt is enabled. 2 The timer overflow interrupt assigned with the vector address shown in the table. 8051 microcontroller jumps to's Ul directly to the vector address on the occurrence of a corresponding interrupt. ™ Timer interrupt in 8051 seas Problem: --Ge-c52—---+--- Generate a square wave of 10KHz on PORTI.0 using Timer0 interrupt. Use TimerO in model with 11.0592 MHz oscillator frequency. As Xtal is 11.0592 MHz we have a machine cycle of 1.085uSec. Hence, the required count to generate a delay of 50mSec. is, Count =(50*10%(-3)} / (1.085x10%(-6) ) = 46080 And mode] has a max count is 216 (0 - 65535) and it increment from 0 — 65535 so we need to load value which is 46080 less from its max. count i.e. 65535. Hence value need to be load is, Value=(65535-Count)+1 =19456= (4C00)Hex So we need to load 400 Hex value in a higher byte and lower byte as, THO = 0x4C & TLO = 0x00 Note that the TFO flag no need to clear by software as a microcontroller clears it after completing the ISR routine. Program for Timer interrupt in 8051 #include /* Include x51 header file */ sbit test = P1"0; /* set test pin0 of portl */ void Timer_init) { TMOD = 0x01; /* Timer0 model */ THO = Ox4c; /* 50ms timer value */ TLO = 0x00; TRO= 1; /* Start timerO */ } void Timer0_ISR{) interrupt 1 /* Timer0 interrupt service routine (ISR) */ { test = /* Toggle port pin */ TH /* 50ms timer value */ lo } int mainivoid) { /* Enable global interrupt */ /* Enable timer0 interrupt */ Timer_init(); while(1); Watch Dog Timer(WDT) Figure 1: A typical watchdog setup a Watchdog timer is a chip external to the a processor A watchdog timer (WDT) is a piece of hardware that can be used to automatically detect software anomalies and reset the processor if any occur. © a watchdog timer is based on a counter that counts down from some initial value to zero. © The embedded software selects the counter’s initial value and periodically restarts it (kicking the dog). = If the counter ever reaches zero before the software restarts it, the software is presumed to be malfunctioning and the processor's reset signal is asserted. = The processor (and the embedded software it's running) will be restarted UU Clock a ‘count —} Watchdog ese Signal a Kekwatchlog iro void main) { SCON=0x50; _//starting of a serial communication// TMOD=0x20; //selected the timer mode// TH1=0x¢0 _// load the baud rate// /imer is ON// '; //store the character inside a register// /icheck the interrupt register// //OFF the timer// while(1); //continuous loop// d #include void main { SCON=0x50; _//starting of a serial communication// TMOD=0x20; //selection of a timer mode// TH1:0xFD_// load the baud rate// TRi=1; — //Timer is ON// PORTO=SBUF; //send the data from SBUF to port0// while(RI=: / checking of an interrupt register// RI=0; TR1=0; — //OFF the timer// while(1); //stop the program when character is received// } UART - 8051 Serial Interrupt Programming Steps 2 (eee eee a 8051 UART has a serial interrupt 1 Whenever data is transmitted or received, serial interrupt flags TI and RI are activated respectively. 2 8051 serial interrupt has a vector address (0023H) 1 where it can jump to serve ISR (Interrupt service routine) if the global and serial interrupt is enabled. a Steps ). Set timer 1 in auto-reload mode. 2, Load THI with value as per required baud rate e.g. for 9600 baud rate load OxPD. 2. Load SCON with serial mode and control bits. e.g. for mode 1 and enable reception load 0x50. 4 Start timer! by setting TRI bit to 1. 5, Enable Global and serial interrupt bit, i.e. EA = 1 and ES = 1. Now whenever data is received or transmitted, the interrupt flag will set and the controller will jump to serial ISR. 7 Note that, TI/RI flag must be cleared by software in ISR UART - 8051 Serial Program for serial interrupt Let's Program 8051 (here AT89C51) to receive character data serially at 9600 baud rate in mode 1 and send received data on port 1 using a serial interrupt. + In this example, after a byte is received, the RI flag is set which will generate a serial interrupt After the interrupt, 8051 will jump to the serial ISR routine. In the ISR routine, we will send data to port 1. include "Include x51 header file void Seria SRO interupt 4 { ri See PL = SUF; "Give received data on port 1*/ Ae /* Enable global interrupt */ RI = 0; 7? Clear Ri flag */ 5 /* Enable serial interupt*/ ) void main() void UART_Init() { ‘ HOD = 0x20; /* Timer 1, 8-bit auto reload mode */ Pt = 0x00; 1" Make P1 output */ TH = OxFD; * Load value for 9600 baud rate */ Ext_int_init() 5 /* Call Ext. interrupt initialize */ ScON = 0x505 /* Mode 1, reception enable */ aT Init (); ‘TRI = /* Start timer 1 */ while(1); UART in interrupt mode #include Include x51 header fle #include define bBufferSize 20 volatile char teBuffer[txBufferSize); volatile char txBuiferindex; void Ext int Init) { EA=1;/" Enable global interrupt / ES =1;/" Enable Ext interrupt0"/ } void UART Init) { TMOD = 0x20;/" Timer 1, 6-bit auto reload mode”/ TH1 = OxFD; / Load value for 9600 baud rate”/ ‘SCON = 0x50; /* Mode 1, reception enable / TR1= 4;/* Start timer 17/ } ‘void Serial_tSRO interrupt + © iecria=at (xButtar{xSutterincex}!*0) SBUF = DButterixButterincext+]: Ti=0:/- Clear Titles 7 } weri==a9¢ Pa =SBUF: RI=0:/* Clear RIMlas~/ exBuftert19] = 0: SBUF = SBurteriOl: xButteringex= 4: ? Loic mai © 1 = 0x00: /- Make 2 output 7 : > Call Ext interrupt sey | Encoders and Decoders Encoders 7 6S SSS SSS aaee co Encoders convert 2N lines of input into a code of N bits and Decoders decode the N bits into 2N lines. 0 Encoders og An encoder is a combinational circuit that converts binary information in the form of a 2N input lines into N output lines, which represent N bit code for the input. man octal-to-binary encoder takes 8 input lines and generates 3 output lines =Y7 + Yo + Vi 8:3 Encoder al ) a v3 + ¥2 Ae = ¥7 + v5 + "YSYT\a Truth Table =Y7 +6 + YS + 4 Y7 +6 + Y3 +2 Y7 +5 +3 + Y1 Decoders 7 6S SSS SSS aaee Oo Abinary decoder is a digital circuit that converts a binary code into a set of outputs. © The binary code represents the position of the desired output and is used to select the specific output that is active. = The basic principle of a binary decoder is to assign a unique output to each possible binary code. For example, a binary decoder with 4 inputs and 24 = 16 outputs can assign a unique output to each of the 16 possible 4-bit binary codes. = The inputs of a binary decoder are usually active low, meaning that only one input is active (low) at any given time, and the remaining inputs are inactive (high). The active low input is used to select the specific output that is active. = There are different types of binary decoders, including priority decoders, which assign a priority to each output, and error-detecting decoders, which can detect errors in the binary code and generate an error signal. 2 to 4 Binary Decoder A ae, 8 a 5 pape Inverter a Inputs 3 5 @ : DB —o- 6 Q3 HB —a:i8 — Binary puts AND Gates — De 8 ia if D—a-1 Data Lines | Analog to Digital Converter (ADC) Audio Signal Processing 8051 - ADC SS a Analog to Digital Converter (ADC) Oo real-world data like temperature > Transducers > Voltage or current ™ necessary to convert those signals into digital signals G Successive Approximation(SAR) ADC ™ resolution for SAR ADCs ranges from 8 - 18 bits = small form factor with low power consumption = applies a binary search algorithm to convert the values, which is why the internal circuitry may be running at several MHZ but the actual sample rate is much less due to the Successive Approximation algorithm 8051-Successive Approximation ADC circuit al Sample and Hold Circuit Successive 1ev Digital to Analog Converter 8051-Successive Approximation ADC circuit 3 Components of ADC 10 Sample and Hold Circuit 1 takes samples from the analog input signal and hold them for particular period of time and then outputs the sampled part of input signal. This circuit is only useful for sampling few microseconds of input signal. 0 Comparator = compares the two inputs that are applied to it and produces an output. The output value of the comparator indicates which of the inputs is greater or lesser. 1D Digital to Analog Converter ® converts a digital input signal into an analog output signal. 1 Successive Approximation Register with the control 3 Operation | Whenever a new conversation starts, the sample and hold circuit samples the input signal. And that signal is ‘compared with the specific output signal of the DAC. Input Signal Waveform Command Input > Output Waveform 8051-Successive Approximation ADC circuit O Operation © The sampled input signal is 5.2V. = The reference voltage (Vref) of the ADC is Tov. = When — the conversion starts, the successive approximation register sets the most significant bit to 1 and all other bits to zero. = This means the value becomes 1, 0, 0, 0 = which means, for a 10V reference voltage, the DAC will produce a value of 5V which is half of the reference voltage. = Now this voltage will be compared to the input voltage and based on the comparator output, the output of the successive approximation register will be channel. sane say fee = fen} —N, oo a ees soc, otc vee peor en 8051-Successive Approximation ADC circuit 2 (6a Operation At the start of a conversion, the contents of the SAR is initialized so that the most significant bit (MSB) is set to a digital 1 (on) and all the — Veer + remaining bits are set to a digital O (off). This code is fed into the DAC, which then supplies the analog equivalent of this digital code {mic-sacle) into the comparator circuit for comparison with the sampled input voltage. If this analog voltage exceeds Vin the comparator causes the SAR to reset (reject) this bit; otherwise, the bit is left «1 1 (retained). Then the next most significant bit is set to 1 and the same test is done, continuing this hi binary search algorithm until every bit in the SAR : (DAC} has been tested. The resulting code is the ‘ ee digital approximation of the sampled input Poor ent voltage and is finally output by the SAR at the BT 3=0!eT2=1!eT1=0!BTO=1! end of the conversion (EOC) (se) | ' 10s) | Voac ’ 3VREF + W2VReF vn Veer 8051-Successive Approximation ADC circuit al oO Operation J Dif Vin (here 5.2Volts) is greater VOAC> IN than the output of the DAC / eg mthe most significant bit will stay as it is, and the next bit will be 7d set for a new comparison. 0 Otherwise, if the Vin input voltage (5.2 Volts) is less than yy. you ae Kor’ the DAC value, mT fay @ the most significant bit will be set Oe to zero, and the next bit will be set to 1 for a new comparison. 8051-Successive Approximation ADC circuit This is how the successive approximation ADC changes 1 bit at a time to determine the input voltage and produce the output value. © And whatever the value might be in four iterations, it will get the output digital code from the input value. = Finally, a list of all possible combinations for a four-bit successive approximation ADC is shown 8051-Successive Approximation ADC circuit 2 (eee eee O Properties of SAR ADC 0 Conversion Time (Tc) ®@ independent of the input voltage = for an N bit ADC, it will take N clock cycles @ Tc =N x Telk © Conversion Speed ™ around 2 - 5 Mega Samples Per Seconds(MSPS) m LIC2378 (10 MSPS) G Resolution — smallest input change gets recognized, causes a change in output ™ around 8 - 16 bits Digital to Analog Conversion (4-bit DAC) 4 The purpose is to convert the binary word to By = 109 analog voltage or current ™ 3 A weighted resistor DAC produces an analog output, which is almost equal to the digital (binary) input by using binary weighted resistors in the inverting adder circuit 3 As the Non-inverting input of the OpAmp is grounded (pin 3) V=0y, the voltage at the inverting input (pin 2) is also Ov. © Inverting input is a summing point meaning that when one of the switches (DO,D1,D2, D3) is closed, current will flow from -5v(Vref) through that resistor to the summing point. a Then the OpAmp will pull the current on the feedback resistor to produce a proportional eutnurvelfans Digital to Analog Conversion (4 bit DAC) a Closing DO leads to 0.05mA (V/R=5V/100K) | current flow through Feedback resistor Rf=10K “\*Y and produces an output voltage of 10K X | 0.05mA=0.5V. a 5 Closing D1 along with DO leads to 0.1mA (V/R=5V/50K) + 0.05mA (V/R=5V/100K) oy =0.15mA current flow through Feedback resistor 1 Rf=10K and produces an output voltage of 10K X 0.15mA=1.5V. 5 Take Away: The binary weighted resistors produces binary weighted currents which are summed up at the summing point of the OpAmp to produce a proportional output voltage © Technically, it is digital because its fixed Digital to Analag Conversion (4) bit DAC) Vary = 2 Buca Toun Veer MCIT41SG OR EQuiv, Vee Digital to Analog Conversion (4 bit DAC) Theoretical Vi, Veer A 4 AS ae Meer engi { Ab + AR, ADL AL DS s BO BE +23} RIFOR A, SO THAT WtTH ALL DIGITAL ADJUST Vue INPUTS AT ret LEVER 18 EQUAL TO9 961 eri Ae $odear ib? 33° 45+ ats} <10V {#8 -a961¥ Digital to Analog Conversion (8 bit DAC) BE (SSS SSS SSS SESS SESE EEEEEEES a Characteristics 5 Resolution = For 8 bit D/A converter: = For 12 bit D/A converter: 2" 1 Full scale output voltage The maximum output voltage of « 8 bit converter will always have a valve 1 loss than LSB less than named valve{her 10 V) Le, 10/256=0.039-9 10-0.039=9.961V = The maximum output voltage of a 12 bit converter will always have a valve 1 less than LSB less than named valve(here it 15 10 V) ke. 10/4096=0.0024-> 10-0.0024=9.9976V. a Accuracy © Difference between actual and expected output ~ represented as the percentage of the full scale output voltage or ‘current: plus or minus the value of LSB 5 Linearity = How much the output ramp deviates from straight-line as the converter stepped from no switches on to all switches on. 5 Setting Time 1 The time the output takes to get within plus or minus of the final value is called settling time .56->(1/256)=0.0039 1096-9(1 /4096)*100 itis Digital to Analog Conversion (3 bit DAC) ‘The nodal equation at the inverting input terminal's node is: 0+Vnbo 0+Vnbi 0+Vnbo PR | OR * 2R Yo _ Vabo , Vad Vabo Rr PR aR eR bi, bo pars Substituting, R = 2Ryf in above equation. Viel { bh bi, bo my +e fe a m= Mss} pow’ e Sensors: Interfacing ultrasonic sensor with 8051 2 (eee eee 0 Ultrasonic Module HC-SRO4 works on the principle of SONAR and RADAR system. o o The HC-SRO4 module has an ultrasonic transmitter, receiver, and control circuit on a single board. The module has only 4 pins, Vec, Gnd, Trig, and Echo. When a pulse of 10sec or more is given to the Trig pin, 8 pulses of 40 kHz are generated. After this, the Echo pin is made high by the control circuit in the module. The echo pin remains high till it gets the echo signal of the transmitied pulses back. The time for which the echo pin remains high, i.e. the width of the Echo pin gives the time taken for generated ultrasonic sound to travel towards the object and return. Using this time and the speed of sound in air, we can find the distance of the object using a simple formula for distance using speed and time. Ultrasonic Module Sensors: Interfacing ultrasonic sensor with 8051 2 (eee eee Oo Programming Steps © 8051 microcontroller needs to transmit at least 10 us trigger pulse to the HC- SRO4 Trig Pin. o After getting a trigger pulse, HC-SRO4 automatically sends eight 40 kHz sound waves and waits for rising edge output at the Echo pin. When the rising edge capture occurs at the Echo pin which is connected to the input of 8051, start Timer of 8051 and again wait for the falling edge on the Echo pin. © As soon as the falling edge is captured at the Echo pin, the microcontroller reads the count of the Timer. This time count is used to calculate the distance to an object. 2 Distance = (Sound Velocity X Time)/2=34300 X TimerCount X 1.085X10° ‘include vold send_trigger_oulse(){ Teigeer_in= 1s 77 pull trigger pin HGH */ include Deaay_usO3 [7 provide 10uS Delay~/ include > void main ‘define sound. velocity 34300 /* sound velocity in em per second*/ float ciztence measurement, valves Saree aot) unsigned char eistance_in_eml 101; Leo tests 77 initialize 16x2 LCD */ ‘define Clock period 1.085*period in.us _/* period for clock cycle Leo string ny -1,"Distance”)s Anit_eimerOs [7 tottistize Timer/ sbit Trigger pin=P2! ‘Trigger pin */ sbit Echo_pineP2"7; "Echo pin */ void Delay_us¢) senatctegeroulee0s cand tigger { while stene_pi9) (7 Waiting for E TLen0xFS; Two = 12 (Timer Starts THO=OxFF; while(ecno.pin 881TH ‘Watt fore ca eo = 0 Stop the while (TF0==0); £cateutat distance using Umer ‘TRO=0; Nelve » Clock-pertoa ~ sound.velocttys To=0; Gistence,earurenent = (TLOICTHOC void Timer_init() finclude { ‘TMOD = 0x01; ‘/*Timer0 mode1 */ /* Define value to be loaded in timer for PWM period of 20 milli second */ THO = (PWM_Period >> 8);/*20ms timer value */ #define PWM_Period 0xB7FE TL@ = PWM_Period; TRo = 1; /*Starttime0*/ sbit PW_Out_Pin = P20; /* PWM Out Pin for speed control */ } sbit Speed_Inc = P1*0; /* Speed Increment switch pin */ /*Timer0 interrupt service routine (ISR) */ sbit Speed_Dec = P11; /* Speed Decrement switch pin */ void Timer0_ISRQ interrupt 1 sbit Change_Dir = P12; /* Rotation direction change switch pin*/ PWM_Out_Pin !PWM_Out_Pin; sbit M_Pint = P1*6; /* Motor Pin 1 */ if(ewM_out_Pin) sbit M1_Pin2 = P1*7; /* Motor Pin2*/ { io THO = (ON_Period >> 8); unsigned int ON_Period, OFF_Period, DutyCycle, Speed; etary aero /* Function to provide delay of 1ms at 11.0592 MHz */ + void delay(unsigned int count) i { int i,35 THO = oa eae >> 8); TLO = OFF_Period; for(i=0; i Initially Motor Speed & Duty cycle is zero and in either direction */ void Motor_Init() t Speed = 0; m_pint = 15 m_pin2 = 0; ‘Set_DutyCycle_To(Speed) ; + int main t EA = 1; /* Enable global interrupt */ £T@ = 1; /* Enable timer0 interrupt */ Timer_init() Motor_Init() ; while(1) « /* Increment Duty cycle Le. speed by 10% for Speed_Inc Switch iM(Speed_tne == 1) t (Speed < 100) Speed += 105 Set_DutyCycle_To(Speed); while(Speed_tne == 1)3 elay(200)5 ‘/* Decrement Duty cycle ie. speed by 10% for Speed_Dec Switch */ if(Speed_Dec == 1) { if(Speed > 0) Speed -» 10; ‘Set _DutyCycle_To(Speed); while(Speed_Dec == 1); delay (200); » 72 Change rotation direction for Change_Dir Switch */ ¢chanee_ir == 1) < while Cchenge Bir == 15 6149200); External Memory Interfacing BE (SSS SSS SSS SSE SESE ESSE EEEEEEES a While the 8051 has a limited amount of internal memory, it is possible to extend the memory capacity by interfacing it with external memory devices © connecting external memory devices such as RAM and ROM = Involves connecting the memory devices to the microcontroller through a data bus (to send/receive data) and an address bus to select a specific memory location in the memor device. O To interface with external memory, the 8051 microcontroller uses dedicated pins © such as ALE (Address Latch Enable), 1 PSEN (Program Store Enable), and © RD (Read) and WR (Write) signals. O These signals are used to control the flow of data between the microcontroller and the external memory device. External Memory Intertacing when the EA pin is attached to Vec The program fetches to addresses 0000H through OFFFH are directed to the internal ROM in the 8051, and program fetches to addresses 1000H through FFFFH are directed to the external ROM/EPROM. When the EA pin is grounded, all addresses fetched by the program (OOOOH to FFFFH) are led to it. mH OFFA ¢ Eee ess 18 Exoral on) ‘ibGtend — Belkews NO tend Address Decoding * Address decoding is the process of generating chip select (CS) signals from the address bus for each device in the system. * The address bus lines are split into two sections: * The N most significant bits are used to generate the CS signals for different devices. * The M least significant bits are passed to the devices as addresses to the different memory cells. Addrarn dacoding ‘strategy Memory map 2 anawos wee 2 locks An Example * Let’s assume a simple microprocessor with 10 address lines. + Let’s assume we wish to implement 1 KB memory using 128x8 memory chips. * Solution: * We will need 8 memory chips (8 x 128 bytes = 1 KB). * We will need 3 address lines to select each one of the 8 chips. * Each chip will need 7 address lines to address its internal memory cells. ven? Address Decoding for 8051 If multiple ROMs and/or RAMs are interfaced to an 8051, address decoding is required. Typically, a decoder IC such as 74138 is used with its outputs connected to the chip select (/CS) inputs on the memory ICs. 8051 usually uses ROMs as code memory and RAMs as data memory. External ROMs and RAMs are enabled by different hardware signals: * ROMs are enabled by /PSEN signal. * RAMs are enabled by /RD and /WR signals. ROMs and RAMs may have the same address. mec aaeaee! Memory Organization On-chip PPrE enabled vie SEN aaa FRE External Data enables and WR * Accessing External Code Memory © External code memory is ROM enabled by /PSEN signal. © Port O & Port 2 are unavailable as I|/O ports. © Port O is ADO-AD7 bus & Port 2 is A8-A15 bus. Instructions to Access External ROM / Program Memory Mnemonic Operation Copy the contents of the external ROM MOVC A, @ : address formed by adding A and the DPTR, to A+DPTR A This operation will do copy This operation MOVC A, @ aetpe contents of the external ROM address formed + by adding A and the PC, to A. * Accessing External Data Memory © External code memory is RAM enabled by /RD & /WR signals using MOVX instruction. © Port O & Port 2 are unavailable as I/O ports. © Port 0 is ADO-AD7 bus & Port 2 is A8-A15 bus. Instructions to Access External Data Memory MOVX A, In this operation, it will copy the contents of @Rp the external address in Rp to A. MOVX A. Copy the contents of the external address in @DPTR DPTRto A. MOVX @Rp. Copy data from A to the external address in A Rp MOV X DPTR, Copy data from A to the external address in A DPTR.

You might also like