Files
JYF_STC15W4K_wireless_charge/Src/main.c
2025-03-21 01:25:07 +08:00

151 lines
4.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// System
#include <STC15.H>
#include <stdint.h>
#include <stdio.h>
// Config
#include "config.h"
// Driver
#include "iic.h"
#include "timer.h"
#include "uart.h"
// Lib
#include "delay.h"
#include "ina226.h"
#include "keyscan.h"
// #include "dht10.h"
#include "oled12864_drv.h"
void packDataAndSent (uint16_t vol, uint16_t cur, uint16_t pwr, uint16_t tmp,
uint16_t hum); // 数据打包函数
void
SystemClock_Init (void)
{
// 烧录时选择30M主频
CLK_DIV = 0x00; // 时钟不分频30MHz直接输出
}
void
main ()
{
uint32_t last_round_time = 0;
uint32_t this_round_time = 0;
u16 ina226_voltage = 0;
u16 ina226_current = 0;
u16 ina226_power = 0;
char *p_json_str = NULL;
SystemClock_Init (); // 时钟配置
Timer_Init (); // 初始化定时器
EA = 1; // 开总中断
Delay_Init ();
Uart1_Init ();
IIC_Init ();
OLED_Init ();
INA226_Init (0.005f, 2.0f);
// DHT10_Init ();
Key_Init ();
DelayMs (100); // 初始化延时
// OLED显示固定信息
OLED_ShowPrintf (0, 0, "Wireless Charge", OLED_FONT_SIXTEEN, OLED_LEFT_ROLL,
OLED_SHOW);
OLED_ShowPrintf (0, 2, "V", OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, OLED_SHOW);
OLED_ShowPrintf (64, 2, "A", OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, OLED_SHOW);
OLED_ShowPrintf (0, 4, "P", OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, OLED_SHOW);
// OLED_ShowPrintf (64, 4, "T", OLED_FONT_EIGHT, OLED_LEFT_ROLL, OLED_SHOW);
// OLED_ShowPrintf (64, 5, "H", OLED_FONT_EIGHT, OLED_LEFT_ROLL, OLED_SHOW);
OLED_ShowPrintf (32, 3, ".", OLED_FONT_EIGHT, OLED_LEFT_ROLL, OLED_CLS);
OLED_ShowPrintf (96, 3, ".", OLED_FONT_EIGHT, OLED_LEFT_ROLL, OLED_CLS);
OLED_ShowPrintf (32, 5, ".", OLED_FONT_EIGHT, OLED_LEFT_ROLL, OLED_CLS);
// OLED_ShowPrintf (96, 4, ".", OLED_FONT_EIGHT, OLED_LEFT_ROLL, OLED_CLS);
// OLED_ShowPrintf (96, 5, ".", OLED_FONT_EIGHT, OLED_LEFT_ROLL, OLED_CLS);
P2M1 &= 0xFE; // P20为推挽输出
P2M0 |= 0x01;
P20 = 1; // P20为推挽输出
while (1)
{
ina226_voltage = (int16_t)(INA226_ReadBusVoltage () * 100);
OLED_ShowNum (16, 2, ina226_voltage / 100, 2, OLED_FONT_SIXTEEN,
OLED_LEFT_ROLL, OLED_CLS);
OLED_ShowNum (40, 2, ina226_voltage, 2, OLED_FONT_SIXTEEN,
OLED_LEFT_ROLL, OLED_CLS);
ina226_current = (int16_t)(INA226_ReadCurrent () * 100);
OLED_ShowNum (80, 2, ina226_current / 100, 2, OLED_FONT_SIXTEEN,
OLED_LEFT_ROLL, OLED_CLS);
OLED_ShowNum (104, 2, ina226_current, 2, OLED_FONT_SIXTEEN,
OLED_LEFT_ROLL, OLED_CLS);
ina226_power = (int16_t)(INA226_ReadPower () * 100);
OLED_ShowNum (16, 4, ina226_power / 100, 2, OLED_FONT_SIXTEEN,
OLED_LEFT_ROLL, OLED_CLS);
OLED_ShowNum (40, 4, ina226_power, 2, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL,
OLED_CLS);
// 取消温湿度功能
// DHT10_ReadData ();
// OLED_ShowNum (80, 4, DHT10_GetTemperature () / 100, 2,
// OLED_FONT_EIGHT,
// OLED_LEFT_ROLL, OLED_CLS);
// OLED_ShowNum (104, 4, DHT10_GetTemperature (), 2, OLED_FONT_EIGHT,
// OLED_LEFT_ROLL, OLED_CLS);
// OLED_ShowNum (80, 5, DHT10_GetHumidity () / 100, 2, OLED_FONT_EIGHT,
// OLED_LEFT_ROLL, OLED_CLS);
// OLED_ShowNum (104, 5, DHT10_GetHumidity (), 2, OLED_FONT_EIGHT,
// OLED_LEFT_ROLL, OLED_CLS);
// 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);
packDataAndSent (ina226_voltage, // 电压
ina226_current, // 电流
ina226_power, // 功率
0x0CA2, // 温度
0x0BF6 // 湿度
);
Key_Scan ();
OLED_ShowNum (0, 6, Get_KeyEvents (), 3, OLED_FONT_SIXTEEN,
OLED_LEFT_ROLL, OLED_SHOW);
}
}
// 数据打包函数
void
packDataAndSent (uint16_t vol, uint16_t cur, uint16_t pwr, uint16_t tmp,
uint16_t hum)
{
char txBuffer[29]; // 固定长度发送缓冲区
sprintf (txBuffer, "{%04X,%04X,%04X,%04X,%04X}\r\n", vol, cur, pwr, tmp,
hum); // 固定4字符HEX格式
printf (txBuffer); // 输出到串口
}
void
Uart1_Isr (void) interrupt 4
{
if (TI) // 检测串口1发送中断
{
TI = 0; // 清除串口1发送中断请求位
}
if (RI) // 检测串口1接收中断
{
RI = 0; // 清除串口1接收中断请求位
}
}