| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- /*
- @module sensor
- @summary 传感器操作库
- @version 1.0
- @data 2020.03.30
- */
- #include "luat_base.h"
- #include "luat_log.h"
- #include "luat_timer.h"
- #include "luat_malloc.h"
- #include "rtthread.h"
- #include "rthw.h"
- #include <rtdevice.h>
- #include "luat_gpio.h"
- #define CONNECT_SUCCESS 0
- #define CONNECT_FAILED 1
- #define W1_INPUT_MODE PIN_MODE_INPUT_PULLUP
- RT_WEAK void luat_timer_us_delay(size_t us) {
- rt_hw_us_delay(us);
- }
- static void w1_reset(int pin)
- {
- luat_gpio_mode(pin, Luat_GPIO_OUTPUT);
- luat_gpio_set(pin, Luat_GPIO_LOW);
- luat_timer_us_delay(550); /* 480us - 960us */
- luat_gpio_set(pin, Luat_GPIO_HIGH);
- luat_timer_us_delay(40); /* 15us - 60us*/
- }
- static uint8_t w1_connect(int pin)
- {
- uint8_t retry = 0;
- luat_gpio_mode(pin, W1_INPUT_MODE);
- while (luat_gpio_get(pin) && retry < 200)
- {
- retry++;
- luat_timer_us_delay(1);
- };
- if(retry >= 200)
- return CONNECT_FAILED;
- else
- retry = 0;
- while (!luat_gpio_get(pin) && retry < 240)
- {
- retry++;
- luat_timer_us_delay(1);
- };
- if(retry >= 240)
- return CONNECT_FAILED;
- return CONNECT_SUCCESS;
- }
- static uint8_t w1_read_bit(int pin)
- {
- uint8_t data;
- luat_gpio_mode(pin, Luat_GPIO_OUTPUT);
- luat_gpio_set(pin, Luat_GPIO_LOW);
- luat_timer_us_delay(2);
- luat_gpio_set(pin, Luat_GPIO_HIGH);
- luat_gpio_mode(pin, W1_INPUT_MODE);
- luat_timer_us_delay(5);
- if(luat_gpio_get(pin))
- data = 1;
- else
- data = 0;
- luat_timer_us_delay(50);
- return data;
- }
- static uint8_t w1_read_byte(int pin)
- {
- uint8_t i, j, dat;
- dat = 0;
- //rt_base_t level;
- //level = rt_hw_interrupt_disable();
- for (i = 1; i <= 8; i++)
- {
- j = w1_read_bit(pin);
- dat = (j << 7) | (dat >> 1);
- }
- //rt_hw_interrupt_enable(level);
- return dat;
- }
- static void w1_write_byte(int pin, uint8_t dat)
- {
- uint8_t j;
- uint8_t testb;
- luat_gpio_mode(pin, Luat_GPIO_OUTPUT);
- for (j = 1; j <= 8; j++)
- {
- testb = dat & 0x01;
- dat = dat >> 1;
- if(testb)
- {
- luat_gpio_set(pin, Luat_GPIO_LOW);
- luat_timer_us_delay(2);
- luat_gpio_set(pin, Luat_GPIO_HIGH);
- luat_timer_us_delay(60);
- }
- else
- {
- luat_gpio_set(pin, Luat_GPIO_LOW);
- luat_timer_us_delay(60);
- luat_gpio_set(pin, Luat_GPIO_HIGH);
- luat_timer_us_delay(2);
- }
- }
- }
- static int32_t ds18b20_get_temperature(int pin)
- {
- uint8_t TL, TH;
- int32_t tem;
-
- //ds18b20_start(pin);
- w1_reset(pin);
- w1_connect(pin);
- w1_write_byte(pin, 0xcc); /* skip rom */
- w1_write_byte(pin, 0x44); /* convert */
- //ds18b20_init(pin);
- w1_reset(pin);
- w1_connect(pin);
- w1_write_byte(pin, 0xcc);
- w1_write_byte(pin, 0xbe);
- TL = w1_read_byte(pin); /* LSB first */
- TH = w1_read_byte(pin);
- if (TH > 7)
- {
- TH =~ TH;
- TL =~ TL;
- tem = TH;
- tem <<= 8;
- tem += TL;
- tem = (int32_t)(tem * 0.0625 * 10 + 0.5);
- return -tem;
- }
- else
- {
- tem = TH;
- tem <<= 8;
- tem += TL;
- tem = (int32_t)(tem * 0.0625 * 10 + 0.5);
- return tem;
- }
- }
- /*
- 获取DS18B20的温度数据
- @function sensor.ds18b20(pin)
- @int gpio端口号
- @return int 温度数据
- -- 如果读取失败,会返回nil
- while 1 do sys.wait(5000) log.info("ds18b20", sensor.ds18b20(14)) end
- */
- static int l_sensor_ds18b20(lua_State *L) {
- int32_t temp = ds18b20_get_temperature(luaL_checkinteger(L, 1));
- // -55°C ~ 125°C
- if (temp > 1250 || temp < -550) {
- return 0;
- }
- //rt_kprintf("temp:%3d.%dC\n", temp/10, temp%10);
- lua_pushinteger(L, temp);
- return 1;
- }
- #include "rotable.h"
- static const rotable_Reg reg_sensor[] =
- {
- { "ds18b20" , l_sensor_ds18b20 , 0},
- { NULL, NULL , 0}
- };
- LUAMOD_API int luaopen_sensor( lua_State *L ) {
- rotable_newlib(L, reg_sensor);
- return 1;
- }
|