当前位置: 代码迷 >> 驱动开发 >> 【请问】AD转换程序中为什么要按位或
  详细解决方案

【请问】AD转换程序中为什么要按位或

热度:89   发布时间:2016-04-28 10:54:41.0
【请教】AD转换程序中为什么要按位或
今天看了开发板的ADC例程,整个程序比较简单,但是里面有一句我不懂,我已经用黑体标注了,为什么不能直接取得数值然后显示,而是要左移八位然后再与低八位按位或呢?小弟菜鸟,请大家指点!

#include "stdio.h"
#include "sys/types.h"
#include "sys/ioctl.h"
#include "stdlib.h"
#include "termios.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "sys/time.h"
int main(int argc, char **argv)
{
  int fd,ret,adcdat0;
  char buf[2];
  fd=open("/dev/adc_dev",0);
  if(fd<0)
  {
  printf("open adc error\n");
  exit(1);
  }
  else
  {
  printf("success\n");
  while(1)  
  {
  read(fd,buf,2);
adcdat0=buf[1];
adcdat0=adcdat0<<8|buf[0];//这句不理解,为什么要左移八位再按位或呢?
  printf("%x \n",adcdat0);
  sleep(1);
  }
  }
  close(fd);
}


------解决方案--------------------
buf[1]代表高8位,buf[0]为低8位, adcdat0 = buf[1]*256 + buf[0];直接表示,与下面两句是等价的
adcdat0=buf[1];
adcdat0=adcdat0<<8|buf[0];
  相关解决方案