42 #define CHIP_NAME "Titan Micro Electronics TM1638"
43 #define MANUFACTURER_NAME "Titan Micro Electronics"
44 #define SUPPLY_VOLTAGE_MIN 3.0f
45 #define SUPPLY_VOLTAGE_MAX 6.0f
46 #define MAX_CURRENT 200.0f
47 #define TEMPERATURE_MIN -40.0f
48 #define TEMPERATURE_MAX 125.0f
49 #define DRIVER_VERSION 1000
54 #define TM1638_COMMAND_DATA (1 << 6)
55 #define TM1638_COMMAND_DISPLAY (2 << 6)
56 #define TM1638_COMMAND_ADDRESS (3 << 6)
64 static uint8_t a_high_low_shift(uint8_t data)
68 output = (data << 4) | (data >> 4);
69 output = ((output << 2) & 0xCC) | ((output >> 2) & 0x33);
70 output = ((output << 1) & 0xAA) | ((output >> 1) & 0x55);
86 static uint8_t a_tm1638_write(
tm1638_handle_t *handle, uint8_t cmd, uint8_t *buf, uint16_t len)
88 uint8_t inner_buffer[32];
95 for (i = 0; i < len; i++)
97 inner_buffer[i] = a_high_low_shift(buf[i]);
99 if (handle->
spi_write(a_high_low_shift(cmd), inner_buffer, len) != 0)
118 static uint8_t a_tm1638_read(
tm1638_handle_t *handle, uint8_t cmd, uint8_t *buf, uint16_t len)
122 if (handle->
spi_read(a_high_low_shift(cmd), buf, len) != 0)
126 for (i = 0; i < len; i++)
128 buf[i] = a_high_low_shift(buf[i]);
156 handle->
debug_print(
"tm1638: spi_init is null.\n");
162 handle->
debug_print(
"tm1638: spi_deinit is null.\n");
168 handle->
debug_print(
"tm1638: spi_write is null.\n");
174 handle->
debug_print(
"tm1638: spi_read is null.\n");
180 handle->
debug_print(
"tm1638: delay_ms is null.\n");
225 res = a_tm1638_write(handle, cmd, NULL, 0);
236 handle->
debug_print(
"tm1638: spi deinit failed.\n");
273 res = a_tm1638_write(handle, cmd, NULL, 0);
338 res = a_tm1638_write(handle, cmd, NULL, 0);
403 res = a_tm1638_write(handle, cmd, NULL, 0);
468 res = a_tm1638_write(handle, cmd, NULL, 0);
520 uint16_t data[8] = {0};
532 res = a_tm1638_write(handle, cmd, NULL, 0);
542 for (i = 0; i < 8; i++)
547 temp = (data[i] >> 0) & 0xFF;
548 res = a_tm1638_write(handle, cmd, &temp, 1);
557 temp = (data[i] >> 8) & 0xFF;
558 res = a_tm1638_write(handle, cmd, &temp, 1);
571 for (i = 0; i < 8; i++)
573 temp[i * 2 + 0] = data[i] & 0xFF;
574 temp[i * 2 + 1] = (data[i] >> 8) & 0xFF;
577 res = a_tm1638_write(handle, cmd, temp, 8 * 2);
625 res = a_tm1638_write(handle, cmd, NULL, 0);
635 for (i = 0; i < len; i++)
640 temp = (data[i] >> 0) & 0xFF;
641 res = a_tm1638_write(handle, cmd, &temp, 1);
650 temp = (data[i] >> 8) & 0xFF;
651 res = a_tm1638_write(handle, cmd, &temp, 1);
664 for (i = 0; i < len; i++)
666 temp[i * 2 + 0] = data[i] & 0xFF;
667 temp[i * 2 + 1] = (data[i] >> 8) & 0xFF;
670 res = a_tm1638_write(handle, cmd, temp, len * 2);
708 res = a_tm1638_read(handle, cmd, segk, 4);
745 res = a_tm1638_write(handle, cmd, data, len);
782 res = a_tm1638_read(handle, cmd, data, len);
#define TM1638_COMMAND_DISPLAY
#define SUPPLY_VOLTAGE_MAX
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define TM1638_COMMAND_ADDRESS
#define CHIP_NAME
chip information definition
#define TM1638_COMMAND_DATA
chip command definition
driver tm1638 header file
uint8_t tm1638_get_test_mode(tm1638_handle_t *handle, tm1638_bool_t *enable)
get test mode status
uint8_t tm1638_write_segment(tm1638_handle_t *handle, uint8_t addr, uint16_t *data, uint8_t len)
write segment
tm1638_bool_t
tm1638 bool enumeration definition
uint8_t tm1638_set_address_mode(tm1638_handle_t *handle, tm1638_address_mode_t mode)
set address mode
tm1638_address_mode_t
tm1638 address mode enumeration definition
uint8_t tm1638_set_pulse_width(tm1638_handle_t *handle, tm1638_pulse_width_t width)
set pulse width
uint8_t tm1638_deinit(tm1638_handle_t *handle)
close the chip
uint8_t tm1638_clear_segment(tm1638_handle_t *handle)
clear segment
uint8_t tm1638_init(tm1638_handle_t *handle)
initialize the chip
uint8_t tm1638_read_segment(tm1638_handle_t *handle, uint8_t segk[4])
read segment
uint8_t tm1638_set_display(tm1638_handle_t *handle, tm1638_bool_t enable)
enable or disable display
tm1638_pulse_width_t
tm1638 pulse width enumeration definition
uint8_t tm1638_get_display(tm1638_handle_t *handle, tm1638_bool_t *enable)
get display status
uint8_t tm1638_get_address_mode(tm1638_handle_t *handle, tm1638_address_mode_t *mode)
get address mode
uint8_t tm1638_set_test_mode(tm1638_handle_t *handle, tm1638_bool_t enable)
enable or disable test mode
uint8_t tm1638_info(tm1638_info_t *info)
get chip's information
uint8_t tm1638_get_pulse_width(tm1638_handle_t *handle, tm1638_pulse_width_t *width)
get pulse width
uint8_t tm1638_set_reg(tm1638_handle_t *handle, uint8_t cmd, uint8_t *data, uint8_t len)
set the chip register
uint8_t tm1638_get_reg(tm1638_handle_t *handle, uint8_t cmd, uint8_t *data, uint8_t len)
get the chip register
tm1638 handle structure definition
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* spi_deinit)(void)
uint8_t(* spi_write)(uint8_t addr, uint8_t *buf, uint16_t len)
uint8_t(* spi_read)(uint8_t addr, uint8_t *buf, uint16_t len)
tm1638 information structure definition
float supply_voltage_max_v
char manufacturer_name[32]
float supply_voltage_min_v