Download MSC121x User's Guide

Transcript
Referencing SFRs in Assembly and C Languages
3.2 Referencing SFRs in Assembly and C Languages
When writing programs in assembly language, an SFR can be referenced by
its absolute address or by a symbol associated with its address. In C language,
a variable must first be declared, as shown in Example 3−1.
For assembly language programs, declarations that associate common symbols with values are usually grouped in an include file with the name *.inc that
is referenced in the source code. Similarly, for C language, declarations appear in a file with the name *.h.
Example 3−1.Assembly Code and C Code
Purpose
Assembly Code
Output the character ‘A’ to
serial port 0
SBUF0
DATA
Enable interrupt for Timer 1
C Code (Compiler Dependant Directives)
99H
MOV
99H,#41H
MOV
SBUF0,#41H
−
IE
DATA
0A8H
at 0xA8 sfr IE;
ET1
BIT
0ABH
at 0xAB sbit ET1;
ET1
BIT
IE.3
sbit ET1=IE^3;
SETB
ET1
decimation
DATA
0DEH
at 0xDE sfr16 decimation
MOV
decimation,#0E8H
MOV
decimation+1,#3
SBUF0=0x41;
or
Set the decimation ratio for
the ADC to 3E8h
at 0x99 sfr SBUF0;
or
ET1=1;
decimation=1000;
1)
Indicating a hexadecimal number in assembly language requires a trailing H and leading 0 if the first character would otherwise be a letter; for example, 99H or 099H but 0A8H instead of A8H.
2)
In C, a hexadecimal number always starts with 0x; for example, 0x99 and 0xA8.
3)
The keyword ‘sfr16’ cannot be used with TH0:TL0 as Timer0 because the addresses are not adjacent. This is also true
for TH1:TL1. However, ‘sfr16’ is allowed with TH2:TL2 as Timer2 because TH2 and TL2 are adjacent.
Special Functions Registers
3-3