📄 water_good.c
字号:
#include<reg52.h>
#include "adc.h"
#include<INTRINS.H>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
#define ushort unsigned short
sbit EOC = P1^3;
sbit CLK = P1^4;
sbit DIN = P1^5;
sbit DOUT = P1^6;
sbit CS = P1^7;
sbit TURN_CLK=P1^0;
sbit TURN_UCLK=P1^1;
sbit adjust_20=P2^0;
sbit adjust_30=P2^1;
sbit adjust_40=P2^2;
unsigned long idata sys_timer=0;
float idata voltage_con;
float idata voltage_moto;
uint idata voltage_con1;
uint idata voltage_moto1;
uchar idata voltage_adjust;
void adc_init(void);
float adc_voltage(int channel);
float adc_value(int channel);
float voltage_avrg(int channel);
void delay(void);
void adjust_init(void);
void adjust_init(void)
{
if(adjust_20==0)
voltage_adjust=20;
else
if (adjust_30==0)
voltage_adjust=30;
else
voltage_adjust=40;
}
void adc_init(void)
{
CS = 1;
CLK = 0;
}
float adc_voltage(int channel)
{
float r;
switch(channel)
{
case 1:r = adc_value(channel); r= r;return r;// 测量的值放大10 倍
case 0:r = adc_value(channel); r= r;return r;
default:return 0;
}
}
float adc_value(int channel)
{
int i;
uchar d;
ushort r;
d = (((unsigned char)channel)<<4)+0x02;
CS = 0;
for(i=0;i<12;i++)
{
if(i<8)
{
if( d&(1<<(7-i))) DIN = 1;else DIN=0;
}
else
DIN = 0;
CLK = 1;
CLK = 0;
}
DIN = 0;
while(EOC==0);
r = 0;
for(i=0;i<12;i++)
{
CLK = 1;
if(DOUT)r|=(1<<i);
CLK = 0;
}
CS = 1;
return (float)r*5/4095.0;
}
float voltage_avrg(int channel)
{
static float idata r_temp;
char idata i;
r_temp=0;
for(i=0;i<10;i++)
r_temp=r_temp+adc_voltage(channel);
r_temp=r_temp/10;
return r_temp;
}
void delay(void)
{ int idata i;
for(i=0;i<9000;i++);
}
void main(void)
{
/*TURN_CLK=0; //u_clock
TURN_CLK=1;
TURN_UCLK=0; // clock
TURN_UCLK=1;
TURN_CLK=0;
TURN_CLK=1;
TURN_UCLK=0;
TURN_UCLK=1; */
TURN_UCLK=1;
TURN_CLK=1;
adjust_init();
adc_init();
while(1)
{
voltage_con= voltage_avrg(0); // control voltage
voltage_moto= voltage_avrg(1); // moto voltage
delay();
voltage_con1=(uint) (voltage_con*100);
voltage_moto1=(uint) (voltage_moto*100);
delay();
if((voltage_moto1-voltage_adjust)<voltage_con1<(voltage_moto1+voltage_adjust)) //30 1ma voltage_adjust
{
TURN_UCLK=1;
TURN_CLK=1;
}
else
if(voltage_con1<(voltage_moto1-voltage_adjust))
{
TURN_CLK=1;
TURN_UCLK=0;
}
else
{
TURN_UCLK=1;
TURN_CLK=0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -