|
制作出来的实物效果图如下:
话不多说,上单片机代码
//主函数
- #include <intrins.h>
- #include <TM1637.H>
-
- void main()
- {
-
- TM1637_init();
- TM1637_show_digit(0, 1, 0);
- TM1637_show_digit(1, 2, 1);
- TM1637_show_digit(2, 3, 0);
- TM1637_show_digit(3, 4, 0);
-
- while(1)
- {
-
- }
-
- }
复制代码 1637.c
- #include <TM1637.H>
- code unsigned char TM1637_SEG_TAB[] =
- {
- 0x3F // 00111111 0
- ,0x06 // 00000110 1
- ,0x5B // 01011011 2
- ,0x4F // 01001111 3
- ,0x66 // 01100110 4
- ,0x6d // 01101101 5
- ,0x7d // 01111101 6
- ,0x07 // 00000111 7
- ,0x7F // 01111111 8
- ,0x6F // 01101111 9
- ,0x77 // 01110111 A
- ,0x7C // 01111100 b
- ,0x39 // 00111001 C
- ,0x5E // 01011110 d
- ,0x79 // 01111001 E
- ,0x71 // 01110001 F
- };
- void I2C_delay()
- {
- nop_();
- }
-
-
- void I2C_start()
- {
- I2C_SCL = 1;
- I2C_SDA = 1;
-
-
- I2C_delay();
-
-
- I2C_SDA = 0; // 时钟线设置为常态,低电平。
- }
-
-
- /*
- I2C 总线写1个字节
- 不包含应答位的检测
- */
- void I2C_write_byte(char byte_to_write)
- {
- unsigned char i;
-
-
- for(i=0; i<8; i++)
- {
- I2C_SCL = 0;
-
- I2C_SDA = byte_to_write & 0x01;
- I2C_delay();
-
- I2C_SCL = 1;
- I2C_delay();
-
-
- I2C_SCL = 0;
- I2C_delay();
-
-
- byte_to_write >>= 1;
- }
-
- return;
- }
-
-
- void I2C_check_ack()
- {
- // I2C 检查应答位,实际未检查,只发送了应答位时钟,认为TM1637工作正常
-
-
- I2C_SCL = 0;
- I2C_delay();//在第八个时钟下降沿之后延时5us,开始判断ACK 信号
-
- while(I2C_SDA)
- {
- ;
- }
-
-
- I2C_SCL = 1;
- I2C_delay();
-
-
- I2C_SCL=0;
- }
-
-
- void I2C_send_ack()
- {
- I2C_SDA = 0;
- I2C_delay();
-
-
- I2C_SCL = 1;
- I2C_delay();
-
-
- I2C_SCL = 0;
- I2C_delay();
-
-
- I2C_SDA = 1;
- I2C_delay();
- }
-
-
- void I2C_stop()
- {
- I2C_SCL = 0;
- I2C_delay();
-
-
- I2C_SDA = 0;
- I2C_delay();
-
-
- I2C_SCL = 1;
- I2C_delay();
-
-
- I2C_SDA = 1;
- }
-
-
- ///
- //
- // TM1637模块操作函数
- //
- //
-
-
- void TM1637_write_command_byte(unsigned char command)
- {
- I2C_start();
- I2C_write_byte(command);
- I2C_check_ack();
- I2C_stop();
- }
-
-
-
-
- // bright : 0 - 7
- void TM1637_display_switch_and_bright(bit enable_display, unsigned char bright)
- {
- TM1637_write_command_byte(0x80 + (enable_display ? 0x08 : 0x00) + bright);
- }
-
-
-
- void TM1637_write_display_bytes(unsigned char first_addr,
- unsigned char bytes_to_write[],
- unsigned char num_of_bytes)
- {
- unsigned char i;
-
-
- I2C_start();
- I2C_write_byte(0x40);
- I2C_check_ack();
- I2C_stop();
-
-
- I2C_start();
- I2C_write_byte(0xC0 + first_addr);
- I2C_check_ack();
-
-
- for(i=0; i<num_of_bytes; i++)
- {
- I2C_write_byte(bytes_to_write[i]);
- I2C_check_ack();
- }
-
-
- I2C_stop();
- }
-
-
-
- /**
- * @brief 显示数据
- * @param 地址值 addr: 0 - 5
- * @param 数据
- * @param
- * @retval None
- */
- void TM1637_write_display_byte(unsigned char addr, unsigned char segments)
- {
- I2C_start();
- I2C_write_byte(0x40 + addr);
- I2C_check_ack();
- I2C_stop();
-
-
- I2C_start();
- I2C_write_byte(0xC0 + addr);
- I2C_check_ack();
-
-
- I2C_write_byte(segments);
- I2C_check_ack();
-
-
- I2C_stop();
- }
-
-
-
-
-
-
- void TM1637_show_digit(unsigned char pos, unsigned char digit, bit with_dot)
- {
- TM1637_write_display_byte(pos, TM1637_SEG_TAB[digit] | (with_dot ? 0x80 : 0x00));
- }
-
-
-
-
- void TM1637_init()
- {
- unsigned char i;
-
-
- TM1637_display_switch_and_bright(1, TM1637_BRIGHTNESS);
-
-
- for(i=0; i<6; i++)
- {
- TM1637_write_display_byte(i, 0xFF);
- }
-
- for(i=0; i<6; i++)
- {
- TM1637_write_display_byte(i, 0x00);
- }
- }
复制代码
1637.h
- #ifndef __TM1637_H__
- #define __TM1637_H__
- #include"reg52.h"
- #include"intrins.h"
- sbit I2C_SCL = P2^0; //定义模拟IIC总线的时钟线
- sbit I2C_SDA = P2^1; //定义模拟IIC总线的数据线
-
-
- #define TM1637_BRIGHTNESS 4 // 0 - 7 亮度
- void TM1637_show_digit(unsigned char pos, unsigned char digit, bit with_dot);
- void TM1637_init();
- #endif
复制代码
Keil代码下载:
AIP1637.zip
(40.3 KB, 下载次数: 30)
|
评分
-
查看全部评分
|