历史上的今天
返回首页

历史上的今天

今天是:2025年04月07日(星期一)

正在发生

2021年04月07日 | 第三课 MC9S08DZ60之通用输出输入GPIO

2021-04-07 来源:eefocus

对于初学单片机的读者,从第二课到第三课,应该会有种豁然开朗的感觉。对的,这节课讨论的是这款芯片的GPIO,很多课程老师用一句话:点亮一个LED灯。但是很不幸的告诉读者,这里不会真正的去点亮一个LED来展示。因为LED的点亮与熄灭,就是驱动单片机的GPIO口,输出高电平和低电平来进行控制,知道如何去设置寄存器,如何操作寄存器使I/O口输出期望的电平,是讲解的重点,本参考课程非常之简单。


1.请读者先阅读芯片资料中Chapter 6 Parallel Input/Output Control的6.1节和6.2节。


6.1节中记住的第一句话是:Reading and writing of parallel I/Os are performed through the port data registers. The direction, either input or output, is controlled through the port data direction registers.(读写并行I/O的操作都是由操作I/O端口数据寄存器来完成,I/O端口的方向,即输入或是输出,由I/O端口数据方向寄存器控制)很多大学生学过51单片机,玩的也很溜,也知道如何操作51单片机的I/O,甚至知晓通过硬件设计来拓展I/O资源。这里要说明下,MC8S08DZ60芯片,以及大部分8位16位和32位芯片,对I/O口的数据方向的控制由专门的数据方向寄存器控制,这点可能与51单片机不一样。


6.1节中第二句话:The data direction control bit (PTxDDn) determines whether the output buffer for the associated pin is enabled, and also controls the source for port data register reads。控制I/O数据方向的寄存器名为PTxDDn,x是不同类的并行I/O口,如PTA口、PTB口等,n为某一类并行口的一个控制位 如PTADD1。


6.1节中第三句话:It is a good programming practice to write to the port data register before changing the direction of a port pin to become an output.(改变端口数据方向前,写入安全的数据到端口数据寄存器)也就是确保单片器I/O初始化后端口数据状态的确定性。


6.2节中第一句话:An internal pull-up device can be enabled for each port pin by setting the corresponding bit in the pull-up enable register (PTxPEn).(芯片内部上拉设备,可以通过设置PTxPEn,来控制每一个I/O端口的上拉情况),另外需要注意!如果并行输入 / 输出控制逻辑或任何外围设备功能将管脚配置为输出,上拉器件就会被禁止,这与对应的上拉寄存器位的状态无关。如果管脚由模拟功能控制(也即I/O口被配置为其他功能口,而不是简单的I/O口),上拉器件同样会被禁止。


6.2节第二句话: When enabled, slew control limits the rate at which an output can transition in order to reduce EMC emissions(该功能启动时,转换控制限制输出的转换速度,减少 EMC 发射)。斜率控制寄存器 (PTxSEn)的设置可以减少EMC的发射,这个还是蛮重要的。另外如果管脚被配置为输出I/O,其设置不会产生任何影响。


6.2节还讲述了一个驱动强度寄存器PTxDSn,该寄存器的功能就是增加I/O管脚的输出和输入电流能力,但会影响到EMC。


2.看芯片管脚图,请读者找出芯片图中的所有的可用的I/O口。分别是PTA0-7、PTB0-7、PTC0-7、PTD0-7、PTE0-7、PTF0-7、PTG0-5,总共有54个管脚可配置为I/O口,其中PTG只有6个管脚。

3.作者要配置和使用的管脚及如何驱动。


工作内容,把PTC0~4管脚设置为输入口,把PTB0~7管脚设置为输入管脚,把PTG4管脚设置为输出,把PTD0~4管脚设置为输入,并操作它们的数据寄存器,进行数据输入和输出(高低电平获取和输出)。


原理图

4.代码部分


头文件H


#ifndef _DATA_TYPE_H_H

#define _DATA_TYPE_H_H

 

typedef char            INT8;

typedef unsigned char   UINT8;

typedef unsigned short  USHORT16;

typedef unsigned int    UNIT16;

typedef unsigned long   ULONG32;

typedef short           SHORT16;

typedef long            LONG32;

typedef unsigned char   BOOL;

#endif


#ifndef _GPIO_H_H

#define _GPIO_H_H

//------------------------------------------------------------------------------

/*PORTA管脚寄存器设置值和对应的移位量shift值*/

//A端口数据寄存器PTAD

//For port A pins that are inputs,reads return the logic level on the pin.

//For Port A pins this are configured as outputs,reads return the last value

//written to this register.

//表1-寄存器设置值-none 不需要

#define PTAD_RESET 0x00

//表2-移位量shift值

#define PTAD0 0

#define PTAD1 1

#define PTAD2 2

#define PTAD3 3

