122 lines
3.0 KiB
C
122 lines
3.0 KiB
C
// 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 "oled12864_drv.h"
|
||
// #include "sensor_json.h"
|
||
|
||
char txBuffer[29]; // 固定长度发送缓冲区
|
||
void packData (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);
|
||
// Key_Init ();
|
||
|
||
DelayMs (100); // 初始化延时
|
||
|
||
OLED_ShowPrintf (0, 0, "Hello, World!", OLED_FONT_EIGHT, OLED_LEFT_ROLL,
|
||
OLED_SHOW);
|
||
|
||
P2M1 &= 0xFE; // P20为推挽输出
|
||
P2M0 |= 0x01;
|
||
P20 = 1; // P20为推挽输出
|
||
|
||
while (1)
|
||
{
|
||
|
||
OLED_ShowNum (0, 1, GetUpTime (), 10, OLED_FONT_EIGHT, OLED_LEFT_ROLL,
|
||
OLED_SHOW);
|
||
|
||
ina226_voltage = (int16_t)(INA226_ReadBusVoltage () * 100);
|
||
OLED_ShowNum (0, 2, ina226_voltage, 5, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL,
|
||
OLED_SHOW);
|
||
|
||
ina226_current = (int16_t)(INA226_ReadCurrent () * 100);
|
||
OLED_ShowNum (48, 2, ina226_current, 5, OLED_FONT_SIXTEEN,
|
||
OLED_LEFT_ROLL, OLED_SHOW);
|
||
|
||
ina226_power = (int16_t)(INA226_ReadPower () * 100);
|
||
OLED_ShowNum (0, 4, ina226_power, 5, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL,
|
||
OLED_SHOW);
|
||
|
||
// 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);
|
||
|
||
packData (ina226_voltage, // 电压
|
||
ina226_current, // 电流
|
||
ina226_power, // 功率
|
||
0x0CA2, // 温度
|
||
0x0BF6 // 湿度
|
||
);
|
||
printf (txBuffer); // 输出到串口
|
||
|
||
OLED_ShowPrintf (0, 6, p_json_str, OLED_FONT_EIGHT, OLED_LEFT_ROLL,
|
||
OLED_SHOW);
|
||
|
||
// Key_Scan ();
|
||
}
|
||
}
|
||
|
||
// 数据打包函数
|
||
void
|
||
packData (uint16_t vol, uint16_t cur, uint16_t pwr, uint16_t tmp, uint16_t hum)
|
||
{
|
||
sprintf (txBuffer, "{%04X,%04X,%04X,%04X,%04X}\r\n", vol, cur, pwr, tmp,
|
||
hum); // 固定4字符HEX格式
|
||
}
|
||
|
||
void
|
||
Uart1_Isr (void) interrupt 4
|
||
{
|
||
if (TI) // 检测串口1发送中断
|
||
{
|
||
TI = 0; // 清除串口1发送中断请求位
|
||
}
|
||
if (RI) // 检测串口1接收中断
|
||
{
|
||
RI = 0; // 清除串口1接收中断请求位
|
||
}
|
||
}
|