核心程式碼1314

顧語心發表於2020-11-14

#include<STC15F2K60S2.H>
#include"iic.h"
#include"ds1302.h"
#include"onewire.h"
#define uchar unsigned char
#define uint unsigned int

uchar num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};
uchar Digbuff[]={0,0,0,0,0,0,0,0};

uchar j = 0;
uint tt = 0;
uint AD;
uchar n = 0;
long temperature;
extern uchar shijian[];
void Key();
void Key_16();
void Delayms(uint ms);
void Display();
void ALLInit();

void main()
{
EPPROM_Write(0x00,223);Delayms(2);
n = EPPROM_Read(0x00);Delayms(2);
ALLInit();
DS1302_write();
while(1)
{

	//AD轉換
	AD=AD_Read()*196;
	Digbuff[0] = 16;
	Digbuff[1] = 16;
	Digbuff[2] = AD%1000000/100000;
	Digbuff[3] = AD%100000/10000;
	Digbuff[4] = AD%10000/1000;
	Digbuff[5] = AD%1000/100;
	Digbuff[6] = AD%100/10;
	Digbuff[7] = AD%10;

// //溫度讀
// temperature = Tempget();
// Digbuff[6] = temperature%100/10;
// Digbuff[7] = temperature%10;
// Digbuff[4]=temperature/100000;
// Digbuff[5]=temperature%100000/10000;
// Digbuff[6]=temperature%10000/1000;
// Digbuff[7]=temperature%1000/100;

// //時鐘讀
// DS1302_read();
// Digbuff[0] = shijian[2]/10;
// Digbuff[1] = shijian[2]%10;
// Digbuff[2] = 16;
// Digbuff[3] = shijian[1]/10;
// Digbuff[4] = shijian[1]%10;
// Digbuff[5] = 16;
// Digbuff[6] = shijian[0]/10;
// Digbuff[7] = shijian[0]%10;
// Key_16();

// //EPPROM 讀
// Digbuff[0] = 16;
// Digbuff[1] = 16;
// Digbuff[2] = n%1000000/100000;
// Digbuff[3] = n%100000/10000;
// Digbuff[4] = n%10000/1000;
// Digbuff[5] = n%1000/100;
// Digbuff[6] = n%100/10;
// Digbuff[7] = n%10;
Key();
Display();

}

}

void ALLInit()
{
P2=0xa0;P0=0x00;P2=0x80;P0=0xff;
P2=0xc0;P0=0xff;P2=0xff;P0=0xff;
}
void Key()
{
if(P300)
{
Delayms(5);
if(P30
0)
{
Digbuff[0] = 1;
}
while(!P30);
}
else if(P31 == 0)
{
Delayms(5);
if(P310)
{
Digbuff[0] = 2;
}
while(!P31);
}
else if(P32 == 0)
{
Delayms(5);
if(P32
0)
{
Digbuff[0] = 3;
}
while(!P32);
}
else if(P33 == 0)
{
Delayms(5);
if(P33==0)
{
Digbuff[0] = 4;
}
while(!P33);
}
}
void Key_16()
{
uchar temp;
P44=0;P42=1;P3=0xff;
temp=P3&0x0f;
if(temp!=0x0f)
{
Delayms(5);
temp=P3&0x0f;
if(temp!=0x0f)
{
switch(temp)
{
case 0x0e:Digbuff[1]=8; break;
case 0x0d:Digbuff[1]=7; break;
case 0x0b:Digbuff[1]=6; break;
case 0x07:Digbuff[1]=5; break;
}
}
while(P3!=0xff);
}

P44=1;P42=0;P3=0xff;
temp=P3&0x0f;
if(temp!=0x0f)
{
	Delayms(5);
	temp=P3&0x0f;
	if(temp!=0x0f)
	{
		switch(temp)
		{
			case 0x0e:Digbuff[2]=1;	break;
			case 0x0d:Digbuff[2]=2;	break;
			case 0x0b:Digbuff[2]=3;	break;
			case 0x07:Digbuff[2]=4;	break;
		}
	}
	while(P3!=0xff);
}

P44=1;P42=1;P3=0xdf;
temp=P3&0x0f;
if(temp!=0x0f)
{
	Delayms(5);
	temp=P3&0x0f;
	if(temp!=0x0f)
	{
		switch(temp)
		{
			case 0x0e:Digbuff[3]=2;	break;
			case 0x0d:Digbuff[3]=4;break;
			case 0x0b:Digbuff[3]=6;break;
			case 0x07:Digbuff[3]=8;break;
		}
	}
	while(P3!=0xdf);
}

P44=1;P42=1;P3=0xef;
temp=P3&0x0f;
if(temp!=0x0f)
{
	Delayms(5);
	temp=P3&0x0f;
	if(temp!=0x0f)
	{
		switch(temp)
		{
			case 0x0e:Digbuff[4]=5;break;
			case 0x0d:Digbuff[4]=6;break;
			case 0x0b:Digbuff[4]=7;break;
			case 0x07:Digbuff[4]=8;break;
		}
		while(P3!=0xef);
	}
}

}

