当前位置:编程学习 > 网站相关 >>

S3C6410 DS18B20温度传感器驱动(二) --- 时序分析

1.初始化序列
 
unsigned int init_ds18b20(void)
{
 unsigned tmp;
 unsigned int val;
 s3c_gpio_cfgpin(ds18b20_table[0],S3C_GPIO_SFN(1));
 tmp = readl(S3C64XX_GPNDAT);
 tmp = tmp | 0x0100;
 writel(tmp,S3C64XX_GPNDAT);
 udelay(2);
 tmp = tmp & 0xfeff;
 writel(tmp,S3C64XX_GPNDAT);
 udelay(500);
 
 tmp = tmp | 0x0100;
 writel(tmp,S3C64XX_GPNDAT);
 udelay(60);
 s3c_gpio_cfgpin(ds18b20_table[0],S3C_GPIO_SFN(0));
 tmp = readl(S3C64XX_GPNDAT);
 val = tmp & 0x0100;
 udelay(500);
 s3c_gpio_cfgpin(ds18b20_table[0],S3C_GPIO_SFN(1));
 tmp = readl(S3C64XX_GPNDAT);
 tmp = tmp | 0x0100;
 writel(tmp,S3C64XX_GPNDAT);
 return val;
}
 
 
 
2. 写时序
static void write_ds18b20(unsigned char data)
{
 unsigned tmp;
 //unsigned char tmp1;
 int i;
 s3c_gpio_cfgpin(ds18b20_table[0],S3C_GPIO_SFN(1));
 for(i = 0;i < 8;i ++)
 {
  tmp = readl(S3C64XX_GPNDAT);
  tmp = tmp | 0x0100;
  writel(tmp,S3C64XX_GPNDAT);
  udelay(2);
  tmp = tmp & 0xfeff;
  writel(tmp,S3C64XX_GPNDAT);
  if(data & 0x01)
  {
   tmp = tmp | 0x0100;
  }else
  {
   tmp = tmp & 0xfeff; 
  }
  writel(tmp,S3C64XX_GPNDAT);
  udelay(60);
  data >>= 1;
 }
 tmp = tmp | 0x0100;
 writel(tmp,S3C64XX_GPNDAT);
 
 return;
}
 
 
 
3. 读时序
unsigned char read_ds18b20(void)
{
 unsigned tmp;
 unsigned char data;
 int i;
 data = 0;
 for(i = 0; i < 8;i ++)
 {
  s3c_gpio_cfgpin(ds18b20_table[0],S3C_GPIO_SFN(1));
  tmp = readl(S3C64XX_GPNDAT);
  tmp = tmp | 0x0100;
  writel(tmp,S3C64XX_GPNDAT);
  udelay(2);
  tmp = tmp & 0xfeff;
  writel(tmp,S3C64XX_GPNDAT);
  udelay(2);
  tmp = tmp | 0x0100;
  writel(tmp,S3C64XX_GPNDAT);
  udelay(8);
 
  data >>= 1;
  s3c_gpio_cfgpin(ds18b20_table[0],S3C_GPIO_SFN(0));
  tmp = readl(S3C64XX_GPNDAT);
  if(tmp & 0x0100)
  {
   data = data | 0x80;
  }
  udelay(50);
 }
 s3c_gpio_cfgpin(ds18b20_table[0],S3C_GPIO_SFN(1));
 tmp = readl(S3C64XX_GPNDAT);
 tmp = tmp | 0x0100;
 writel(tmp,S3C64XX_GPNDAT);
 return data;
}
 
 
 
 
4.执行序列
unsigned int result[2];
 unsigned int ret;
 ret = init_ds18b20();
 if(ret)
 {
  printk("reset error!\n");
  return -1;
 }
// write_data(0);
 write_ds18b20(0xcc);
 write_ds18b20(0x44);
// mdelay(800);
 ret = init_ds18b20();
 if(ret)
 {
  printk("reset error!\n");
  return -1;
 }
 write_ds18b20(0xcc);
 write_ds18b20(0xbe);
 result[0] = read_ds18b20();
 result[1] = read_ds18b20();
// printk("%x,%x\n",result[0],result[1]);
 result[1] <<= 8;
 result[0] = result[0] | result[1];
 
补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,