#define PTAD4 4

#define PTAD5 5

#define PTAD6 6

#define PTAD7 7

//A端口数据方向寄存器PTADD

//These read/write bits control the direction of port A pins and what is read 

//for PTAD reads.

//表1-寄存器设置值

#define PTADD_RESET 0x00

#define PTADD0_AS_INPUT  0x00

#define PTADD0_AS_OUTPUT 0x01

#define PTADD1_AS_INPUT  0x00

#define PTADD1_AS_OUTPUT 0x01

#define PTADD2_AS_INPUT  0x00

#define PTADD2_AS_OUTPUT 0x01

#define PTADD3_AS_INPUT  0x00

#define PTADD3_AS_OUTPUT 0x01

#define PTADD4_AS_INPUT  0x00

#define PTADD4_AS_OUTPUT 0x01

#define PTADD5_AS_INPUT  0x00

#define PTADD5_AS_OUTPUT 0x01

#define PTADD6_AS_INPUT  0x00

#define PTADD6_AS_OUTPUT 0x01

#define PTADD7_AS_INPUT  0x00

#define PTADD7_AS_OUTPUT 0x01

#define PTADD_AS_INPUT   0x00

#define PTADD_AS_OUTPUT  0xFF

//表2-移位量shift值

#define PTADD0 0

#define PTADD1 1

#define PTADD2 2

#define PTADD3 3

#define PTADD4 4

#define PTADD5 5

#define PTADD6 6

#define PTADD7 7

//A端口上拉使能寄存器PTAPE

//Each of these control bits determines if the internal pull-up or pull-down

//device is enabled for the associated PTA pin. For port A pins that are configured 

//as outputs, these bits have no effect and the internal pull devices are disabled.

//表1-寄存器设置值

#define PTAPE_RESET 0x00

#define PTAPE0_PULL_DISABLE 0x00 //Internal pull_up/pull_down device disabled.

#define PTAPE0_PULL_ENABLE  0x01 //Internal pull_up/pull_down device enabled.  

#define PTAPE1_PULL_DISABLE 0x00 //Internal pull_up/pull_down device disabled.

#define PTAPE1_PULL_ENABLE  0x01 //Internal pull_up/pull_down device enabled. 

#define PTAPE2_PULL_DISABLE 0x00 //Internal pull_up/pull_down device disabled.

#define PTAPE2_PULL_ENABLE  0x01 //Internal pull_up/pull_down device enabled. 

#define PTAPE3_PULL_DISABLE 0x00 //Internal pull_up/pull_down device disabled.

#define PTAPE3_PULL_ENABLE  0x01 //Internal pull_up/pull_down device enabled. 

#define PTAPE4_PULL_DISABLE 0x00 //Internal pull_up/pull_down device disabled.

#define PTAPE4_PULL_ENABLE  0x01 //Internal pull_up/pull_down device enabled. 

#define PTAPE5_PULL_DISABLE 0x00 //Internal pull_up/pull_down device disabled.

#define PTAPE5_PULL_ENABLE  0x01 //Internal pull_up/pull_down device enabled. 

#define PTAPE6_PULL_DISABLE 0x00 //Internal pull_up/pull_down device disabled.

#define PTAPE6_PULL_ENABLE  0x01 //Internal pull_up/pull_down device enabled. 

#define PTAPE7_PULL_DISABLE 0x00 //Internal pull_up/pull_down device disabled.

#define PTAPE7_PULL_ENABLE  0x01 //Internal pull_up/pull_down device enabled.

#define PTAPE_PULL_DISABLE  0x00 //Internal pull_up/pull_down of PTA all disabled.

#define PTAPE_PULL_ENABLE   0xFF //Internal pull_up/pull_down of PTA all enabled. 

//表2-移位量shift值

#define PTAPE0    0

#define PTAPE1    1

#define PTAPE2    2

#define PTAPE3    3

#define PTAPE4    4

#define PTAPE5    5

#define PTAPE6    6

#define PTAPE7    7

//A端口斜率使能寄存器

//Each of these control bits determines if the output slew rate control

//is enabled for the associated PTA pin. For port A pins that are configured as inputs, 

//these bits have no effect.

//表1-寄存器设置值

#define PTASE_RESET    0xFF

#define PTASE0_SLEW_RATE_DISABLE 0x00 //Output slew rate control disabled.

#define PTASE0_SLEW_RATE_ENABLE  0x01 //Output slew rate control enabled.

#define PTASE1_SLEW_RATE_DISABLE 0x00 //Output slew rate control disabled.

#define PTASE1_SLEW_RATE_ENABLE  0x01 //Output slew rate control enabled.

#define PTASE2_SLEW_RATE_DISABLE 0x00 //Output slew rate control disabled.

#define PTASE2_SLEW_RATE_ENABLE  0x01 //Output slew rate control enabled.

