INA226测量电压、电流、功率记录

最近突然想自己做一个测量电压电流的电路,于是在网上买了一块INA226模块做电流电压功率检测设备

硬件图:

原理图:

硬件电路接线图:

实测第一种接法可测量电流、电压、功率,但是有一定误差

第二种接法只测量电流

附上程序:

通用软件IIC驱动程序-CSDN博客

INA226.h

#ifndef __INA226_H__
#define __INA226_H__
#include "app_api.h"

#define READ_ADDR                  0x81	 //A0=GND,A1=GND // R_addr=1, W_addr=0
#define WRITE_ADDR                 0x80

#define Config_Reg                 0x00  //配置寄存器  读写
#define Shunt_V_Reg                0x01  //分流电压    读
#define Bus_V_Reg                  0x02  //总线电压    读
#define Power_Reg                  0x03  //电源功率    读
#define Current_Reg                0x04  //电流        读
#define Calib_Reg                  0x05  //校准,设定满量程范围以及电流和功率测数的 
#define Mask_En_Reg                0x06  //屏蔽 使能 警报配置和转换准备就绪
#define Alert_Reg                  0x07  //包含与所选警报功能相比较的限定值
#define Man_ID_Reg                 0xFE  //0x5449
#define ID_Reg                     0xFF  //0x2260

uint16_t INA226_Read2Byte(uint8_t reg_addr);
uint8_t INA226_Write2Byte(uint8_t reg_addr,uint16_t reg_data);
void INA226_Init(void);
void INA226_TEXT(void);

float INA226_GetVoltage(void);//获取总线电压mV
float INA226_GetShunt_Current(void);//获取分流电流mA
float INA226_GetShuntVoltage(void);//分流电压mV
float INA226_Get_Power(void);//获取功率
#endif

INA226.c

#include "INA226.h"
#include "myiic.h"
//	 
//本程序只供学习使用,未经作者许可,不得用于其它任何用途
//宇文工作室编写
//电流检测驱动 代码	   
//宇文工作室@haitian
//修改日期:2023/9/9
//All rights reserved									  
//
 
 
 //读2个字节,reg_addr为读的寄存器地址
 uint16_t INA226_Read2Byte(uint8_t reg_addr)
{
	uint16_t reg_data=0;
	uint16_t temp=0;
	IIC_Start();
	IIC_Send_Byte(WRITE_ADDR);
	if(IIC_Wait_Ack())return 0;
	IIC_Send_Byte(reg_addr);   
	if(IIC_Wait_Ack())return 0;
	IIC_Start();
	IIC_Send_Byte(READ_ADDR);
	if(IIC_Wait_Ack())return 0;
	reg_data= IIC_Read_Byte(1);
	reg_data=(reg_data<<8)&0xFF00;
	temp=IIC_Read_Byte(0);
	IIC_Stop();
	reg_data|=temp;
	return reg_data;
}

//写2个字节,reg_addr为写的寄存器地址,reg_data为写的数据
uint8_t INA226_Write2Byte(uint8_t reg_addr,uint16_t reg_data)
{        
	uint8_t data_high=(uint8_t)((reg_data&0xFF00)>>8);
	uint8_t data_low=(uint8_t)reg_data&0x00FF;
	IIC_Start();
	IIC_Send_Byte(WRITE_ADDR);   
	if(IIC_Wait_Ack())return 0;
	IIC_Send_Byte(reg_addr );    
	if(IIC_Wait_Ack())return 0;        
	IIC_Send_Byte(data_high);
	if(IIC_Wait_Ack())return 0;        
	IIC_Send_Byte(data_low);
	if(IIC_Wait_Ack())return 0;                 
	IIC_Stop();
	delay_ms(2);
	return 1;
}

void INA226_Init(void)
{
  IIC_Init();
  delay_ms(5);
  //写配置寄存器
	INA226_Write2Byte(Config_Reg, 0x4527);//0100_010_100_100_111 //16次平均,1.1ms,1.1ms,连续测量分流电压和总线电压
  //写校准寄存器
	INA226_Write2Byte(Calib_Reg, 0x0A00);//LSB选择0.02mA,分压电阻选0.0001R Cal=0.00512/(0.02mA*0.0001R)=2560
}

void INA226_TEXT(void)
{
  DEBUG_ASP("\r\n\n");
  DEBUG_ASP("data=%-5d, Bus_V   =%f mV\r\n", INA226_Read2Byte(Bus_V_Reg),INA226_Read2Byte(Bus_V_Reg)*1.25*0.001);
  DEBUG_ASP("data=%-5d, Shunt_V =%f mV\r\n", INA226_Read2Byte(Shunt_V_Reg),INA226_Read2Byte(Shunt_V_Reg)*2.5*0.001);
  DEBUG_ASP("data=%-5d, Curent  =%f mA\r\n", INA226_Read2Byte(Current_Reg),INA226_Read2Byte(Current_Reg)*0.02);
  DEBUG_ASP("data=%-5d, Power   =%f mW\r\n", INA226_Read2Byte(Power_Reg),INA226_Read2Byte(Power_Reg)*0.02*25);
  DEBUG_ASP("\r\n\n");
}
 
