22 Register Indirect Addressing ModeIn this mode, register is used as a pointer to the data.MOV ; move content of RAM loc.Where address is held by Ri into A( i=0 or 1 )MOV @R1,BIn other word, the content of register R0 or R1 is sources or target in MOV, ADD and SUBB insructions.Example:Write a program to copy a block of 10 bytes from RAM location sterting at 37h to RAM location starting at 59h.Solution:MOV R0,37h ; source pointerMOV R1,59h ; dest pointerMOV R2,10 ; counterL1: MOVMOV @R1,AINC R0INC R1DJNZ R2,L1jumpACOE343 - Embedded Real-Time Processor Systems - Frederick University

23 Indexed Addressing Mode And On-Chip ROM AccessThis mode is widely used in accessing data elements of look-up table entries located in the program (code) space ROM at the 8051MOVCA= content of address A +DPTR from ROMNote:Because the data elements are stored in the program (code ) space ROM of the 8051, it uses the instruction MOVC instead of MOV. The “C” means code.ACOE343 - Embedded Real-Time Processor Systems - Frederick University

27 16-bit, BCD and Signed Arithmetic in 8051Exercise:Write a program to add n 16-bit number. Get n from port 1. And sent Sum to SSDa) in hexb) in decimalWrite a program to subtract P1 from P0 and send result to LCD(Assume that “ACAL DISP” display A to SSD )ACOE343 - Embedded Real-Time Processor Systems - Frederick University

32 ACOE343 - Embedded Real-Time Processor Systems - Frederick UniversityHow to use the stackYou can use the stack as temporary storage for variables when calling functionsRLC A ;you can only rotate ACall functionDIV AB ; A has the wrong value!!!!!…function: MOV A, #5 ;values are for example sakeMOV B, #10MUL AB ;you can only multiply on ARETACOE343 - Embedded Real-Time Processor Systems - Frederick University

37 ACOE343 - Embedded Real-Time Processor Systems - Frederick UniversitySJMP and LJMP:LJMP(long jump)LJMP is an unconditional jump. It is a 3-byte instruction in which the first byte is the opcode, and the second and third bytes represent the 16-bit address of the target location. The 20byte target address allows a jump to any memory location from 0000 to FFFFH.SJMP(short jump)In this 2-byte instruction. The first byte is the opcode and the second byte is the relative address of the target location. The relative address range of 00-FFH is divided into forward and backward jumps, that is , within -128 to +127 bytes of memory relative to the address of the current PC.ACOE343 - Embedded Real-Time Processor Systems - Frederick University

39 ACOE343 - Embedded Real-Time Processor Systems - Frederick UniversityCALL InstructionsAnother control transfer instruction is the CALL instruction, which is used to call a subroutine.LCALL(long call)In this 3-byte instruction, the first byte is the opcode an the second and third bytes are used for the address of target subroutine. Therefore, LCALL can be used to call subroutines located anywhere within the 64K byte address space of the 8051.ACOE343 - Embedded Real-Time Processor Systems - Frederick University

40 ACOE343 - Embedded Real-Time Processor Systems - Frederick UniversityACALL (absolute call)ACALL is 2-byte instruction in contrast to LCALL, which is 13 bytes. Since ACALL is a 2-byte instruction, the target address of the subroutine must be within 2K bytes address because only 11 bits of the 2 bytes are used for the address. There is no difference between ACALL and LCALL in terms of saving the program counter on the stack or the function of the RET instruction. The only difference is that the target address for LCALL can be anywhere within the 64K byte address space of the 8051 while the target address of ACALL must be within a 2K-byte range.ACOE343 - Embedded Real-Time Processor Systems - Frederick University

52 Port 1 as Input（Read from Port）In order to make P1 an input, the port must be programmed by writing 1 to all the bit.MOV A,#0FFH ;A= BMOV P1,A ;make P1 an input portBACK: MOV A,P ;get data from P0MOV P2,A ;send data to P2SJMP BACKTo be an input port, P0, P1, P2 and P3 have similar methods.Program is to read data from P0 and then send data to P1ACOE343 - Embedded Real-Time Processor Systems - Frederick University

56 Read-modify-write FeatureRead-modify-write InstructionsTable C-6This features combines 3 actions in a single instruction：1. CPU reads the latch of the port2. CPU perform the operation3. Modifying the latch4. Writing to the pinNote that 8 pins of P1 work independently.ACOE343 - Embedded Real-Time Processor Systems - Frederick University

57 Port 1 as Input（Read from latch）Exclusive-or the Port 1：MOV P1,#55H ;P1=AGAIN: XOR P1,#0FFH ;complementACALL DELAYSJMP AGAINNote that the XOR of 55H and FFH gives AAH.XOR of AAH and FFH gives 55H.The instruction read the data in the latch (not from the pin).The instruction result will put into the latch and the pin.ACOE343 - Embedded Real-Time Processor Systems - Frederick University

59 You are able to answer this Questions:How to write the data to a pin？How to read the data from the pin？Read the value present at the external pin.Why we need to set the pin first？Read the value come from the latch（not from the external pin）.Why the instruction is called read-modify write?ACOE343 - Embedded Real-Time Processor Systems - Frederick University

60 ACOE343 - Embedded Real-Time Processor Systems - Frederick UniversityOther PinsP1, P2, and P3 have internal pull-up resisters.P1, P2, and P3 are not open drain.P0 has no internal pull-up resistors and does not connects to Vcc inside the 8051.P0 is open drain.Compare the figures of P1.X and P0.X. However, for a programmer, it is the same to program P0, P1, P2 and P3.All the ports upon RESET are configured as output.ACOE343 - Embedded Real-Time Processor Systems - Frederick University

62 ACOE343 - Embedded Real-Time Processor Systems - Frederick UniversityPort 0（pins 32-39）P0 is an open drain.Open drain is a term used for MOS chips in the same way that open collector is used for TTL chips. When P0 is used for simple data I/O we must connect it to external pull-up resistors.Each pin of P0 must be connected externally to a 10K ohm pull-up resistor.With external pull-up resistors connected upon reset, port 0 is configured as an output port.Open drain is a term used for MOS chips in the same way that open collector is used for TTL chips.ACOE343 - Embedded Real-Time Processor Systems - Frederick University

81 Reading timers on the flyReading the Timers on the Fly To read the contents of a timer while it is running (ie; on the fly) poses a problem.MOV A, TH0 ;Let TH0 = 07 and TL0 = FFMOV R6, TL0 ;Now TH0=08 and TL0=00 but A=07 and R6=00!The solution is to read the high byte, then read the low byte, then read the high byte again. If the two readings of the low-byte are not the same repeat the procedure. The code for this method is detailed below. tryAgain:MOV A, TH0 MOV R6, TL0 CJNE A, TH0, tryAgainif the first reading of the hi-byte (in A) is not equal to current reading in the hi-byte (TH0) try againMOV R7, A; if both readings of the hi-byte are the same move the first reading into R7 - the overall reading is now in R7R6 ACOE343 - Embedded Real-Time Processor Systems - Frederick University

