PIC单片机PIC16F1786编程之ADC
PIC16F1786初始化:
void ADC_Init(void)
{
ANSELA = 0b00000001; //AN0 for AD
ANSELB = 0b00000000; //数字IO
ADCON2 = 0b00001111; //反向输入端为ADNREF
ADCON1 = 0b10010000; //右对齐 VDD VSS FRC
ADCON0 = 0b00000001; //ADON = 1 12bit
}
相关寄存器
ANSELA:PORTA ANALOG SELECT REGISTER 0x18C
0=Digital I/O;
1=Analog Input;
如果设置一个pin为Analog Input,其相应的TRIS控制位必须先设为Input mode
ANSELA = 0b00000001;
该语句设置AN0,即管脚2(RA0)为Analog Input
在main()函数中调用ADC_Init(void) 之前,有:
TRISA = 0b11000011;即是RA0、RA1、RA6、RA7为Input Mode
RA2、RA3、RA4、RA5为Output Mode
ANSELB:PORTB ANALOG SELECT REGISTER 0x18D
R/W-1/1 U-0 R/W-1/1 R/W-1/1 R/W-1/1 R/W-1/1 R/W-1/1 R/W-1/1
-ANSB6ANSB5ANSB4ANSB3ANSB2ANSB1ANSB0bit7 bit0
说明:
R=Readable bit, W=Writable bit, U=Unimplemented bit,read as "0",
u=Bit is unchanged, x=Bit is unknown, -n/n=Value at POR and BOR/value at all other resets,
'1'=Bit is set, '0=Bit is cleaed.
RB<6:0>:
0=Digital I/O;
1=Analog Input;
ADCON2:ADC Control REGISTER 2 0X09F
R/W-0/0 R/W-0/0 R/W-0/0 R/W-0/0 R/W-0/0 R/W-0/0 R/W-0/0 R/W-0/0
TRIGSEL<3:0>CHSN<3:0>bit7 bit0
说明:
R=Readable bit, W=Writable bit, U=Unimplemented bit,read as "0",
u=Bit is unchanged, x=Bit is unknown, -n/n=Value at POR and BOR/value at all other resets,
'1'=Bit is set, '0=Bit is cleaed.
bit7-4 TRIGSEL<3:0> :Auto-conversion Trigger Source Selection Bits:
1111 = Reserved,Auto-conversion Trigger disabled;
1110 = Reserved,Auto-conversion Trigger disabled;
1101 = Reserved,Auto-conversion Trigger disabled;
1100 = Reserved;
1011 = Reserved;
1010 = Reserved;
1001 = PSMC2 falling edge event;(Programmable Switch Mode Control)
1000 = PSMC2 rising edge event;
0111 = PSMC2 period match event;
0110 = PSMC1 falling edge event;
0101 = PSMC1 rising edge event;
0100 = PSMC1 period match event;
0011 = Reserved,Auto-conversion Trigger disabled;
0010 = CCP2,Auto-conversion Trigger;
0001 = CCP1,Auto-conversion Trigger;
0000 = Disabled;
bit3-0 CHSN<3:0>:Negative Differential Input Channel Select Bits;
When ADON =0,all multiplexer inputs are disconnected。
1111 = ADC Negative reference,selected by ADNREF;
1110 = No Channel Connected;
1101 = AN13;
1100 = AN12;
1011 = AN11;
1010 = AN10;
1001 = AN9;
1000 = AN8;
0111 = Reserved;
0110 = Reserved;
0101 = Reserved;
0100 = AN4;
0011 = AN3;
0010 = AN2;
0001 = AN1;
0000 = AN0;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ADCON2 = 0b00001111;
该语句设置ADC Control Register 2,即AD 变换控制寄存器2。
TRIGSEL<3:0> = 0000:禁止自动触发。
CHSN<3:0> = 1111:反向输入端为ADNREF。
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ADCON1:ADC Control REGISTER 1 0x09E
R/W-0/0 R/W-0/0 R/W-0/0 R/W-0/0 U-0 R/W-0/0 R/W-0/0 R/W-0/0
ADFMADCS<2:0>─ADNREFADPREF<1:0>bit7 bit0
说明:
R=Readable bit, W=Writable bit, U=Unimplemented bit,read as "0",
u=Bit is unchanged, x=Bit is unknown, -n/n=Value at POR and BOR/value at all other resets,
'1'=Bit is set, '0=Bit is cleaed.
bit7 ADFM :ADC Result Format Select Bit(ADC 结果格式设置)
1 = 2‘s complement format(补码格式2进制);
0 = Sign-magnitude result format(符号数格式);
bit6-4 ADCS<2:0>:ADC Conversion Clock Select Bits:
111 = FRC(clock supplied from a dadicated FRC Oscillator);//专业快速RC振荡器
110 = FOSC/64;
101 = FOSC/16;
100 = FOSC/4;
011 = FRC(clock supplied from a dadicated FRC Oscillator);//专业快速RC振荡器
010 = FOSC/32;
001 = FOSC/8;
000 = FOSC/2;
bit3 Unimplemented:Read as ’0‘;
bit2 ADNREF : ADC Negative Voltage Reference Configuration bit:(ADC 负项参考电压配置)
1 = Vref- is connected to external Vref- pin;
0 = Vref- is connected to Vss;
bit1-0 ADPREF<1:0>:ADC Positive Voltage Reference Configuration bit:
11 = Vref+ is connected internally to FVR buffer 1;
10 = Reserved;
01=Vref+ is connected to Vref+ pin;
00 = Vref+ is connected to VDD;
ADCON0:ADC Control REGISTER 0 0x09D
R/W-0/0 R/W-0/0 R/W-0/0 R/W-0/0 U-0 R/W-0/0 R/W-0/0 R/W-0/0
ADRMDCHS<4:0>GO/nDONEADONbit7 bit0
说明:
R=Readable bit, W=Writable bit, U=Unimplemented bit,read as "0",
u=Bit is unchanged, x=Bit is unknown, -n/n=Value at POR and BOR/value at all other resets,
'1'=Bit is set, '0=Bit is cleaed.
bit7 ADRMD :ADC Result Mode Bit(ADC 结果模式)
1 = ADRESL and ADRESH provide data formatted for a 10-bit result;
0 = ADRESL and ADRESH provide data formatted for a 12-bit result;
bit6-2 CHS<6:2>:PositiveDifferential Input Channel Select Bits;(正的差分输入通道选择)
11111 = FVR(Fixed Voltage Reference) Buffer 1 Output;
11110 = DAC_Output;
。。。
01110 = Reserved,No Channel connected;
01101 = AN13;
01100 = AN12;
01011 = AN11;
01010 = AN10;
01001 = AN9;
01000 = AN8;
。。。
00100 = AN4;
00011 = AN3;
00010 = AN2;
00001 = AN1;
00000 = AN0;
bit1 GO/nDONE : ADC Conversion status bit;
1 = ADC Conversion cycle in progress,Setting this bit starts an ADC conversion cysle
.this bit is automatically cleared by hardware when the ADC conversion
has completed;
0 = ADC conversion completed;
bit0 ADON:ADC Enable bit;
1 = ADC is enabled;
0 = ADC is disabled;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ADCON1 = 0b10010000;
该语句设置ADC Control Register 1,即AD 变换控制寄存器1。
ADFM=1:ADC输出格式位2进制补码;
ADCS<2:0>=0b001:ADC变换时钟为FOSC/8;
ADNREF=0:Vss为参考电压;
ADPREF<1:0>=0b00:ADC正向参考电压为VDD
ADCON0 = 0b00000001;
ADRMD = 0:ADC结果为12位;
CHS<6:2>=0b00000:正向差分输入通道位AN0;
GO/nDONE=0:ADC变换完成;
ADON = 1:ADC is enabled。
AD Conversion 步骤:以AIN0为例:
1、端口配置
Disable pin output driver
TRISAbits.TRISA0 = 1;//input
Configure pin as analog
ANSELAbits.ANSA0 = 1;//INPUT
2、配置ADC模块
select ADC clock
ADCON1bits.ADCS
配置参考电压:
//VREF- is connected to Vss
ADCON1bits.ADNREF = 0;
//VREF+ is connected to VDD
ADCON1bits.ADPREF = 0b00;
选择ADC输入通道:ADCON0bits.CHS
Turn on ADC module
3、配置ADC interrupt(可选)
clear ADC interrupt flag:ADIF=0;
ADC中断使能:ADIE=1;
外设中断使能:PEIE=1;
总的中断使能:GIE=1
4、延时
5、开始变换,ADGO=1
6、两种等待变换结束:
查询ADGO位为1;
ADC中断中;
7、读取ADC转换结果
8、清除ADIF
声明:以上内容来源于网络,如有侵权请联系我们(123@shiyan.com)删除! 转发了
页:
[1]