⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 led(20071020).c

📁 本人写的STC单片机EEPROM功能程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "STC89C58.h"
#include <intrins.h>
#include <ctype.h>
#include <string.h>
#include <absacc.h>
#include <stdlib.h>
#include <stdio.h>
#include "typedef.h"

// define	XT0		221184
#define	XT1		110592

#define	STC89C58		1
#define	W77E58			0

#define	STCSPD_1		0					// STC单片机用单倍速
#define	STCSPD_2		1					// STC单片机用双倍速

#define	vT01MS				4
#define	vT010MS				10
#define	vT01S				100
#define	vLedFlashTimeVal	38

#define	vKeyVal_Null		0x00
#define	vKeyVal_Set			0x01

#define	vKeyAct_Null		0
#define	vKeyAct_Push		1
#define	vKeyAct_Pop			2
#define	vKeyAct_Keep		3

#define	vBtnVal_Null		0x00
#define	vBtnVal_ST			0x08
#define	vBtnVal_Up			0x10
#define	vBtnVal_Down		0x20
#define	vBtnVal_All			0x30


#define	vBtnAct_Null			0
#define	vBtnAct_Push		1
#define	vBtnAct_Pop			2
#define	vBtnAct_Keep		3
#define	vBtnAct_All			4
#if STC89C58
#include "STC89C58.h"
#elif W77E58
#include "W77E58.h"
#endif

#if	STC89C58
#if	STCSPD_1
#define	vBaudTime		0xff 				// 0xfa对应9600Hz波特率,但若要查看信令解码数据,则要用57600Hz(0xff)
#define	vT0HVal			0xfc
#define	vT0LVal			0x76
#elif STCSPD_2
#define	vBaudTime		0xfa				// 0xfa对应9600Hz波特率,但若要查看信令解码数据,则要用57600Hz(0xff)
#define	vT0HVal			0xfe	//0xfc//0xfe//0xff
#define	vT0LVal			0x0c	//0x76//0x43//0x19
#endif

#elif W77E58
#define	vBaudTime		0xff 				// 0xfa对应9600Hz波特率,但若要查看信令解码数据,则要用57600Hz(0xff)
#define	vT0HVal			0xfc
#define	vT0LVal			0x76
#endif

sbit	KS0				=P2^3;
sbit	KS1				=P2^4;
sbit	KS2				=P2^5;

uchar	data		cT01ms;						// 定时器0中的1ms,10ms,及1s的时基计数器
uchar	data 		cT010ms;
uchar	data		cT01s;
uint	data		iDY_Timer;					// 通用定时器计数值
uchar	idata		cDY_LedFlash;				// 指示灯闪烁时隔计数
uchar	idata		cDY_KeyKeep;				// 按键延时计时器
uint				iKeyVal;

uchar			cKeySetValue;				// 键盘进行项目设置时的数值
uchar			cBtnVal;						// 按钮取值
uchar			cBtnValPre;					// 原按钮取值
uchar			vSecond;
uchar			vMinute;
uchar			vHour;

uchar code Led_Data[28]={
    0x05,		//0
    0xDD,		//1
    0x46,		//2
    0x54,		//3
    0x9C,		//4
    0x34,		//5
    0x24,		//6
    0x5D,		//7
    0x04,		//8
    0x14,		//9
    0x0C,		//A
    0xA4,		//b
    0x27,		//C
    0xE6,		//c
    0xC4,		//d
    0x26,		//E
    0x2E,		//F
    0x8C,		//H
    0xAC,		//h
    0xA7,		//L
    0xEC,		//n
    0x0D,		//N
    0xE4,		//o
    0x0E,		//P
    0xEE,		//r
    0xA6,		//t
    0x85,		//U
    0xFE,		//-
};

uchar vEEprom;
bit b_Run;

uchar digbit;     				// 字位
uchar wordbuf[4];    			// 字型码缓冲区
uint idata time0_tmp;   		// 临时变量,用于计算定时

#define DIGPORT   P1 			// 定义字位口
#define WORDPORT  P0 			// 定义字型口

#define UseAddr	 		0x9000	//0x8000~0xf3ff空间地址
#define vSecondAddr		0x9001
#define vMinuteAddr		0x9002
#define vHourAddr		0x9003

#define BaseAddr		0x8000	//起始地址
#define EndSectoraddr	0xf200
#define EndAddr	 		0xf3ff	//末地址

#define PerSector		512		//定义扇区大小

sfr isp_data=0xe2;
sfr isp_addrh=0xe3;
sfr isp_addrl=0xe4;
sfr isp_cmd=0xe5;
sfr isp_trig=0xe6;
sfr isp_contr=0xe7;

//uchar eeprom_read(uint addres);
//void eeprom_write(uint address,uchar wdata);
//void eeprom_eares(uint addres);//扇区擦除。

//uchar getcode(uchar i);   // 根据共阴极字型编码表获取字型代码函数
//void display(void);    // 显示函数
/**************************************************************************
*    函数原型: void eeprom_eares(uint addres)
*    功    能: 扇区擦除
**************************************************************************/
void eeprom_eares(uint addres){
	unsigned i;
	isp_addrl=addres;     //低位地址
	isp_addrh=addres>>8;  //高位地址
	isp_contr=0x01;
	isp_contr=isp_contr|0x80; //设时间与充ISP操作。
	isp_cmd=0x03;         //扇区命命令
	isp_trig=0x46;        //触发
	isp_trig=0xb9;        //触发启动。
	for(i=0;i<3;i++);
	isp_addrl=0xff;
	isp_addrh=0xff;
	isp_contr=0x00;
	isp_cmd=0x00;
	isp_trig=0x00;
}

