📄 pressure.c
字号:
#include <STC12C5412.H>
#include <port.h> //mcu端口的使用定义
#include <intrins.h>
#include <stc12c5412ad_adc.h>
#define K 1 //压力值放大系数
#define Pressure_Max 40 //水压力保护阈值
struct Pressure{
unsigned int Pressure; //扩大100倍的水压值
unsigned char Pressure_Mode; //设定的水压级别
unsigned int Pressure_control_time_count; //压强调整超过一定的次数后
//仍不行,则报警
unsigned char xx; //当前角度
unsigned char yy; //目标角度
};
//?????结构如何指定存储器类型
struct Pressure My_Pressure={0,3,0,53,53};
//电动阀控制表格
unsigned char code Tab1[8]={0,52,51,53,55,56,57,61}; //从小开大
unsigned char code Tab2[8]={0,37,38,39,41,42,44,48}; //从大关小
/*---------------------------------------------------------------------------------------
长延时程序
输入参数为1,延时0.1S
2,延时0.9S
3, 延时1.8S
4, 延时2.6S
11,延时8.6S
-----------------------------------------------------------------------------------------*/
static void Delay_100ms(unsigned char x)
{
unsigned int count=7700;
while(x--){
while(count--){
_nop_();
}
}
}
/*-----------------------------------------------------------------------------------------
短延时程序
--------------------------------------------------------------------------------------------*/
void Delay_40ms(unsigned char x)
{
unsigned int count=2000;
while(x--){
while(count--){
_nop_();
}
}
}
/*----------------------------------------------------------------------------------------
水压检测
说明:返回值扩大了100倍
-----------------------------------------------------------------------------------------*/
void Get_Pressure(void)
{
//static unsigned int Sum_Pressure=0; //扩大100倍使用
//static unsigned char Sum_Count=0;
//unsigned int Pressure; //压强变量 扩大100倍使用
unsigned int My_ADC_Result;
My_ADC_Result=stc12c512ad(4);; //p1.4为水压检测端口
My_Pressure.Pressure=(unsigned int)(100*((5.02/1024)*My_ADC_Result)); //3.00为单片机供电电压 8位转换精度
//Sum_Pressure=Sum_Pressure+Pressure;
//Sum_Count++;
//if(Sum_Count==3){ //计算3次平均值
// Sum_Count=0;
// Jun_Pressure=(unsigned int)(Sum_Pressure/3/10); //扩大10倍的值
// Sum_Pressure=0;
//}
// return(Pressure);
}
/*--------------------------------------------------------------------------------------------------
水压控制程序
-----------------------------------------------------------------------------------------------------*/
void Pressure_Control(void)
{
if(My_Pressure.xx<My_Pressure.yy){
Motor_TurnDown=1;
Motor_TurnUp=0;
My_Pressure.xx++;
}
else if(My_Pressure.xx>My_Pressure.yy){
Motor_TurnUp=1;
Motor_TurnDown=0;
My_Pressure.xx--;
}
else{
Motor_TurnUp=1;
Motor_TurnDown=1;
}
}
/*-------------------------------------------------------------------------------------------
电动阀门控制参数更新
----------------------------------------------------------------------------------------------*/
void Motor_Parament_UpData(void)
{
}
/*-------------------------------------------------------------------------------------------
电动阀门开机位置初始化程序
功能:先将电动阀门关死,然后控制打开一个合适的角度
---------------------------------------------------------------------------------------------*/
void Motor_Position_Init(void)
{
Motor_TurnUp=1;
Motor_TurnDown =0; //电动阀门初始关死
Delay_100ms(11);
Motor_TurnDown=1;
Motor_TurnUp=0;
Delay_100ms(2);//延时0.9s
Delay_100ms(1);//延时0.1s
Delay_100ms(1);//延时0.1s
Delay_100ms(1);//延时0.1s
// Delay_40ms(1);
Motor_TurnUp=1; //经过合适延时,电动阀门转到合适位置后,停转
My_Pressure.xx=53; //初始化电动阀门已转过的角度,计算的此数值
Delay_100ms(4); //延时3s,让整个水管中充满较大压力
magnet_valve3=0; //突然打开电磁阀门,靠水压排除管中空气
}
/*------------------------------------------------------------------------------------------
水压突燃增大保护控制程序
-------------------------------------------------------------------------------------------*/
/*void Over_Pressure_Protecting(void)
{
if(My_Pressure.Jun_Pressure>=Pressure_Max){
magnet_valve3=1; //出水电磁阀关
magnet_valve1=0; //排水电磁阀打开
}
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -