嵌入式设计应用

#include以普通ASCII格式(如使用Microsoft记事本程序)保存该文件。在命令提示符下,键入sdcc sdcctest.c,然后回车。如像图2那样没有任何反应,则说明程序编译成功。char str[6] = "MAXIM"; bit flag; void main(void) { if (strcmp(str,"MAXIM") == 0) flag = 0; else flag = 1; while(1); // program loop }

union
{
unsigned char a_byte;
struct
{
unsigned char bit0 : 1;
unsigned char bit1 : 1;
unsigned char bit2 : 1;
unsigned char bit3 : 1;
unsigned char bit4 : 1;
unsigned char bit5 : 1;
unsigned char bit6 : 1;
unsigned char bit7 : 1;
} a_bit;
} a;
bit b;
void main(void)
{
a.a_byte = 0x05;
a.a_bit.bit6 = 1;
b = 1;
while(1); // program loop
}
Assembly listing (.rst file):
...
159 ;sdcctest.c:21: a.a_byte = 5;
160 ; genPointerSet
161 ; genNearPointerSet
162 ; genDataPointerSet
0031 75 21 05 163 mov _a,#0x05
164 ;sdcctest.c:23: a.a_bit.bit6 = 1;
165 ; genPointerSet
166 ; genNearPointerSet
0034 78 21 167 mov r0,#_a
168 ; genPackBits
0036 E6 169 mov a,@r0
0037 44 40 170 orl a,#0x40
0039 F6 171 mov @r0,a
172 ;sdcctest.c:25: b = 1;
173 ; genAssign
003A D2 00 174 setb _b
175 ;sdcctest.c:27: while(1); // program loop
...
尽管在声明中“a”看起来是位寻址存储器,但汇编列表文件(来自由SDCC生成的.rst文件)表明变量并没有使用位寻址。在列表中不要混淆“a”和“_a”。“a”指累加器,而“_a”指变量。#include "sdcc_reg420.h"
data unsigned char outPort0 = 0x4A;
void main(void)
{
P0 = outPort0;
while (1); // program loop
}
该例中使用的定义文件sdcc_reg420.h见附录A。#include "sdcc_reg420.h"
xdata unsigned char ioPorts[2];
void main(void)
{
PMR |= 0x01; // Enable internal 1K SRAM
ioPorts[0] = 0x4A;
ioPorts[1] = 0x56;
P0 = ioPorts[0];
P1 = ioPorts[1];
while (1); // program loop
}
idata#include "sdcc_reg420.h"
idata unsigned int port0_x2;
void main(void)
{
while (1) // program loop
{
port0_x2 = P0 * 2;
}
}
pdata#include "sdcc_reg420.h"
code unsigned char out[10] = {0x03,0x45,0xFA,0x43,0xDD,
0x1A,0xE0,0x00,0x87,0x91};
void main(void)
{
data unsigned char i = 0;
while (1) // program loop
{
P0 = out[i++];
if (i==10)
i=0;
}
}
bit#include "sdcc_reg420.h"
#define ESCAPE 0x1B
bit esc_char_flag = 0;
void main(void)
{
P1 = 0x00;
while (!esc_char_flag)
{
if (P0 == ESCAPE)
esc_char_flag = 1;
}
P1 = 0xFF;
while (1); // program loop
}
sfrsfr at 0x80 P0;
sfr at 0x90 P1;
void main(void)
{
P0 = 0x00;
P1 = 0xFF;
while (1); // program loop
}
sbitsfr at 0x80 P0; // Port 0
sbit at 0x80 P0_0; // Port 0 bit 0
sbit at 0x81 P0_1; // Port 0 bit 1
sbit at 0x82 P0_2; // Port 0 bit 2
sbit at 0x83 P0_3; // Port 0 bit 3
sbit at 0x84 P0_4; // Port 0 bit 4
sbit at 0x85 P0_5; // Port 0 bit 5
sbit at 0x86 P0_6; // Port 0 bit 6
sbit at 0x87 P0_7; // Port 0 bit 7
void main(void)
{
P0 = 0x00; // P0 = 0x00
P0_4 = 1; // P0 = 0x10
while (1); // program loop
}
#include "sdcc_reg420.h"
unsigned char a = 0x4A;
unsigned int b = 0x0000;
unsigned char c[64] = {0x00};
unsigned char at 0x0010 y;
unsigned char at 0x0010 z;
void main(void)
{
for(b=0; b<64; b++)
c[b] = 0xAA;
y = 0xF1;
z = 0xF2;
a = c[5];
while (1); // program loop
}
使用SDCC时,尽管变量"y"和"z"分配同一个位置,也可进行无错误或警告的编译。如果要运行该程序,我们认为程序(a = c[5])中"a"最终将被设置为0xAA。但情况并非如此。"a"最终被分配的值为0xF2。Area Addr Size Decimal Bytes (Attributes)
-------------------------------- ---- ---- ------- ----- ------------
. .ABS. 0000 0000 = 0. bytes (ABS,OVR)
Value Global
-------- --------------------------------
...
0010 _y
0010 _z
...
Area Addr Size Decimal Bytes (Attributes)
-------------------------------- ---- ---- ------- ----- ------------
DSEG 0008 0043 = 67. bytes (REL,CON)
Value Global
-------- --------------------------------
0008 _a
0009 _b
000B _c
注意,变量名称前的下划线是由编译器添加的。如果"c"位于地址0x000B,长度为64字节,那么它将覆盖位于地址0x0010处的变量"y"和"z"。#include "sdcc_reg420.h"
data unsigned char at 0x002F n_byte;
bit at 0x78 n_bit0;
bit at 0x79 n_bit1;
bit at 0x7A n_bit2;
bit at 0x7B n_bit3;
bit at 0x7C n_bit4;
bit at 0x7D n_bit5;
bit at 0x7E n_bit6;
bit at 0x7F n_bit7;
void main(void)
{
n_byte = 0x00;
n_bit4 = 1;
P0 = n_byte; // P0 = 0x10
while (1); // program loop
}
sdcc --model-small sdcctest.c或者
sdcc --model-large sdcctest.c不要链接使用不同存储器模式编译的模块或目标文件。
void interrupt_identifier (void) interrupt interrupt_number using bank_number
{
...
}
其中interrupt_identifier可以是任意有效的SDCC函数名,interrupt_number代表中断在中断向量表中的位置。表1列出了DS89C430/450系列微控制器支持的每个中断的中断号。可选参数bank_number用于指示SDCC采用哪个寄存器区存储ISR中的局部变量。| Interrupt Name | Interrupt Vector | Interrupt Number |
| External Interrupt 0 | 0x03 | 0 |
| Timer 0 Overflow | 0x0B | 1 |
| External Interrupt 1 | 0x13 | 2 |
| Timer 1 Overflow | 0x1B | 3 |
| Serial Port 0 | 0x23 | 4 |
| Timer 2 Overflow | 0x2B | 5 |
| Power Fail | 0x33 | 6 |
| Serial Port 1 | 0x3B | 7 |
| External Interrupt 2 | 0x43 | 8 |
| External Interrupt 3 | 0x4B | 9 |
| External Interrupt 4 | 0x53 | 10 |
| External Interrupt 5 | 0x5B | 11 |
| Watchdog Interrupt | 0x63 | 12 |
#include "sdcc_reg420.h"
volatile unsigned char n = 0x4A;
void sci1ISR (void) interrupt 7
{
if (RI_1)
{
n = SBUF1+1; // Save Rx byte
RI_1 = 0; // Reset SCI_1 Rx interrupt flag
}
else if (TI_1)
{
SBUF1 = n; // Load byte to Tx
TI_1 = 0; // Reset SCI_1 Tx interrupt flag
}
}
void main(void)
{
// 1. Init Serial Port
EA = 0; // Enable global interrupt mask
SCON1 = 0x50; // Set SCI_1 to 8N1, Rx enabled
TMOD |= 0x20; // Set Timer 1 as Mode 2
TH1 = 0xDD; // Set SCI_1 for 2400 baud
TR1 = 1; // Enable Timer 1
ES1 = 1; // Enable interrupts for SCI_1
EA = 1; // Disable global interrupt mask
// 2. Initiate SCI_1 Tx
SBUF1 = n;
// 3. Program loop...
while (1);
}
#include "sdcc_reg420.h"
unsigned char a;
void main(void)
{
// program loop...
while (1)
{
a = P0;
_asm
nop
nop
nop
inc _a
_endasm;
P1 = a;
}
}
SDCC还可用于C和汇编函数接口,这是较深入的问题;请参考SDCC手册,了解详细信息。/* * sdcc_reg420.h * * MAXIM INTEGRATED PRODUCTS * * Special Function Register definitions file * DS89C430/450 Ultra-High Speed 8051-compatible uCs * */ #ifndef __REG420_H__ #define __REG420_H__ /* BYTE Registers */ sfr at 0x80 P0; sfr at 0x81 SP; sfr at 0x82 DPL; sfr at 0x83 DPH; sfr at 0x84 DPL1; sfr at 0x85 DPH1; sfr at 0x86 DPS; sfr at 0x87 PCON; sfr at 0x88 TCON; sfr at 0x89 TMOD; sfr at 0x8A TL0; sfr at 0x8B TL1; sfr at 0x8C TH0; sfr at 0x8D TH1; sfr at 0x8E CKCON; sfr at 0x90 P1; sfr at 0x91 EXIF; sfr at 0x96 CKMOD; sfr at 0x98 SCON0; sfr at 0x99 SBUF0; sfr at 0x9D ACON; sfr at 0xA0 P2; sfr at 0xA8 IE; sfr at 0xA9 SADDR0; sfr at 0xAA SADDR1; sfr at 0xB0 P3; sfr at 0xB1 IP1; sfr at 0xB8 IP0; sfr at 0xB9 SADEN0; sfr at 0xBA SADEN1; sfr at 0xC0 SCON1; sfr at 0xC1 SBUF1; sfr at 0xC2 ROMSIZE; sfr at 0xC4 PMR; sfr at 0xC5 STATUS; sfr at 0xC7 TA; sfr at 0xC8 T2CON; sfr at 0xC9 T2MOD; sfr at 0xCA RCAP2L; sfr at 0xCB RCAP2H; sfr at 0xCC TL2; sfr at 0xCD TH2; sfr at 0xD0 PSW; sfr at 0xD5 FCNTL; sfr at 0xD6 FDATA; sfr at 0xD8 WDCON; sfr at 0xE0 ACC; sfr at 0xE8 EIE; sfr at 0xF0 B; sfr at 0xF1 EIP1; sfr at 0xF8 EIP0; /* BIT Registers */ /* P0 */ sbit at 0x80 P0_0; sbit at 0x81 P0_1; sbit at 0x82 P0_2; sbit at 0x83 P0_3; sbit at 0x84 P0_4; sbit at 0x85 P0_5; sbit at 0x86 P0_6; sbit at 0x87 P0_7; /* TCON */ sbit at 0x88 IT0; sbit at 0x89 IE0; sbit at 0x8A IT1; sbit at 0x8B IE1; sbit at 0x8C TR0; sbit at 0x8D TF0; sbit at 0x8E TR1; sbit at 0x8F TF1; /* P1 */ sbit at 0x90 P1_0; sbit at 0x91 P1_1; sbit at 0x92 P1_2; sbit at 0x93 P1_3; sbit at 0x94 P1_4; sbit at 0x95 P1_5; sbit at 0x96 P1_6; sbit at 0x97 P1_7; /* SCON0 */ sbit at 0x98 RI_0; sbit at 0x99 TI_0; sbit at 0x9A RB8_0; sbit at 0x9B TB8_0; sbit at 0x9C REN_0; sbit at 0x9D SM2_0; sbit at 0x9E SM1_0; sbit at 0x9F SM0_0; sbit at 0x9F FE_0; /* P2 */ sbit at 0xA0 P2_0; sbit at 0xA1 P2_1; sbit at 0xA2 P2_2; sbit at 0xA3 P2_3; sbit at 0xA4 P2_4; sbit at 0xA5 P2_5; sbit at 0xA6 P2_6; sbit at 0xA7 P2_7; /* IE */ sbit at 0xA8 EX0; sbit at 0xA9 ET0; sbit at 0xAA EX1; sbit at 0xAB ET1; sbit at 0xAC ES0; sbit at 0xAD ET2; sbit at 0xAE ES1; sbit at 0xAF EA; /* P3 */ sbit at 0xB0 P3_0; sbit at 0xB1 P3_1; sbit at 0xB2 P3_2; sbit at 0xB3 P3_3; sbit at 0xB4 P3_4; sbit at 0xB5 P3_5; sbit at 0xB6 P3_6; sbit at 0xB7 P3_7; /* IP0 */ sbit at 0xB8 LPX0; sbit at 0xB9 LPT0; sbit at 0xBA LPX1; sbit at 0xBB LPT1; sbit at 0xBC LPS0; sbit at 0xBD LPT2; sbit at 0xBE LPS1; /* SCON1 */ sbit at 0xC0 RI_1; sbit at 0xC1 TI_1; sbit at 0xC2 RB8_1; sbit at 0xC3 TB8_1; sbit at 0xC4 REN_1; sbit at 0xC5 SM2_1; sbit at 0xC6 SM1_1; sbit at 0xC7 SM0_1; /* T2CON */ sbit at 0xC8 CP_RL_2; sbit at 0xC9 C_T_2; sbit at 0xCA TR_2; sbit at 0xCB EXEN_2; sbit at 0xCC TCLK; sbit at 0xCD RCLK; sbit at 0xCE EXF_2; sbit at 0xCF TF_2; /* PSW */ sbit at 0xD0 PARITY; sbit at 0xD0 P; sbit at 0xD1 F1; sbit at 0xD2 OV; sbit at 0xD3 RS0; sbit at 0xD4 RS1; sbit at 0xD5 F0; sbit at 0xD6 AC; sbit at 0xD7 CY; /* WDCON */ sbit at 0xD8 RWT; sbit at 0xD9 EWT; sbit at 0xDA WTRF; sbit at 0xDB WDIF; sbit at 0xDC PFI; sbit at 0xDD EPFI; sbit at 0xDE POR; sbit at 0xDF SMOD_1; /* EIE */ sbit at 0xE8 EX2; sbit at 0xE9 EX3; sbit at 0xEA EX4; sbit at 0xEB EX5; sbit at 0xEC EWDI; /* EIP0 */ sbit at 0xF8 LPX2; sbit at 0xF9 LPX3; sbit at 0xFA LPX4; sbit at 0xFB LPX5; sbit at 0xFC LPWDI; #endif
全部0条评论
快来发表一下你的评论吧 !