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

📄 niuju1.c

📁 基于S51单片机的C源码 对学习S51单片机的同学很有帮助 包含了实验室的和多模块程序
💻 C
📖 第 1 页 / 共 3 页
字号:

/*----------------------------------------------------*/
/*RIGTH  →   0
  定点        1
  查询        2
  UP      ↑  3
  巡检        4
  LEFT    ←  5
  快速    K   6
  打印        7
  设定        8
  DOWN    ↓  9
  消音    *   10
  确认    #   11
  */
void getkey(void)
{
static data key_num=0;
data uchar ch,i;  /*xdata*/
	ch=getkey0();
	if(flag_key){
		if(ch==key_sfr&&ch!=0xff){
			minuter=0;
			return;
			}

		flag_key=0;
		key_sfr0=0;
		key_num=0;
		T_key=0;
		key_sfr=0xff;
		return;
	        }
	key_sfr=ch;
	rst_wdog();
	if(ch==0xff){
		key_sfr0=0;
		key_num=0;
		if(T_key>2){ //4
                            T_key=0;
                            //bee=0;
                            }
		return;
		}
	for(i=0; i<10; i++){ //20
		ch=0;
		ch=0;
		ch=0;
		ch=0;
		ch=getkey0();
		if(key_sfr==ch)continue;
		minuter=0;
		return;
	  }
	  /*
	key_num++;
	if(key_num<2)return;
	key_num=0;
	*/
	if(T_key<2)return; //4
	from_sfr_sfr0();
	flag_key=1;
	//bee=1;
	for(i=0;i<200;i++)ch=0;
//	for(i=0;i<200;i++)ch=0;
//	for(i=0;i<200;i++)ch=0;
	/*bee=0; 2001.3.*/
}
/*----------------------------------------------------*/
void rst_wdog(void)
{
x25_cs=0;
x25_cs=0;
x25_cs=1;
}
        /*
        outbyt:
        Sends byte to EEPROM
        This routine shifts out a byte, starting with the MSB to the EEPROM
        */
void outbyt(uchar ib)
{
data uchar i;
ab=ib;
for(i=0;i<8;i++){
        clk=0;
        din=abit7;
        ab<<=1;
        clk=1;
        }
din=0;
}

/*
inbyt:
        Receives byte from EEPROM
        This routine receives a byte MSB first, from the EEPROM
        Output: received byte
        */
uchar inbyt(void)
{
data  uchar i;
dout25=1;
for(i=0;i<8;i++){
        clk=1;
        clk=0;
        ab<<=1;
        abit0=dout25;
        }
return ab;
 }
 /*
byte read:
Single byte read
This routine sends the command to read asingle byte from the EEPROM
memory array
output : read byte
*/
uchar byte_read(uint loc)
{
data uchar ch;
data union{
	uchar ch[2];
	uint  i;
	}u;
u.i=loc;
clk=0;x25_cs=0;u.ch[0]=((u.ch[0]&0x01)<<3)|0x03;
outbyt(u.ch[0]);  /* out read command*/
outbyt(u.ch[1]);
ch=inbyt();
clk=0;
x25_cs=1;
return ch;
}

/*
        rdsr_cmd
        Read status register
        This routine sends the command to read the status register
        Output : status register
        */
uchar rdsr_cmd(void)
{
data uchar ch;
        clk=0;
        x25_cs=0;
        outbyt(5); /*read status register instruction (rdsr)*/
        ch=inbyt(); /*read status register*/
        clk=0;
        x25_cs=1;
        return ch;
}


/*
wip_poll
        write in progress polling
        This routine polls for completion of a nonvolatile write cycle by
        examining the WIP bit of the status register
        */
void wip_poll(void)
{
data uchar i;
for(i=0;i<99;i++){
        ab=rdsr_cmd();
        if(!abit0) break;
        }
i=0;
 }
