#include #include #define TM1628_DIO_PORTNAME B #define TM1628_DIO_BIT 5 #define TM1628_CLK_PORTNAME B #define TM1628_CLK_BIT 7 #define TM1628_STB_PORTNAME B #define TM1628_STB_BIT 4 #ifndef _CONCAT_ #define _CONCAT_ #define CONCAT(a, b) a ## b #define CONCAT_DDR(portname) CONCAT(DDR, portname) #define CONCAT_PORT(portname) CONCAT(PORT, portname) #define CONCAT_PIN(portname) CONCAT(PIN, portname) #endif /* CONCAT */ #define TM1628_DIO_DDR CONCAT_DDR(TM1628_DIO_PORTNAME) #define TM1628_DIO_PORT CONCAT_PORT(TM1628_DIO_PORTNAME) #define TM1628_DIO_PIN CONCAT_PIN(TM1628_DIO_PORTNAME) #define TM1628_DIO_0 TM1628_DIO_PORT &= ~(1 << TM1628_DIO_BIT) #define TM1628_DIO_1 TM1628_DIO_PORT |= (1 << TM1628_DIO_BIT) #define TM1628_DIO_VALUE (TM1628_DIO_PIN & (1 << TM1628_DIO_BIT)) #define TM1628_CLK_DDR CONCAT_DDR(TM1628_CLK_PORTNAME) #define TM1628_CLK_PORT CONCAT_PORT(TM1628_CLK_PORTNAME) #define TM1628_CLK_PIN CONCAT_PIN(TM1628_CLK_PORTNAME) #define TM1628_CLK_0 TM1628_CLK_PORT &= ~(1 << TM1628_CLK_BIT) #define TM1628_CLK_1 TM1628_CLK_PORT |= (1 << TM1628_CLK_BIT) #define TM1628_STB_DDR CONCAT_DDR(TM1628_STB_PORTNAME) #define TM1628_STB_PORT CONCAT_PORT(TM1628_STB_PORTNAME) #define TM1628_STB_PIN CONCAT_PIN(TM1628_STB_PORTNAME) #define TM1628_STB_0 TM1628_STB_PORT &= ~(1 << TM1628_STB_BIT) #define TM1628_STB_1 TM1628_STB_PORT |= (1 << TM1628_STB_BIT) #define SEG_NULL 0x00 #define SEG_P 0x73 #define SEG_N 0x37 #define SEG_U 0x3e #define SEG_L 0x38 #define SEG_H 0x76 #define SEG_NEG 0x40 #define LEFT 0 #define RIGHT 1 unsigned char _data[10], brightness = 0; long n = 0; unsigned char number[] = { 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71 }; void TM1628_TxByte(unsigned char _data) { unsigned char i = 0; TM1628_CLK_1; TM1628_DIO_DDR |= (1 << TM1628_DIO_BIT); for (i = 0; i < 8; i++) { TM1628_CLK_0; if (_data & 0x01) { TM1628_DIO_1; } else { TM1628_DIO_0; } TM1628_CLK_1; _data >>= 1; } _delay_us(2); } unsigned char TM1628_RxByte(void) { unsigned char _data = 0, i = 0; TM1628_CLK_1; TM1628_DIO_DDR &= ~(1 << TM1628_DIO_BIT); TM1628_DIO_1; for (i = 0; i < 8; i++) { TM1628_CLK_0; _data >>= 1; TM1628_CLK_1; _delay_us(1); if (TM1628_DIO_VALUE) { _data |= 0x80; } } return _data; } void TM1628_WriteCommand(unsigned char command) { TM1628_STB_0; TM1628_TxByte(command); TM1628_STB_1; } void TM1628_Display(unsigned char *p_data, unsigned char brightness) { unsigned char i = 0, j = 0, temp = 0; unsigned char dis_arr[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; for (i = 0; i < 7; i++) { temp = 0; for (j = 0; j < 8; j++) { if(p_data[j] & (1 << i)) { temp |= (1 << j); } } dis_arr[i << 1] = temp; } for (i = 0; i < 7; i++) { temp = 0; for (j = 0; j < 2; j++) { if(p_data[j + 8] & (1 << i)) { temp |= (1 << j); } } dis_arr[(i << 1) + 1] = temp; } TM1628_WriteCommand(0x03); TM1628_WriteCommand(0x40); TM1628_STB_0; TM1628_TxByte(0xc0); _delay_us(1); for (i = 0; i < 14; i++) { TM1628_TxByte(dis_arr[i]); } TM1628_STB_1; if (brightness > 7) { brightness = 7; } TM1628_WriteCommand(0x88 + brightness); } //数据,进制,起始位置,占用位数,对齐方式,亮度 void TM1628_DisplayNumber(long n, unsigned char ary, unsigned char start, unsigned char size, unsigned char left_right, unsigned char brightness) { unsigned char i = 0, temp[size], neg_flag = 0, length = 0; if (-n > 0) { n = -n; neg_flag = 1; } for (i = 0; ((i < size) && (n > 0)); i++) { temp[i] = number[n % ary]; n /= ary; } if (i == 0) { temp[i] = number[0]; i = 1; } if (neg_flag && (i < size)) { temp[i] = SEG_NEG; i++; } length = i; if (left_right == LEFT) { for (i = 0; i < length; i++) { _data[start + i] = temp[length - i - 1]; } for (i = length; i < size; i++) { _data[start + i] = SEG_NULL; } } else if (left_right == RIGHT) { for (i = 0; i < size - length; i++) { _data[start + i] = SEG_NULL; } for (i = size - length; i < size; i++) { _data[start + i] = temp[size - i - 1]; } } TM1628_Display(_data, brightness); } unsigned char TM1628_ReadKey(void) { unsigned char i = 0, temp = 0; TM1628_STB_0; TM1628_TxByte(0x42); _delay_us(100); for (i = 0; i < 5; i++) { _delay_us(2); temp = TM1628_RxByte(); if (temp) { break; } } TM1628_STB_1; if (temp & 0x03) { temp = (2 * i + 1) + ((temp & 0x03) - 1) * 10; } else if (temp & 0x18) { temp = (2 * (i + 1)) + (((temp & 0x18) >> 3) - 1) * 10; } return temp; } void key_process(unsigned char key) { if (key != 0) { TM1628_DisplayNumber(key, 10, 2, 2, LEFT, brightness); } if (key == 1 && brightness < 7) { brightness++; TM1628_DisplayNumber(brightness, 10, 0, 1, LEFT, brightness); } else if (key == 11 && brightness > 0) { brightness--; TM1628_DisplayNumber(brightness, 10, 0, 1, LEFT, brightness); } else if (key == 10) { n++; TM1628_DisplayNumber(n, 10, 5, 5, RIGHT, brightness); } else if (key ==20) { n--; TM1628_DisplayNumber(n, 10, 5, 5, RIGHT, brightness); } } int main(void) { unsigned char i = 0, key = 0, now = 0, repeat_flag = 0; unsigned int repeat_count = 0; _delay_ms(100); TM1628_STB_1; TM1628_STB_DDR |= (1 << TM1628_STB_BIT); TM1628_CLK_1; TM1628_CLK_DDR |= (1 << TM1628_CLK_BIT);
上一篇:AVR/Arduino定时/计数器、中断入门
下一篇:Arduino+Avr libc制作Badusb原理及示例讲解
推荐阅读最新更新时间:2024-11-06 16:28
设计资源 培训 开发板 精华推荐
- EVAL-AD7655CB,用于 AD7655、16 位、500 Ksps PulSAR 模数转换器的评估板
- 有源DC恒流电子负载
- 使用 Richtek Technology Corporation 的 RT8152F 的参考设计
- 使用 LTC1867IGN 16 位、8 通道、200 Ksps ADC 实现单端输入的典型应用电路
- LT1086CM 5V、1.5A 低压差正稳压器的典型应用
- 星球大战遥控器 改 萝莉遥控器 主板
- AD8604ARUZ 高端运算放大器电流监视器的典型应用
- 持续气道正压通气 (CPAP) 通过保持肺泡张开并防止它们在呼气阶段完全塌陷来帮助患者呼吸
- 适用于空调应用的,使用VIPer35LD的16 W三路输出准谐振反激转换器
- AM1G-2405DZ ±5V 1 瓦 DC-DC 转换器的典型应用