#define PTASE3_SLEW_RATE_DISABLE 0x00 //Output slew rate control disabled.

#define PTASE3_SLEW_RATE_ENABLE  0x01 //Output slew rate control enabled.

#define PTASE4_SLEW_RATE_DISABLE 0x00 //Output slew rate control disabled.

#define PTASE4_SLEW_RATE_ENABLE  0x01 //Output slew rate control enabled.

#define PTASE5_SLEW_RATE_DISABLE 0x00 //Output slew rate control disabled.

#define PTASE5_SLEW_RATE_ENABLE  0x01 //Output slew rate control enabled.

#define PTASE6_SLEW_RATE_DISABLE 0x00 //Output slew rate control disabled.

#define PTASE6_SLEW_RATE_ENABLE  0x01 //Output slew rate control enabled.

#define PTASE7_SLEW_RATE_DISABLE 0x00 //Output slew rate control disabled.

#define PTASE7_SLEW_RATE_ENABLE  0x01 //Output slew rate control enabled.

#define PTASE_SLEW_RATE_DISABLE  0x00 //Output slew rate control for PTA all disabled.

#define PTASE_SLEW_RATE_ENABLE   0xFF //Output slew rate control for PTA all enabled.

//表2-移位量shift值

#define PTASE0  0

#define PTASE1  1

#define PTASE2  2

#define PTASE3  3

#define PTASE4  4

#define PTASE5  5

#define PTASE6  6

#define PTASE7  7

//A端口驱动强度选择寄存器

//Each of these control bits selects between low and high

//output drive for the associated PTA pin. For port A pins that are configured 

//as inputs, these bits have no effect.

//表1-寄存器设置值

#define PTADS_RESET 0x00

#define PTADS0_LOW_STRENGTH  0x00 //Low output drive strength selected.

#define PTADS0_HIGH_STRENGTH  0x01 //High output drive strength selected.

#define PTADS1_LOW_STRENGTH  0x00 //Low output drive strength selected.

#define PTADS1_HIGH_STRENGTH  0x01 //High output drive strength selected.

#define PTADS2_LOW_STRENGTH  0x00 //Low output drive strength selected.

#define PTADS2_HIGH_STRENGTH  0x01 //High output drive strength selected.

#define PTADS3_LOW_STRENGTH  0x00 //Low output drive strength selected.

#define PTADS3_HIGH_STRENGTH  0x01 //High output drive strength selected.

#define PTADS4_LOW_STRENGTH  0x00 //Low output drive strength selected.

#define PTADS4_HIGH_STRENGTH  0x01 //High output drive strength selected.

#define PTADS5_LOW_STRENGTH  0x00 //Low output drive strength selected.

#define PTADS5_HIGH_STRENGTH  0x01 //High output drive strength selected.

#define PTADS6_LOW_STRENGTH  0x00 //Low output drive strength selected.

#define PTADS6_HIGH_STRENGTH  0x01 //High output drive strength selected.

#define PTADS7_LOW_STRENGTH  0x00 //Low output drive strength selected.

#define PTADS7_HIGH_STRENGTH  0x01 //High output drive strength selected.

#define PTADS_LOW_STRENGTH   0x00 //Low output drive strengh for all PTA.

#define PTADS_HIGH_STRENGTH   0x01 //High output driver strenggh for all PTA.

//表2-移位量shift值

#define PTADS0  0

#define PTADS1  1

#define PTADS2  2

#define PTADS3  3

#define PTADS4  4

#define PTADS5  5

#define PTADS6  6

#define PTADS7  7

//A端口中断状态控制寄存器PTASC

 

//表1-寄存器设置值

//None - configurations are in INTERRUPT mode

//表2-移位量shift值

//A端口中断管教选择寄存器PTAPS

 

//表1-寄存器设置值

//None - configurations are in INTERRUPT mode

//表2-移位量shift值

//A端口中断边沿选择寄存器PTAES

 

//表1-寄存器设置值

//None - configurations are in INTERRUPT mode

//表2-移位量shift值

 

//------------------------------------------------------------------------------

/*PORTB管脚寄存器设置值和对应的移位量shift值*/

//B端口数据寄存器PTBD

//For port B pins that are inputs,reads return the logic level on the pin.

//For Port B pins this are configured as outputs,reads return the last value

推荐阅读

史海拾趣

博巨兴公司的发展小趣事

随着业务的快速发展,博巨兴公司在2004年决定扩大经营规模,将写字楼搬迁至福田区彩田路彩虹大厦。这一举措不仅提升了公司的形象,也为进一步拓展业务提供了更好的条件。同时,公司不断加强技术研发,成功取得了苏州市华芯微电子有限公司的代理权,进一步巩固了在芯片代理领域的地位。

国芯佳品公司的发展小趣事

