1 基本原理
1.1 AD原理
见前面的文章
1.2 温度储传感器原理
1.3 FVR原理
2 实现代码
/*---------------------------------函数功能:-------------------------------------
采集MCU的温度,并且用RC0口(LED)来判断是否采集完成,如果温度采集AD完成,则
* AD对应的标志位ADIF=1,如果使能有效,则会产生一个中断,此时LED灯亮。
编程思路:参考手册的P131 To do an A/D Conversion, follow these steps
----------------------------------------------------------------------------*/
// PIC16F15323 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FEXTOSC = ECH // External Oscillator mode selection bits (EC above 8MHz; PFM set to high power)
#pragma config RSTOSC = EXT1X // Power-up default value for COSC bits (EXTOSC operating per FEXTOSC bits)
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)
// CONFIG2
#pragma config MCLRE = ON // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)
// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = ON // WDT operating mode (WDT enabled regardless of sleep; SWDTEN ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)
// CONFIG4
#pragma config BBSIZE = BB512 // Boot Block Size Selection bits (512 words boot block size)
#pragma config BBEN = OFF // Boot Block Enable bit (Boot Block disabled)
#pragma config SAFEN = OFF // SAF Enable bit (SAF disabled)
#pragma config WRTAPP = OFF // Application Block Write Protection bit (Application Block not write protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block not write protected)
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration Register not write protected)
#pragma config WRTSAF = OFF // Storage Area Flash Write Protection bit (SAF not write protected)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)
// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit (UserNVM code protection disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include //#include"delay.h"//调用延时子函数的头文件 /*-----------宏定义--------------*/ #define uint unsigned int #define uchar unsigned char uint ADbuf=0; // 缓存AD转换结果 /*-----------子函数声明--------------*/ /*-----------主函数--------------*/ void main() { //-------------------1、Configure Port:---------------------// // The corresponding data direction register is TRISA. // Setting a TRISA bit (= 1) will make the corresponding PORTA pi an input. // Clearing a TRISA bit (= 0) will make the corresponding PORTA pin an output. // 解释为什么需要TRISA0=1? After the A/D module has been configured as desired, // the selected channel must be acquired before the conversion is started. // The analog input channels must have their corresponding TRIS bits selected as inputs. TRISA0=1; // RA0口为输入口,电压输入口 TRISC0=0; // AD响应中断对应的LED灯的数据方向为输出 // 1 = Port pin is > VIH,即高电平 ; 0 = Port pin is < VIL,即低电平 RA0=0; // 要不要这行语句没有影响,因为该端口是输入端口 RC0=0; // LED灯的初值为灭 //The ANSELA register (Register 14-4) is used to configure the Input mode of an I/O pin to analog. // Setting the appropriate ANSE 如果ANSEL=1,IO口为模拟输入,如果ANSEL=0,数字IO口。 ANSA0=1; // Set RA0 to analog ANSELA0,为什么见头文件 while(1) // 死循环,单片机初始化后,就一直运行这个死循环 { //-------------------2、Configure the ADC module:---------------------// //(1)寄存器ADCON1: ADFM ADCS<2:0> - - ADPREF<1:0> // 10-BIT ADC CONVERSION RESULT FORMAT ADFM=1; // AD Result Format Select bit. 0 = Left justified. 1 = Right justified //The source of the conversion clock is software selectable via the ADCS<2:0> bits ADCS2=0;ADCS1=0;ADCS0=0; // Select ADC conversion clock FOSC/2 // The ADPREF<1:0> bits of the ADCON1 register provides control of the positive voltage reference ADPREF1=0;ADPREF0=0; // Select voltage reference 00 =VREF+ is connected to VDD //(2)寄存器ADCON0: CHS<5:0> GO/DONE ADON //The CHS<5:0> bits of the ADCON0 register determine which channel is connected to the sample and hold circuit. //CHS5=0;CHS4=0;CHS3=0;CHS2=0;CHS1=0;CHS0=0; // Select ADC input channel 选择RA0(AN0)作为模拟输入 CHS5=1;CHS4=1;CHS3=1;CHS2=1;CHS1=0;CHS0=0; // Select ADC input channel Temperature sensor 选择温度传感器作为模拟输入 //ADC Conversion Status bit. 1 = ADC conversion cycle in progress. Setting this bit starts an ADC conversion cycle. // This bit is automatically cleared by hardware when the ADC conversion has completed. // 0 = ADC conversion completed/not in progress GOnDONE=0; // AD Conversion Status bit。为什么叫用GO_DONE看一下xc.h头文件 //To enable the ADC module, the ADON bit of the ADCON0 register must be set to a ‘1’. ADON=1; //Turn on ADC module 模数转换器使能位。 1 = A/D converter module is powered up //----------新增部分:温度传感器寄存器配置( CHS5=1;CHS4=1;CHS3=1;CHS2=1;CHS1=0;CHS0=0;)---------------// // FVREN: Fixed Voltage Reference Enable bit. 1 = Fixed Voltage Reference is enabled FVREN=1; // FVRRDY: Fixed Voltage Reference Ready Flag bit(1). 1 = Fixed Voltage Reference output is ready for use FVRRDY=1; // Temperature Indicator Enable bit. 1 = T emperature Indicator is enabled TSEN=1; // T emperature Indicator Range Selection bit. 1 = T emperature in High Range VOUT = 3VT ; 0 = T emperature in Low Range VOUT = 2VT TSRNG=0; //Min. VDD, TSRNG = 0 (Low Range) >= 1.8 // ADFVR<1:0>: ADC FVR Buffer Gain Selection bit 00 = ADC FVR Buffer is off // ADFVR1=0;ADFVR0=1; //01 = ADC FVR Buffer Gain is 1x, (1.024V) ADFVR1=0;ADFVR0=0; //00 = ADC FVR Buffer is off //--------------------3、Configure ADC interrupt (optional):---------------------// //ADIE=0; //禁止AD中断 //AD中断使能 ADIE=1; ADIF=0; // Clear ADIF bit INTEDG=1; // Interrupt Edge Select bit 1 = Interrupt on rising edge of INT pin PEIE=1; // 允许外设中断 GIE=1; // 总中断允许 //----------ADC BLOCK DIAGRAM框图有下面两个寄存器,但是AD采集程序没有用到?--------------// // AUTO CONVERSION TRIGGER // TRIGSEL<3:0>;
上一篇:PIC单片机汇编指令集合
下一篇:PIC16F15323单片机 (ADC)--汇编+C语言
推荐帖子
- 伺服电机驱动器
- 有人使用STM32芯片做伺服电机驱动器的吗?有没有这方面的资料或者方案啊?伺服电机驱动器有的,我有见过。你可以参考ST的DEMO。ST在电机伺服方面是有所专攻的,STM32和STM8都有的选啊交流伺服还是直流伺服?我目前倒是打算用他做直流伺服应该有。。。我也想用啊急需资料我们这里有现成的方案:http:
- xyw6813927 stm32/stm8
- 微波物位开关
- 谁做过微波物位开关?微波物位开关楼主的问题是?回复楼主eplj_163的帖子我想了解下。给我的设计提点思路
- eplj_163 模拟电子
- 关于单片机的字库设计
- 当需要液晶显示的时候,汉字的显示一直不是那么方便(在没有*字库的情况下)。如果纯粹取模的话,就要考虑自作一个字库需要花费的时间和占用的flash资源。对于UCOS来说,就像楼主使用的破开发板,只有256Kflash,一个16*16GB点阵字库就要占用200K+,自然是不可能放入用户源码的。于是乎,楼主开始使用*字库,关于*字库碰到的一些问题在这里给大家简单说说,有用的话的就给楼主赞一个,哈哈哈1.关于GB2312,可百度。2.字库一般选用flash器件,采用SPI协议通信,通
- Jacktang 微控制器 MCU
- 【Arrow SoC】从QSPI Flash 启动
- 本帖最后由chenzhufly于2014-12-1910:19编辑 IntroductionPreparetheBootfilesWritetheBootfilestotheQSPIserialflashConfiguretheSoCKitboardtobootfromQSPIserialflashIntroductionThissolutiondocumentsthe
- chenzhufly Altera SoC
- 【2024 DigiKey 创意大赛】AI全功能环境监测站
- 一、作品简介1.1作品功能介绍本次创意大赛中,我设计并实现了一个AI全能环境监测站。该设备通过集成多款高精度气体传感器,能够全面监测环境中的多种气体成分,如温湿度、空气质量、二氧化碳和各类有害气体等。所有环境数据不仅可以实时显示在MatrixRGB点阵屏上,还能够通过AdafruitIO云平台上传,实现多设备多应用间的环境监测数据共享。用户可以通过网页端查看不同环境指标的实时数据及其变化趋势,获取清晰的历史数据图表和变化曲线。为了增强用户体验,该系统还加入了智
- MioChan DigiKey得捷技术专区
设计资源 培训 开发板 精华推荐
- L5150CS 5 V 低压差稳压器的典型应用
- LT3089EFE 线性稳压器的典型应用电路采用 IMON 消除镇流器电阻压降
- 正负电源模块
- AD9680-1000EBZ、9680CE04B AD9680-1000高速ADC评估板针对全模拟输入频率范围进行了优化
- LT3663EMS8E-5、5V 降压转换器的典型应用
- Si4122GM-EVB,Si4122G-BM PLL 频率合成器评估板
- NCP301HSN27T1 2.7V 电压检测器的典型应用,用于具有附加迟滞的微处理器复位电路
- 使用 Analog Devices 的 LTC2401 的参考设计
- LDK130C29R 2.9V、300 mA 低静态电流、极低噪声 LDO 的典型应用固定版本电路
- 使用 Infineon Technologies AG 的 OM7612ST 的参考设计