/**************************************************************************
*    函数原型: void eeprom_write(uint addres,uchar write_data)
*    功    能: 写数据
**************************************************************************/
void eeprom_write(uint addres,uchar write_data){
	uchar i;
	isp_data=write_data;   //要写入的数据。
	isp_addrl=addres;     //低位地址
	isp_addrh=addres>>8;  //高位地址
	isp_contr=0x01;
	isp_contr=isp_contr|0x80; //设时间与充ISP操作。
	isp_cmd=0x02;         //写命令
	isp_trig=0x46;        //触发
	isp_trig=0xb9;        //触发启动。
	for(i=0;i<3;i++);
	isp_addrl=0xff;
	isp_addrh=0xff;
	isp_contr=0x00;
	isp_cmd=0x00;
	isp_trig=0x00;
}
/**************************************************************************
*    函数原型: void eeprom_read(uint addres)
*    功    能: 重新设置看门狗
**************************************************************************/
uchar eeprom_read(uint addres){
	uchar i,z;
	isp_addrl=addres;     //低位地址
	isp_addrh=addres>>8;  //高位地址
	isp_contr=0x01;
	isp_contr=isp_contr|0x80; //设时间与充ISP操作。
	isp_cmd=0x01;         //写命令
	isp_trig=0x46;        //触发
	isp_trig=0xb9;        //触发启动。
	for(i=0;i<3;i++);
	isp_addrl=0xff;
	isp_addrh=0xff;
	isp_contr=0x00;
	isp_cmd=0x00;
	isp_trig=0x00;
	z=isp_data;
	return(z);
}

/**************************************************************************
*    函数原型: void DogReset(void)
*    功    能: 重新设置看门狗
**************************************************************************/
void DogReset(void) reentrant{

#if STC89C58
#if STCSPD_1
	WDT_CONTR=0x35;
#elif STCSPD_2
	WDT_CONTR=0x36;
#endif
#elif W77E58
	TA=0xaa;
	TA=0x55;
	RWT=1;
	_nop_();
	_nop_();
#endif
}

/**************************************************************************
*    函数原型: void Dog_Init(void)
*    功    能: 看门狗设置
**************************************************************************/
void Dog_Init(void){

#if STC89C58
#if STCSPD_1
	WDT_CONTR=0x34;			// 2秒
#elif STCSPD_2
	WDT_CONTR=0x35;			// 2秒
#endif
#elif W77E58
	CKCON&=0x3f;				// 3秒
	CKCON|=0xc0;
/*	_nop_();
	_nop_();
	_nop_();
	TA=0xaa;
	TA=0x55;
	WDCON=0;*/
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	TA=0xaa;
	TA=0x55;
	WDCON=2;
#endif
}

/**************************************************************************
*    函数原型: void delay_20us(uint Count)
*    功    能: 延时Count个20us
**************************************************************************/
/*
void delay_20us(register uint Count){

	register uchar T;

	for(;Count>0;Count--){
		for(T=0;T<5;T++){
#if STC89C58
#if STCSPD_1
			// STC89C58,22.1184MHz晶振,12分频,此时延时准确
			;
#elif STCSPD_2
			_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
			_nop_(); _nop_(); _nop_();
#endif
#elif W77E58
			_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
			_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
			_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
			_nop_();
#endif
			}
		DogReset();
		}
}

/**************************************************************************
*    函数原型: void delay_ms(uint Count)
*    功    能: 延时Count个ms
**************************************************************************/
void delay_ms(register uint Count){

	register uchar T;

	for(;Count>0;Count--){
		for(T=0;T<250;T++){
#if STC89C58
#if STCSPD_1
			// STC89C58,22.1184MHz晶振,12分频,此时延时准确
			_nop_(); _nop_(); _nop_(); _nop_();
#elif STCSPD_2
			_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
			_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
			_nop_(); _nop_();
#endif
#elif W77E58
			_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
			_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
			_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
			_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
#endif
			}
		DogReset();
		}
}

/***********************************************************************************
函数名:uchar keyscan(void)
功能:按键扫描
返回值:
***********************************************************************************/
uchar keyscan(void){

	return(~P2&0x38);

}

/**************************************************************************
*    函数原型: uchar ButtonAction(void);
*    功    能: 按钮动作类型
	 返 回 值: 当按钮为空,  返回值为vBtnType_Null=0
	 		   当按钮按下时,返回值为vBtnType_Push=1
	           当按钮弹起时,按回值为vBtnType_Pop =2
	           当按钮长按时,返回值为vBtnType_Keep=3
*************************************************************************/
uchar keyAction(void){

	uchar cButton;

	cButton=keyscan();
	if(cButton==cBtnVal){
		// 无按键
		if(cButton==vBtnVal_Null){
			return(vBtnAct_Null);
		}
		// 按键保持
		else{
			return(vBtnAct_Keep);
		}
	}
	else if(cButton==vBtnVal_All){
		return(vBtnAct_All);
	}
/*	else if(cButton==vBtnVal_Debug){
		return(vBtnAct_Debug);
		}*/
	else{
		delay_ms(10);
		if(keyscan()==cButton){
			// 按键弹起
			if(cButton==vBtnVal_Null){
				cBtnValPre=cBtnVal;
				cBtnVal=cButton;
				return(vBtnAct_Pop);
			}
			// 按键按下

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -