Modular Programming: Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Modular Programming: Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
assemble build/make
adder.M51
Map file shows where variables Symbol Table in M51 file:
------ DO
are stored. One map file is
D:0008H SYMBOL x
generated per project. D:000AH SYMBOL y
D:000CH SYMBOL z
------- ENDDO
do
statements
while (expression);
bdata
idata
xdata
Example:
bit bit new_flag; //stored in 20-2F
sbit sbit LED = P1^6;
sfr sfr SP = 0x81; //stack pointer
$INCLUDE (c8051F020.h)
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
C Data Types With Extensions
Blinking!
#pragma asm
put your assembly code here
#pragma endasm
build/make assemble
program.OBJ
Must use this path for C programs with in-line assembly
It is also necessary to add #pragma SRC to code
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Example Switch/LED Program
#include <c8051F020.h>
#pragma SRC // Need this to generate .SRC file
void PORT_Init (void);
char Get_SW(void) {
#pragma ASM
mov a, P3
anl a, #80h ; mask all but P3.7
mov R7, a ; function value (char) returned in R7
#pragma ENDASM
}
void Set_LED(void) {
#pragma ASM
setb P1.6
Functions can be implemented
#pragma ENDASM
}
in assembly language
void Clr_LED(void) {
#pragma ASM
clr P1.6
#pragma ENDASM
}
void PORT_Init (void){ XBR2 = 0x40; // Enable crossbar and enable P1.6 (LED) as push-pull output}
P1MDOUT |= 0x40; // enable P1.6 (LED) as push-pull output
}
void main(void) {
PORT_Init(); Main function
while (1)
if (Get_SW()) Set_LED();
else Clr_LED();
}
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Interfacing with C
Example: Temperature Sensor program
Configures the external oscillator
Configures the ADC0 for temp. sensor
Configures Port1 so LED can be used
Configures Timer3 to synch the ADC0
Uses ADC0 ISR to take temperature samples and
averages 256 of them and posts average to global
variable
Main program compares average temp. to room temp.
and lights LED if temp is warmer.
Temp_2.c
ADC 0 42380
TempC
156
Temperature Conversion
0 Constant Folding: The compiler performs calculations that reduce expressions to numeric constants,
where possible.This includes calculations of run-time addresses.
Simple Access Optimizing: The compiler optimizes access of internal data and bit addresses in the
8051 system.
Jump Optimizing: The compiler always extends jumps to the final target. Jumps to jumps are deleted.
1 Dead Code Elimination: Unused code fragments and artifacts are eliminated.
Jump Negation: Conditional jumps are closely examined to see if they can be streamlined or eliminated
by the inversion of the test logic.
2 ....
3
4
5
6
7
8
9 Common Block Subroutines: Detects recurring instruction sequences and converts them into
subroutines. Cx51 evenrearranges code to obtain larger recurring sequences.
Therefore, if
not connected
to anything,
inputs are read
as 1.
Subroutines
Interrupt
ret
Service
Routine Background thread
reti