第一节 知识点
1.adc
(1)10 位模数转换器(a/d)模块:28 引脚器件的模数(analog-to-digitala/d)转换器具有10 路输入,而40/44 引脚器件的模数转换器则具有13 路输入。a/d 模块能将一个模拟输入信号转换成相应的10 位数字信号。
(2)此模块有五个寄存器:
• a/d 转换结果高位寄存器(adresh)
• a/d 转换结果低位寄存器(adresl)
• a/d 转换控制寄存器0 (adcon0):a/d 模块的工作方式由adcon0寄存器控制。
• a/d 转换控制寄存器1 (adcon1):端口引脚的功能由adcon1 寄存器配置。
• a/d 转换控制寄存器2 (adcon2):由adcon2 寄存器配置a/d 时钟源,编程采集时间和对齐方式。
(3)执行a/d 转换时应该遵循以下步骤:
配置a/d 模块:
• 配置模拟引脚、参考电压和数字i/o (通过adcon1 寄存器)
• 选择a/d 输入通道(通过adcon0 寄存器)
• 选择a/d 采集时间(通过adcon2 寄存器)
• 选择a/d 转换时钟(通过adcon2 寄存器)
• 使能a/d 模块(通过adcon0 寄存器)需要时,配置a/d 中断:
• 清零adif 位
• 将adie 位置1
• 将gie 位置1如果需要,等待所需的采集时间。启动转换:
• 将go/done 位置1 (adcon0 寄存器)等待a/d 转换完成,通过以下两种方法之一判断转换是否完成:
• 查询go/done 位是否被清零或 等待a/d 中断读取a/d 结果寄存器(adresh:adresl),需要时将adif 位清零。如需再次进行a/d 转换,返回步骤1 或步骤2。将每位的a/d 转换时间定义为tad,在下一次采集开始前至少需要等待2 个tad。(4)a/d 采集要求
为了使a/d 转换器达到规定精度,必须使充电保持电容(chold)充满至输入通道的电平。图19-3 给出了模拟输入的电路模型。电源阻抗(rs)和内部采样开关阻抗(rss)直接影响电容chold 充电的时间。采样开关(rss)阻抗值随器件电压(vdd)不同而改变。电源阻抗将影响模拟输入的偏移电压(由于引脚泄漏电流的原因)。模拟信号源的最大阻抗推荐值为2.5 kω。在选择(改变)了模拟输入通道之后,必须对通道进行采样才能启动转换,采样时间必须大于最小采集时间。
(5) 采样时间计算:
1.adc的原理框图:
2.与adc相关的寄存器:
我们设置vcfg1=0,采用bss作为参考电压vref-;
设置vcfg0=0,采用vdd作为vref+的参考电压。
配置pcfg3:pcfg0 进行采集模拟量的端口配置。
配置adfm,adc转化结构的格式是左对齐还是右对齐,这是因为adc转化结果是10位的需要两个8位寄存器存储。
acqt2:acqt0:a/d 采集时间选择位;
adcs2:adcs0:a/d 转换时钟选择位。
3.比如我们在实际中要采集电压,典型的电路图如下:
02第二节 代码设计
1.我们新建两个文件:
(1) adc_sample.h
/* microchip technology inc. and its subsidiaries. you may use this software* and any derivatives exclusively with microchip products.* file: adc_sample.h* author: greg* comments:* revision history: 2018-06-21*/// this is a guard condition so that contents of this file are not included// more than once. #ifndef _adc_sample_h_#define _adc_sample_h_#include // include processor files - each processor file is guarded. #define channel_0_on 0b0000#define channel_1_on 0b0001#define channel_2_on 0b0010#define channel_3_on 0b0011#define channel_4_on 0b0100#define channel_5_on 0b0101#define channel_6_on 0b0110#define channel_7_on 0b0111#define adc_channel_select adcon0bits.chs#define adc_enable adcon0bits.adon=1#define adc_disable adcon0bits.adon=0#define adc_status adcon0bits.godone#define adc_start adcon0bits.go=1#define adc_port_dir trisavoid adc_channel_config(void);double adc_converter_ddecimal(unsigned int adc_data);double adc_process_show(unsigned char channel_selected );double adc_process_select_work(unsigned char channel_selected);#endif /* xc_header_template_h */(2)adc_sample.c
#include#include adc_sample.hstatic unsigned int adc_data[8]=0;void adc_channel_config(void){ adc_port_dir=0xff; adcon1bits.pcfg=0b0111; //select an0-an7 channel to a_d converter adcon1bits.vcfg=0b00; //voltage reference for vss,vdd. adcon2bits.adfm=1; // reslut right justed adcon2bits.acqt=0b001; adcon2bits.adcs=0b000;}/*this fucntion is to converter result of adc to decimal result.*/double adc_converter_ddecimal(unsigned int adc_data){ double temp; temp=(double) adc_data*5.0; return temp/1023.0;}/** this is first way to get adc sample result.*/double adc_process_show(unsigned char channel_selected){ double adc_temp0=0; switch (channel_selected){ case 0: adc_channel_select=channel_0_on; break; case 1: adc_channel_select=channel_1_on; break; case 2: adc_channel_select=channel_2_on; break; case 3: adc_channel_select=channel_3_on; break; case 4: adc_channel_select=channel_4_on; break; case 5: adc_channel_select=channel_5_on; break; case 6: adc_channel_select=channel_6_on; break; case 7: adc_channel_select=channel_7_on; break;} adc_enable; adc_start; while(adc_status);// adc_data[0]=0x00ff&adresl;// adc_data[0]|=adresh< <8; adc_data[0]=adres; adc_temp0=adc_converter_ddecimal(adc_data[channel_selected]); return adc_temp0;}/** this is second way to get adc sample result.*/double adc_process_select_work(unsigned char channel_selected){ double adc_temp0=0; adc_channel_select=channel_selected; adc_enable; adc_start; while(adc_status);// adc_data[0]=0x00ff&adresl;// adc_data[0]|=adresh< <8; adc_data[0]=adres; adc_temp0=adc_converter_ddecimal(adc_data[0]); return adc_temp0;}(3)main.c
// pic18f4520 configuration bit settings// 'c' source line config statements// author:greg// title:adc 采样// config1h#pragma config osc = hs // oscillator selection bits (hs oscillator)#pragma config fcmen = off // fail-safe clock monitor enable bit (fail-safe clock monitor disabled)#pragma config ieso = off // internal/external oscillator switchover bit (oscillator switchover mode disabled)// config2l#pragma config pwrt = off // power-up timer enable bit (pwrt disabled)#pragma config boren = sbordis // brown-out reset enable bits (brown-out reset enabled in hardware only (sboren is disabled))#pragma config borv = 3 // brown out reset voltage bits (minimum setting)// config2h#pragma config wdt = off // watchdog timer enable bit (wdt disabled (control is placed on the swdten bit))#pragma config wdtps = 32768 // watchdog timer postscale select bits (1:32768)// config3h#pragma config ccp2mx = portc // ccp2 mux bit (ccp2 input/output is multiplexed with rc1)#pragma config pbaden = on // portb a/d enable bit (portbpins are configured as analog input channels on reset)#pragma config lpt1osc = off // low-power timer1 oscillator enable bit (timer1 configured for higher power operation)#pragma config mclre = off // mclr pin enable bit (re3 input pin enabled; mclr disabled)// config4l#pragma config stvren = off // stack full/underflow reset enable bit (stack full/underflow will not cause reset)#pragma config lvp = off // single-supply icsp enable bit (single-supply icsp disabled)#pragma config xinst = off // extended instruction set enable bit (instruction set extension and indexed addressing mode disabled (legacy mode))// config5l#pragma config cp0 = off // code protection bit (block 0 (000800-001fffh) not code-protected)#pragma config cp1 = off // code protection bit (block 1 (002000-003fffh) not code-protected)#pragma config cp2 = off // code protection bit (block 2 (004000-005fffh) not code-protected)#pragma config cp3 = off // code protection bit (block 3 (006000-007fffh) not code-protected)// config5h#pragma config cpb = off // boot block code protection bit (boot block (000000-0007ffh) not code-protected)#pragma config cpd = off // data eeprom code protection bit (data eeprom not code-protected)// config6l#pragma config wrt0 = off // write protection bit (block 0 (000800-001fffh) not write-protected)#pragma config wrt1 = off // write protection bit (block 1 (002000-003fffh) not write-protected)#pragma config wrt2 = off // write protection bit (block 2 (004000-005fffh) not write-protected)#pragma config wrt3 = off // write protection bit (block 3 (006000-007fffh) not write-protected)// config6h#pragma config wrtc = off // configuration register write protection bit (configuration registers (300000-3000ffh) not write-protected)#pragma config wrtb = off // boot block write protection bit (boot block (000000-0007ffh) not write-protected)#pragma config wrtd = off // data eeprom write protection bit (data eeprom not write-protected)// config7l#pragma config ebtr0 = off // table read protection bit (block 0 (000800-001fffh) not protected from table reads executed in other blocks)#pragma config ebtr1 = off // table read protection bit (block 1 (002000-003fffh) not protected from table reads executed in other blocks)#pragma config ebtr2 = off // table read protection bit (block 2 (004000-005fffh) not protected from table reads executed in other blocks)#pragma config ebtr3 = off // table read protection bit (block 3 (006000-007fffh) not protected from table reads executed in other blocks)// config7h#pragma config ebtrb = off // boot block table read protection bit (boot block (000000-0007ffh) not protected from table reads executed in other blocks)// #pragma config statements should precede project file includes.// use project enums instead of #define for on and off.#include#include adc_sample.h#include#include usart_dr.h//#includeplib/adc.h//#include delays.hint main(void){ unsigned char channel_select=0; double adc_temp0; adc_channel_config(); usart_port_dir_init(); usart_config_init(); printf(test for printf functionrtn); // printf 函数实现成功! printf(adc 采样实现rtn); // printf 函数实现成功! while(1) { adc_temp0=adc_process_show(channel_select); printf(adc var:%frtn,adc_temp0); adc_temp0=adc_process_select_work(channel_0_on); printf(adc var:%frtn,adc_temp0); } return 0; }下载到开发板中看效果吧,代码的注释很清楚我就不解释了。
台积电晶圆平均售价同比上涨22.8%!
绘制高精地图如何面对无人驾驶的到来
奥迪Q5一款动感而全能的SUV,科技感十足并且非常好的提升日常使用的便利性,它和宝马X4两款车,你选谁?
新型钠离子电池,拥有极高的能量密度
新加坡实现在可见光波段光束中探测红外波段光束
Microchip PIC系列8位单片机入门教程(6)ADC
屏幕指纹识别+异形全面屏,vivo X30首曝
oppor11预约正式开启,堪称6月最值得期待手机
电力智能配电系统
89C51单片机中断知识要点
射频微波MLCC第一股达利凯普成功上市
存储技术对5G手机的有多重要?
iphone8什么时候上市?iphone8最新消息:iphone8还是会照常发布,但是问题还是蛮多的
计算边缘如何去寻求平衡
双流12亿非晶硅项目开工资本盯紧薄膜光伏电池
人工智能技术渗入各行各业 商业化前景未来可期
频频有相关专利被爆出 小米新款折叠屏手机稳了
共享单车利用 NB-IoT 现网并支持 2G 转 NB-IoT 全类型业务
绝缘型反激式转换器的性能评估-性能评估事例中所使用电源IC的概述和应掌握的特征
多功能仪表接线说明Acrel Multi-function Meter Wiring Instruction