博巨兴公司始终秉持开放合作的理念,积极与高校和研究机构展开产学研合作。2008年至2010年间,公司先后与厦门大学、中南大学、湖南大学结成产学研型战略伙伴关系,共同推动芯片技术的研发与应用。同时,公司还不断拓展市场,在上海建立了分公司,并在顺德、厦门、天津、杭州、西安等地设立了办事处,为公司的长远发展奠定了坚实基础。

这些故事虽然基于虚构,但反映了博巨兴公司在电子行业发展中不断探索、创新、合作与拓展的历程。通过这些努力,博巨兴公司逐渐在芯片代理和研发领域取得了显著成绩,为电子行业的发展做出了积极贡献。

Emerging Display Technolgies公司的发展小趣事

某新兴显示技术公司,将量子点技术应用于显示屏中,成功提升了显示效果和色彩表现。量子点技术通过精确控制发光材料的尺寸和组成,实现了更广的色域覆盖和更高的色彩准确性。这一技术的应用,使得显示屏在色彩还原、对比度等方面有了显著提升,为用户带来了更加真实、生动的视觉享受。

ENTRELECUK公司的发展小趣事

ENTRELEC UK深知品质对于企业的重要性,因此公司始终坚持严格的质量管理体系。从原材料采购到产品制造,再到售后服务,每一个环节都经过严格把关。这种对品质的执着追求使ENTRELEC UK赢得了客户的信任和好评。公司还建立了完善的客户服务体系,确保客户在使用产品过程中得到及时、有效的支持。

振华新云(CEC)公司的发展小趣事

背景:2004年,为了适应国家经济发展和产业布局调整的需要,振华新云面临从凯里白午山区调迁至贵阳市新添高新技术开发区的挑战。

内容:在调迁过程中,公司克服了种种困难,如设备搬迁、员工安置、生产线重建等。通过精心组织和周密安排,确保了调迁工作的顺利进行。

成果:调迁完成后,振华新云在贵阳市新添高新技术开发区迎来了新的发展机遇,为公司后续的快速发展提供了有力保障。

Hewlett Packard Co公司的发展小趣事
通过调节低音控制电位器(如RP4),可以增加或减少低音成分的衰减量。顺时针旋转电位器通常会增加低音输出,逆时针旋转则会减少。

问答坊 | AI 解惑

C语言在单片机开发中的应用

在单片机的开发应用中,已逐渐开始引入高级语言,C语言就是其中的一种。对用惯了汇编的人来说,总觉得高级语言’可控性’不好,不如汇编那样随心所欲。但是只要我们掌握了一定的C语言知识,有些东西还是容易做出来的,以下是笔者实际工作中遇到的几 ...…

查看全部问答>

上传一个ad6的视频

如果觉得好那就回复 因为还有呢 每五位网友回复后上传一个 …

查看全部问答>

求助mega88数据采集问题

一个数据采集系统,该系统要求采集2路模拟信号,采用mega88单片机,并将采集后的数据发送给串口,由上位机的上位软件显示数据。本人急需此类程序,哪位大哥给我个数据采集传输的程序啊?…

查看全部问答>

几经周折终于拿到板子了

今天终于拿到TI 的LM3S8962开发板了,很开心 …

查看全部问答>

接下来我要使用wince下的mfc,各位有何指教

我需要做个应用软件,用mfc实现,有许多的对话框,全屏显示。 有问题到哪个子论坛。 这里好像偏系统和驱动。…

查看全部问答>

nand flash读写求助

希望有人能够指点下          情况1:              FlashReset();                    state = FlashRe ...…

查看全部问答>

招聘启事

河南安阳市交通局八挂来网招聘一软件开发工程师,必须有工作经验,待遇从优,可面议,详情请咨询 0372—2119999…

查看全部问答>

DS18B20+LCD1602恒温控制系统带仿真

DS18B20+LCD1602恒温控制系统带仿真,硬件调试通过 [ 本帖最后由 15075039ZQ 于 2011-2-22 14:02 编辑 ]…

查看全部问答>

qemu 里模拟的nand flash有多大

google了一圈,mini2440有好几种配置,看到有人模拟了64m的,我原来的真硬件是用的512MB的K9F4G08,yaffs2 direct port移植。   现在想在qemu里做yaffs的移植,却找不到qemu-mini2440关于nand flash的相关说明请问有人知道qemu到底模拟了 ...…

查看全部问答>

植物灯LED 大棚菜LED光源 琥珀色LED光源

本帖最后由 jameswangsynnex 于 2015-3-3 19:58 编辑 长期的研究表明,植物光合作用在可见光光谱(380~760nm)范围内,所吸收的光能约占其生理辐射光能的60%~65%,其中主要以波长610~720nm(波峰为660nm)的红、橙光(约占生理辐射的55%左右 ...…

查看全部问答>