#include "stc8.h"
#include "intrins.h"
typedef unsigned char uchar8; // 无符号8位整型变量
typedef signed char char8; // 有符号8位整型变量
typedef unsigned int uint16; // 无符号32位整型变量
typedef signed int int16; // 有符号32位整型变量
typedef unsigned long ulong32; //
typedef signed long long32; //
sbit start =P4^0;
sbit stop =P6^4;
sbit watch =P6^5;
sbit F1_5 =P6^6;
sbit change =P6^7;
sbit led1 =P5^0;
sbit led2 =P5^1;
sbit led3 =P3^6;
sbit led4 =P3^7;
sbit led5 =P4^1;
sbit led6 =P7^0;
sbit pwm0 =P2^0;
sbit pwm1 =P2^1;
sbit pwm2 =P2^2;
sbit pwm3 =P2^3;
sbit pwm4 =P2^4;
sbit pwm5 =P2^5;
sbit LCD_RS=P7^4;
sbit LCD_RW=P7^5;
sbit LCD_EN=P7^6;
bit flag_watch0;
bit flag_watch1;
bit flag_watch2;
bit flag_watch3;
bit flag_watch4;
bit flag_watch5;
uchar8 cnt_watch;
uchar8 key_watch;
uchar8 key_stop;
uchar8 flag_Hz;
ulong32 cnt_times;
uint16 Voltage[]={0,0,0,0,0,0,0,0,0};
uint16 cnt_t0;
uint16 cnt_t1;
uint16 adc1;
uint16 adc2;
uint16 adc3;
uint16 adc4;
uint16 adc5;
uint16 adc6;
void InitADC();
uint16 adc_data(uchar8 channel);
/*----------------------------
Software delay function
----------------------------*/
void Delay20ms() //@24.000MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 2;
j = 211;
k = 231;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay()
{
_nop_();
_nop_();
_nop_();
_nop_();
}
//忙检测函数,判断bit7是0,允许执行;1禁止
void Read_Busy()
{ uchar8 i;
P0 = 0xff;
LCD_RS=0;
LCD_RW=1;
do
{ LCD_EN=1;
i=P0;
LCD_EN=0; //使能,用完就拉低,释放总线
}while(i & 0x80);
}
//写命令
void LCD_Write_Command(uchar8 com)
{ Read_Busy();
LCD_RS=0;
LCD_RW=0;
P0=com;
LCD_EN=1;
LCD_EN=0;
}
//写数据
void LCD_Write_Data(uchar8 dat)
{ Read_Busy();
LCD_RS=1;
LCD_RW=0;
P0=dat;
LCD_EN=1;
LCD_EN=0;
}
//初始化
void LCD_Initialise()
{
LCD_Write_Command(0x38);
LCD_Write_Command(0x0c);
LCD_Write_Command(0x06);
LCD_Write_Command(0x01);
}
void timer_int() //定时器初始化
{
TMOD=0x00;
TL0 = 0xC0; //设置定时初值
TH0 = 0x63; //设置定时初值
TL1 = 0xC0; //设置定时初值
TH1 = 0x63; //设置定时初值
ET0=1;
ET1=1;
}
void INT_PWM()
{
pwm0=0;
pwm1=0;
pwm2=0;
pwm3=0;
pwm4=0;
pwm5=0;
}
void key_scan() //功能按键
{
if(!start)
{
Delay20ms();
if(!start)
{
while(!start);
key_stop++;
if(key_stop==1)
{
TR0=1;
TR1=1;
}
if(key_stop==2)
{
TR0=0;
TR1=0;
cnt_t0=0;
cnt_t1=0;
key_stop=0;
INT_PWM();
}
}
}
if(!stop)
{
Delay20ms();
if(!stop)
{
while(!stop);
if(TR0==0)
cnt_times=0;
}
}
if(!watch)
{
Delay20ms();
if(!watch)
{
while(!watch);
key_watch++;
if(key_watch==2)
{
key_watch=0;
}
}
}
if(!F1_5)
{
Delay20ms();
if(!F1_5)
{
while(!F1_5);
flag_Hz++;
if(flag_Hz==1)
{
cnt_t0=0;
cnt_t1=0;
INT_PWM();
}
if(flag_Hz==2)
{
cnt_t0=0;
cnt_t1=0;
INT_PWM();
flag_Hz=0;
}
}
}
}
void led_control()
{
adc1=adc_data(1);
adc2=adc_data(2);
adc3=adc_data(3);
adc4=adc_data(4);
adc5=adc_data(5);
adc6=adc_data(6);
if(adc1>2800)
{
led1=0;
led6=1;
}
if(adc2>2800)
{
led2=0;
led1=1;
}
if(adc3>2800)
{
led3=0;
led2=1;
}
if(adc4>2800)
{
led4=0;
led3=1;
}
if(adc5>2800)
{
led5=0;
led4=1;
}
if(adc6>2800)
{
led6=0;
led5=1;
}
}
void pwm0_watch_1Hz()//1Hz监控
{
if(key_watch)
{
if(flag_watch0)
{
if(adc1<2800||adc2>2800||adc3>2800||adc4>2800||adc5>2800||adc6>2800)
{
TR0=0;
TR1=0;
cnt_t0=0;
cnt_t1=0;
INT_PWM();
key_stop=0;
flag_watch0=0;
}
}
if(flag_watch1)
{
if(adc1>2800||adc2<2800||adc3>2800||adc4>2800||adc5>2800||adc6>2800)
{
TR0=0;
TR1=0;
cnt_t0=0;
cnt_t1=0;
INT_PWM();
key_stop=0;
flag_watch1=0;
}
}
if(flag_watch2)
{
if(adc1>2800||adc2>2800||adc3<2800||adc4>2800||adc5>2800||adc6>2800)
{
TR0=0;
TR1=0;
cnt_t0=0;
cnt_t1=0;
INT_PWM();
key_stop=0;
flag_watch2=0;
}
}
if(flag_watch3)
{
if(adc1>2800||adc2>2800||adc3>2800||adc4<2800||adc5>2800||adc6>2800)
{
TR0=0;
TR1=0;
cnt_t0=0;
cnt_t1=0;
INT_PWM();
key_stop=0;
flag_watch3=0;
}
}
if(flag_watch4)
{
if(adc1>2800||adc2>2800||adc3>2800||adc4>2800||adc5<2800||adc6>2800)
{
TR0=0;
TR1=0;
cnt_t0=0;
cnt_t1=0;
INT_PWM();
key_stop=0;
flag_watch4=0;
}
}
if(flag_watch5)
{
if(adc1>2800||adc2>2800||adc3>2800||adc4>2800||adc5>2800||adc6<2800)
{
TR0=0;
TR1=0;
cnt_t0=0;
cnt_t1=0;
INT_PWM();
key_stop=0;
flag_watch5=0;
}
}
}
}
void pwm0_watch_5Hz()//5Hz监控
{
if(key_watch)
{
if(flag_watch0)
{
if(adc1<2800||adc2>2800||adc3>2800||adc4>2800||adc5>2800||adc6>2800)
{
TR0=0;
TR1=0;
cnt_t0=0;
cnt_t1=0;
INT_PWM();
key_stop=0;
flag_watch0=0;
}
}
if(flag_watch1)
{
if(adc1>2800||adc2<2800||adc3>2800||adc4>2800||adc5>2800||adc6>2800)
{
TR0=0;
TR1=0;
cnt_t0=0;
cnt_t1=0;
INT_PWM();
key_stop=0;
flag_watch1=0;
}
}
if(flag_watch2)
{
if(adc1>2800||adc2>2800||adc3<2800||adc4>2800||adc5>2800||adc6>2800)
{
TR0=0;
TR1=0;
cnt_t0=0;
cnt_t1=0;
INT_PWM();
key_stop=0;
flag_watch2=0;
}
}
if(flag_watch3)
{
if(adc1>2800||adc2>2800||adc3>2800||adc4<2800||adc5>2800||adc6>2800)
{
TR0=0;
TR1=0;
cnt_t0=0;
cnt_t1=0;
INT_PWM();
key_stop=0;
flag_watch3=0;
}
}
if(flag_watch4)
{
if(adc1>2800||adc2>2800||adc3>2800||adc4>2800||adc5<2800||adc6>2800)
{
TR0=0;
TR1=0;
cnt_t0=0;
cnt_t1=0;
INT_PWM();
key_stop=0;
flag_watch4=0;
}
}
if(flag_watch5)
{
if(adc1>2800||adc2>2800||adc3>2800||adc4>2800||adc5>2800||adc6<2800)
{
TR0=0;
TR1=0;
cnt_t0=0;
cnt_t1=0;
INT_PWM();
key_stop=0;
flag_watch5=0;
}
}
}
}
void display() //LCD显示设置
{
LCD_Write_Command(0x86);
LCD_Write_Data('W');
LCD_Write_Data('A');
LCD_Write_Data('T');
LCD_Write_Data(':');
if(!key_watch)
{
LCD_Write_Data('O');
LCD_Write_Data('F');
LCD_Write_Data('F');
}
else
{
LCD_Write_Data('O');
LCD_Write_Data('N');
LCD_Write_Data(' ');
}
LCD_Write_Command(0xc0);//显示频率
if(!flag_Hz)
{
LCD_Write_Data('1');
}
else LCD_Write_Data('5');
LCD_Write_Data('H');
LCD_Write_Data('Z');
if(cnt_times==99999999) cnt_times=0;
Voltage[8]=(cnt_times/100000000);
Voltage[7]=(cnt_times/10000000)%10;
Voltage[6]=(cnt_times/1000000)%10;
Voltage[5]=(cnt_times/100000)%10;
Voltage[4]=(cnt_times/10000)%10;
Voltage[3]=(cnt_times/1000)%10;
Voltage[2]=(cnt_times/100)%10;
Voltage[1]=(cnt_times/10)%10;
Voltage[0]=cnt_times%10;
LCD_Write_Command(0xc5); //显示计数次数
LCD_Write_Data(Voltage[8]+0x30);
LCD_Write_Data(Voltage[7]+0x30);
LCD_Write_Data(Voltage[6]+0x30);
LCD_Write_Data(Voltage[5]+0x30);
LCD_Write_Data(Voltage[4]+0x30);
LCD_Write_Data(Voltage[3]+0x30);
LCD_Write_Data(Voltage[2]+0x30);
LCD_Write_Data(Voltage[1]+0x30);
LCD_Write_Data(Voltage[0]+0x30);
}
void INT_IO() //IO模式设置
{
P1M1=0x7e;//0111 1110 01推挽 10高阻输入
P1M0=0x00;//0000 0000
P5M1=0x00;
P5M0=0x00;
}
uint16 AD_get(uchar8 channel) //获取AD值
{
ADC_CONTR=0xc0|channel; // 1100 0000 ADC_POWER ADC_FLAG ADC_START - CHS3 CHS2 CHS1 CHS0
Delay();
return(ADC_RES*16+ADC_RESL/16);
ADC_RES = 0; //Clear previous result
ADC_RESL = 0;
}
#define N 11
uint16 adc_data(uchar8 channel) //AD值换算及滤波
{
uint16 value_buf[N];
uint16 count,i,j,temp;
for(count=0;count<N;count++)
{
value_buf[count] = AD_get(channel);
}
for(j=0;j<N-1;j++)
{
for(i=0;i<N-j;i++)
{
if(value_buf>value_buf[i+1])
{
temp =value_buf;
value_buf= value_buf[i+1];
value_buf[i+1] = temp;
}
}
}
value_buf[(N-1)/2]=(uint16)((ulong32)value_buf[(N-1)/2]*4940/4096);
return value_buf[(N-1)/2];
}
void MCLK()//选择系统时钟源
{
P_SW2 = 0x80;
CKSEL = 0x00; //选择内部IRC ( 默认 )
CLKDIV = 0x00; //时钟不分频
P_SW2 = 0x00;
}
//void RST()//系统复位设置
//{
// RSTCFG=0x50;
//}
void main()
{
MCLK();
// RST();
timer_int();
InitADC();
LCD_Initialise();
INT_IO();
INT_PWM();
EA=1;
while(1)
{
key_scan();
led_control();
if(!flag_Hz)
{
pwm0_watch_1Hz();
}
if(flag_Hz)
{
pwm0_watch_5Hz();
}
display();
}
}
void timer0_int() interrupt 1 //定时器0中断输出脉冲
{
cnt_t0++;
if(!flag_Hz)
{
switch(cnt_t0)
{
case 1 :pwm0=1;break;
case 26 :pwm0=0;break;
case 51 :pwm1=1;break;
case 76 :pwm1=0;break;
case 101:pwm2=1;break;
case 126:pwm2=0;break;
case 151:pwm3=1;break;
case 176:pwm3=0;break;
case 201:pwm4=1;break;
case 226:pwm4=0;break;
case 251:pwm5=1;break;
case 276:pwm5=0;break;
case 301:cnt_t0=0;cnt_times++;break;
default:break;
}
}
if(flag_Hz)
{
switch(cnt_t0)
{
case 1 :pwm0=1;break;
case 6 :pwm0=0;break;
case 11:pwm1=1;break;
case 16:pwm1=0;break;
case 21:pwm2=1;break;
case 26:pwm2=0;break;
case 31:pwm3=1;break;
case 36:pwm3= 0;break;
case 41:pwm4=1;break;
case 46:pwm4=0;break;
case 51:pwm5=1;break;
case 56:pwm5=0;break;
case 61:cnt_t0=0;cnt_times++;break;
default:break;
}
}
}
void timer1_int() interrupt 3 //定时器1中断 监控
{
cnt_t1++;
if(!flag_Hz)
{
switch(cnt_t1)
{
case 3 :flag_watch0=1;break;
case 23 :flag_watch0=0;break;
case 53 :flag_watch1=1;break;
case 73 :flag_watch1=0;break;
case 103:flag_watch2=1;break;
case 123:flag_watch2=0;break;
case 153:flag_watch3=1;break;
case 173:flag_watch3=0;break;
case 203:flag_watch4=1;break;
case 223:flag_watch4=0;break;
case 253:flag_watch5=1;break;
case 273:flag_watch5=0;break;
case 301:cnt_t1=0;break;
default:break;
}
}
if(flag_Hz)
{
switch(cnt_t1)
{
case 2 :flag_watch0=1;break;
case 5 :flag_watch0=0;break;
case 12:flag_watch1=1;break;
case 15:flag_watch1=0;break;
case 22:flag_watch2=1;break;
case 25:flag_watch2=0;break;
case 32:flag_watch3=1;break;
case 35:flag_watch3=0;break;
case 42:flag_watch4=1;break;
case 45:flag_watch4=0;break;
case 52:flag_watch5=1;break;
case 55:flag_watch5=0;break;
case 61:cnt_t1=0;break;
default:break;
}
}
}
void ADC_int() interrupt 5
{
ADC_CONTR &= ~ADC_FLAG;
}
/*----------------------------
Initial ADC sfr
----------------------------*/
void InitADC()
{
ADCCFG = 0x08; //设置ADC时钟为系统时钟/2/16/16
EADC = 1; //使能ADC中断
ADC_RES = 0; //Clear previous result
ADC_RESL = 0;
} |