Przeglądaj źródła

add: 添加ht1621驱动

https://gitee.com/openLuat/LuatOS/issues/I9FA2X
Wendal Chen 1 rok temu
rodzic
commit
72e55cc365

+ 116 - 0
components/ht1621/binding/luat_lib_ht1621.c

@@ -0,0 +1,116 @@
+/*
+@module  ht1621
+@summary 液晶屏驱动(HT1621/HT1621B)
+@version 1.0
+@author  wendal
+@date    2024.04.15
+@tag     LUAT_USE_GPIO
+@usage
+-- 需要接3个GPIO引脚, 然后给ht1621接好供电
+-- 假设 CS脚接   模块的 GPIO4
+-- 假设 DATA脚接 模块的 GPIO5
+-- 假设 WR脚接   模块的 GPIO3
+local seg = ht1621.setup(4, 5, 3)
+ht1621.lcd(seg, true) -- 背光亮起
+ht1621.data(seg, 0, 0xeb) -- 位置0显示数字1
+*/
+#include "luat_base.h"
+#include "luat_ht1621.h"
+#include "luat_gpio.h"
+
+/*
+初始化ht1621
+@api ht1621.setup(pin_cs, pin_data, pin_wr)
+@int 片选引脚, 填模块的GPIO编码
+@int 数据引脚, 填模块的GPIO编码
+@int WR引脚, 填模块的GPIO编码
+@return userdata 返回ht1621对象
+@usage
+local seg = ht1621.setup(4, 5, 3)
+ht1621.data(seg, 0, 0xeb)
+*/
+static int l_ht1621_setup(lua_State *L) {
+    int pin_cs = luaL_checkinteger(L, 1);
+    int pin_data = luaL_checkinteger(L, 2);
+    int pin_wr = luaL_checkinteger(L, 3);
+    luat_ht1621_conf_t* conf = lua_newuserdata(L, sizeof(luat_ht1621_conf_t));
+    conf->pin_cs = pin_cs;
+    conf->pin_data = pin_data;
+    conf->pin_wr = pin_wr;
+    luat_ht1621_init(conf);
+    return 1;
+}
+
+/*
+LCD开关
+@api ht1621.lcd(seg, onoff)
+@userdata ht1621.setup返回的ht1621对象
+@boolean true开,false关
+@return nil 无返回值
+@usage
+local seg = ht1621.setup(4, 5, 3)
+ht1621.lcd(seg, true)
+*/
+static int l_ht1621_lcd(lua_State *L) {
+    luat_ht1621_conf_t* conf = lua_touserdata(L, 1);
+    if (conf == NULL) return 0;
+    int onoff = lua_toboolean(L, 2);
+    luat_ht1621_lcd(conf, onoff);
+    return 0;
+}
+
+/*
+展示数据
+@api ht1621.data(seg, addr, sdat)
+@userdata ht1621.setup返回的ht1621对象
+@int 地址, 0-6, 超过6无效
+@int 数据, 0-255
+@return nil 无返回值
+@usage
+local seg = ht1621.setup(4, 5, 3)
+ht1621.lcd(seg, true)
+ht1621.data(seg, 0, 0xF1)
+-- 附数字0-9的值表
+-- 0,1,2,3,4,5,6,7,8,9
+-- 0xeb,0x0a,0xad,0x8f,0x4e,0xc7,0xe7,0x8a,0xef,0xcf
+*/
+static int l_ht1621_data(lua_State *L) {
+    luat_ht1621_conf_t* conf = lua_touserdata(L, 1);
+    if (conf == NULL) return 0;
+    int addr = luaL_checkinteger(L, 2);
+    int sdat = luaL_checkinteger(L, 3);
+    luat_ht1621_write_data(conf, addr, sdat);
+    return 0;
+}
+
+/*
+发送指令
+@api ht1621.cmd(seg, cmd)
+@userdata ht1621.setup返回的ht1621对象
+@int 指令, 0-255
+@return nil 无返回值
+@usage
+-- 具体指令请查阅硬件手册
+*/
+static int l_ht1621_cmd(lua_State *L) {
+    luat_ht1621_conf_t* conf = lua_touserdata(L, 1);
+    if (conf == NULL) return 0;
+    int cmd = luaL_checkinteger(L, 2);
+    luat_ht1621_write_cmd(conf, cmd);
+    return 0;
+}
+
+#include "rotable2.h"
+static const rotable_Reg_t reg_ht1621[] =
+{
+    { "setup" ,           ROREG_FUNC(l_ht1621_setup)},
+    { "lcd" ,             ROREG_FUNC(l_ht1621_lcd)},
+    { "data" ,           ROREG_FUNC(l_ht1621_data)},
+    { "cmd" ,             ROREG_FUNC(l_ht1621_cmd)},
+	{ NULL,               ROREG_INT(0)}
+};
+
+LUAMOD_API int luaopen_ht1621( lua_State *L ) {
+    luat_newlib2(L, reg_ht1621);
+    return 1;
+}

+ 54 - 0
components/ht1621/include/luat_ht1621.h

@@ -0,0 +1,54 @@
+#ifndef __SEGMENTLCD__H__
+#define __SEGMENTLCD__H__
+
+#include "luat_base.h"
+#include "luat_gpio.h"
+
+//定义HT1621的命令 
+#define  ComMode    0x52  //4COM,1/3bias  1000    010 1001  0  
+#define  RCosc      0x30  //内部RC振荡器(上电默认)1000 0011 0000 
+#define  LCD_on     0x06  //打开LCD 偏压发生器1000     0000 0 11 0 
+#define  LCD_off    0x04  //关闭LCD显示 
+#define  Sys_en     0x02  //系统振荡器开 1000   0000 0010 
+#define  CTRl_cmd   0x80  //写控制命令 
+#define  Data_cmd   0xa0  //写数据命令 
+
+//设置变量寄存器函数
+// #define sbi(x, y)  (x |= (1 << y))   /*置位寄器x的第y位*/
+// #define cbi(x, y)  (x &= ~(1 <<y ))  /*清零寄器x的第y位*/  
+
+//IO端口定义
+#define LCD_DATA 4
+#define LCD_WR 5
+#define LCD_CS 6
+
+#ifndef HIGH
+#define HIGH 1
+#endif
+
+#ifndef LOW
+#define LOW 1
+#endif
+
+typedef struct luat_ht1621_conf {
+    int pin_data;
+    int pin_wr;
+    int pin_cs;
+}luat_ht1621_conf_t;
+
+//定义端口HT1621数据端口 
+#define LCD_DATA1    luat_gpio_set(LCD_DATA,HIGH) 
+#define LCD_DATA0    luat_gpio_set(LCD_DATA,LOW) 
+#define LCD_WR1      luat_gpio_set(LCD_WR,HIGH)  
+#define LCD_WR0      luat_gpio_set(LCD_WR,LOW)   
+#define LCD_CS1      luat_gpio_set(LCD_CS,HIGH)  
+#define LCD_CS0      luat_gpio_set(LCD_CS,LOW)
+
+
+void luat_ht1621_init(luat_ht1621_conf_t *conf);
+void luat_ht1621_write_cmd(luat_ht1621_conf_t *conf, uint8_t cmd);
+void luat_ht1621_write_data(luat_ht1621_conf_t *conf, uint8_t addr, uint8_t data);
+void luat_ht1621_lcd(luat_ht1621_conf_t *conf, int onoff);
+
+
+#endif

+ 62 - 0
components/ht1621/src/luat_ht1621.c

@@ -0,0 +1,62 @@
+
+#include "luat_base.h"
+#include "luat_ht1621.h"
+#include "luat_gpio.h"
+#include "luat_timer.h"
+
+static void luat_ht1621_SendBit(luat_ht1621_conf_t *conf, unsigned char sdat,unsigned char cnt) //data 的高cnt 位写入HT1621,高位在前 
+{ 
+	unsigned char i;
+	int pin_wr = conf->pin_wr;
+	int pin_data = conf->pin_data;
+	for(i=0;i<cnt;i++) 
+	{ 
+		luat_gpio_set(pin_wr, 0);
+		luat_timer_us_delay(20); 
+		if(sdat&0x80)
+		{
+			luat_gpio_set(pin_data, 1);
+		}
+		else
+		{
+			luat_gpio_set(pin_data, 0);
+		}
+		luat_timer_us_delay(20);
+		luat_gpio_set(pin_wr, 1);
+		luat_timer_us_delay(20);
+		sdat<<=1;
+	} 
+	luat_timer_us_delay(20);  
+}
+
+
+void luat_ht1621_init(luat_ht1621_conf_t *conf) {
+	luat_gpio_mode(conf->pin_cs, LUAT_GPIO_OUTPUT, LUAT_GPIO_PULLUP, 0);
+	luat_gpio_mode(conf->pin_data, LUAT_GPIO_OUTPUT, LUAT_GPIO_PULLUP, 0);
+	luat_gpio_mode(conf->pin_wr, LUAT_GPIO_OUTPUT, LUAT_GPIO_PULLUP, 1);
+	luat_ht1621_lcd(conf, 0);
+	luat_ht1621_write_cmd(conf, Sys_en);
+	luat_ht1621_write_cmd(conf, RCosc);
+	luat_ht1621_write_cmd(conf, ComMode);
+	luat_ht1621_lcd(conf, 1);
+}
+
+void luat_ht1621_write_cmd(luat_ht1621_conf_t *conf, uint8_t cmd) {
+	luat_gpio_set(conf->pin_cs, 0);
+	luat_ht1621_SendBit(conf, 0x80,4);    		//写入标志码“100”和9 位command 命令,由于 
+	luat_ht1621_SendBit(conf, cmd,8); 		    //没有使有到更改时钟输出等命令,为了编程方便 
+	luat_gpio_set(conf->pin_cs, 1); //直接将command 的最高位写“0” 
+}
+
+void luat_ht1621_write_data(luat_ht1621_conf_t *conf, uint8_t addr, uint8_t sdat) {
+	addr<<=2; 
+	luat_gpio_set(conf->pin_cs, 0);
+	luat_ht1621_SendBit(conf, 0xa0,3);     //写入标志码“101” 
+	luat_ht1621_SendBit(conf, addr,6);     //写入addr 的高6位 
+	luat_ht1621_SendBit(conf, sdat,8);    //写入data 的8位 
+	luat_gpio_set(conf->pin_cs, 1);
+}
+
+void luat_ht1621_lcd(luat_ht1621_conf_t *conf, int onoff) {
+	luat_ht1621_write_cmd(conf, onoff ? LCD_on : LCD_off);
+}

+ 4 - 0
luat/include/luat_libs.h

@@ -184,4 +184,8 @@ LUAMOD_API int luaopen_spislave( lua_State *L );
 
 // WLAN 裸数据收发
 LUAMOD_API int luaopen_wlan_raw(lua_State *L);
+
+// 液晶屏驱动
+LUAMOD_API int luaopen_ht1621(lua_State *L);
+
 #endif