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

📄 display.c

📁 上学时做的一个51单片机按键显示程序
💻 C
字号:
/*
* Copyright(c)2005 - 2009 danise .China
   周建勇
   jsn_ze@yahoo.com.cn
  arcbravezhou@yahoo.com.cn
* All rights reserved.
* Redistribution and use in source and bianry forms
* with or without modification ,are permitted provided
* that following conditions are met:
* 
* 1.Redistrubution of source code must retain the 
* above copyright notice,this list of conditions and
* following disclaimer.
* 
* 2.Redistributions in binary form must reproduce the 
* above copyright notice,this list of conditions and 
* following disclaimer in the documentation and /or other
* materials provided with the distribution.
* 
* Alternately, this acknowledgment may appear in the software
* itself,if and wherever such third-party acknowledgements
* normally appear.
*
* This software is designed for key board and led display.
* 
	
*/
#include "./include/display.h"
#include "intrins.h"

#include "./include/global.h"
#include "./include/system.h"


/**>
 *	display code tables
*/
UINT8 code clrbuf[LED_MAX_NUM]={ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
/*
	ledcheck table "1. 2. 3. 4. 5. 6. 7. 8. 9. - 2.0"
*/
UINT8 code scrchktab[]=
	       {0xa,0xa,0xa,0xa,0xa,0xa,0xa,0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,
		    0xa,0xa,0x2,0xc,0xd,0xa,0xa,0xa,0xa,0xa,0xa,0xa,0xa,0xa};
			    /* version 2.0*/


UINT8 code R_ledtable[]={0xa,0xeb,0x4c,0xc8,0xa9,0x98,0x18,0xcb,0x8,0x89,
						/*0 , 1  , 2  , 3  , 4 ,  5   , 6 , 7  ,8 ,  9*/
							0x9,0x1e,0x1c,0x1d,0x29,0xff,0xff,0xff,0xfb
						/*   A , c  , e/ ,  f , h  , [] , [],  [] , -  */

};
UINT8 code R_dot_ledtable[]={0x2,0xe3,0x44,0xc0,0xa1,0x90,0x10,0xc3,0x0,0x81
						/*    0 , 1 ,  2  , 3  , 4  , 5   , 6 , 7   , 8 , 9 */
						,0xff,0xfb,0x2,0xeb
						/*[]   -    0.   1*/
};
UINT8 code L_ledtable[]={0x48,0xeb,0x8c,0x89,0x2b,0x19,0x18,0xcb,0x8,0xb,
					 /*	  0  , 1  ,2   ,3   , 4  , 5  , 6 ,  7  , 8 , 9 */
				    0xa,0x5c,0x1c,0x1e,0x2a,0xff,0xff,0xff,0xbf}; 
				   /*A , c , e  , f  ,  h  ,  [], []   []   - */
  				
UINT8 code L_dot_ledtable[]={0x40,0xe3,0x84,0x81,0x23,0x11,0x10,0xc3,0x0,0x3,0xff,0xbf,0x40,0xeb};
                      /*      0.  , 1.  , 2  , 3  , 4  , 5  , 6  , 7  , 8 , 9 []    -   0.    1*/
/* a structure for leds display */
STR_LEDBCDCODE LedsShowStr;
/* screen save table */
UINT8 scrsavebuf[]={0,0,0,0,0,0,0,14,0,0,0,0,17,17,17,17,17,17,17,17};
UINT8 scrindex = 0;
UINT8 scrflag =0; /* screen save flag */
/**/

					
/****
 * show one byte figure ,it will be called by "LedMultiByteDisplay"
*/
void LedOneByteDisplay( UINT8  dat, UINT8 position )
{
	UINT8 data temp = 0x0;
	UINT8  i;

	if( position == POSITION_UP ){
		UPLED_CLK = 0;
		for(i=0;i<8;i++)
		{
			temp = dat & 0x01;
			dat= dat >> 1 ;
			if( 0x0 == temp )
				UPLED_DATA = 0;		
			else
				UPLED_DATA = 1;	
			
		UPLED_CLK = 1;
		_nop_();
		UPLED_CLK = 0;
		}
		
	}
	else
	if( position == POSITION_DOWN ){
		DOWNLED_CLK = 0;
		for(i=0;i<8;i++)
		{
			temp = dat & 0x01;
			dat=dat >> 1 ;
			if( 0x0 == temp )
				DOWNLED_DATA = 0;				
			
			else
				DOWNLED_DATA = 1;	
			
		DOWNLED_CLK = 1;
		_nop_();
		DOWNLED_CLK = 0;
		}
		
		
	
	}
}
/**
buf is a arrary filled with led display code.
this function just show the led display code directly ,
never do other thing 
*/
void LedMultiByteDisplay( UINT8 * buf ,UINT8 position ) reentrant
{
	UINT8 data i;
	for(i=0;i<LED_MAX_NUM;i++)
	{
		LedOneByteDisplay(*buf ++ ,position );
	}	

}
/*
clear up leds
*/
void ClearUpLeds(void)
{	
	LedMultiByteDisplay(clrbuf, POSITION_UP );
}
/*
clear down leds
*/
void ClearDownLeds(void)
{
	LedMultiByteDisplay(clrbuf, POSITION_DOWN );
}

/*

*/
void CkeckLights( void )
{
	UINT8 T = 0x01;
	UINT8 i;
	for(i=0;i<=LIGHTS_MAX_NUM;i++)
	{
		SparkLights( T );
		T <<= 1;	
		wait( 10 );
	}

}
/*****************************************************************\
 *check leds when starting system 
 *
\*****************************************************************/
void CheckLeds( void )
{
	
	UINT8  ledshowcode[LED_MAX_NUM];
	UINT8 i;	
	LedsShowStr.isdot = 0;
	LedsShowStr.dotposition = 7;
	LedsShowStr.length = 8;
	for(i=0;i<23;i++)
	{
		ClearUpLeds();
		ClearDownLeds();
		memcpy(&LedsShowStr.ledbuf[0],scrchktab+i ,LED_MAX_NUM );
		TranslateLedCode(LedsShowStr , ledshowcode , 0);/* with mode 0 */
		LedMultiByteDisplay( ledshowcode , POSITION_UP );
		LedMultiByteDisplay( ledshowcode , POSITION_DOWN );
		wait(10);
		
	}
	/*reset ledshowstr */
	RESETLEDSHOWSTR();
	
}
/*
 * mode =0; need not to clear head zero
 * mode =1; need to clear head zero
*/
void TranslateLedCode(STR_LEDBCDCODE  cobcd ,UINT8 * coled,UINT8 mode)
{
	UINT8 data i;
	UINT8 isheadzero = FALSE ;
	if( mode == 1 ){
		if(!cobcd.isdot && cobcd.length == 1 && cobcd.ledbuf[7]==0 ){
			memset(coled,0xff,8);
			coled[7]=0xa;
			return;
		}
		if( cobcd.isdot && cobcd.length == 1 && cobcd.ledbuf[7]==0){
			memset(coled,0xff,8);
			coled[7]=0x2;
			return;
		}
		
			for(i=0;i<LED_MAX_NUM; i++)
			{//for begin
		
				if( 0x1== ( i & 0x01 )  ){ /*right */	//if _1		
					if((cobcd.ledbuf[i]==0x0 && isheadzero==FALSE )){//if _2
				   		coled[i] = 0xff;
					}//if 2_
					else{
							coled[i]=R_ledtable[cobcd.ledbuf[i]];
							isheadzero = TRUE;
					}			
				
				}// if 1_
				else{/* left */
					if((cobcd.ledbuf[i]==0x0 && isheadzero==FALSE )){
				   		coled[i] = 0xff;
					}
					else{
						coled[i]=L_ledtable[cobcd.ledbuf[i]];
						isheadzero = TRUE;
					}
				
				}
			}//for end 
        
	/*add dot*/
	if( cobcd.isdot == 1 ){ /* 1 ->  " ."*/
		if( (cobcd.dotposition &0x01)==0x1 ){
		/* right */
			coled[cobcd.dotposition]=R_dot_ledtable[cobcd.ledbuf[cobcd.dotposition]];

		}
		else{
	    /* left */
			coled[cobcd.dotposition]=L_dot_ledtable[cobcd.ledbuf[cobcd.dotposition]];
		}
	}

		return;
						
	}
	if( mode == 0 ){
		for(i=0;i<LED_MAX_NUM;i++)
		{
			if(0x1 == (i & 0x01)){
			/* right */
				coled[i]=R_dot_ledtable[cobcd.ledbuf[i]];
			}
			else{
			/* left */
				coled[i]=L_dot_ledtable[cobcd.ledbuf[i]];
			}
		}
		return;
	}

}
/**
*when key pressing,it will invoke this function to add a
*key value to display buffer.
*/
void addKyeValHexBuf(UINT8 keyVal )
{
	UINT8 len = 0;
	sreensaveclk = 0;
	SparkLights( LIGHT_PAY );
	len = LedsShowStr.length ;	
	if(0x0==len){
		if( 0x0 == keyVal ){
			/*press 0 first*/
			
			LedsShowStr.length = 0x1;
			LedsShowStr.isdot = 0x0;
			LedsShowStr.dotposition = LED_MAX_NUM-1; 
			/*in order to show single 0 */		
			Enter_Critical_OS();
			event_word |= EVENT_LED_DOWN_UPDATE;
			Exit_Critical_OS();
			return;
		}
		if(0x0a == keyVal ){
			/* press . first */
			LedsShowStr.length = 0x1;
			LedsShowStr.isdot = 0x1;
			LedsShowStr.dotposition = LED_MAX_NUM-1;
			Enter_Critical_OS();
			event_word |= EVENT_LED_DOWN_UPDATE;
			Exit_Critical_OS();
			return;
			
		}
	
	}
	if(	LedsShowStr.length == 0x1 && LedsShowStr.isdot==0 && 0==LedsShowStr.ledbuf[7] ){
		if(0xa!=keyVal)
			return;		
	}
    /* cant be 0.0xxxx */
	if(	LedsShowStr.length == 0x1 && LedsShowStr.isdot==1 && 0==LedsShowStr.ledbuf[7] ){
		if(0x0==keyVal)
			return;		
	}
	if((LED_MAX_NUM-1 - LedsShowStr.dotposition) >= 2) return;
	if( len > 3 ) return; /* void overflood buf */
	if( keyVal>=0 && keyVal<= 0x9 ){/* val is correct */
		MoveOneByte(&LedsShowStr.ledbuf[0],0);/* left move */
		LedsShowStr.ledbuf[LED_MAX_NUM-1]= keyVal;
		LedsShowStr.length ++;
		
		if(0x1 == LedsShowStr.isdot ){
			LedsShowStr.dotposition--;//move dot ahead one byte			
		}
	
    } 
	else if( keyVal == 0xa && 0x0==LedsShowStr.isdot  ){ /* dot coming */
		LedsShowStr.isdot = 1;
		LedsShowStr.dotposition = LED_MAX_NUM-1  ;		
	}
    /* laungh event */
	Enter_Critical_OS();
	event_word |= EVENT_LED_DOWN_UPDATE;
	Exit_Critical_OS();
	/*light L1*/
	

}

void UpateDownLeds()
{
	
	UINT8 idata ledupdatetbuf[LED_MAX_NUM];
	TranslateLedCode(LedsShowStr , ledupdatetbuf , 1);/* with mode 1 */
	LedMultiByteDisplay(ledupdatetbuf , POSITION_DOWN );

	
}
void UpateLeds(bit ud)
{	
	UINT8 idata ledupdatetbuf[LED_MAX_NUM];
	TranslateLedCode(LedsShowStr , ledupdatetbuf , 1);/* with mode 1 */
	LedMultiByteDisplay(ledupdatetbuf , ud );	
}
/*
direction : 0-left / 1- right 
*/
void MoveOneByte(UINT8 * buf ,UINT8 direction )
{
	UINT8	i;
	if( 0x0 == direction ){//if_1
	  for(i = 0;i< LED_MAX_NUM - 1 ;i++)
	  {
			buf[i]=buf[i+1];			
	  }
	  buf[i] = 0x0;
    }//if _1
	else if(0x1 == direction ){//if_2
				for(i = LED_MAX_NUM-1 ;i>0;i--)
				{
					buf[i]=buf[i-1];
				}
				buf[i]=0x0;
         }//if_2
	
	
}
void clearallleds( void )
{
	/*reset ledshowstr */
	RESETLEDSHOWSTR();
	SparkLights ( 0x0 );
	Enter_Critical_OS();
	event_word |= EVENT_LED_DOWN_UPDATE;
	Exit_Critical_OS();
}

void backspace(void )
{
	
	if( LedsShowStr.length > 0 )
	{		
		if( 0x1 == LedsShowStr.isdot){
	 		LedsShowStr.dotposition ++;
			if( LedsShowStr.dotposition >LED_MAX_NUM-1 )
			   LedsShowStr.isdot = 0;
		}
	    
		MoveOneByte(&LedsShowStr.ledbuf[0],1);/// right  move 
		LedsShowStr.length --;
		if(0==LedsShowStr.length){
			RESETLEDSHOWSTR();
		}
		Enter_Critical_OS();
		event_word |= EVENT_LED_DOWN_UPDATE;
		Exit_Critical_OS();
		
	}
}
/*  + --- - - - - - - - - - - - - - - - - - - - - - - - - - +
	|	    	|	length	|	cmd	 |	 data	|	crc 	|
	|	8bits	|	 8bits  |  8bits |   32bits |  8bits    |
*/
void enter( void )
{ 	
	i2cbuf[0] = 0x6; /* length */
	i2cbuf[1] = 0x0; 
	i2cbuf[2] = 0x0; 
	i2cbuf[4] = LedsShowStr.ledbuf[LedsShowStr.dotposition]|(LedsShowStr.ledbuf[LedsShowStr.dotposition-1]<<4);
	i2cbuf[3] = LedsShowStr.ledbuf[LedsShowStr.dotposition-2]|(LedsShowStr.ledbuf[LedsShowStr.dotposition-3]<<4);
	if(0x7==LedsShowStr.dotposition)
		  i2cbuf[5] = 0x0;
	else if(0x6==LedsShowStr.dotposition)
		  i2cbuf[5] = LedsShowStr.ledbuf[LedsShowStr.dotposition+1]<<4;
	else 
		  i2cbuf[5] = (LedsShowStr.ledbuf[LedsShowStr.dotposition+1]<<4)|LedsShowStr.ledbuf[LedsShowStr.dotposition+2];
	i2cbuf[6]=i2cbuf[0]+i2cbuf[1]+i2cbuf[2]+i2cbuf[3]+i2cbuf[4]+i2cbuf[5];
	i2cbuf[6]=~i2cbuf[6];
	/*lunch event */
	Enter_Critical_OS();
	event_word |= EVENT_I2C_DATA_SEND;
	Exit_Critical_OS();	
	RESETLEDSHOWSTR();
}


/*

*/
void SparkLights(UINT8 state )
{
	if( state & 0x01 ) L5= 0;  
	 else L5= 1;
	if( state & 0x02 ) L4= 0;  
	 else L4 =1;
	if( state & 0x04 ) L3= 0;
	 else L3 =1;
	if( state & 0x08 ) L2= 0;
	 else L2 =1;
	if( state & 0x10 ) L1= 0;
	 else L1 =1;
	if( state & 0x20 ) L0= 0;
	 else L0 =1;	
}
void error(UINT8 e )
{
	UINT8 idata ledupdatetbuf[LED_MAX_NUM];
	switch (e)
	{
		case 0x0c:  LedsShowStr.length = 3;
					LedsShowStr.isdot  = 0;
					LedsShowStr.dotposition = 7;
					LedsShowStr.ledbuf[5]= 12;
					LedsShowStr.ledbuf[6]= 0x0;
					LedsShowStr.ledbuf[7]= 11;	
					TranslateLedCode(LedsShowStr , ledupdatetbuf , 1);/* with mode 1 */
					LedMultiByteDisplay(ledupdatetbuf , POSITION_DOWN );
					RESETLEDSHOWSTR();

		break; 
    }
}
/*
*When no events happen,system will start screen save
*/
void CheckSreenSave(void)
{	
	if( sreensaveclk >=10000 ){		
		event_word |= EVENT_SCREEN_SAVE;
		scrflag = 1;/* start screen save */
	}
}
/*
*this function will show stuff in scrsavebuf buffer.
*/
void SaveSreen()
{
	UINT16 i;
	UINT8  ledshowcode[LED_MAX_NUM];
	sreensaveclk = 0;
	LedsShowStr.length = 8;
	LedsShowStr.isdot = 0;	
	LedsShowStr.dotposition = 7;
	scrindex = 0;
	SparkLights ( 0x0 );
	while( scrflag )
	{
		ClearDownLeds();
		ClearUpLeds();
		memcpy(&LedsShowStr.ledbuf[0],scrsavebuf+scrindex ,LED_MAX_NUM );
		scrindex=( scrindex + 1 ) % 13 ;
		TranslateLedCode(LedsShowStr , ledshowcode , 1);/* with mode 1 */	
		LedMultiByteDisplay( ledshowcode , POSITION_DOWN );
//	wait(100);
		for(i=0;i<65530;i++);
	}
	clearallleds();
	
}

⌨️ 快捷键说明

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