《IINA219电流测量与51单片机应用》是一本专注于讲解如何利用51系列单片机进行精确电流测量及其相关电路设计的应用技术书籍,适合电子工程专业学生及技术人员阅读。
以下是使用51单片机与INA219模块进行电流、电压及功率测量的代码示例:
```c
#include LCD1602.h
#include INA219_DRV.h
#include common.h
#define TH0_VALUE 0x4B // 定义定时器TH0初始值,对应50ms周期。
#define TL0_VALUE 0xFF
sbit BtnRefreshMode = P3^2; // 刷新模式按钮引脚定义
bit RefreshRate = 0; // 刷新速率标志位:0表示慢速刷新(每秒1.25次),1表示快速刷新(每秒两次)
bit RefreshFlag = 0;
void RefreshData(void) {
unsigned short BusVolt, Current, Power;
unsigned short OffsetCurrent, OffsetPower;
BusVolt = INA219_GetBusVolt(); // 获取母线电压
PrintChar(0, 0, (BusVolt / 10000) + 0);
PrintChar(1, 0, ((BusVolt % 10000) / 1000) + 0);
PrintChar(3, 0, ((BusVolt % 100) / 10) + 0);
PrintChar(4, 0, (BusVolt % 10) + 0);
OffsetCurrent = (BusVolt >> 9) + 2; // 在无负载情况下,测量不同电压条件下的电流统计数据并进行曲线拟合。
Current = INA219_GetCurrent();
if(Current > OffsetCurrent)
Current -= OffsetCurrent;
else
Current = 0;
PrintChar(10, 0, (Current / 1000) + 0);
PrintChar(12, 0, ((Current % 100) / 10) + 0);
PrintChar(13, 0, (Current % 10) + 0);
OffsetPower = (((BusVolt >> 3) * OffsetCurrent)/125)+6;
Power = INA219_GetPower();
if(Power > OffsetPower)
Power -= OffsetPower;
else
Power = 0;
PrintChar(0, 1, (Power / 10000) + 0);
PrintChar(1, 1, ((Power % 10000) / 1000) + 0);
PrintChar(3, 1, ((Power % 100) / 10) + 0);
PrintChar(4, 1, (Power % 10) + 0);
}
int main(void){
INA219_Init(); // 初始化INA219模块
LCD_Init(); // 初始化LCD显示
TMOD &= 0xF0;
TMOD |= 0x01;
TH0 = TH0_VALUE;
TL0 = TL0_VALUE;
PrintChar(2, 0, .);
PrintChar(6, 0, V); // 显示单位
PrintChar(11, 0, .);
PrintChar(15, 0, A);
PrintChar(2, 1, .);
PrintChar(6, 1, W);
RefreshData();
EA = 1;
ET0 = 1;
TR0 = 1;
while (1) {
if (RefreshFlag) { // 判断是否需要刷新显示
RefreshData();
RefreshFlag = 0;
}
if(BtnRefreshMode == 0){
Delay_us(5000);
if(BtnRefreshMode == 1){
Delay_us(5000);
if(BtnRefreshMode == 1) { // 按钮去抖
RefreshRate = !RefreshRate;
if (RefreshRate)
PrintChar(14, 1, 5);
else
PrintChar(14, 1, 8);
}
}
}
}
return 0; // 程序不会到达这里,只是一个示例结束标记。
}
void Timer0ISR(void) interrupt 1 using 2{
static unsigned char Counter = 0;
TH0 = TH0_VALUE;
TL0 = TL0_VALUE;
if (Counter < (15 - RefreshRate * 6)) // 根据刷新速率调整定时器中断频率
Counter++;
else {
Counter = 0;
RefreshFlag = 1;
}
}
```
这段代码通过