Files
JYF_STC15W4K_wireless_charge/Src/main.c

109 lines
2.7 KiB
C
Raw Normal View History

2025-03-18 22:49:46 +08:00
// System
2025-03-18 19:34:19 +08:00
#include <STC15.H>
2025-03-18 22:49:46 +08:00
#include <stdint.h>
#include <stdio.h>
2025-03-20 16:36:10 +08:00
// Config
#include "config.h"
2025-03-18 19:34:19 +08:00
// Driver
#include "iic.h"
2025-03-20 16:36:10 +08:00
#include "timer.h"
2025-03-18 22:49:46 +08:00
#include "uart.h"
2025-03-18 19:34:19 +08:00
// Lib
#include "delay.h"
2025-03-18 22:49:46 +08:00
#include "ina226.h"
2025-03-20 16:36:10 +08:00
#include "keyscan.h"
2025-03-18 19:34:19 +08:00
#include "oled12864_drv.h"
2025-03-20 16:36:10 +08:00
// #include "sensor_json.h"
2025-03-18 19:34:19 +08:00
2025-03-18 22:49:46 +08:00
void
SystemClock_Init (void)
{
// 烧录时选择30M主频
CLK_DIV = 0x00; // 时钟不分频30MHz直接输出
2025-03-18 19:34:19 +08:00
}
2025-03-18 22:49:46 +08:00
void
main ()
{
2025-03-20 16:36:10 +08:00
uint32_t last_round_time = 0;
uint32_t this_round_time = 0;
2025-03-19 17:25:17 +08:00
u16 ina226_voltage = 0;
u16 ina226_current = 0;
u16 ina226_power = 0;
2025-03-20 16:36:10 +08:00
// char *p_json_str = NULL;
2025-03-19 17:25:17 +08:00
2025-03-18 22:49:46 +08:00
SystemClock_Init (); // 时钟配置
Timer_Init (); // 初始化定时器
2025-03-18 22:49:46 +08:00
EA = 1; // 开总中断
Delay_Init ();
Uart1_Init ();
IIC_Init ();
OLED_Init ();
2025-03-19 17:25:17 +08:00
INA226_Init (0.005f, 2.0f);
2025-03-20 16:36:10 +08:00
Key_Init ();
2025-03-18 19:34:19 +08:00
2025-03-18 22:49:46 +08:00
DelayMs (100); // 初始化延时
2025-03-18 19:34:19 +08:00
OLED_ShowPrintf (0, 0, "Hello, World!", OLED_FONT_EIGHT, OLED_LEFT_ROLL,
2025-03-18 22:49:46 +08:00
OLED_SHOW);
2025-03-18 19:34:19 +08:00
2025-03-19 14:56:17 +08:00
P2M1 &= 0xFE; // P20为推挽输出
P2M0 |= 0x01;
P20 = 1; // P20为推挽输出
2025-03-18 22:49:46 +08:00
while (1)
{
2025-03-20 16:36:10 +08:00
OLED_ShowNum (0, 1, GetUpTime (), 10, OLED_FONT_EIGHT, OLED_LEFT_ROLL,
2025-03-18 22:49:46 +08:00
OLED_SHOW);
2025-03-19 17:25:17 +08:00
ina226_voltage = (int16_t)(INA226_ReadBusVoltage () * 1000);
2025-03-20 16:36:10 +08:00
OLED_ShowNum (0, 2, ina226_voltage, 5, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL,
OLED_SHOW);
2025-03-18 22:49:46 +08:00
2025-03-19 17:25:17 +08:00
ina226_current = (int16_t)(INA226_ReadCurrent () * 1000);
2025-03-20 16:36:10 +08:00
OLED_ShowNum (48, 2, ina226_current, 5, OLED_FONT_SIXTEEN,
2025-03-19 17:25:17 +08:00
OLED_LEFT_ROLL, OLED_SHOW);
ina226_power = (int16_t)(INA226_ReadPower () * 1000);
2025-03-20 16:36:10 +08:00
OLED_ShowNum (0, 4, ina226_power, 5, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL,
2025-03-18 22:49:46 +08:00
OLED_SHOW);
2025-03-20 16:36:10 +08:00
// DelayMs (100); // 初始化延时
last_round_time = this_round_time;
this_round_time = GetUpTime_10Ms ();
OLED_ShowNum (4, 7, this_round_time - last_round_time, 3,
OLED_FONT_EIGHT, OLED_LEFT_ROLL, OLED_SHOW);
// p_json_str = make_sensor_json (ina226_voltage, // 电压
// ina226_current, // 电流
// ina226_power, // 功率
// 0x0CA2, // 温度
// 0x0BF6 // 湿度
// );
// printf ("data:%s",p_json_str); // 打印数据
// OLED_ShowPrintf (0, 6, p_json_str, OLED_FONT_EIGHT, OLED_LEFT_ROLL,
// OLED_SHOW);
Key_Scan ();
2025-03-18 19:34:19 +08:00
}
}
2025-03-18 22:49:46 +08:00
void
Uart1_Isr (void) interrupt 4
2025-03-18 19:34:19 +08:00
{
2025-03-18 22:49:46 +08:00
if (TI) // 检测串口1发送中断
{
TI = 0; // 清除串口1发送中断请求位
}
if (RI) // 检测串口1接收中断
{
RI = 0; // 清除串口1接收中断请求位
}
2025-03-18 19:34:19 +08:00
}