STC8G七彩数字时钟、GPIO设置、DS1302使用、热敏电阻测温、SM5166P和MBI5024控制数码管显示

 元件:STC8G1K17、ds1302、热敏电阻RT、两个按键Up(S2)、Set(S1)

 IO口设置详细见:关于STC15系列的IO口如何配置—简单易懂超详细_想当程序猿的凯旋大大的博客-CSDN博客_stc单片机 配置io口

代码如下:(代码不算非常完整,未使用中断)

目录

main.c

GPIO.c

GPIO.h

SMG.c

SMG.h

ds1302.c

ds1302.h

RT.c

RT.h

DLKey.c

DLKey.h

Yanshi.c

Yanshi.h


main.c

#include <STC8G.H>
#include "SMG.h"
#include "ds1302.h"
#include "RT.h"
#include "DLKey.h"

#define uchar unsigned char
#define uint unsigned int

#define FOSC 22118400L
#define T1MS (65536-FOSC/1000)      //1T模式
//#define T1MS (65536-FOSC/12/1000) //12T模式		 1
#define BAUD 115200             //串口波特率
#define S1_S0 0x40              //P_SW1.6
#define S1_S1 0x80              //P_SW1.7

char shi1=7, fen1=59, miao1=50;
char key_num=0;
uint wen_du=0;
uint m=6;
uint color1=4,color2=5,color3=6,color4=7;
uchar wen_du1=0,wen_du2=0;
char shu;
char moshi1=0;
char jiemian = 0; //界面
char zh_dian1 = 7, zh_dian2 = 21; //整点闹钟
unsigned char shi,fen,miao;

//=================================================================================================
//使用说明:时钟有24H、12H两种显示模式,通过Up键切换Set确定,时分也通过Up键更改Set确定,时钟显示模式下按Set键切换为温度显示界面

void main()
{
	uchar mo_shi=0;
	
	GPIO_InitTypeDef	GPIO_InitStructure;
	GPIO_InitStructure.Pin  = GPIO_Pin_0|GPIO_Pin_1;		//指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_PullUp;		//指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P1,&GPIO_InitStructure);	//初始化
	GPIO_InitStructure.Pin  = GPIO_Pin_All;		//指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_PullUp;		//指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P3,&GPIO_InitStructure);	//初始化
	GPIO_InitStructure.Pin  = GPIO_Pin_All;		//指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_PullUp;		//指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P5,&GPIO_InitStructure);	//初始化
	GPIO_InitStructure.Pin  = GPIO_Pin_6|GPIO_Pin_7;		//指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_HighZ;		//指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P1,&GPIO_InitStructure);	//初始化
	
	
	DS1302_XieDate(shi1, fen1, miao1);
	InitADC();
	DLKey_Init();
	while(1)
	{
		DS1302_GetDate(); //时钟
		key_num=DLKey_GetKeynum();
		if((jiemian==0 && key_num == 2) || (jiemian==1 && key_num == 2) || (jiemian==4 && key_num == 2)) //设置时间显示模式24H12H
		{
			if(jiemian==1)
			{
				moshi1++;
				moshi1=moshi1%2;
			}
			if(moshi1==0)
			{
				shu=24;
			}
			else
			{
				shu=12;
			}
			jiemian=1; //1
		}
		else if(jiemian==1 && key_num == 1) //闪烁时钟
		{
			jiemian=2;
		}
		else if(jiemian==2 && key_num == 1) //闪烁分钟
		{
			jiemian=3;
		}
		else if(jiemian==2 && key_num == 2) //修改时间时钟
		{
			shi1=(shi/16)*10+(shi%16); //shi到shi1为十六进制转十进制
			shi1++;
			shi1=shi1%24;
			DS1302_XieShiDate(shi1);
		}
		else if(jiemian==3 && key_num == 2) //修改时间分钟
		{
			fen1=(fen/16)*10+(fen%16);
			fen1++;
			fen1=fen1%60;
			DS1302_XieFenDate(fen1);
		}
		else if(jiemian==3 && key_num == 1) //修改时间分钟
		{
			jiemian=0;
		}
		else if(jiemian==0 && key_num == 1) //显示温度界面
		{
			jiemian=4;
		}
		else if(jiemian==4 && key_num == 1) //显示温度界面
		{
			jiemian=0;
		}
		
//===========================================================
//显示以上界面
//时钟界面
		DS1302_GetDate(); //时钟
		shi1=(shi/16)*10+(shi%16); //shi到shi1为十六进制转十进制
		fen1=(fen/16)*10+(fen%16);
		wen_du = ADC_convert(); //温度
		wen_du1 = wen_du / 1000;
		wen_du2 = wen_du % 1000 / 100;
		if(jiemian==0) //时钟显示界面
		{
			if(moshi1==0)
			{
				if(shi1 > 9)
				{
					Show(shi1/10,shi1%10+18,fen1/10,fen1%10,color1,color2,color3,color4);
				}
				else
				{
					Show(36,shi1%10+18,fen1/10,fen1%10,color1,color2,color3,color4);
				}
			}
			else if(moshi1==1)
			{
				if(shi1/12 > 9 && shi < 12)
				{
					Show(shi1%12/10,shi1%12%10,fen1/10,fen1%10,color1,color2,color3,color4);
				}
				else if(shi1%12 <= 9 && shi < 12)
				{
					Show(36,shi1%12%10+18,fen1/10,fen1%10,color1,color2,color3,color4);
				}
				else if(shi1/12 <= 9 && shi < 12)
				{
					Show(shi1%12/10+18,shi1%12%10,fen1/10,fen1%10,color1,color2,color3,color4);
				}
				else
				{
					Show(35,shi1%12%10+18,fen1/10,fen1%10,color1,color2,color3,color4);
				}
			}
		}
		else if(jiemian==1) //2412H
		{
			Show(shu/10,shu%10,16,36,1,1,1,1);
		}
		else if(jiemian==2) //闪烁时钟
		{
			do
			{
				if(shi1 > 9)
				{
					Show(shi1/10,shi1%10+18,fen1/10,fen1%10,color1,color2,color3,color4);
				}
				else
				{
					Show(36,shi1%10+18,fen1/10,fen1%10,color1,color2,color3,color4);
				}
				Delay10ms();
			} while(--m);
			m=3;
			do
			{
				Show(36,35,fen1/10,fen1%10,color1,color2,color3,color4);
				Delay10ms();
			} while(--m);
			m=6;
		}
		else if(jiemian==3) //闪烁分钟
		{
			do
			{
				if(shi1 > 9)
				{
					Show(shi1/10,shi1%10+18,fen1/10,fen1%10,color1,color2,color3,color4);
				}
				else
				{
					Show(36,shi1%10+18,fen1/10,fen1%10,color1,color2,color3,color4);
				}
				Delay10ms();
			} while (--m);
			m=3;
			do
			{
				if(shi1 > 9)
				{
					Show(shi1/10,shi1%10+18,36,36,color1,color2,color3,color4);
				}
				else
				{
					Show(36,shi1%10+18,36,36,color1,color2,color3,color4);
					Delay10ms();
				} 
			}while (--m);
			m=6;
		}
		else if(jiemian==4) //显示温度界面
		{
			Show(36,wen_du1,wen_du2,30,color1,color2,color3,color4);
		}
	}
}

GPIO.c

/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.STCMCUDATA.com  ---------------------------------------*/
/* --- QQ:  800003751 -------------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序            */
/*---------------------------------------------------------------------*/

/***************	功能说明	****************

本文件为STC8系列的端口初始化程序,用户几乎可以不修改这个程序.

******************************************/

#include	"GPIO.h"

//========================================================================
// 函数: u8	GPIO_Inilize(u8 GPIO, GPIO_InitTypeDef *GPIOx)
// 描述: 初始化IO口.
// 参数: GPIOx: 结构参数,请参考timer.h里的定义.
// 返回: 成功返回0, 空操作返回1,错误返回2.
// 版本: V1.0, 2012-10-22
//========================================================================
unsigned char	GPIO_Inilize(unsigned char GPIO, GPIO_InitTypeDef *GPIOx)
{
	if(GPIO > GPIO_P7)				return 1;	//空操作
	if(GPIOx->Mode > GPIO_OUT_PP)	return 2;	//错误
	if(GPIO == GPIO_P0)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P0M1 &= ~GPIOx->Pin,	P0M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)		P0M1 |=  GPIOx->Pin,	P0M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P0M1 |=  GPIOx->Pin,	P0M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P0M1 &= ~GPIOx->Pin,	P0M0 |=  GPIOx->Pin;	 //推挽输出
	}
	if(GPIO == GPIO_P1)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P1M1 &= ~GPIOx->Pin,	P1M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)		P1M1 |=  GPIOx->Pin,	P1M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P1M1 |=  GPIOx->Pin,	P1M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P1M1 &= ~GPIOx->Pin,	P1M0 |=  GPIOx->Pin;	 //推挽输出
	}
	if(GPIO == GPIO_P2)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P2M1 &= ~GPIOx->Pin,	P2M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)		P2M1 |=  GPIOx->Pin,	P2M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P2M1 |=  GPIOx->Pin,	P2M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P2M1 &= ~GPIOx->Pin,	P2M0 |=  GPIOx->Pin;	 //推挽输出
	}
	if(GPIO == GPIO_P3)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P3M1 &= ~GPIOx->Pin,	P3M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)		P3M1 |=  GPIOx->Pin,	P3M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P3M1 |=  GPIOx->Pin,	P3M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P3M1 &= ~GPIOx->Pin,	P3M0 |=  GPIOx->Pin;	 //推挽输出
	}
	if(GPIO == GPIO_P4)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P4M1 &= ~GPIOx->Pin,	P4M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)		P4M1 |=  GPIOx->Pin,	P4M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P4M1 |=  GPIOx->Pin,	P4M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P4M1 &= ~GPIOx->Pin,	P4M0 |=  GPIOx->Pin;	 //推挽输出
	}
	if(GPIO == GPIO_P5)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P5M1 &= ~GPIOx->Pin,	P5M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)		P5M1 |=  GPIOx->Pin,	P5M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P5M1 |=  GPIOx->Pin,	P5M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P5M1 &= ~GPIOx->Pin,	P5M0 |=  GPIOx->Pin;	 //推挽输出
	}
	if(GPIO == GPIO_P6)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P6M1 &= ~GPIOx->Pin,	P6M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)		P6M1 |=  GPIOx->Pin,	P6M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P6M1 |=  GPIOx->Pin,	P6M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P6M1 &= ~GPIOx->Pin,	P6M0 |=  GPIOx->Pin;	 //推挽输出
	}
	if(GPIO == GPIO_P7)
	{
		if(GPIOx->Mode == GPIO_PullUp)		P7M1 &= ~GPIOx->Pin,	P7M0 &= ~GPIOx->Pin;	 //上拉准双向口
		if(GPIOx->Mode == GPIO_HighZ)		P7M1 |=  GPIOx->Pin,	P7M0 &= ~GPIOx->Pin;	 //浮空输入
		if(GPIOx->Mode == GPIO_OUT_OD)		P7M1 |=  GPIOx->Pin,	P7M0 |=  GPIOx->Pin;	 //开漏输出
		if(GPIOx->Mode == GPIO_OUT_PP)		P7M1 &= ~GPIOx->Pin,	P7M0 |=  GPIOx->Pin;	 //推挽输出
	}
	return 0;	//成功
}

