简易数字电子钟程序设计
时间: 2023-11-10 11:15:21 浏览: 23
以下是一个简易数字电子钟的程序设计,基于STM32F103C8T6微控制器和DS3231时钟芯片,使用C语言编写:
1. 头文件和宏定义
```c
#include "stm32f10x.h"
#include "delay.h"
#include "i2c.h"
#include "ds3231.h"
#define DS3231_ADDR 0x68
#define I2C_SPEED 400000
#define SCL_PIN GPIO_Pin_6
#define SDA_PIN GPIO_Pin_7
#define SCL_PORT GPIOB
#define SDA_PORT GPIOB
#define KEY1_PIN GPIO_Pin_12
#define KEY2_PIN GPIO_Pin_13
#define KEY3_PIN GPIO_Pin_14
#define KEY4_PIN GPIO_Pin_15
#define KEY_PORT GPIOA
#define LED_PIN GPIO_Pin_8
#define LED_PORT GPIOA
```
2. 初始化函数
```c
void init(void)
{
SystemInit();
delay_init();
I2C_GPIO_Config(SCL_PORT, SCL_PIN, SDA_PORT, SDA_PIN);
I2C_Configuration(I2C_SPEED);
DS3231_Init(DS3231_ADDR);
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_PORT, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = KEY1_PIN | KEY2_PIN | KEY3_PIN | KEY4_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(KEY_PORT, &GPIO_InitStructure);
}
```
3. 数码管显示函数
```c
void display(unsigned char h, unsigned char m, unsigned char s)
{
unsigned char buf[4] = {0, 0, 0, 0};
buf[0] = h / 10;
buf[1] = h % 10;
buf[2] = m / 10;
buf[3] = m % 10;
for (unsigned char i = 0; i < 4; i++)
{
GPIO_ResetBits(LED_PORT, LED_PIN);
shiftOut(buf[i]);
GPIO_SetBits(LED_PORT, LED_PIN);
delay_ms(1);
}
}
```
4. 主函数
```c
int main(void)
{
unsigned char h = 0, m = 0, s = 0;
unsigned char h_alarm = 0, m_alarm = 0;
init();
while (1)
{
h = DS3231_GetHour(DS3231_ADDR);
m = DS3231_GetMinute(DS3231_ADDR);
s = DS3231_GetSecond(DS3231_ADDR);
if (GPIO_ReadInputDataBit(KEY_PORT, KEY1_PIN) == RESET)
{
h++;
if (h > 23)
h = 0;
DS3231_SetHour(DS3231_ADDR, h);
delay_ms(100);
}
if (GPIO_ReadInputDataBit(KEY_PORT, KEY2_PIN) == RESET)
{
m++;
if (m > 59)
m = 0;
DS3231_SetMinute(DS3231_ADDR, m);
delay_ms(100);
}
if (GPIO_ReadInputDataBit(KEY_PORT, KEY3_PIN) == RESET)
{
h_alarm++;
if (h_alarm > 23)
h_alarm = 0;
delay_ms(100);
}
if (GPIO_ReadInputDataBit(KEY_PORT, KEY4_PIN) == RESET)
{
m_alarm++;
if (m_alarm > 59)
m_alarm = 0;
delay_ms(100);
}
display(h, m, s);
if (h == h_alarm && m == m_alarm && s == 0)
{
GPIO_SetBits(LED_PORT, LED_PIN);
delay_ms(500);
GPIO_ResetBits(LED_PORT, LED_PIN);
delay_ms(500);
}
}
}
```
该程序实现了时钟的显示、时间调整、闹钟设置和闹钟响铃等功能。通过按键控制,可以对时间和闹钟时间进行调整,并可以开启或关闭闹钟。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)