解释一下这个代码: adc_result = ADRESH; adc_result = (adc_result << 8) | ADRESL;
时间: 2023-10-23 08:11:46 浏览: 32
这段代码是用来读取模拟信号转换器(ADC)的转换结果的。在8位微控制器中,ADC转换结果被分为两部分:高8位和低2位。ADRESH寄存器存储高8位,ADRESL寄存器存储低2位。
第一行代码 adc_result = ADRESH; 将ADRESH寄存器的值读取到adc_result变量中,这是读取高8位。
第二行代码 adc_result = (adc_result << 8) | ADRESL; 将adc_result变量左移8位,即将高8位向左移动8位,低2位补0。然后将ADRESL寄存器的值读取到低8位中,使用按位或运算符将低8位和高8位组合成一个16位的adc_result变量。最终得到的结果是一个16位的整数,其中高8位是转换结果的高8位,低8位是转换结果的低2位。
相关问题
ads2 = ((ADRESH << 8) + ADRESL);
这是一行 C 语言代码,其中的符号 << 表示左移运算符,+ 表示加法运算符,ADRESH 和 ADRESL 是用于存储 ADC 转换结果的寄存器,ads2 是一个变量,用于存储 ADC 转换结果的十进制值。
该行代码的作用是将 ADRESH 中的 8 位数据左移 8 位,并将 ADRESL 中的 8 位数据加到左移后的数据上,得到一个 16 位的 ADC 转换结果。具体来说,ADRESH 中存储的是 ADC 转换结果的高 8 位数据,ADRESL 中存储的是 ADC 转换结果的低 8 位数据,左移 8 位后,高 8 位数据移动到了左移后的 8 位,低 8 位数据变成了 0。然后将 ADRESL 中的 8 位数据加到左移后的结果上,得到了一个 16 位的 ADC 转换结果,该结果存储在变量 ads2 中,可以用于后续的数据处理和显示等操作。
/* * File: ADC.c * Author: tlfte *AD转换,结果在C口和D口的LED上显示,能进行各种通道选择和参考电压,结果对齐方式选择 * Created on 2018年8月6日, 上午10:07 练习AD结果的计算验证,AD_RESULT=VIN×1023÷VREF,讲解887头文件的作用 */ // PIC16F887 Configuration Bit Settings // 'C' source line config statements // CONFIG1 #pragma config FOSC = XT // Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD) #pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled) #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled) #pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled) #pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled) #pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming) // CONFIG2 #pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V) #pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include <xc.h> #define _XTAL_FREQ 4000000 //指明时钟晶振为4MHz,使delay宏定义可以正常使用 void CSH(void); unsigned int AD_SUB(char k); void main( ) { unsigned int y; CSH(); while(1) { __delay_ms(100); //每隔100毫秒循环一次 y=AD_
SUB(0b00000110); //选择通道为AN6,即RA2口 PORTD=y; //将结果的低8位输出到D口LED灯上 y>>=8; //将结果的高8位移位到低8位 PORTC=y; //将结果的高8位输出到C口LED灯上 } } void CSH(void) //初始化函数 { TRISA=0xff; TRISC=0x00; TRISD=0x00; ANSEL=0x40; //选择AN6通道为模拟输入 ADCON1=0xC0; //设置参考电压为VDD和VSS,右对齐结果 } unsigned int AD_SUB(char k) //AD转换函数 { unsigned int AD_RESULT; ADCON0=k<<2; //将选择的通道左移两位,放到ADCON0的低4位上,同时清除其他位 ADCON0|=0x03; //启动AD转换 while(ADCON0&0x02); //等待AD转换完成 AD_RESULT=ADRESH; //将结果的高8位移位到低8位 AD_RESULT<<=8; //将结果的低8位移位到高8位 AD_RESULT|=ADRESL; //将结果的低8位放入结果的低8位上 return AD_RESULT; }