GPIO.h

/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.STCMCUDATA.com  ---------------------------------------*/
/* --- QQ:  800003751 -------------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序            */
/*---------------------------------------------------------------------*/

#ifndef __Init_H_
#define __Init_H_

#include	"config.h"

#define	GPIO_PullUp		0	//上拉准双向口
#define	GPIO_HighZ		1	//浮空输入
#define	GPIO_OUT_OD		2	//开漏输出
#define	GPIO_OUT_PP		3	//推挽输出

#define	GPIO_Pin_0		0x01	//IO引脚 Px.0
#define	GPIO_Pin_1		0x02	//IO引脚 Px.1
#define	GPIO_Pin_2		0x04	//IO引脚 Px.2
#define	GPIO_Pin_3		0x08	//IO引脚 Px.3
#define	GPIO_Pin_4		0x10	//IO引脚 Px.4
#define	GPIO_Pin_5		0x20	//IO引脚 Px.5
#define	GPIO_Pin_6		0x40	//IO引脚 Px.6
#define	GPIO_Pin_7		0x80	//IO引脚 Px.7
#define	GPIO_Pin_All	0xFF	//IO所有引脚
	
#define	GPIO_P0			0		//
#define	GPIO_P1			1
#define	GPIO_P2			2
#define	GPIO_P3			3
#define	GPIO_P4			4
#define	GPIO_P5			5
#define	GPIO_P6			6
#define	GPIO_P7			7


typedef struct
{
	unsigned char	Mode;		//IO模式,  		GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	unsigned char	Pin;		//要设置的端口	
} GPIO_InitTypeDef;

unsigned char	GPIO_Inilize(unsigned char GPIO, GPIO_InitTypeDef *GPIOx);

#endif

SMG.c

#include "SMG.h"

long code SMG_DM[]={
//    0   1    2    3    4     5    6   7    8    9   10A  11B  12C  13D  14E  15F  16H  17P
    0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x76,0xF3, //无点
//   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   -   ./:   灭
    0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1,0x02,0x80,0x00}; //加点

