13-io扩展技术与存储器映射扩展
IO扩展技术与存储器映射扩展
io扩展方式代码如下:
#include <REGX52.H>
void Delay_ms(unsigned int xms) {
unsigned int i,j;
for(i = 0; i < xms;i++) {
for(j= 0; j< 299;j++);
}
}
void _74HC138(unsigned char n) {
switch(n) {
case 4:
P2 = (P2 & 0x1f) | 0x80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
}
}
void System_Init(void) {
_74HC138(5);
P0 = 0x00;
_74HC138(4);
P0 = 0xFF;
}
void LED_Running() {
_74HC138(4);
P0 = 0xF0;
Delay_ms(1000);
P0 = 0x0F;
Delay_ms(1000);
P0 = 0xFF;
Delay_ms(1000);
}
void SMG_Running(void) {
unsigned char i;
for(i = 0; i < 8;i++) {
_74HC138(6);
P0 = 0x01 << i;
_74HC138(7);
P0 = 0x00;
Delay_ms(1000);
}
P0 = 0xFF;
Delay_ms(1000);
}
void main(void) {
System_Init();
while(1) {
LED_Running();
SMG_Running();
}
}
存储器扩展方式代码如下:
#include <REGX52.H>
#include "absacc.h"
void Delay_ms(unsigned int xms) {
unsigned int i,j;
for(i= 0 ; i< xms;i++) {
for(j=0; j < 299; j++);
}
}
void System_Init(void) {
XBYTE[0X8000] = 0XFF;
XBYTE[0XA000] = 0X00;
}
void LED_Running(void) {
XBYTE[0X8000] = 0XF0;
Delay_ms(1000);
XBYTE[0X8000] = 0X0F;
Delay_ms(1000);
XBYTE[0X8000] = 0XFF;
Delay_ms(1000);
}
void SMG_Running(void) {
unsigned char i;
for(i=0;i<8;i++) {
XBYTE[0XC000] = 0X01 << i;
XBYTE[0XE000] = 0X00;
Delay_ms(1000);
}
XBYTE[0XE000] = 0XFF;
Delay_ms(1000);
}
void main(void) {
System_Init();
while(1) {
LED_Running();
SMG_Running();
}
}
注意事项:
- XBYTE后面是中括号[]
- 如果用到矩阵键盘,就不雅用寄存器扩展方式,因为与P3^6产生冲突