void Delayms(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
for(j=845;j>0;j–);
}

void Display()
{
uchar i;
for(i=0;i<8;i++)
{
P2=0xc0;P0=0x01<<i;P2=0xff;P0=0xff;
if(i==2)
P0=num[Digbuff[i]]&0x7f;
else
P0=num[Digbuff[i]];
Delayms(1);
P0=0xff;
}
}

#include<STC15F2K60S2.H>
#include “intrins.h”

#define uchar unsigned char
#define uint unsigned int

#define somenop {nop();nop();nop();nop();nop();nop();nop();nop();nop(); nop();}
sbit TX = P1^0;
sbit RX = P1^1;
uchar code tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
uchar Dis_buf[] = {11,11,11,11,11,11,9,9};

unsigned int intr = 0;
bit ms_flag;
unsigned int t = 0;

void Send_wave(void);
void Timer0Init(void) ;
void Timer1Init(void);

void Delayms(int ms);
void Allinit();

void Display();
void main()
{
unsigned int distance;

Timer0Init();

Timer1Init();
Allinit();

EA = 1;
ET0 = 1;  



while(1)
{
	if(ms_flag)
	{
		ms_flag = 0;
		Send_wave();
		TR1 = 1;  //啟動計時
		while((RX == 1) && (TF1 == 0));
		TR1 = 0;
		
		if(TF1 == 1)
		{
			TF1 = 0;
			distance = 9999;
		}
		else
		{		
			t = TH1;
			t <<= 8;
			t |= TL1;
			distance = (unsigned int)(t*0.017);
			distance = distance/12;
		}
		TH1 = 0;
		TL1 = 0;
	}
						 
	Dis_buf[5] = distance/100;
	Dis_buf[6] = distance%100/10;
	Dis_buf[7] = distance%10; 
	Display();
}

}

void Timer0Init(void) //2毫秒@11.0592MHz
{
AUXR |= 0x80; //定時器時鐘1T模式
TMOD &= 0xF0; //設定定時器模式
TL0 = 0x9A; //設定定時初值
TH0 = 0xA9; //設定定時初值
TF0 = 0; //清除TF0標誌
TR0 = 1; //定時器0開始計時
}

void Timer1Init(void) //2毫秒@11.0592MHz
{
AUXR |= 0x40; //定時器時鐘1T模式
TMOD &= 0x0F; //設定定時器模式
}

void Timer0(void) interrupt 1
{
TL0 = 0x9A;
TH0 = 0xA9;

if(++intr == 100)
{
	ms_flag = 1;
	intr = 0;

}
}

void Send_wave(void)
{
unsigned char i = 8;

do
{
	TX = 1;
	somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;		
	TX = 0;
	somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;		
}
while(i--);

}

void Delayms(int ms)
{
int i,j;
for(i=ms;i>0;i–)
for(j=845;j>0;j–);
}