void WM(unsigned int guan)
{
	
	EN=1;
	if(guan == 1)
	{
		D0=0;
		D1=0;
	}
	else if(guan == 2)
	{
		D0=1;
		D1=0;
	}
	else if(guan == 3)
	{
		D0=0;
		D1=1;
	}
	else if(guan == 4)
	{
		D0=1;
		D1=1;
	}
}

void DM(long dat, color)
{
	unsigned char i;
	//long data1=0xffffff00;
	//data1 = data1 | dat;
	long temp = 0x00;
	long temp1 = 0x00;
	//红色
	if(color==2)//绿色
	{
		temp = dat;
		temp <<= 8;
		dat = temp;
	}
	else if(color==3)//蓝色
	{
		temp = dat;
		temp <<= 16;
		dat = temp;
	}
	else if(color==4)//黄色
	{
		temp = dat;
		temp <<= 8;
		temp = temp+dat;
		dat = temp;
	}
	else if(color==5)//紫色
	{
		temp = dat;
		temp <<= 16;
		temp = temp+dat;
		dat = temp;
	}
	else if(color==6)//青色
	{
		temp = dat;
		temp <<= 16;
		temp1 = dat;
		temp1 <<= 8;
		temp = temp + temp1;	
		dat = temp;
	}
	else if(color==7)//白色
	{
		temp = dat;
		temp <<= 16;
		temp1 = dat;
		temp1 <<= 8;
		temp = temp + temp1 + dat;	
		dat = temp;
	}
	LE = 1;
	for(i=0;i<32;i++)
  {
    SDI=(dat & 0x80000000)?1:0;
    CLK=1;
    dat<<=1;
    CLK=0;
	  delay(100);			
  }
}

void SMG_Show(unsigned int n1,n2,n3)
{
	CLK = 0;
  SDI = 0;
	EN=0;
	DM(SMG_DM[n2],n3);
	WM(n1);
	delay(1000);
	
}

void Show(unsigned int n1,n2,n3,n4,n5,n6,n7,n8)
{
	SMG_Show(1,n1,n5);
	SMG_Show(2,n2,n6);
	SMG_Show(3,n3,n7);
	SMG_Show(4,n4,n8);
}

SMG.h

#ifndef __SMG_H_
#define __SMG_H_
 
#include <STC8G.H>
#include "intrins.h"
#include "GPIO.h"
#include "Yanshi.h"

sbit D0 = P5^4;
sbit D1 = P5^5;
sbit CLK = P3^7;
sbit SDI = P1^0;
sbit LE = P3^6;
sbit EN = P1^1;

void WM(unsigned int guan);
void DM(long dat,color);
void Yanse(unsigned int color);
void SMG_Show(unsigned int n1,n2,n3);
void Show(unsigned int n1,n2,n3,n4,n5,n6,n7,n8);
 
#endif

ds1302.c

/*
  程序说明: DS1302驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT107单片机综合实训平台 8051,12MHz
  日    期: 2011-8-9
*/

#include "ds1302.h"     //===================修改
#include <intrins.h>


sbit SCLK=P3^3;		
sbit IO=P3^4;		
sbit RST = P3^5;   // DS1302复位												

void Write_Ds1302(unsigned  char temp) 
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCLK=0;
		IO=temp&0x01;
		temp>>=1; 
		SCLK=1;
	}
}   

void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     
{
 	RST=0;	_nop_();
 	SCLK=0;	_nop_();
 	RST=1; 	_nop_();  
 	Write_Ds1302(address);	
 	Write_Ds1302(dat);		
 	RST=0; 
}

unsigned char Read_Ds1302_Byte ( unsigned char address )
{
 	unsigned char i,temp=0x00;
 	RST=0;	_nop_();
 	SCLK=0;	_nop_();
 	RST=1;	_nop_();
 	Write_Ds1302(address);
 	for (i=0;i<8;i++) 	
 	{		
		SCLK=0;
		temp>>=1;	
 		if(IO)
 		temp|=0x80;	
 		SCLK=1;
	} 
 	RST=0;	_nop_();
 	SCLK=0;	_nop_();
	SCLK=1;	_nop_();
	IO=0;	_nop_();
	IO=1;	_nop_();
	return (temp);			
}

//===============================以下部分需要修改
void DS1302_XieDate(unsigned char h,unsigned char m,unsigned char s)
{
	h=(h/10)*16+(h%10); //ds1302为16进制,将十进制转换为十六进制
	m=(m/10)*16+(m%10);
	s=(s/10)*16+(s%10);
	Write_Ds1302_Byte(0x8e,0);           //写保护
	Write_Ds1302_Byte(0x84,h); //写入小时
	Write_Ds1302_Byte(0x82,m); //写入分钟
	Write_Ds1302_Byte(0x80,s); //写入秒
	Write_Ds1302_Byte(0x8e,0x80);        //写保护
}
 