/*
byte_write:
Single byte write
This routine sends the command to write a single byte to the EEPROM
memory array
*/
void byte_write(uint loc,uchar x)
{
//uchar *p,ch;
data union{
	uchar ch[2];
	uint  i;
	}u;
x25_WP=1;
clk=0;x25_cs=0;
outbyt(0x06); /*write enable*/
x25_cs=1; x25_cs=0;
u.i=loc;
clk=0;x25_cs=0;u.ch[0]=((u.ch[0]&0x01)<<3)|0x02;
outbyt(u.ch[0]);  /* out read command*/
outbyt(u.ch[1]);
outbyt(x);
clk=0; x25_cs=1;
wip_poll();
}

/*----------------------------------------------------------------------*/
/*---------------------------------------*/
void get_buff(char *buff1,uchar n1)
{
data uchar i,n,*buff;
data uint base;  /*xdata*/
n=n1; buff=buff1;
base=0x60+(n-1)*12;
for(i=0;i<9;i++){
	buff[i]=byte_read(base+i);
	}
buff[9]=byte_read(base+9); /*trap_set*/
}
/*---------------------------------------*/
/*---------------------------------------*/
/*n==1--7*/
void save_buff(char  *buff1,uchar n1)
{
data uchar i,n,*buff;
data uint base;
n=n1; buff=buff1;
base=0x60+(n-1)*12;
for(i=0;i<9;i++){
	byte_write(0x60+(n-1)*12+i,buff[i]);
	}
//byte_write(0x60+(n-1)*12+9,ip);
byte_write(0x60+(n-1)*12+10,0x55);
}
/*---------------------------------------*/
/*---------------------------------------------------------*/
void bit8_serial_input(uchar ix)
{
data uchar i,j,nc;
P_E=0;    //clk=0;
ab=ix;
for(i=0;i<8;i++){
	P_RW=abit7;    //din=abit7;
	ab<<=1;
	P_E=1;
	for(j=0;j<10;j++)nc=0;
	P_E=0;
	}
}
/*---------------------------------------------------------*/

void instruct(uchar dat)
{
data uchar ch;
P_RS=1;   //选通
ch=0xf8;
bit8_serial_input(ch);
ch=dat&0xf0;
bit8_serial_input(ch);
ch=dat<<4;
bit8_serial_input(ch);
P_RS=0;  //结束作业
}
void write_abyte(uchar dat)
{
data uchar ch;
P_RS=1;  //读数据--不选通
ch=0xfa;  //RS=1 RW=0;
bit8_serial_input(ch);
ch=dat&0xf0;  //RS=1 RW=0;
bit8_serial_input(ch);
ch=dat<<4;
bit8_serial_input(ch);
P_RS=0;  //结束作业
}


/*
   写一个汉字到x,Y坐标处其中X为8列y为4行
   */
void  write_word(uint w)
{
data  uchar ch,cl;
ch=w>>8;
cl=w;
write_abyte(ch);
write_abyte(cl);
}
/*-------------------------------------------*/
/*-------------------------------------------*/
dis_hz_str(uchar x, uchar y, uchar *p_hz)
{
data uchar loc,*p;
data uint   xi;
code uchar xline[5]={0,1,3,2,4};
x=xline[x];
instruct(home);
loc=((x<<3)+y-9)|0x80;
instruct(loc); p=p_hz;
xi=((*p++)<<8)|*p++;
while(xi){
     write_word(xi);   //*p++;// );
     xi=((*p++)<<8)|*p++;   //*p++; xi=xi|*p++;
     }
}
/*
显示字母数字在x,y处
*/
void dis_str(uchar x, uchar y, uchar *str)
{
data uchar loc,*p;
code uchar xline[5]={0,1,3,2,4};
p=str;
x=xline[x];
instruct(home);
loc=(16*(x-1)+y-1);
if(loc&0x01){
       loc/=2;
       instruct(loc|0x80);
       //ch=' ';   //字符串前加空格(0x20 )
       write_abyte(' ');
       }else{
       loc/=2;
       instruct(loc|0x80);
       }
while(*p){
       write_abyte(*p++);
       }
//instruct(0x9e); write_abyte(' ');
}