82 Generating delays using the timersTo generate a 50 ms (or 50,000 us) delay we start the timer counting from 15,536. Then, 50,000 steps later it will overflow. Since each step is 1 us (the timer's clock is 1/12 the system frequency) the delay is 50,000 us. 0 MOV TMOD, #10H; set up timer 1 as 16-bit interval timer CLR TR1 ; stop timer 1 (in case it was started in some other subroutine) MOV TH1, #3CH MOV TL1, #0B0H ; load 15,536 (3CB0H) into timer 1 SETB TR1 ; start timer 1 JNB TF1, $; repeat this line while timer 1 overflow flag is not set CLR TF1; timer 1 overflow flag is set by hardware on transition from FFFFH - the flag must be reset by software CLR TR1 ; stop timer 1ACOE343 - Embedded Real-Time Processor Systems - Frederick University

83 Generating long delaysIf the microcontroller has a system clock frequency of 12 MHz then the longest delay we can get from either of the timers is 65,536 usec.To generate delays longer than this, we need to write a subroutine to generate a delay of (for example) 50 ms and then call that subroutine a specific number of times.... MOV TMOD, #10H; set up timer 1 as 16-bit interval timerfiftyMsDelay:CLR TR1 ; stop timer 1 (in case it was started in some other subroutine) MOV TH1, #3CH MOV TL1, #0B0H ; load 15,536 (3CB0H) into timer 1 SETB TR1 ; start timer 1 JNB TF1, $; repeat this line while timer 1 overflow flag is not set CLR TF1; timer 1 overflow flag is set by hardware on transition from FFFFH - the flag must be reset by software CLR TR1 ; stop timer 1 RET oneSecDelay:PUSH PSW PUSH AR0 ; save processor status MOV R0, #20 ; move 20 (in decimal) into R0loop:CALL fiftyMsDelay ; call the 50 ms delay DJNZ R0, loop ; 20 times - resulting in a 1 second delay POP AR0 POP PSW ; retrieve processor status RETACOE343 - Embedded Real-Time Processor Systems - Frederick University

91 Programming microcontrollers using high-level languagesMost programs can be written exclusively using high-level code like ANSI CExtensionsTo achieve low-level (Assembly) efficiency, extensions to high-level languages are requiredRestrictionsDepending on the compiler, some restrictions to the high-level language may applyACOE343 - Embedded Real-Time Processor Systems - Frederick University

99 ACOE343 - Embedded Real-Time Processor Systems - Frederick UniversityThe infinite loopA loop with no termination condition or one that will never be met may be unwanted in computer systems, but common in embedded systems.ACOE343 - Embedded Real-Time Processor Systems - Frederick University

111 Signals (Analog - Digital)246810121416u(V)17359t (S)Analog Signalcan take infinity valuescan change at any time10101110111111001000Digital Signalcan take one of 2 values (0 or 1)can change only at distinct timesReconstruction of an analog signal from a digital one (Can take only predefined values)1001011001010100246810121416u(V)17359t (S)ADC100101100101101011111110100011000100D3D2D1D011111DACACOE343 - Embedded Real-Time Processor Systems - Frederick University

112 ACOE343 - Embedded Real-Time Processor Systems - Frederick UniversityQUANTIZATION ERRORThe difference between the true and quantized value of the analog signalInevitable occurrence due to the finite resolution of the ADCThe magnitude of the quantization error at each sampling instant is between zero and half of one LSB.Quantization error is modeled as noise (quantization noise)ACOE343 - Embedded Real-Time Processor Systems - Frederick University

113 SAMPLING FREQUENCY (RATE)The frequency at which digital values are sampled from the analog input of an ADCA low sampling rate (undersampling) may be insufficient to represent the analog signal in digital formA high sampling rate (oversampling) requires high bitrate and therefore storage space and processing timeA signal can be reproduced from digital samples if the sampling rate is higher than twice the highest frequency component of the signal (Nyquist-Shannon theorem)Examples of sampling ratesTelephone: 4 KHz (only adequate for speech, ess sounds like eff)Audio CD: 44.1 KHzRecording studio: 88.2 KHzACOE343 - Embedded Real-Time Processor Systems - Frederick University

114 Digital to Analog ConvertersVout(mV)151015202530354045505560657075The analog signal at the output of a D/A converter is linearly proportional to the binary code at the input of the converter.If the binary code at the input is 0001 and the output voltage is 5mV, thenIf the binary code at the input becomes 1001, the output voltage will become45mVIf a D/A converter has 4 digital inputs then the analog signal at the output can have one out of …… values.16If a D/A converter has N digital inputs then the analog signal at the output can have one out of ……. values.2ΝACOE343 - Embedded Real-Time Processor Systems - Frederick University

115 Characteristics of Data ConvertersNumber of digital linesThe number bits at the input of a D/A (or output of an A/D) converter.Typical values: 8-bit, 10-bit, 12-bit and 16-bitCan be parallel or serialMicroprocessor CompatibilityMicroprocessor compatible converters can be connected directly on the microprocessor bus as standard I/O devicesThey must have signals like CS, RD, and WRActivating the WR signal on an A/D converter starts the conversion process.PolarityPolar: the analog signals can have only positive valuesBipolar: the analog signals can have either a positive or a negative valueFull-scale outputThe maximum analog signal (voltage or current)Corresponds to a binary code with all bits set to 1 (for polar converters)Set externally by adjusting a variable resistor that sets the Reference Voltage (or current)ACOE343 - Embedded Real-Time Processor Systems - Frederick University

116 Characteristics of Data Converters (Cont…)ResolutionThe analog voltage (or current) that corresponds to a change of 1LSB in the binary codeIt is affected by the number of bits of the converter and the Full Scale voltage (VFS)For example if the full-scale voltage of an 8-bit D/A converter is 2.55V the the resolution is:VFS/(2N-1) = 2.55 /(28-1) 2.55/255 = 0.01 V/LSB = 10mV/LSBConversion TimeThe time from the moment that a “Start of Conversion” signal is applied to an A/D converter until the corresponding digital value appears on the data lines of the converter.For some types of A/D converters this time is predefined, while for others this time can vary according to the value of the analog signal.Settling TimeThe time needed by the analog signal at the output of a D/A converter to be within 10% of the nominal value.ACOE343 - Embedded Real-Time Processor Systems - Frederick University

120 ACOE343 - Embedded Real-Time Processor Systems - Frederick UniversityProgramming Example 1Write a program to generate a positive ramp at the output of an 8-bit D/A converter with a 2V amplitude and a 1KHz frequency. Assume that the full scale voltage of the D/A converter is 2.55V. The D/A converter is in P0 and the WR signal is in P1.1main(){do {for (i=0;i<200;i++)P1_0=1;P0=i;delayu(5);}} while (1)ACOE343 - Embedded Real-Time Processor Systems - Frederick University

121 D/A Converters exampleWrite a program to generate the waveform, shown below, at the output of an 8-bit digital to analog converter. The period of the waveform should be approximately 8 ms. Assume that a time delay function with a 1 μs resolution is available. The full scale output of the converter is 5.12 V and the address of the DAC is P0, while the WR signal is in P1.1.Assuming that an 8-bit A/D converter is used to interface a temperature sensor measuring temperature values in the temperature range, specify: The resolution in of the system inThe digital output word for a temperature of 32.5The temperature corresponding to a digital output word ofACOE343 - Embedded Real-Time Processor Systems - Frederick University