void DS1302_GetDate()
{
	shi=Read_Ds1302_Byte(0x85); //读取小时
	fen=Read_Ds1302_Byte(0x83); //读取分钟
}

void DS1302_XieShiDate(unsigned char h) //单独写入小时
{
	h=(h/10)*16+(h%10);
	Write_Ds1302_Byte(0x8e,0);           //写保护
	Write_Ds1302_Byte(0x84,h);
	Write_Ds1302_Byte(0x8e,0x80);        //写保护
}
 
void DS1302_XieFenDate(unsigned char m) //单独写入分钟
{
	m=(m/10)*16+(m%10);
	Write_Ds1302_Byte(0x8e,0);           //写保护
	Write_Ds1302_Byte(0x82,m);
	Write_Ds1302_Byte(0x8e,0x80);        //写保护
}

//void DS1302_XieMiaoDate(unsigned char s) //单独写入秒
//{
//	s=(s/10)*16+(s%10);
//	Write_Ds1302_Byte(0x8e,0);           //写保护
//	Write_Ds1302_Byte(0x80,s);
//	Write_Ds1302_Byte(0x8e,0x80);        //写保护
//}

ds1302.h

#ifndef __DS1302_H
#define __DS1302_H

#include <STC8G.H>
extern unsigned char shi,fen,miao;

void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );

void DS1302_XieDate(unsigned char h,unsigned char m,unsigned char s);
void DS1302_GetDate();
void DS1302_XieShiDate(unsigned char h);
void DS1302_XieFenDate(unsigned char m);
void DS1302_XieMiaoDate(unsigned char s);
#endif

RT.c

#include "RT.h"

void InitADC()
{
	ADC_RES = 0;
	ADC_RESL = 0;
	ADCCFG = 0x0f; //设置 ADC 时钟为系统时钟/2/16/16
	ADC_CONTR = 0x86; //使能 ADC 模块
}


unsigned int GetADCResult(void)
{
	unsigned int value;
	ADC_CONTR |= 0x40; //启动 AD 转换
	_nop_(); 
	_nop_(); 
	_nop_(); 
	_nop_(); 
	_nop_(); 
	_nop_();
	while (!(ADC_CONTR & 0x20)); //查询 ADC 完成标志
	ADC_CONTR &= ~0x20; //清完成标志
	
	value = 0;
	value = ADC_RES; //读取 ADC 结果
	value = ((value)*4 + ADC_RESL / 64);
	ADC_RES = 0;
	ADC_RESL = 0;
	return value; //Return ADC result
}

unsigned int ADC_convert(void)
{
	unsigned int temp;
	temp = GetADCResult(); //热敏AD采样
	temp = (unsigned int) ( ( 3950.0 / ( 11.33657 + log( 6.04 * (float)temp / ( 1024.0 - (float)temp ) ) ) - 278.15) * 100 );//273.15
	return temp;
}

RT.h

#ifndef __RT_H_
#define __RT_H_

#include <STC8G.H>
#include "intrins.h"
#include <math.h>
#include "GPIO.h"

void InitADC();
unsigned int GetADCResult(void);
unsigned int ADC_convert(void);
#endif

DLKey.c

#include "DLKey.h"

void DLKey_Init()
{
	Set=1;
	Up=1;
}
 
unsigned char DLKey_GetKeynum()
{
	unsigned char key_num=0;
	
	if(Set==0) //扫描第一行
	{
		Delay10ms();
		if(Set==0)
		{
			key_num=1;
		}
		while(Set==0);
	}
	else if(Up==0) //扫描第二行
	{
		Delay10ms();
		if(Up==0)
		{
			key_num=2;
		}
		while(Up==0);
	}
	DLKey_Init();
	return key_num;
}

DLKey.h

#ifndef __DLKEY_H_
#define __DLKEY_H_

#include <STC8G.H>
#include "YanShi.h"

sbit Up = P3^1;
sbit Set = P3^0;
 
void DLKey_Init();
unsigned char DLKey_GetKeynum();
 
#endif

Yanshi.c

#include "YanShi.h"
 
void Delay10ms()		//@12.000MHz
{
	unsigned char i, j;
 
	i = 117;
	j = 184;
	do
	{
		while (--j);
	} while (--i);
}

void delay(unsigned int i)
{
   while(i>0)i--;
}

Yanshi.h

#ifndef __YANSHI_H_
#define __YANSHI_H_
 
#include <STC8G.h>
#include "intrins.h"
 
void Delay10ms();
void delay(unsigned int i);
 
#endif

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值