3. Indicator lights. Due to the powerful driving capability of TM1640, support for dual-color LEDs has been added this time. It can display up to three colors, giving you more options for your calendar! A power indicator light has also been added (only increasing power consumption). What? You mean WS2812? Your money is brought by the strong wind! ! !
6. A calendar is a calendar after all, and it also needs to display time. The DS1302 chip is used here. Although I have a lot of DS1307 in stock at home, I have not adjusted it yet, so I still use the more familiar DS1302. At least I can read the time. Since it is just The calendar does not need to display time, so the accuracy requirements are not that strict, so DS3231 is not used.
It should be noted here that the device used by the buzzer is C95297 .The schematic diagram found in Lichuang Mall is incorrect., when welding or drawing the schematic diagram, pay attention to distinguish the position of each pin. The following two pictures are from the product manuals provided by Lichuang Mall and Lichuang Mall respectively.
2. In order to facilitate the update of DS1302, the corresponding reading and writing are specially introduced. Pins are convenient for using other MCUs to operate it. As shown in the figure, they correspond to the data, clock, and enable pins of DS1302 respectively.
TM1640 driver code
void i2c_start() {
digitalWrite(scl, 1);
delayMicroseconds(2);
digitalWrite(sda, 1);
delayMicroseconds(2);
digitalWrite(sda, 0);
delayMicroseconds(2);
digitalWrite(scl, 0);
delayMicroseconds(2);
}
void i2c_stop() {
digitalWrite(scl, 0);
delayMicroseconds(2);
digitalWrite(sda, 0);
delayMicroseconds(2);
digitalWrite(scl, 1);
delayMicroseconds(2);
digitalWrite(sda, 1);
delayMicroseconds(2);
}
void i2c_write(uint8_t data) {
for (int i = 0; i < ;= 7; i++) {
if (data % 2) {
digitalWrite(sda, 1);
delayMicroseconds(2);
digitalWrite(scl, 0);
delayMicroseconds(2);
digitalWrite(scl, 1);
delayMicroseconds(2);
digitalWrite(scl, 0);
delayMicroseconds(2);
digitalWrite(sda, 0);
}
else
{
delayMicroseconds(2);
digitalWrite(sda, 0);
delayMicroseconds(2);
digitalWrite(scl, 0);
delayMicroseconds(2);
digitalWrite(scl, 1);
delayMicroseconds(2);
digitalWrite(scl, 0);
delayMicroseconds(2);
digitalWrite(sda, 0);
}
data /= 2;
}
}
DS1302 driver code, transferred here
void _nextBit()
{
digitalWrite(_pin_clk, HIGH);
delayMicroseconds(1);
digitalWrite(_pin_clk, LOW);
delayMicroseconds(1);
}
uint8_t _readByte()
{
uint8_t byte = 0;
for (uint8_t b = 0; b < 8; b++)
{
if (digitalRead(_pin_dat) == HIGH) byte |= 0x01 << b;
_nextBit();
}
return byte;
}
void _writeByte(uint8_t value)
{
for (uint8_t b = 0; b < 8; b++)
{
digitalWrite(_pin_dat, (value & 0x01) ? HIGH : LOW);
_nextBit();
value >>= 1;
}
}
void _setDirection(int direction) {
pinMode(_pin_dat, direction);
}
void _prepareRead(uint8_t address)
{
_setDirection(OUTPUT);
digitalWrite(_pin_ena, HIGH);
uint8_t command = 0b10000001 | address;
_writeByte(command);
_setDirection(INPUT);
}
void _prepareWrite(uint8_t address)
{
_setDirection(OUTPUT);
digitalWrite(_pin_ena, HIGH);
uint8_t command = 0b10000000 | address;
_writeByte(command);
}
void _end()
{
digitalWrite(_pin_ena, LOW);
}
uint8_t _dec2bcd(uint8_t dec)
{
return ((dec / 10 * 16) + (dec % 10));
}
uint8_t _bcd2dec(uint8_t bcd)
{
return ((bcd / 16 * 10) + (bcd % 16));
}
void get_time() { //获取时间
_prepareRead(REG_BURST);
second = _bcd2dec(_readByte() & 0b01111111);
minute = _bcd2dec(_readByte() & 0b01111111);
hour = _bcd2dec(_readByte() & 0b00111111);
day = _bcd2dec(_readByte() & 0b00111111);
mouth = _bcd2dec(_readByte() & 0b00011111);
dow = _bcd2dec(_readByte() & 0b00000111);
year = _bcd2dec(_readByte() & 0b01111111);
_end();
Serial_print_s("Now time is ");
Serial_print_i(year);
Serial_print_s("-");
Serial_print_i(mouth);
Serial_print_s("-");
Serial_print_i(day);
Serial_print_s("-");
Serial_print_i(hour);
Serial_print_s(":");
Serial_print_i(minute);
Serial_print_s(":");
Serial_println_i(second);
}
void set_time(int sec, int minute, int hour, int day, int mouth, int dow, int year) { //设置时间
_prepareWrite(REG_WP);
_writeByte(0b00000000);
_end();
_prepareWrite(REG_BURST);
_writeByte(_dec2bcd(sec % 60 ));
_writeByte(_dec2bcd(minute % 60 ));
_writeByte(_dec2bcd(hour % 24 ));
_writeByte(_dec2bcd(day % 32 ));
_writeByte(_dec2bcd(mouth % 13 ));
_writeByte(_dec2bcd(dow % 8 ));
_writeByte(_dec2bcd(year % 100));
_writeByte(0b10000000);
_end();
}
void set_ram(uint8_t add, uint8_t data) { //写RAM
_prepareWrite(REG_WP);
_writeByte(0b00000000);
_end();
_prepareWrite(add);
_writeByte(data);
_writeByte(0b10000000);
_end();
}
uint8_t read_ram(uint8_t add) { //读RAM
_prepareRead(add);
uint8_t readen = _readByte();
_end();
Serial_print_s("I has read a byte :");
Serial_print_i(readen);
}
Code for calculating dates. You can calculate the number of weeks and the number of the day based on the number of weeks on the first day of the month.
int nW(int Day) {
int Nn = Day + nD - 1;
int n_W = Nn / 7 + 1;
int n_D = Nn % 7;
//if (!n_D){
//n_D = 7;}
/*Serial.print(Day);
Serial.print("是第");
Serial.print(n_W);
Serial.print("周的星期");
Serial.println(n_D);
return n_W * 10 + n_D;*/
}
Documents printed directly are opaque because the bottom layer is not windowed, so light cannot come through.
All reference designs on this site are sourced from major semiconductor manufacturers or collected online for learning and research. The copyright belongs to the semiconductor manufacturer or the original author. If you believe that the reference design of this site infringes upon your relevant rights and interests, please send us a rights notice. As a neutral platform service provider, we will take measures to delete the relevant content in accordance with relevant laws after receiving the relevant notice from the rights holder. Please send relevant notifications to email: bbs_service@eeworld.com.cn.
It is your responsibility to test the circuit yourself and determine its suitability for you. EEWorld will not be liable for direct, indirect, special, incidental, consequential or punitive damages arising from any cause or anything connected to any reference design used.
Supported by EEWorld Datasheet