void Allinit()
{
P2=0XA0;P0=0X00;P2=0X80;P0=0XFF;
P2=0XC0;P0=0XFF;P2=0XFF;P0=0XFF;
}
void Display()
{
uchar i;
for(i=0;i<8;i++)
{
P2=0XC0;P0=(0X01<<i);P2=0XFF;P0=tab[Dis_buf[i]];
Delayms(1);
P0=0xff;
}
}
/*******************************************************************************

#include<STC15F2K60S2.H>
#include “IIC.h”

uchar code tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,
0X40,0X79,0X24,0X30,0X19,0X12,0X02,0X78,0X00,0X10,
0XBF,0XFF,0X8E,0XC1};// “-” “關” “F” “U”
unsigned char Digbuf[8] = {10,11,12,13,14,15,16,17};
uchar S1_Flag=0,S2_Flag=0,S3_Flag=0,S4_Flag=0;
uchar Digcom=0;
uchar Key_temp=0xff,key=0xff;

unsigned char AD=0;
unsigned int AD_Show=0;

unsigned int fan=0,zheng=0,zheng_now=0,fan_now=0;
unsigned char t_flag=0;

unsigned int intr=0;
unsigned int Time=0;
unsigned int freq=0;

unsigned char Qiehuan = 1 ;
unsigned char AD_Out = 1 ;
unsigned char LED_Show = 1 ;
unsigned char DIG_Show = 1 ;

unsigned char LED_Bit=0XFF;

void delayms(int ms);
void allinit(void);
void keyscan(void);

void Timer2Init(void);//1毫秒@12.000MHz
void Timer0Init(void); //5微秒@12.000MHz
void DIG(void);
void DIG_Close(void);

void main(void)
{
allinit();
Timer2Init();
Timer0Init();
while(1)
{
if((intr499)|(intr999))
{
AD=AD_read(0X03);
if(AD_Out2){AD_write(AD);}
else if(AD_Out
1){AD_write(102);}
}
DIG();
keyscan();
}
}

void keyscan(void)
{
Key_temp=P3; // 1111 1110
if( (Key_temp&0x0f) != 0x0f) // 1111 1110 & 0000 1111 =0000 1110
{
delayms(5);
Key_temp=P3;
if( (Key_temp&0x0f) != 0x0f)
{
key=Key_temp&0x0f; //key = 0000 1110
}
}

if(((key&0x0f) !=0x0f)&&((Key_temp&0x0f) == 0x0f))
{
	delayms(5);
	if( (Key_temp&0x0f) == 0x0f)
	{
		switch(key)
		{
			case 0x0e:
				if(DIG_Show==1){DIG_Show=2;DIG_Close();}
				else if(DIG_Show==2){DIG_Show=1;}
				key=0xff;break;
			case 0x0d:
				if(LED_Show==1){LED_Show=2;	P2|=0X80;	P2&=0X9F;	P0=0XFF;}
				else if(LED_Show==2){LED_Show=1;}
				key=0xff;break;
			case 0x0b:
				if(AD_Out==1){AD_Out=2;}
				else if(AD_Out==2){AD_Out=1;}									
			key=0xff;break;
			case 0x07:
				if(Qiehuan==1){Qiehuan=2;}
				else if(Qiehuan==2){Qiehuan=1;}
			key=0xff;break;
		}	
	}
}

}

//中斷服務程式
void timer2int() interrupt 12 //中斷入口
{
if(DIG_Show1)
{
P2|=0XC0;//開啟位選573 U8
P2&=0XDF;
P0=(1<<Digcom);
P2|=0XE0;
P2&=0XFF;//開啟段選573 U7
P0=tab[Digbuf[Digcom]];
if(++Digcom
8)Digcom=0;
}

intr++;

if(intr==969)
{
	TR0 = 1;ET0 = 1 ;
}
else if(intr==999)
{
	intr=0;
	TR0 = 0;ET0 = 0 ;
	Time=(fan_now+zheng_now)*5;
	freq=1000000/Time;
}

// if(intr==999)
// {
// intr=0;
// freq=(TH0<<8)|TL0;
// TL0=0;TH0=0;
// }

if(LED_Show==1)
{
	if(Qiehuan==1){P2|=0X80;P2&=0X9F;LED_Bit|=0X02; LED_Bit&=0xFE; P0=LED_Bit;}
	else if(Qiehuan==2){P2|=0X80;	P2&=0X9F;LED_Bit|=0X01; LED_Bit&=0xFD; P0=LED_Bit;}
	
	if((AD_Show<150)|((AD_Show>=250)&(AD_Show<350))){P2|=0X80;	P2&=0X9F; LED_Bit|=0X04; P0=LED_Bit;}
	else if((AD_Show>=350)|((AD_Show>=150)&(AD_Show<250))){P2|=0X80;	P2&=0X9F; LED_Bit&=0XFB; P0=LED_Bit;}
	
	if((freq<1000)|((freq>=5000)&(freq<10000))){P2|=0X80;	P2&=0X9F; LED_Bit|=0X08; P0=LED_Bit;}
	else if((freq>10000)|((freq>=1000)&(freq<50000))){P2|=0X80;	P2&=0X9F; LED_Bit&=0XF7; P0=LED_Bit;}
	
	if(AD_Out==1){P2|=0X80;	P2&=0X9F; LED_Bit|=0X10; P0=LED_Bit;}
	else if(AD_Out==2){P2|=0X80;	P2&=0X9F; LED_Bit&=0XEF; P0=LED_Bit;}		
}

}

void DIG(void)
{
if(Qiehuan1)
{
AD_Show=AD*1.96;
Digbuf[0]=23;Digbuf[1]=21;Digbuf[2]=21;Digbuf[3]=21;Digbuf[4]=21;
Digbuf[5]=AD_Show/100+10;Digbuf[6]=AD_Show%100/10;Digbuf[7]=AD_Show%10;
}
else if(Qiehuan
2)
{
Digbuf[0]=22;Digbuf[1]=21;Digbuf[2]=21;
if(freq>=10000){Digbuf[3]=freq/10000;Digbuf[4]=freq%10000/1000;Digbuf[5]=freq%1000/100;Digbuf[6]=freq%100/10;Digbuf[7]=freq%10;}
else if(freq>=1000){Digbuf[3]=21;Digbuf[4]=freq%10000/1000;Digbuf[5]=freq%1000/100;Digbuf[6]=freq%100/10;Digbuf[7]=freq%10;}
else if(freq>=100){Digbuf[3]=21;Digbuf[4]=21;Digbuf[5]=freq%1000/100;Digbuf[6]=freq%100/10;Digbuf[7]=freq%10;}
else if(freq>=10){Digbuf[3]=21;Digbuf[4]=21;Digbuf[5]=21;Digbuf[6]=freq%100/10;Digbuf[7]=freq%10;}
}
}

void DIG_Close(void)
{
P2|=0XC0;//開啟位選573 U8
P2&=0XDF;
P0=0XFF;//選擇所有數碼管
P2|=0XE0;//開啟位選573 U7
P2&=0XFF;
P0=0XFF;//關閉所有數碼管
}

/*
void Timer0Init(void) //5微秒@12.000MHz
{
AUXR |= 0x80; //定時器時鐘1T模式
TMOD &= 0xF0; //設定定時器模式
TL0 = 0xC4; //設定定時初值
TH0 = 0xFF; //設定定時初值
TF0 = 0; //清除TF0標誌
}
*/

