历史上的今天
今天是:2025年07月26日(星期六)
2021年07月26日 | MC9S12XEP100 ATD模块 驱动程序
2021-07-26 来源:eefocus
这几天在研究ATD模块,把芯片手册的ATD模块通读了好几遍,这里是本人对手册的翻译:http://blog.csdn.net/lin_strong/article/details/78286661。本以为完全理解了这个模块,结果一上手才发现还是有很多坑。目前已发现的坑已经写在.c文件里头了。也可能是因为封装模块时测试是用的芯片仿真才导致的那些坑,后面有空的话进行进一步的测试。
自己对ATD模块进行了一个简单的封装。发现网上流传的大部分示例都是使用同步的方式(轮询转换完成标志位)来获得结果,不符合我追求效率的强迫症性格,所以这个模块中,我是使用了异步事件通知的方式实现的。同时由于硬件提供的多通道方法不够灵活,通道间必须连续;于是自己还加了个用软件方式实现的多通道,实现能够跳着通道扫描自己需要的AN端口。
当前版本没有实现外部触发功能,只是封装了转换功能;同时由于目前我在使用uCOS-II,所以直接使用了一些OS提供定义和变量,如果不使用uCOS的话,.c文件中大致讲了下修改的方法。
下面给出源代码。
ATD.h:
/*
*********************************************************************************************************
*
*
* ATD SUPPORT PACKAGE
* Freescale MC9S12XEP100
* 飞思卡尔单片机ATD支持包
*
* File : ATD.h
* By : Lin Shijun(http://blog.csdn.net/lin_strong)
* Date : 2017/10/22
* Version : V1.0
* Note : 1.Just for convenient,the current version only support right unsigned result.
* 2.you should point ATD0's interrupt address(default $FFD2) to ATD0_RxTxISR(see ATD.s).
* 3.this implementation don't support external trigger. If you want this feature,some modification
* must be made.
* 4.this module take the asynchronous method to inform a successful conversion event.
* Listen to onATDConverted event to get the result.
* be careful,this event is called by ISR, return as soon as possible.
* 5.this implementation is based on uC/OS-II,if you don't use this OS,see comment in ATD.c
*********************************************************************************************************
*/
#ifndef ATD_MODULE_H
#define ATD_MODULE_H
/*
*********************************************************************************************************
* INCLUDES
*********************************************************************************************************
*/
#include #include "MC9S12XEP100.h" /* ********************************************************************************************************* * CONSTANTS ********************************************************************************************************* */ #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif #ifndef NULL #define NULL 0x00 #endif //typedef byte INT8U; //typedef word INT16U; // ATD通道编号 参数名为portNum的需使用这个宏 // Number of ATD channel,use this marco when arugment name is "portNum" #define ATD0_NUM 0 #define ATD1_NUM 1 #define ATD2_NUM 2 #define ATD3_NUM 3 #define ATD4_NUM 4 #define ATD5_NUM 5 #define ATD6_NUM 6 #define ATD7_NUM 7 #define ATD8_NUM 8 #define ATD9_NUM 9 #define ATD10_NUM 10 #define ATD11_NUM 11 #define ATD12_NUM 12 #define ATD13_NUM 13 #define ATD14_NUM 14 #define ATD15_NUM 15 #define ATDNULL 0xFF // ATD通道 参数名为ports的需使用这个宏,可以使用位运算多选,如 ATD0_PORT | ATD2_PORT // bit of ATD channel,use this marco when arugment name is "ports", can use '|' to multiselect,e.g ATD0_PORT | ATD2_PORT #define ATD0_PORT 0x0001 #define ATD1_PORT 0x0002 #define ATD2_PORT 0x0004 #define ATD3_PORT 0x0008 #define ATD4_PORT 0x0010 #define ATD5_PORT 0x0020 #define ATD6_PORT 0x0040 #define ATD7_PORT 0x0080 #define ATD8_PORT 0x0100 #define ATD9_PORT 0x0200 #define ATD10_PORT 0x0400 #define ATD11_PORT 0x0800 #define ATD12_PORT 0x1000 #define ATD13_PORT 0x2000 #define ATD14_PORT 0x4000 #define ATD15_PORT 0x8000 // INIT option #define ATDINIT_PRECISION_8BIT 0 // 8位精度转换结果 #define ATDINIT_PRECISION_10BIT 1 #define ATDINIT_PRECISION_12BIT 2 #define ATDINIT_SAMPLETIME_4CIRCLE 0 // 4个A/D时钟周期 #define ATDINIT_SAMPLETIME_6CIRCLE 1 #define ATDINIT_SAMPLETIME_8CIRCLE 2 #define ATDINIT_SAMPLETIME_10CIRCLE 3 #define ATDINIT_SAMPLETIME_12CIRCLE 4 #define ATDINIT_SAMPLETIME_16CIRCLE 5 #define ATDINIT_SAMPLETIME_20CIRCLE 6 #define ATDINIT_SAMPLETIME_24CIRCLE 7 #define ATDINIT_RSPINFRZ_IGNORE 0 // 当背景调试下遇到断点时,忽略它。 #define ATDINIT_RSPINFRZ_COMPLETE 2 // 当背景调试下遇到断点时,完成当前转换序列然后再冻结。 #define ATDINIT_RSPINFRZ_FREEZE 3 // 当背景调试下遇到断点时,立即冻结。 // sample option #define ATD_SAMPLE_MODE_ONESHOT 0x00 // 采样一次 #define ATD_SAMPLE_MODE_CONTINUE 0x01 // 连续采样 #define ATD_SAMPLE_START_INIDLE 0x00 // 如果在采样过程中调用它,则会忽视采样请求并报错 #define ATD_SAMPLE_START_ANYWAY 0x02 // 不管是否在采样中,都立刻重新开始采样 // error code #define ATD_ERR_NONE 0x00 // if no error #define ATD_ERR_INVALIDCHANNEL 0x01 // if the specified channel is invalid #define ATD_ERR_ARG_OUTOFRANGE 0x02 // if the arugment is out of range #define ATD_ERR_RUNNING 0x03 // if want to start sampling when the module is running. #define ATD_ERR_NOTINIT 0x04 // if the module haven't init. #define ATD_ERR_UNKNOWN 0x05 // if unknown error happen. /* ********************************************************************************************************* * CONFIGURE ********************************************************************************************************* */ #ifndef BUS_CLOCK #define BUS_CLOCK 32000000L // specify the frequency of bus clock,or define it somewhere. #endif #define ATD_PRECISION ATDINIT_PRECISION_12BIT // specify the precision of atd result(see ATDINIT_PRECISION_XXXX). #define ATD_SAMPLETIME ATDINIT_SAMPLETIME_4CIRCLE // specify the time of a sample (see ATDINIT_SAMPLETIME_XXXX). #define ATD_DISCHARGE TRUE // whether to discharge before start sampling. #define ATD_STOPINWAIT TRUE // whether to stop when in wait mode(will coninue when resume). #define ATD_RSPINFRZ ATDINIT_RSPINFRZ_COMPLETE // specify what to do when in freeze mode(see ATDINIT_RSPINFRZ_XXXX). // code generation #define ATD_ARGUMENT_CHECK_EN TRUE // whether to add argument check code to each function // NOTE: if TRUE,you can save a few code,however,it become // your responsibility to ensure the validity of arguments. #define ATD_CODE_SAMPLEMULT_EN TRUE // TRUE:enable code for ATD_StartSampleMult #define ATD_CODE_SAMPLEREQ_EN TRUE // TRUE:enable code for ATD_isSampling #define ATD_CODE_SAMPLESTOP_EN TRUE // TRUE:enable code for ATD_StopSample // the maxvalue of different percision. #if(ATD_PRECISION == ATDINIT_PRECISION_8BIT) #define ATD_MAXVALUE 0xFF #elif(ATD_PRECISION == ATDINIT_PRECISION_10BIT) #define ATD_MAXVALUE 0x3FF #elif(ATD_PRECISION == ATDINIT_PRECISION_12BIT) #define ATD_MAXVALUE 0xFFF #else #error "please select the valid precision." #endif /*
史海拾趣
|
有时因为没有电路图,但是自己所从事的行业好歹与电子沾边,于是总喜欢拆开来看看或者动手调调。 有的时候,不小心就被某个地方电一下。 哎,现在有点怕了。 请大家说说你们是否被电过?如何预防?… 查看全部问答> |
|
请教有哪位兄弟用过9261/9263+WINCE5.0没,我刚到一新公司用9263开发产品,打算用WINCE的操作系统,公司买了一个EBD9261的开发板,这个开发板商却不提供WINCE的技术支持,给的一个BSP也不能用,我在MCUZONE上下载了一个,也没法正常引导NK,不知有 ...… 查看全部问答> |
|
function gps; sDataPath = \'D:\\s\\\'; sDataFile = dir(sDataPath); %sDataPath这是你存放dat数据文件的路径 m = length(sDataFile); k = 0; for i = 1:m if sDataFile(i).isdir co ...… 查看全部问答> |
|
我用LPC2132做AD采样,但其中的几块电路AD采不上数,现象类似于短路,但测试没有短路,除AD外其它运行正常,可不可能是2132的AD坏了呢?… 查看全部问答> |
|
看看这个帖子,什么都明白了 https://bbs.eeworld.com.cn/thread-290320-1-1.html ydw621会员放错误文档,完全是骗论坛币… 查看全部问答> |
|
写代码时,一个小模块用到三八译码器,可是有问题,不能通过语法检查。看了半天也没看出来毛病在哪里。代码如下:module decoder_vtf; // Inputs reg [2:0] in; // Outputs &nbs ...… 查看全部问答> |




