本设计:
基于51单片机的交流电流检测系统设计(程序+原理图+PCB)
原理图:Altium Designer
程序编译器:keil 5
编程语言:C语言
编号C0024
完整链接:
【腾讯文档】C0024 网盘链接
https://docs.qq.com/doc/DS29rd2Rmdk5CT3Jz
本设计由51单片机电路+数码管显示电路+交流电压检测电路+交流电流检测电路+电源电路组成。数码管实时显示交流电流值和交流电压值。
原理图(提供源文件):
PCB(提供源文件):
源程序(提供源文件):
#include<reg51.h>
#include "intrins.h"
#include "delay.h"
#include "display_seg.h"
#include<timer.h>
sfr P1M1 = 0x91; //PxM1.n,PxM0.n =00--->Standard, 01--->push-pull
sfr P1M0 = 0x92; // =10--->pure input, 11--->open drain
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xB1;
sfr P3M0 = 0xB2;
sfr P4M1 = 0xB3;
sfr P4M0 = 0xB4;
sfr P5M1 = 0xC9;
sfr P5M0 = 0xCA;
sfr P6M1 = 0xCB;
sfr P6M0 = 0xCC;
sfr P7M1 = 0xE1;
sfr P7M0 = 0xE2;
sfr ADC_CONTR = 0xBC; //ADC控制寄存器
sfr ADC_RES = 0xBD; //ADC高8位结果
sfr ADC_LOW2 = 0xBE; //ADC低2位结果
sfr P1ASF = 0x9D; //P1口第2功能控制寄存器
#define ADC_POWER 0x80 //ADC电源控制位
#define ADC_FLAG 0x10 //ADC完成标志
#define ADC_START 0x08 //ADC起始控制位
#define ADC_SPEEDLL 0x00 //540个时钟
#define ADC_SPEEDL 0x20 //360个时钟
#define ADC_SPEEDH 0x40 //180个时钟
#define ADC_SPEEDHH 0x60 //90个时钟
void InitADC();
unsigned char adVtab[5];
unsigned char adAtab[5];
unsigned char count = 0;
unsigned char GetADCResult(unsigned char ch);
void main (void)
{
unsigned int result_voltage = 0;
unsigned int result_current = 0;
unsigned int acV= 0;
unsigned int acA= 0; //扩大100倍
Init_Timer0();
//初始化端口
P0M0 = 0x00;
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
P6M0 = 0x00;
P6M1 = 0x00;
P7M0 = 0x00;
P7M1 = 0x00;
InitADC(); //初始化ADC
while (1) { /* endless loop */
result_voltage = GetADCResult(7);//采集电压值
result_current = GetADCResult(6);//采集电流值
//显示电压值
adVtab[count] = result_voltage;
adAtab[count] = result_current;
count++;
if(count >= 5)
{
acV = ((adVtab[0] +adVtab[1] +adVtab[2] +adVtab[3] +adVtab[4] )/5 )*220/125; // ad值为127实时 1.57v ad电压220
acA = ((adAtab[0] +adAtab[1] +adAtab[2] +adAtab[3] +adAtab[4] )/5 )*72/100; // ad为 时 电流为0.72A 1.24V 扩大100倍电流
count = 0;
}
// acV = result_voltage * 220 / 125 ;
// if(acV < 20 ) acV = 0;
displayDat[0] = SEG_DATA[acV/100];
displayDat[1] = SEG_DATA[(acV%100)/10];
displayDat[2] = SEG_DATA[acV%10];
//高位为0时的消除处理
if(displayDat[0]==SEG_DATA[0]){
displayDat[0] = 0xff;
if(displayDat[1]==SEG_DATA[0]){
displayDat[1]=0xff;
}
}
// }
DelayMs(100);
}
}
/*----------------------------
读取ADC结果
----------------------------*/
unsigned char GetADCResult(unsigned char ch)
{
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;
_nop_(); //等待4个NOP
_nop_();
_nop_();
_nop_();
while (!(ADC_CONTR & ADC_FLAG));//等待ADC转换完成
ADC_CONTR &= ~ADC_FLAG; //Close ADC
return ADC_RES; //返回ADC结果
}
/*----------------------------
初始化ADC
----------------------------*/
void InitADC()
{
P1ASF = 0xc0; //设置P1口为AD口
ADC_RES = 0; //清除结果寄存器
ADC_CONTR = ADC_POWER | ADC_SPEEDLL;
DelayMs(2); //ADC上电并延时
}
资料清单如下:
阅读全文