void Timer0Init(void) //計數模式
{
AUXR |= 0x80;
TMOD = 0x05;
TL0=0;
TH0=0;
TR0=1;
}

/*
void tm0_isr() interrupt 1
{
if(P340)
{
fan++;
if(t_flag
0)
{
t_flag=1;
zheng_now=zheng;
zheng=0;
}
}
else if(P341)
{
zheng++;
if(t_flag
1)
{
t_flag=0;
fan_now=fan;
fan=0;
}
}
}
*/

void Timer2Init(void) //1毫秒@12.000MHz
{
AUXR |= 0x04; //定時器時鐘1T模式
T2L = 0x20; //設定定時初值
T2H = 0xD1; //設定定時初值
AUXR |= 0x10; //定時器2開始計時
IE2 |= 0x04; //開定時器2中斷
EA=1;
}

void delayms(int ms)
{
int i,j;
for(i=ms;i>0;i–)
for(j=845;j>0;j–);
}

void allinit(void)
{
P2|=0XA0;
P2&=0XBF;
P0=0X00;//關閉蜂鳴器,繼電器

P2|=0X80;
P2&=0X9F;
P0=0XFF;//關閉LED燈

P2|=0XC0;//開啟位選573   U8
P2&=0XDF;
P0=0XFF;//選擇所有數碼管
P2|=0XE0;//開啟位選573   U7
P2&=0XFF;
P0=0XFF;//關閉所有數碼管

}

相關文章