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

📄 driver.c.bak

📁 用keil开发的.单片机税控器程序.单片机用的是AT公司的.upsd3245
💻 BAK
📖 第 1 页 / 共 4 页
字号:
/*
* Copyright (c) 2004,成都港顺科技发展有限公司
* All rights reserved.
*
* 编 译 器:Keil:C Compiler:7.20;Assembler:7.10
* 工程名称:POS-Test.UV2
* 文件名称:Common.C
* 摘    要:硬件驱动
*
* 单 片 机:uPSD3254
* 当前版本:0.4
* 作    者:李凯、雍继兵
* 完成日期:2004-12-7 14:45
*
* LCD显示字库采用16×16 GBK字库,纵向取模
* VFD为10位9段显示
* 键盘8×8矩阵+键盘锁8档=8×9矩阵
*/

#include "Main.h"

///////////////////////////////////////////////////////////////////////////////
//      KEY Control
#define KeyEnable     P47 = 0
#define KeyDisable    P47 = 1
#define KeyAdr      0xe00c
///////////////////////////////////////////////////////////////////////////////

#define NandFalshE    0x800D

#define PrintAdr    0x800E

///////////////////////////////////////////////////////////////////////////////
//              Serial Flash Control

#define FlashDisable    P4|=0X0F,P47=1
#define FlashEnable   P47=0
#define Mcs1_Enable   P4=(P4&0xF0)|0X03
#define Mcs2_Enable     P4=(P4&0xF0)|0X04
#define Mcs3_Enable     P4=(P4&0xF0)|0X0a
#define Mcs4_Enable     P4=(P4&0xF0)|0X06

//        M25P80 and M45PE80 and M25P16 Instruction   
//
#define FlashWE        	0x06      //Write Enable
#define FlashWD        	0x04      //Write Disable
#define FlashRSR       	0x05      //Read Status Register
#define FlashWSR       	0x01      //Write Status Register
#define FlashRD        	0x03      //FlashRD Data Bytes
#define FlashFRD     	0x0B      //FlashRD Data Bytes at Higher Speed
#define FlashPW        	0x0A      //Page Write
#define FlashPP        	0x02      //Page Program
#define FlashPE        	0xDB      //Page Erase
#define FlashSE        	0xD8      //Sector Erase
#define FlashBE        	0xC7      //Bulk Erase
#define FlashDP        	0xB9      //Deep Power-down
#define FlashRDP       	0xAB      //Release form Deep Power-down
///////////////////////////////////////////////////////////////////////////////

#define VFD_Enable    P47=0,P4=(P4&0xF0)|0x02
#define VFD_Forbid    P4|=0X0F,P47=1

#define OpenCashbox   P47=0,P4=(P4&0xF0)|0x01
#define CloseCashbox  P4|=0X0F,P47=1

#define BeepOn    P47=0,P4&=0xF0
#define BeepOff   P4|=0X0F,P47=1

sbit  VFDCLK=P4^4;   //P4.1
sbit  VFDSDA=P4^6;   //p4.0

sbit  I2CSDA=P3^6;
sbit  I2CSCK=P3^7;

volatile uchar xdata SerialFlash[5] _at_ 0x6DFB;
volatile uchar xdata TxdBuf [256] _at_ 0x6F00; //Data Buffer for UART1、2 Transmitter
volatile uchar xdata RxdBuf [256] _at_ 0x6E00; //Data Buffer for UART1、2 Receiver
uint xdata TxdLen;      //no. of data to FlashBE transimitted
uint xdata TxdNum;      //no. of data transmitted
uint xdata RxdLen;      //no. of data to FlashBE received
uint xdata RxdNum;      //no. of data received
uchar xdata PwrFlg;
uchar xdata TxdFlg;
uchar xdata RxdFlg;

//---------------------------INT Begin--------------------------------------------------

void PowerInt(void) interrupt IE0_VECTOR 
{
	PwrFlg=1;
//	LcdDisplay(0,16,"进入掉电中断!",3);	
}

void SimInt (void) interrupt	IE1_VECTOR 
{

}

