蓝桥杯基础模块9:IO口扩展与存储器映射

一、模块题目

蓝桥杯基础模块9:IO口扩展与存储器映射_第1张图片

二、原理简述

三、源码

(1)I0扩展

//头文件
#include "reg52.h"
//延时函数
void Delay(unsigned int t)
{
	while(t--);
	while(t--);
}
//573锁存器初始化选择
void SelectHC573(unsigned char channel)
{
	switch(channel)
	{
	  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 LEDRuning()
{
	SelectHC573(4);
	P0 = 0xf0; 
	Delay(60000);
	Delay(60000);
	P0 = 0x0f; 
	Delay(60000);
	Delay(60000);
	P0 = 0xff;
	Delay(60000);
	Delay(60000);
}

void SMGRunning()
{
	unsigned char i;
	for(i = 0; i < 8; i++)
	{
		SelectHC573(6);
		P0 = 0x01 << i;
		SelectHC573(7);
		P0 = 0x00; 
		Delay(60000);
		Delay(60000);
	}
	P0 = 0xff;
	Delay(60000);
	Delay(60000);
}
//主函数
void main()
{
	while(1)
	{
		LEDRuning();
		SMGRunning();
	}
}

(2)存储器扩展

//头文件
#include "reg52.h"
#include "absacc.h"
//延时函数
void Delay(unsigned int t)
{
	while(t--);
	while(t--);
}

void LEDRuning()
{
	XBYTE[0x8000] = 0xf0;
	Delay(60000);
	Delay(60000);
	XBYTE[0x8000] = 0x0f; 
	Delay(60000);
	Delay(60000);
	XBYTE[0x8000] = 0xff;
	Delay(60000);
	Delay(60000);
}

void SMGRunning()
{
	unsigned char i;
	for(i = 0; i < 8; i++)
	{
		XBYTE[0xc000] = 0x01 << i;
		XBYTE[0xe000] = 0x00; 
		Delay(60000);
		Delay(60000);
	}
	XBYTE[0xe000] = 0xff;
	Delay(60000);
	Delay(60000);
}
//主函数
void main()
{
	while(1)
	{
		LEDRuning();
		SMGRunning();
	}
}

四、开发板烧录结果

如题。程序运行时注意跳帽的选择与更改,用于切换IO口扩展和存储器扩展。

你可能感兴趣的:(单片机,单片机)