void initial_screen(void)
{
data uchar i,j;
P0=0xff;
P_RS=0;           //串行--不选通
//P_PSB=0;
P_E=0;   //串行方式
instruct(f_set);    //0x30;     //8位并行接口 基本指令
for(i=0;i<30;i++){}
for(i=0;i<250;i++){}
for(i=0;i<250;i++){}
instruct(f_set);    //0x30;     //8位并行接口 基本指令
for(i=0;i<30;i++){}
for(i=0;i<250;i++){}
for(i=0;i<250;i++){}
instruct(dis_on);     //0x0f);     //开启屏幕 光标 闪烁
for(i=0;i<30;i++){}
instruct(clear);     //0x01);     //清屏幕 clear
for(j=0;j<10;j++){
   for(i=0;i<250;i++){}
   }
instruct(in_mode);     //0x06);    //光标右移、地址增1 方式
for(i=0;i<30;i++){}
instruct(home);
}
/*---------------------------------------------------------*/
/*---------------------------------------------------------*/
void itoa(int x,unsigned char *str2)
//void itoa(int x)
{
data unsigned int n;
idata unsigned char chn,*p,*p2,str1[5];
data unsigned char i,j;
p2=str2;
if(x<0){
	*p2++='-';
	x=-x;
	}
n=x;
p=&str1;
j=0;
while(n!=0){
	chn=n%10;
	n=n/10;
	*p++=chn+'0';
	j++;
	}
if(j!=0){
	p--;
	for(i=0;i<j;i++)*p2++=*p--;
	}
else    *p2++='0';
*p2=0;
return;
}
/*---------------------------------------------------------*/
void init(void)
{
/*
        实际用12MHZ晶振 BAUDRATE 4800
        */

/*TMOD=0x21;      /T1 use COM  T0 count  */
TR1=1;
SCON=0x50;   //f0;
TH1=0xf3;     /*f3 smod=1;  for 4800 baud at 12Mhz  C book pager 125 */
/*TH1=0x2e;  for 2400 baud at 11.059 C book pager 125 */
PCON=0x80;   /*4800 pcon=0x80    pc set as 18h */
TB8=1;
SM2=0;
SBUF=0;
/*r_in=SBUF;*/
REN=1;
RI=0;
ES=1;
//EA=1;
}
/*----------------------------------------------------*/
/*********************************************************/
//振铃计数 脉冲从P1.0(T2)引脚输入   1秒钟内如果计数大于M,则认为已经有 振铃信号输入
void init_t2(void)
{
T2CON=0x06;    //16位计数方式 从P1.0输入计数脉冲
TH2=0; TL2=0;
TR2=1;
}

/*---------------------------------------------------------*/
void initial(void)
{
TMOD=0X21;
TH0=0X3C; TL0=0Xa6;
PT1=1;
ET0=TR0=1;
//EA=1;
init();
}

/*********************************************************/
void t0int(void) interrupt 1 using 1
{
/*TH0=0X3C; TL0=0XB6; 50ms */
/*TH0=0xfc; TL0=0x1e;*/ /*1ms*/
/*TH0=0x3c; TL0=0xa6;*/
/*每次62.5MS=62500=F424H=(0BDCH)补码
        每16次中断为1秒
        */
TH0=0x0b; TL0=0xe0;  /*DC+4=E0H*/

/***********/
   T_50ms=1;T_key++;
   ms_timing++;
   if((ms_timing&0x10)!=0){
	ms_timing=0;second++;
        if((second%10)==0){
              time_n++;
              if(time_n>6)time_n=7;
              n_v=TL2; TL2=0; flag_n=1;
              }
	 second2++;  second3++;
	if(second>=60){
		second=0;minute++;minuter++;
		if(minute>=60){
			minute=0;hour++;//if(hour>=24)hour=0;
			}
		}
	}
}
/*********************************************************/
void t1int(void) interrupt 3 using 2
{
return;
}
/*----------------------------------------------------*/

/*interrupt [0x23] void SCON_int (void);     Serial Port */
    /* Serial Port */
void SCON_int(void) interrupt 4 using 3
{
//uchar status;
if(RI){
   chint=SBUF;
   flag_232C=1;
   RI=0;
   }
if(TI)TI=0;
}
/*---------------------------------------------------------*/
/*---------------------------------------*/

/*
	parameters is read from EEROM in the x25045

⌨️ 快捷键说明

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