/*------------------------------------------------------------------------------*/
//             UART 0 Interrupt service routine
/* ---------------------------------------------------------------------------- */
void Uart0Int (void) interrupt SIO_VECTOR 
{
	if (RI)
	{  		
		if (RxdLen > RxdNum)
	    {
	    	RxdBuf [RxdNum] = SBUF;
			RxdNum++;
	   	}
	   	RI = 0;
	   	if (RxdLen <= RxdNum)RxdFlg=1;
	}
	else
	{	 	
	  	if (TxdLen > TxdNum)
	    {
	    	SBUF = TxdBuf [TxdNum];
	    	TxdNum++;
	    }
	    TI = 0;
	    if (TxdLen <= TxdNum)TxdFlg=1;
	}
}

/*------------------------------------------------------------------------------*/
//             UART 1 Interrupt service routine
/* ---------------------------------------------------------------------------- */

void Uart1Int (void) interrupt SIO1_VECTOR 
{
	if ( SCON2 & 0x01)
	{
	  	SCON2 &= 0xFE;				//RI = 0;
	  	if (RxdNum<RxdLen)
	    {
	    	RxdBuf [RxdNum] = SBUF2;
	    	RxdNum+=1;
	    }
	    if (RxdLen <= RxdNum)RxdFlg=1;
  	}
	else
	{
	  	SCON2 &= 0xFD;
	  	if (TxdNum<TxdLen)
	    {
	    	SBUF2 = TxdBuf [TxdNum];
	  		TxdNum+=1;	  						//TI = 0;
	  	}
	  	if (TxdLen <= TxdNum)TxdFlg=1;
	}
}

//---------------------------INT End----------------------------------------------------

//---------------------------Uart Begin--------------------------------------------------


/*
Timer1:
模式1、3波特率= 2SMOD/32 × fosc/12×[256 - (TH1)]
timer_reg_value = (unsigned char)(256 - (OSC * baud_ratio)/ (32 * 12 * baudrate));

Timer2:
模式1、3波特率=fosc/(32×[65536-(RC2H,RC2L)])
timer_reg_value = (unsigned int) (65536 - (OSC / (32 * baudrate)));
*/
void UartSet(uchar ch,uchar timer,uchar mode,uchar net,uint baudrate)
{
	unsigned char ratio,sconx;
	unsigned int value;
//	unsigned long tp;

	sconx=0;
	switch(mode)
	{
		case 	0:	break;
		case 	1:	sconx |= 0x40;	break;
		case 	2:	sconx |= 0x80;	break;
		case 	3:	sconx |= 0xC0;	break;
		default	:	sconx |= 0x40;	break;
	}
	if(net==1)sconx |= 0x20;		//如果允许多机通信

	RCLK=0;
	TCLK=0;
	PCON &= 0xF3;		//RCLK1,TCLK1=0

	if(ch==0)
	{
		REN=0;						//禁止UART0接收
		ES = 0;						//关UART0中断
		if(timer==2){RCLK=1;TCLK=1;}//TCLK和RCLK=1,使用Timer2 for UART1
	}
	else
	{
		SCON2 &= 0xEF;				//禁止UART1接收,REN2=0
		IEA &= 0xEF;				//关UART1中断,ES2 = 0;
		if(timer==2)PCON |= 0x0C;	//TCLK1和RCLK1=1,使用Timer2 for UART2
	}

	if(timer==1)
	{//timer 1:24MHz时钟时,最好不用Timer1做波特率时钟,误差太大
		ET1=0;                  //关定时器1中断
    	TR1=0;
    	TMOD &= 0x0F;			// clear timer 1 mode bits
		TMOD |= 0x20;			//Set timer1 自动重载模式,按定时器操作来配置
		if(baudrate==9600 || baudrate==38400)
		{
			ratio=2;
			if(ch==0)PCON |= 0x80;		//SMOD=1;
			else	 PCON |= 0x40;		//SMOD1=1;
		}
		else
		{//baudrate==19200 or 4800,19200误差比较大
			ratio=1;
			if(ch==0)PCON &= 0x7F;		//SMOD=0;
			else	 PCON &= 0xBF;		//SMOD1=0;
		}
		if(mode==1 || mode==3)
		{//Mode 1 or Mode 3
//			tp = OSC/384;		//12*32=384
//			TH1 = 256 - (unsigned char)(tp*ratio/baudrate);
			TH1 = 256 - (unsigned char)(Timer1Baudrate*ratio/baudrate);
			TL1 = TH1;
			TR1=1;         		//启动定时器1
		}
	}
	else
	{//timer 2
		ET2=0;
		TR2=0;
		if(mode==1 || mode==3)
		{
//			tp = OSC/32;
//			value = (unsigned int)(65536 - (tp/baudrate));
			value = (unsigned int)(65536 - (Timer2Baudrate/baudrate));
			RCAP2H = value>>8;
			RCAP2L = value;
			PCON &= 0xBF;		//SMOD1 = 0,Only Bps=4800 or 9600 or 19200
			T2MOD=1;
			TR2 = 1;			//启动定时器2
		}
	}
	if(ch==0)
	{
		SCON = sconx | 0x10 ;	//SET SM0,SM1,SM2;REN=1;TI=0;RI=0;TB8=0;RB8=0
//		ES = 1;					//开UART0中断
	}
	else
	{
		SCON2 = sconx | 0x10 ;	//SET SM0,SM1,SM2;REN=1;TI=0;RI=0;TB8=0;RB8=0
//		IEA |= 0x10;			//开UART1中断,ES2 = 1;
	}
}