//获取总线电压mV
float INA226_GetVoltage(void)
{
  float Bus_V;
  Bus_V=INA226_Read2Byte(Shunt_V_Reg)*1.25*0.001;//总线电压LSB固定1.25mV
  DEBUG_ASP("data=%-5d, Bus_V  =%f mV\r\n", INA226_Read2Byte(Current_Reg),Bus_V);
  return Bus_V;
}

//分流电压mV
float INA226_GetShuntVoltage(void)
{
  float Shunt_V;
  Shunt_V=INA226_Read2Byte(Shunt_V_Reg)*2.5*0.001;//分流电压LSB固定2.5uV
  DEBUG_ASP("data=%-5d, Shunt_V=%f mV\r\n", INA226_Read2Byte(Current_Reg),Shunt_V);
  return Shunt_V;
}

//获取分流电流mA
float INA226_GetShunt_Current(void)
{
   float Curent;
   Curent=INA226_Read2Byte(Current_Reg)*0.02; //分流电流LSB选择0.02mA
   DEBUG_ASP("data=%-5d, Curent =%f mA\r\n", INA226_Read2Byte(Current_Reg),Curent);
   return Curent;
}

//获取功率
float INA226_Get_Power(void)
{
  float Power;
  Power=INA226_Read2Byte(Power_Reg)*0.02*25;//功率LSB固定分流电流LSB的25倍
  DEBUG_ASP("data=%-5d, Power=%f mW\r\n", INA226_Read2Byte(Power_Reg),Power);
  return Power;
}




### 回答1: 在 TURBO C 2.0 中,您可以使用 biosdisk() 函数来格式化软盘。 下面是一段代码示例: ``` #include <stdio.h> #include <dos.h> int main() { // 定义扇区的大小 unsigned int sector_size[3] = {512, 512, 1024}; // 定义填充字节 unsigned char fill_char[3] = {0xF6, 0xF6, 0x48}; // 格式化磁道 70 面 0 的前三个扇区 for (int i = 0; i < 3; i++) { int result = biosdisk(2, 0, 70, 0, i + 1, 1, sector_size[i], fill_char[i]); if (result != 0) { printf("格式化扇区 %d 失败,返回码: %d\n", i + 1, result); } } return 0; } ``` 请注意,在使用 biosdisk() 函数之前,需要包含 dos.h 头文件。 希望这能帮到您! ### 回答2: 使用TURBO C 2.0语言,可以通过以下代码格式化软盘的70磁道0面,并分别格式化3个扇区,大小分别为512字节、512字节和1024字节。其中,前两个扇区使用F6填充,第三个扇区使用48填充。 ```c #include<stdlib.h> #include<stdio.h> #include<dos.h> void formatFloppyDisk(){ union REGS regs; regs.h.ah = 0x0;// To format a floppy disk, we set AH=0 regs.h.dl = 0;// Drive number (0=A, 1=B, etc.) regs.x.cx = 0;// Track number to format regs.h.dh = 0;// Head number regs.h.al = 0;// Sector size (0=default, 1=512 bytes, 2=1024 bytes, 3=2048 bytes etc.) int FILL_BYTE = 0;// The byte value to fill the sectors with during formatting int NUM_SECTORS = 3;// Number of sectors to format // To format 70th track 0th head regs.x.ax = 0x1301; // 0x13 = Reset disk system, 01H = Reset only specified drive int86(0x13, &regs, &regs); // BIOS interrupt to reset disk system for (int i=0; i<NUM_SECTORS; i++){ regs.x.ax = 0x3101; // 0x31 = Write Format, 01H = Format only current track regs.x.bx = 0x0001; // 0x00 = Drive A:, 01H = Head 1, 0 = Generate ID Field depending on the disk in the drive 1 = Keep the ID Field all zeros regs.x.cx = 0x0170; // Track number=70(0-79 range) regs.h.dh = 0x00; // Head number=0 or 1 regs.h.al = 0x02; // Control byte=always zero regs.x.dx = i+1; // Sector number starting from 1 regs.x.si = 0x0000; // segment and offset of read/write buffer regs.x.di = 0x0000; // segment and offset of result if(i == 2){ FILL_BYTE = 0x48; // Fill the third sector with 48 regs.x.ax = 0x3102; // 0x31 = Write Format, 02H = Format sequential tracks immediately following the one being formatted }else{ FILL_BYTE = 0xF6; // Fill the first two sectors with F6 } regs.h.ah = FILL_BYTE; // Fill the sector with specified byte int86(0x13, &regs, &regs); // BIOS interrupt to format the specified sector } } int main(){ formatFloppyDisk(); return 0; } ``` 上述代码使用了INT 0x13,即BIOS中断服务例程,来执行软盘格式化操作。通过设置寄存器的不同参数,可以指定要格式化的磁道、面、扇区大小和填充字节。在这个例子中,我们格式化了软盘70磁道0面的3个扇区,前两个扇区使用F6填充,第三个扇区使用48填充。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值