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

📄 sub.c

📁 contain many examples code for I2c,UART,string ,digital convert, read/write to EEprom in microchip P
💻 C
字号:

#include "hard_def.h"
extern void random_write(uint mem_adr, byte dat);
extern byte random_read(uint  mem_adr);
extern void sci_PutByte(uchar);
extern void fill_treat_data(uint);


#define	TEST_SEARCH_ID   0
 uchar check_hex(uchar);


//extern 		sys_infor.sys_userid; // system informattion 


ulong  asc_hex(uchar  *code_Asc,uchar style)
{

	uchar n,b,c;
	ulong sum;
	
	sum=(*code_Asc);
	
	c=style;
	for(b=0;b<c;b++)
		{
		sum=sum<<4;
		sum=sum+(*(code_Asc++));
		}
		
	//sum=((trans_table[3]*100)+(trans_table[2]*10)+trans_table[1]);
	return  sum;
}
//	purpose 	:transfer ascii char to hex
//	input 	:ascii code point
//    output    :decode numbers of ascii char
void
decode_ascii(uchar *code,uchar code_num)
{
	uchar b;
	//ulong sum;
	//(*code)=0;
	code-=1;
	for(b=0;b<code_num;b++)
		{
		if( (*(++code))  <  0x40 )  (*(code))-=0x30;
		else		  (*(code))-=0x37;
		}
}


//	purpose 	:	write 5byte data to eeprom
//   input 	:	address
//   output 	:	data

void
write_e2rom(uint add_e,uchar *e_data)
{
	uchar	n;
	uint	b1;
	b1=add_e;
	//putst("\n write data to eeprom   \n ");
	di();
	for(n=0;n<4;n++)
		{
		random_write(b1, *(++e_data));
		b1++;
		}
	ei();

}

//	purpose		:	read data from eeprom and send to terminal by uart
// 	input		:	start address
//	output		:	terminal
void
read_4byte(uint  start_byte)
{
	uchar n,c,d;
	uint b2;
	b2=start_byte;
	b2+=3;
	for(n=0;n<4;n++)
		{
		asm("clrwdt");
		c=random_read(b2);	
		d=c>>4;
		sci_PutByte(check_hex(d));
		sci_PutByte(check_hex(c));
		b2--;
		}

}
 #if 0
//	purpose 		:	write 4byte data to eeprom
//   	input 		:	address
//   output 		:	data
static uchar ldata[4];
ulong
read_e2rom(uint  add_e)
{
	uint	b1;
	putst("\n");
	putst("H");
	b1=day_add;
	read_4byte(b1);
	//sys_infor.sys_userid;
	putst("-");
	b1=add_e;
	read_4byte(b1);
	putst("-E-");
}
#endif
//	purpose		: serch next unexecute id in eeprom
//	input		: point form treat_table.
//	output		: fill data in the treat_table
#if TEST_SEARCH_ID
static uint 	ii;
#endif

void
search_treat_id(uchar *treat_block)
{	
#if 0
	uchar n;
	uint	search_byte,wri_pt ,serh_cnt;
	//random_write(0xb, 0x43);
	//putst(" find n word");
	//b=random_read(0x0b);
	//sci_PutByte(b);
	//ii=0;
	
	serh_cnt=0; //search count =0

		for(search_byte=0x0c;search_byte<MAX_TREAT_NUMBER;search_byte+=0x0c)
			{
			#if TEST_SEARCH_ID
			ii++;
			#endif
			serh_cnt++;
			asm("clrwdt");
			if(	random_read(search_byte) ==   'N'  )
				{
					(*treat_block)=RAM_READ;
					search_byte-=0x0b;
					for(n=0;n<=EEROM_MAX_WORD;n++)
						{
						(*++treat_block)=random_read(search_byte);
						search_byte++;
						asm("clrwdt");
						}
					//locate the E-byte in eeprom addres
					wri_pt=(search_byte-1);	
					(*++treat_block)=wri_pt;
					(*++treat_block)=wri_pt>>8;
					
					search_byte=EEROM_MAX_SIZE;				
				}

			
			}
		if(serh_cnt>=0x03ff)  (*treat_block)=NON_TREAT_ID;

		

		
	//system never write any data in 24128 ,system into idle mode
	
#endif

}
uchar
check_hex(uchar hex_dec)
{
	uchar	d;
	d=hex_dec;
	d&=0x0f;
	if(d<0x0a)	d+=0x30;
	else	 d+=0x37;
	return d;
}

//id_mark




//	purpose	:	get user id
//	input	:	
//	output	:
void
read_day_eerom(uchar *uid)
{
	uchar	n;
	uint		b;
	b=day_add;
	for(n=0;n<4;n++)		*(++uid)=random_read(b+n);
		
}
//	purpose	:	disenable  freqence
//	input	:	
//	output	:
void
dis_freq(void)
{
	RC0=0;
	RC1=0;
	RC2=0;
	RC3=0;
}

 void
 break_point (void)
 {
	for(;;)
		{
		putst("break;");
		asm("clrwdt");	
		}


 }
#if	UART_MODE
// purpose : show the value in uart
// input 	   : show value	
// out_put  : uart
uchar
show_value(register const char *str, uchar s_value)
{
	uchar c,d;
		
	asm("clrwdt");
	while((*str)!=0)
	{
	sci_PutByte(*str);
    	if (*str==13) sci_PutByte(10); 
   	if (*str==10) sci_PutByte(13);
	str++;
	}
	c=s_value;	
	d=c>>4;

	//putst(*tran);
	putst("---0x");

	sci_PutByte(check_hex(d));
	sci_PutByte(check_hex(c));
	return 0;
}
#endif
# if 0
//	 purpose 	:	 decode the hex to decimal and send to UART
//	inPut	:	HEX CODE 
//	output	:	UART
void
hex2dec(char)
{
char h_high,l_low;
temp_vr=(sys_infor.sys_time&0x0ff);
i_high=(temp_vr/10);
i_low=(temp_vr% 10);
sci_PutByte(check_hex(i_high));	
sci_PutByte(check_hex(i_low));	
}

#endif

⌨️ 快捷键说明

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