void UartTxd(uint Len,uchar *str)
{
	COM1CS;
	UARTC;
	UART1_Mod1_Timer1_9600;
	IEA &= 0xEF;			//关UART2中断,ES2 = 0;
	TxdNum=0;
	TxdLen=Len;
	while(TxdNum<TxdLen)
	{
		SBUF2 = str[TxdNum];
		while((SCON2 & 0x02)==0);
		SCON2 &= 0xFC;
		TxdNum++;
	}
//	TR1=0;
}

void UartRxd(uint Len,uchar *str)
{
	COM1CS;
	UARTC;
	UART1_Mod1_Timer1_9600;
	IEA &= 0xEF;			//关UART2中断,ES2 = 0;
	RxdNum=0;
	RxdLen=Len;
	while(RxdNum<RxdLen)
	{
		while((SCON2 & 0x01)==0);
		SCON2 &= 0xFC;
		str[RxdNum]=SBUF2;
		RxdNum++;
		//if(RxdNum==1)RxdLen=str[0]+1;
	}
//	TR1=0;
}

void ScanerIni()
{
	UART1_Mod1_Timer1_9600;
	UARTA;	
	COM1CS;	
	RxdNum=200;
	RxdLen=215;
	RxdFlg=0;
	IEA |= 0x10;				//开UART1中断,ES2 = 1;
}



#if RS485
void RS485Txd(uint Len,uchar *str)
{
	UART1CS;
	UARTD;
	UART1_Mod1_Timer1_9600;
	IEA &= 0xEF;			//关UART2中断,ES2 = 0;
	TxdNum=0;
	TxdLen=Len;
	while(TxdNum<TxdLen)
	{
		SBUF2 = str[TxdNum];
		while((SCON2 & 0x02)==0);
		SCON2 &= 0xFC;
		TxdNum++;
	}
}

void RS485Rxd(uint Len,uchar *str)
{
	UART1CS;
	UARTA;
	UART1_Mod1_Timer1_9600;
	IEA &= 0xEF;			//关UART2中断,ES2 = 0;
	RxdNum=0;
	RxdLen=Len;
	while(RxdNum<RxdLen)
	{
		while((SCON2 & 0x01)==0);
		SCON2 &= 0xFC;
		str[RxdNum]=SBUF2;
		RxdNum++;
		//if(RxdNum==1)RxdLen=str[0]+1;
	}
}
#endif

//---------------------------Uart End----------------------------------------------------


