STC单片机DS1302+ssd1306 oled时钟显示

127 篇文章 302 订阅 ¥9.90 ¥99.00

STC单片机DS1302+ssd1306 oled时钟显示


  • 📌相关篇《STC单片机对DS1302数据读写

  • ✨效果演示:
    在这里插入图片描述

  • 🌿实验对象:STC12C5A60S2

  • 🌿屏幕型号:I2C ssd1306 0.96“ oled

  • 🌿晶振频率:11.059MHz

  • 🌿串口波特率:9600

  • 📜串口打印读取信息:
    在这里插入图片描述

🛠引脚说明

  				STC12C5A60S2-----DS1302 
 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
STC51是一种常用的单片机DS1302是一种实时时钟芯片,OLED1306是一种OLED显示屏,蜂鸣器是一种用于发出声音的电子元件。下面是一个简单的STC51+DS1302闹钟代码+蜂鸣器+OLED1306的示例: ```c #include <reg51.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int sbit SDA = P2^0; // I2C数据线 sbit SCL = P2^1; // I2C时钟线 sbit BEEP = P3^7; // 蜂鸣器 sbit OLED_SCL = P1^0; // OLED时钟线 sbit OLED_SDA = P1^1; // OLED数据线 sbit OLED_RST = P1^2; // OLED复位引脚 uchar code table[] = { // 数字0-9的字模表 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F }; uchar second, minute, hour; // 当前时间 uchar alarm_hour, alarm_minute; // 闹钟时间 void delay(uint t) { // 延时函数 while (t--); } void I2C_Start() { // I2C起始信号 SDA = 1; SCL = 1; _nop_(); _nop_(); SDA = 0; _nop_(); _nop_(); SCL = 0; } void I2C_Stop() { // I2C停止信号 SDA = 0; SCL = 1; _nop_(); _nop_(); SDA = 1; } void I2C_SendByte(uchar dat) { // I2C发送一个字节 uchar i; for (i = 0; i < 8; i++) { SDA = dat >> 7; dat <<= 1; SCL = 1; _nop_(); _nop_(); SCL = 0; } SDA = 1; SCL = 1; _nop_(); _nop_(); SCL = 0; } uchar I2C_ReceiveByte() { // I2C接收一个字节 uchar i, dat = 0; SDA = 1; for (i = 0; i < 8; i++) { dat <<= 1; SCL = 1; _nop_(); _nop_(); dat |= SDA; SCL = 0; } return dat; } void DS1302_Write(uchar addr, uchar dat) { // DS1302写入一个字节 I2C_Start(); I2C_SendByte(0x80); I2C_SendByte(addr); I2C_SendByte(dat); I2C_Stop(); } uchar DS1302_Read(uchar addr) { // DS1302读取一个字节 uchar dat; I2C_Start(); I2C_SendByte(0x81); I2C_SendByte(addr); dat = I2C_ReceiveByte(); I2C_Stop(); return dat; } void DS1302_Init() { // DS1302初始化 DS1302_Write(0x8e, 0x00); // 禁止写保护 DS1302_Write(0x80, 0x00); // 关闭时钟暂停 } void DS1302_GetTime() { // 获取当前时间 uchar i; uchar time[7]; for (i = 0; i < 7; i++) { time[i] = DS1302_Read(0x81 + i); } second = (time[0] & 0x0f) + ((time[0] >> 4) & 0x07) * 10; minute = (time[1] & 0x0f) + ((time[1] >> 4) & 0x07) * 10; hour = (time[2] & 0x0f) + ((time[2] >> 4) & 0x03) * 10; } void DS1302_SetTime() { // 设置当前时间 uchar time[7]; time[0] = (second % 10) | ((second / 10) << 4); time[1] = (minute % 10) | ((minute / 10) << 4); time[2] = (hour % 10) | ((hour / 10) << 4); time[3] = 0x01; time[4] = (alarm_minute % 10) | ((alarm_minute / 10) << 4); time[5] = (alarm_hour % 10) | ((alarm_hour / 10) << 4); time[6] = 0x00; DS1302_Write(0x8e, 0x00); // 禁止写保护 DS1302_Write(0x80, 0x00); // 关闭时钟暂停 DS1302_Write(0xbe, time[0]); DS1302_Write(0xbe + 1, time[1]); DS1302_Write(0xbe + 2, time[2]); DS1302_Write(0xbe + 3, time[3]); DS1302_Write(0xbe + 4, time[4]); DS1302_Write(0xbe + 5, time[5]); DS1302_Write(0xbe + 6, time[6]); } void OLED_WriteCmd(uchar cmd) { // OLED写入命令 uchar i; OLED_SCL = 0; OLED_SDA = 0; OLED_SCL = 1; OLED_SCL = 0; for (i = 0; i < 8; i++) { OLED_SCL = 0; OLED_SDA = (cmd >> (7 - i)) & 0x01; OLED_SCL = 1; OLED_SCL = 0; } } void OLED_WriteData(uchar dat) { // OLED写入数据 uchar i; OLED_SCL = 0; OLED_SDA = 1; OLED_SCL = 1; OLED_SCL = 0; for (i = 0; i < 8; i++) { OLED_SCL = 0; OLED_SDA = (dat >> (7 - i)) & 0x01; OLED_SCL = 1; OLED_SCL = 0; } } void OLED_SetPos(uchar x, uchar y) { // OLED设置坐标 OLED_WriteCmd(0xb0 + y); OLED_WriteCmd(((x & 0xf0) >> 4) | 0x10); OLED_WriteCmd((x & 0x0f) | 0x01); } void OLED_Clear() { // OLED清屏 uchar i, j; for (i = 0; i < 8; i++) { OLED_SetPos(0, i); for (j = 0; j < 128; j++) { OLED_WriteData(0x00); } } } void OLED_ShowChar(uchar x, uchar y, uchar chr) { // OLED显示字符 uchar c = chr - ' '; if (x > 127 || y > 7) { return; } OLED_SetPos(x, y); uchar i; for (i = 0; i < 8; i++) { OLED_WriteData(table[c * 8 + i]); } } void OLED_ShowString(uchar x, uchar y, uchar *str) { // OLED显示字符串 while (*str != '\0') { OLED_ShowChar(x, y, *str); x += 8; str++; } } void BEEP_On() { // 打开蜂鸣器 BEEP = 0; } void BEEP_Off() { // 关闭蜂鸣器 BEEP = 1; } void main() { DS1302_Init(); DS1302_SetTime(); while (1) { DS1302_GetTime(); if (hour == alarm_hour && minute == alarm_minute) { BEEP_On(); delay(50000); BEEP_Off(); delay(50000); } OLED_Clear(); OLED_ShowString(0, 0, "Time:"); OLED_ShowChar(48, 0, hour / 10 + '0'); OLED_ShowChar(56, 0, hour % 10 + '0'); OLED_ShowChar(64, 0, ':'); OLED_ShowChar(72, 0, minute / 10 + '0'); OLED_ShowChar(80, 0, minute % 10 + '0'); OLED_ShowChar(88, 0, ':'); OLED_ShowChar(96, 0, second / 10 + '0'); OLED_ShowChar(104, 0, second % 10 + '0'); } } ``` 这段代码实现了一个简单的闹钟功能,使用STC51单片机控制DS1302实时时钟芯片获取当前时间,并通过OLED1306显示显示时间,当时间与设定的闹钟时间相同时,蜂鸣器会发出声音。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值