#include "stc12c5a.h"
#include "intrins.h"
#include "tlc2543.h" //ad
#include "main.h" //主要注释
#include "st7565.c" //12864
#include "x9103.c"
/****************************************
*五路AD: 分别采样10次,
*IN0:输入电压、
*IN1:输出电压、
*IN3、IN3:输出电流(1)、
*IN4:负载识别 电阻
********************************************/
unsigned int xdata AD_[5][10] = 0;
double temp[5] = 0;
uint AD_num[5]=0,e = 0;
uchar aps=0,adtime=0,ad_chunnel = 0,showts = 0;
uchar ocp = 32;//过流保护 一次0.1
uint AD_Iz = 0.00;
bit d2buff = 0,ocpbuff = 0,load = 0;
void delay(uint t)
{
while(t--);
}
/* 硬件SPI
void InitSPI()
{
AUXR1 = 0X00;
SPDAT = 0; //initial SPI data
SPSTAT = SPIF | WCOL; //SPI状态寄存器
SPCTL = SPEN | MSTR | SPSPEEDH; //SPI控制寄存器
}
uchar WriteByte(uchar temp)
{
// SPCTL = SPEN | MSTR | SPSPEEDH; //SPI控制寄存器
SPDAT = temp;
while (!(SPSTAT & SPIF)); //wait send complete
SPSTAT = SPIF | WCOL; //spi状态寄存器
delay(20);
return SPDAT;
}
*/
/*
uint revAD(uchar chunnel)
{
uint doc = 0 , a = 0, b = 0;
ad_cs = 0;
a = WriteByte( (chunnel<<4) | 0x0c );
b = WriteByte(0);
ad_cs = 1;
a <<= 8;
doc = a | b;
doc >>= 4;
return doc;
}*/
/*************软件模拟SPI*****************************/
uint readAD(uchar chunnel)
{
uchar i = 0,dout = ad_chunnel_select[chunnel];
uint din=0;
_nop_();
_nop_();
_nop_();
ad_cs = 0;
// ad_clock = 0;
for(i = 0;i < 16;i++)
{
ad_clock = 1;
if(dout & 0x80)ad_in = 1;
else ad_in = 0;
dout <<=1;
ad_clock = 0;
_nop_();
}
for(i=0;i<16;i++)
{
if(ad_out == 1)din |= 0x0001;
ad_clock = 1;
din <<= 1;
ad_clock = 0;
_nop_();
}
ad_cs = 1;
din >>= 4;
return (din);
}
/************费尔曼滤波*****************
double KalmanFilter(const double ResrcData,
double ProcessNiose_Q,
double MeasureNoise_R)
{
double R = MeasureNoise_R;
double Q = ProcessNiose_Q;
static double x_last;
double x_mid = x_last;
double x_now;
static double p_last;
double p_mid ;
double p_now;
double kg;
x_mid=x_last; //x_last=x(k-1|k-1),x_mid=x(k|k-1)
p_mid=p_last+Q; //p_mid=p(k|k-1),p_last=p(k-1|k-1),Q=??
kg=p_mid/(p_mid+R); //kg?kalman filter,R???
x_now=x_mid+kg*(ResrcData-x_mid);
p_now=(1-kg)*p_mid;
p_last = p_now;
x_last = x_now;
return x_now;
}
*/
/******************冒泡排序****************************/
void bubble(unsigned int* array,uchar len)
{
uchar i = 0,j = 0;
unsigned int tmp;
for(i = 0;i < len;i++)
{
for(j=0;j < len;j++)
{
if(array[j] > array[i])
{
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
}
}
}
}
/*******************计算平均值*******************************************/
double Average(unsigned int *array,unsigned char index,unsigned char len)
{
double aver=0,count=index;
int i;
for(i=index;i<(len-index);i++)
{
aver+=array[i];
aver/=(double)(count++==index?1:2);
}
return aver;
}
/****************12864显示结果********************/
void ad_show()
{
/**********************输入电压**********************************/
lcd_putnumstr(64,0,0,AD_num[0]/10000);
lcd_putnumstr(72,0,0,AD_num[0]%10000/1000);
Lcd12864_Write16CnCHAR(80,0,0,".");
lcd_putnumstr(88,0,0,AD_num[0]%10000%1000/100);
// lcd_putnumstr(96,0,0,AD_num[0]%10000%1000%100/10);
/******************************输出电流****************************/
lcd_putnumstr(64,2,0,AD_Iz/10);
Lcd12864_Write16CnCHAR(72,2,0,".");
lcd_putnumstr(80,2,0,AD_Iz%10);
/******************************输出电压****************************/
lcd_putnumstr(64,4,0,AD_num[3]/1000);
Lcd12864_Write16CnCHAR(72,4,0,".");
lcd_putnumstr(80,4,0,AD_num[3]%1000/100);
// lcd_putnumstr(88,4,0,AD_num[3]%1000%100/10);
if(load)
{
lcd_putnumstr(40,6,0,ocp/10);
Lcd12864_Write16CnCHAR(48,6,0,".");
lcd_putnumstr(56,6,0,ocp%10);
lcd_putnumstr(88,6,0,AD_num[4]/1000);
Lcd12864_Write16CnCHAR(96,6,0,".");
lcd_putnumstr(102,6,0,AD_num[4]%1000/100);
lcd_putnumstr(112,6,0,AD_num[4]%1000%100/10);
Lcd12864_Write16CnCHAR(120,6,0,"K");
}
else if(!load)
{
/******************************过流保护****************************/
lcd_putnumstr(56,6,0,ocp/10);
Lcd12864_Write16CnCHAR(64,6,0,".");
lcd_putnumstr(72,6,0,ocp%10);
}
}
uchar x9c = 0;
double JRvalue(double value)
{
double Iz = value / 1000.0;
double Jr = (3.00 - value)/Iz;
return Jr;
}
uchar swit()
{
uchar te = 0xff;
if(sw1 == 0)te=1;
if(sw2 == 0)te=2;
if(sw3 == 0)te=3;
if(sw4 == 0)te=4;
if(sw5 == 0)te=5;
return te;
}
void zdInit()
{
AUXR = 0x80;
TMOD = 0x01;
TH0 = (65536-200)/256;
TL0 = (65536-200)%256;
ET0 = 1;
TR0 = 1;
EA = 1;
}
void systemInit()
{
zdInit();
P4SW = 0x70;
x9cInit(); //
x9c10Run(1,50);x9c10Run(0,0);//第一个50 第二个 0
Lcd12864_Init();
Lcd12864_ClearScreen(0x00);//12864清屏
Lcd12864_Write16CnCHAR(0,0,0,"Uin : V");
Lcd12864_Write16CnCHAR(0,2,0,"Iout : A");
Lcd12864_Write16CnCHAR(0,4,0,"Uout : V");
ocpOff;
}
void systemRun()
{
uchar test = 0,swvalue = 0;
Lcd12864_Write16CnCHAR(0,6,0,"OCP : ");
while(test == 0)
{
ad_show();
swvalue = swit();
if(swvalue != 0xff)
{
switch(swvalue)
{
case 1 :
if(mmdz103 < 95) x9cc(1,0,1);
else x9cc(1,0,1);
break;
case 2 :
if(mmdz102 < 95)x9cc(0,1,1);
else x9cc(0,1,1);
break;
case 3 : if(ocp < 40)ocp += 1;break;
case 4 : if(ocp > 5)ocp -= 1;break;
case 5 :
load = ~load;LED2 = 1;
Lcd12864_Write16CnCHAR(0,6,0," ");
Lcd12864_Write16CnCHAR(0,6,0,"OCP: R: ");
test = 1;
break;
default:break;
}
delay(5000);
while(swit() != 0xff);
}
}
while(test == 1)
{
ad_show();
swvalue = swit();
if(swvalue != 0xff)
{
switch(swvalue)
{
case 1 :
if(mmdz103 < 95) x9cc(1,0,1);
else x9cc(1,0,1);
break;
case 2 :
if(mmdz102 < 95)x9cc(0,1,1);
else x9cc(0,1,1);
break;
case 3 : if(ocp < 40)ocp += 1;;break;
case 4 : if(ocp > 5)ocp -= 1;break;
case 5 :
load = ~load;LED2 = 1;
Lcd12864_Write16CnCHAR(0,6,0," ");
Lcd12864_Write16CnCHAR(0,6,0,"OCP : ");
test = 0;
break;
default:break;
}
delay(5000);
while(swit() != 0xff);
}
}
}
void zd1() interrupt 1
{
unsigned char i=0;
TH0 = (65536-200)/256;
TL0 = (65536-200)%256;
e++;
adtime++;
if(adtime == 10)//2.0ms 采样一次 分别四路
{
adtime = 0;
AD_[ad_chunnel][aps] = readAD(ad_chunnel);
ad_chunnel++;
}
if(ad_chunnel == 5){ad_chunnel = 0;aps++;}
if(aps == 10) //100ms 计算一次
{
aps = 0;
for(i = 0;i < 5;i++)
{
bubble(AD_[i],10); //冒泡
temp[i] = Average(AD_[i],1,9); //平均值
//输入电压
if(i == 0){temp[i] = ((temp[i] * 6.0) * addTcl2543) *1000;}
else if(i == 1){temp[i] = (((temp[i]) * addTcl2543) *1000.0);}
else if(i == 2){temp[i] = (((temp[i]) * addTcl2543) *1000.0);}
else if(i == 3){temp[i] = (((temp[i] * 2.0) * addTcl2543) *1000.0);}
else if(i == 4) {
temp[i] = (temp[i] * addTcl2543);
temp[i] = JRvalue(temp[i]);
}
}
}
if(e == 5000) //1s 输出一次
{
if(ocpbuff)LED2=0;
else {
d2buff = ~d2buff;
if(!load)LED2 = d2buff;
}
e = 0;
/* if(showts == 1)AD_Iz = temp[1];
else if(showts == 2)
{
AD_Iz += temp[2];
AD_num[1] = AD_Iz;
if( (AD_num[1]/100) > ocp){ocpOn;ocpbuff = 1;}
else {ocpOff;ocpbuff = 0;}
}
else */
AD_num[showts] = temp[showts];
if(showts == 2){
AD_Iz = (AD_num[1]/100) + (AD_num[2]/100);
if(AD_Iz > ocp){ocpOn;ocpbuff = 1;}
else {ocpOff;ocpbuff = 0;}
}
showts++;
if(showts == 5)showts=0;
}
}
void main()
{
systemInit();
// x9cInit(); /
白话机器学习
- 粉丝: 1w+
- 资源: 7671
最新资源
- 搜索引擎lucen的相关介绍 从事搜索行业程序研发、人工智能、存储等技术人员和企业
- 基于opencv-dnn和一些超过330 FPS的npu
- 房屋租赁管理系统 java项目ssm框架开发,全套视频教程
- MATLAB代码:计及电转气协同的含碳捕集与垃圾焚烧电厂优化调度 关键词:碳捕集 电厂 需求响应 优化调度 电转气协同调度 参考文档:《计及电转气协同的含碳捕集与垃圾焚烧电厂优化调度》完全复现
- 关键词:微网 优化调度 深度强化学习 A3C 需求响应 编程语言:python平台 主题:基于改进A3C算法的微网优化调度与需求响应管理 内容简介: 代码主要做的是基于深度强化学习的微网
- web网页,三次平时作业+大作业+Acwing笔记
- cruise软件模型,混动仿真模型,IMMD架构混联混动仿真模型,Cruise混动仿真模型,混联混动汽车动力性经济性仿真 关于模型 1.本模型是基于IMMD架构搭载的混联混动仿真模型,关于IMMD架
- C#上位机开发源码 上位机项目源代码 采用基于RS485通讯总线的ModbusRtu协议,支持用户权限管理、sqlite数据库、实时曲线、历史曲线、历史报表、导出Excel、主界面布局可调带记忆等功能
- 基于plc的污水处理,组态王动画仿真,带PLC源代码,组态王源代码,图纸,IO地址分配
- Rhino(犀牛)插件ladybug-tools-1-8-0
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