From 8f5aa0e0a77799a72e7f2282781c2648299fa5b3 Mon Sep 17 00:00:00 2001 From: jxh Date: Wed, 19 Mar 2025 18:22:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90time3=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=8F=90=E4=BE=9B=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 4 +++- Driver/iic.c | 2 +- Driver/{timer4.c => timer.c} | 43 ++++++++++++++++++++++++++++++++++-- Driver/timer.h | 23 +++++++++++++++++++ Driver/timer4.h | 14 ------------ Lib/KeyScan.c | 22 ++++++++++++++++++ Lib/KeyScan.h | 16 ++++++++++++++ Lib/delay.h | 2 +- Src/main.c | 29 +++++------------------- 9 files changed, 113 insertions(+), 42 deletions(-) rename Driver/{timer4.c => timer.c} (60%) create mode 100644 Driver/timer.h delete mode 100644 Driver/timer4.h create mode 100644 Lib/KeyScan.c create mode 100644 Lib/KeyScan.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 305a7c1..71ce963 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,8 @@ "iic.h": "c", "config.h": "c", "ina226.h": "c", - "oled12864_drv.h": "c" + "oled12864_drv.h": "c", + "delay.h": "c", + "timer.h": "c" } } \ No newline at end of file diff --git a/Driver/iic.c b/Driver/iic.c index 46458ee..da5652c 100644 --- a/Driver/iic.c +++ b/Driver/iic.c @@ -1,6 +1,6 @@ #include "iic.h" -// 延时函数(2us) +// 延时函数(1us) void IIC_Delay (void) { diff --git a/Driver/timer4.c b/Driver/timer.c similarity index 60% rename from Driver/timer4.c rename to Driver/timer.c index c846466..46bed1e 100644 --- a/Driver/timer4.c +++ b/Driver/timer.c @@ -1,7 +1,46 @@ -#include "timer4.h" +#include "timer.h" +// 初始化所有定时器 +void +Timer_Init (void) +{ + Timer3_Init (); // 定时器3初始化 + Timer4_Init (); // 定时器4初始化 +} + +// 定时器3 +uint32_t uptime = 0; +uint8_t time3_cnt = 0; +// 定时器3初始化 +void +Timer3_Init (void) // 10毫秒@30.000MHz +{ + T4T3M &= 0xFD; // 定时器时钟12T模式 + T3L = 0x58; // 设置定时初始值 + T3H = 0x9E; // 设置定时初始值 + T4T3M |= 0x08; // 定时器3开始计时 + IE2 |= 0x20; // 使能定时器3中断 +} +// 定时器3的中断服务 +void +Timer3_Isr (void) interrupt 19 +{ + time3_cnt++; + if (time3_cnt >= 100) + { + uptime++; + time3_cnt = 0; + } +} +// 获取系统时间(单位:秒) +uint32_t +GetUpTime (void) +{ + return uptime; +} + +// 定时器4 u8 Timer4_OF = 0; // 定时器4溢出标志 - // 定时器4初始化(16位自动重载模式) void Timer4_Init (void) diff --git a/Driver/timer.h b/Driver/timer.h new file mode 100644 index 0000000..ab42b1f --- /dev/null +++ b/Driver/timer.h @@ -0,0 +1,23 @@ +#ifndef __TIMER_H__ +#define __TIMER_H__ + +#include + +#include "config.h" + +// #define FOSC 30000000UL // 系统时钟30MHz + +// 初始化所有Timer +void Timer_Init (void); + +// Timer3 用于记录系统运行时间 +void Timer3_Init (void); +uint32_t GetUpTime (void); + +// Timer4 用于短时间延时 +void Timer4_Init (void); +void Timer4_DelayUs (u16 us); +void Timer4_DelayMs (u16 ms); +void Timer4_Stop (void); + +#endif \ No newline at end of file diff --git a/Driver/timer4.h b/Driver/timer4.h deleted file mode 100644 index f8135e7..0000000 --- a/Driver/timer4.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef __TIMER4_H__ -#define __TIMER4_H__ - -#include "config.h" -#include - -// #define FOSC 30000000UL // 系统时钟30MHz - -void Timer4_Init (void); -void Timer4_DelayUs (u16 us); -void Timer4_DelayMs (u16 ms); -void Timer4_Stop (void); - -#endif \ No newline at end of file diff --git a/Lib/KeyScan.c b/Lib/KeyScan.c new file mode 100644 index 0000000..aebe4a0 --- /dev/null +++ b/Lib/KeyScan.c @@ -0,0 +1,22 @@ +#include "KeyScan.h" + +void main() { + Key_Init(); // 初始化按键模块 + Timer0_Init(); // 配置1ms定时器 + + while(1) { + // 在定时器中断服务例程中调用Key_Scan() + if(Get_KeyEvents()) { + // 执行按键处理逻辑 + uint8_t keys = Get_KeyEvents(); + if(keys & 0x08) { /* KEY1处理 */ } + if(keys & 0x04) { /* KEY2处理 */ } + // ...其他按键处理 + } + } +} + +// // Timer0中断服务函数 +// void Timer0_ISR() interrupt 1 { +// Key_Scan(); // 定时扫描按键 +// } diff --git a/Lib/KeyScan.h b/Lib/KeyScan.h new file mode 100644 index 0000000..d2f411e --- /dev/null +++ b/Lib/KeyScan.h @@ -0,0 +1,16 @@ +/* KeyScan.h - 按键模块接口 */ +#ifndef __KEY_SCAN_H__ +#define __KEY_SCAN_H__ + +// 按键引脚定义(与硬件布局一致) +sbit KEY1 = P3^0; +sbit KEY2 = P3^1; +sbit KEY3 = P3^2; +sbit KEY4 = P3^3; + +// 函数声明 +void Key_Init(void); +void Key_Scan(void); +uint8_t Get_KeyEvents(void); + +#endif \ No newline at end of file diff --git a/Lib/delay.h b/Lib/delay.h index de67905..5809e32 100644 --- a/Lib/delay.h +++ b/Lib/delay.h @@ -2,7 +2,7 @@ #define __DELAY_H__ #include "config.h" -#include "timer4.h" +#include "timer.h" void Delay_Init (void); void DelayUs (u16 us); diff --git a/Src/main.c b/Src/main.c index bfc3736..5947529 100644 --- a/Src/main.c +++ b/Src/main.c @@ -26,54 +26,38 @@ main () u16 ina226_current = 0; u16 ina226_power = 0; - u16 loop_counter = 0; - SystemClock_Init (); // 时钟配置 + Timer_Init (); // 初始化定时器 EA = 1; // 开总中断 Delay_Init (); Uart1_Init (); - printf ("IIC_Init()\r\n"); IIC_Init (); - - printf ("OLED_Init()\r\n"); OLED_Init (); - - printf ("INA226_Init()\r\n"); INA226_Init (0.005f, 2.0f); DelayMs (100); // 初始化延时 - printf ("OLED_ShowPrintf()\r\n"); - OLED_ShowPrintf (0, 0, "Hello, oWorld!", OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, + OLED_ShowPrintf (0, 0, "Hello, World!", OLED_FONT_EIGHT, OLED_LEFT_ROLL, OLED_SHOW); - OLED_ShowPrintf (0, 2, "Hello, 2World!", OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, - OLED_CLS); - - OLED_ShowPrintf (0, 4, "Hello, 4Wo4rld!", OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, - OLED_CLS); - P2M1 &= 0xFE; // P20为推挽输出 P2M0 |= 0x01; P20 = 1; // P20为推挽输出 - OLED_ShowNum (4, 4, 123456789, 10, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, - OLED_SHOW); while (1) { - loop_counter++; - OLED_ShowNum (8, 0, loop_counter, 10, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, + OLED_ShowNum (4, 1, GetUpTime (), 10, OLED_FONT_EIGHT, OLED_LEFT_ROLL, OLED_SHOW); ina226_voltage = (int16_t)(INA226_ReadBusVoltage () * 1000); - OLED_ShowNum (4, 2, ina226_voltage, 10, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, - OLED_SHOW); + OLED_ShowNum (4, 2, ina226_voltage, 10, OLED_FONT_SIXTEEN, + OLED_LEFT_ROLL, OLED_SHOW); ina226_current = (int16_t)(INA226_ReadCurrent () * 1000); OLED_ShowNum (4, 4, ina226_current, 10, OLED_FONT_SIXTEEN, @@ -83,8 +67,7 @@ main () OLED_ShowNum (4, 6, ina226_power, 10, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, OLED_SHOW); - DelayMs (1000); - + DelayMs (500); } }