其他分享
首页 > 其他分享> > 外接DS18B20温度传感器

外接DS18B20温度传感器

作者:互联网

参考的文档:

Sound
The internal buzzer is fairly easy to manipulate. It is connected to a general purpose I/O pin, pin B2. Toggling the state of this pin will make noises.

由此可见,hp39gs的蜂鸣器信号直连s3c2410的B组GPIO的第三个引脚(B组GPIO共有11个引脚,可配置为输入/输出)。
硬件改造:
首先拆机,然后找到蜂鸣器焊盘,接着在外壳开合适的孔位,最后使用3.5mm耳机接口将该io口引出。
hp39gs主板图:

hp39gs蜂鸣器焊盘定义:


焊接3.5mm 接口:(我在此处L, R都接在了GPIO信号端,如有供电需求可将L/R信号一个接GPIO,另一个接5V/3.3V)


完成效果图:



程序适配:
GPIO寄存器地址:

// 11位输入/输出端口
// hp39gs蜂鸣器信号02端口
#define rGPBCON    (*(volatile unsigned *)0x56000010) //Port B control
#define rGPBDAT    (*(volatile unsigned *)0x56000014) //Port B data
#define rGPBUP     (*(volatile unsigned *)0x56000018) //Pull-up control B

地址偏移量:

0x56000000	->	0x07A00000
			0x07AFFFFF

GPIO操作:

#include "gpio.h"
void output_set(unsigned char state) {
    //set output
    *GPBCON &= 0x1FFFCF;
    *GPBCON |  = 0x10;
    *GPBDAT = (state) ? ((*GPBDAT) | 0x04) : ((*GPBDAT) & 0x0B);
}

unsigned char input_get() {
    //set input
    *GPBCON &= 0x1FFFCF;
    return (*GPBDAT & 0x4) >> 2;
}

void pull_up() {
    *GPBUP &= 0x04;
}

void pull_up_dis() {
    *GPBUP |= 0x04;
}

DS18B20驱动:

#include "ds18b20.h"

//@75Mhz normal mode
//little endian
//由于sys_timer_t 只能实现ms级定时
//故delay使用传统的i++延时

uint8 ds18b20_init() {
	uint8 state = 1;
	//总线拉低至少480us
	output_set(0);
	delay(3500);//520us
	//释放总线
	output_set(1);
	//等待15到60us
	delay(400);//29.56us
    //读取ds18b20响应数据,正常情况下ds18b20将拉低总线
	state = input_get();
	delay(2000);//107.2us
	output_set(1);
	return state;
}

void ds18b20_write_byte(uint8 data) {
	int i = 0;
	for(; i < 8; i++) {
		//写时隙 拉低总线2μs 释放总线
		output_set(0);
		delay(1);//1.625us
        //写入一个数据,最低位开始
		output_set(data & 0x01);
		delay(1200);//66.04us
        //释放总线,至少1us给总线恢复时间
		output_set(1);
		delay(1);//3.81us
		data >>= 1;
	}
}

uint8 ds18b20_read_byte() {
    uint8 byte = 0, data;
    int i = 0;
    for(; i < 8; i++) {
        //拉低总线2us
        output_set(0);
        delay(1);
        //读时隙产生6us后读取总线数据
        output_set(1);
        delay(2);//5.958us
        //在19us内读2次确保得到正确数据
        data = input_get();
        data = input_get();
        //移位存储, byte务必初始化
        byte |=  (data << i);
        //读时隙必须至少有60us的持续时间
        delay(1200);
        output_set(1);
        delay(1);
    }
    return byte;
}

void ds18b20_convert_temp() {
    ds18b20_init();
    delay(1800);//502.3us
    //跳过ROM操作命令
    ds18b20_write_byte(0xCC);
    //温度转换命令
    ds18b20_write_byte(0x44);
}

void ds18b20_read_temp_cmd() {
    ds18b20_init();
    delay(1800);
    //跳过ROM操作命令
    ds18b20_write_byte(0xCC);
    //发送读取温度命令
    ds18b20_write_byte(0xBE);
}

short ds18b20_read_temp() {
    short temp = 0;
    unsigned char tmh = 0,tml = 0;
    //先写入转换命令
    ds18b20_convert_temp();
    //转换完读取温度
    ds18b20_read_temp_cmd();
    //温度值16位,读低字节
    tml = ds18b20_read_byte();
    //读高字节
    tmh = ds18b20_read_byte();
    //合成温度值16位
    temp = tmh;
    temp <<= 8;
    temp |= tml;
    return temp;
}

unsigned int ds18b20_read_rom_id() {
    unsigned int rom_id = 0;
    unsigned int data;
    ds18b20_init();
    delay(1800);
    ds18b20_write_byte(0x33);
    for (int i = 0; i < 4; i++ ) {
        data = ds18b20_rea  d_byte();
        rom_id |=  (data << (i * 8));
    }
    return rom_id;
}

Aplet程序下载:
https://files.cnblogs.com/files/yanye0xff/hp39gs_ds18b20.zip
GitHub:
https://github.com/Yanye0xFF/hp39gs_ds18b20

最终效果:


标签:温度传感器,外接,DS18B20,ds18b20,总线,delay,set,output,data
来源: https://www.cnblogs.com/yanye0xff/p/14411326.html