//---------------------------I2C Begin----------------------------------------------------
void I2cDelay()
{
	uint i=1500;	//ATMEL:500;ST:1500
	while(--i);
}

void SendAck(void)
{
	I2CSDA=0;
	I2CSCK=1;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	I2CSCK=0;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	I2CSDA=1;
}

//应答信号:
//调用方式:SendNotAck();
void SendNotAck(void)
{
	I2CSDA=1;
	I2CSCK=1;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	I2CSCK=0;
}
//----------------------------------------------
void I2cStart(void)
{
	I2CSCK=0;
	I2CSDA=1; 
	I2CSCK=1;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	I2CSDA=0;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	I2CSCK=0;
	I2CSDA=1; 
}

void I2cStop(void)
{
	I2CSCK=0;
	I2CSDA=0;
	I2CSCK=1;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	I2CSDA=1;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	I2CSCK=0;
}

bit	WaitAck(void)
{
	 uchar errtime=255;//因故障接收方无ACK,超时值为255。
	 
	 I2CSDA=1;
	 I2CSCK=1;
	 while(I2CSDA)
	 {
	 	errtime--;
	 	if(!errtime)
	 	{
	 		I2cStop();
	 		return 0;
	 	}
	 }
	 I2CSCK=0;
	 return	1;
}

void I2cSendByte(uchar dat)
{
	uchar i=8;
	while (i--)
	{
		I2CSCK=0;
		I2CSDA=(bit)(dat&0x80); 
		dat<<=1;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		I2CSCK=1;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
	}
	I2CSCK=0;
}

//接收一个字节
//调用方式:I2C_Receive_Byte();从SDA总线上读取一个字节的数据
uchar I2cReceiveByte(void)
{
	uchar i=8;
	uchar dat=0;
//	I2CSDA=1;
	while (i--)
	{
		dat<<=1;	
		I2CSCK=1;
		_nop_();
		_nop_();	
		_nop_();	
		_nop_();
		dat|=I2CSDA;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		I2CSCK=0;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
	}
	return	dat;
}

//每次最多写入32字节(24C64一页32字节)
uchar I2cPageWr(uchar device,uint adr,uchar len,uchar *dat)
{
	uchar i;
	
	if(device == EepromAdr)
	{
		if(len>32)len=32;
		I2cStart();
		I2cSendByte(device); 	
		if(!WaitAck()) return 0xE0;
		i=(uchar)(adr/256);
		I2cSendByte(i);
		if(!WaitAck()) return 0xE1;
		i=(uchar)(adr % 256);
		I2cSendByte(i);
		if(!WaitAck()) return 0xE2;
		for(i=0;i<len;i++)
		{
			I2cSendByte(dat[i]);
			if(!WaitAck()) return 0xE4;
		}
		I2cStop();	
		I2cDelay();
	}
	else
	{
		I2cStart();
		I2cSendByte(device); 	
		if(!WaitAck()) return 0xE0;		
		i=(uchar)(adr);
		I2cSendByte(i);
		if(!WaitAck()) return 0xE2;
		for(i=0;i<len;i++)
		{
			I2cSendByte(dat[i]);
			if(!WaitAck()) return 0xE4;
		}
		I2cStop();	
		I2cDelay();
	}
		
//	I2cStart();  
//	if(device == EepromAdr && len>32)len=32;
//	I2cSendByte(device); 	
//	if(!WaitAck()) return 0xE0;
//	if(device == EepromAdr)
//	{
//		i=(uchar)(adr/256);
//		I2cSendByte(i);
//		if(!WaitAck()) return 0xE1;
//		i=(uchar)(adr % 256);
//		I2cSendByte(i);
//		if(!WaitAck()) return 0xE2;
//	}
//	else
//	{
//		i=(uchar)adr;
//		I2cSendByte(i);
//		if(!WaitAck()) return 0xE3;
//	}
//	for(i=0;i<len;i++)
//	{
//		I2cSendByte(dat[i]);
//		if(!WaitAck()) return 0xE4;
//	}
//	I2cStop();	
//	I2cDelay();
	return 1;
}

⌨️ 快捷键说明

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