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

📄 isd4004.c

📁 这个是基于单片机的对于频率计的设计。可以输出不同的频率
💻 C
字号:
//-----------------------------------------------------------------------------
// F00x_Ports_SwitchLED.c
//-----------------------------------------------------------------------------
// Copyright 2005 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program demonstrates how to configure port pins as digital inputs
// and outputs.  The C8051F005 target board has one push-button switch 
// connected to a port pin and one LED.  The program constantly checks the 
// status of the switch and if it is pushed, it turns on the LED.
//
//
// How To Test:
//
// 1) Download code to a 'F005 target board
// 2) Ensure that the J1 and J3 headers are shorted
// 3) Push the button (P1.7) and see that the LED turns on 
//
//
// FID:            00X000002
// Target:         C8051F00x
// Tool chain:     Keil C51 7.50 / Keil EVAL C51
// Command Line:   None
//
// Release 1.0
//    -Initial Revision (GP)
//    -15 NOV 2005
//

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <c8051f000.h>                 // SFR declarations

#define SYSCLK             11059200  



sbit CS=P0^7;
sbit KEY4=P3^7;


sbit rd     = P1^7;

sbit wr		= P1^6;
sbit addr0	= P1^0;
sbit addr1	= P1^1;
sbit addr2	= P1^2;
sbit addr3	= P1^3;
sbit addr4	= P1^4;

sbit data0	= P2^0;
sbit data1	= P2^1;
sbit data2	= P2^2;
sbit data3	= P2^3;


void write (int addr, int datain);
unsigned int read();


unsigned int Count1msInc;
unsigned char K;
unsigned int addrl,addrh;

unsigned int adl[ ]={0x00,0x96,0x2c,0xc2,0x58,0xee,0x84,0x1a,0xb0,0x46,0xdc,0x72,0x08,0x9e};//,0x34};//,0xca};  // 1s=5h
unsigned int adh[ ]={0x00,0x00,0x01,0x01,0x02,0x02,0x03,0x04,0x04,0x05,0x05,0x06,0x07,0x07};//,0x08};//,0x08};



exchg();
BELL1();
BELL2();
//RECORD();
PLAY0000H();
RECORD(unsigned int addrl,unsigned int addrh,unsigned char time);
PLAY0096H(unsigned int addrl,unsigned int addrh,unsigned char time);



DELAY(unsigned int delay)
{
	unsigned int J;
	for(J=0;J<delay;J++);
}

void Timer0_Init(void )
{
	CKCON|=0x8;
	//TMOD|=0x1;
	TR0=0;
	TH0=(-SYSCLK/1000)>>8;
	TL0=-SYSCLK/1000;
	IE|=0x2;
}

void Timer0_ISR(void ) interrupt 1   //中断
{
	TH0=(-SYSCLK/1000)>>8;
	TL0=-SYSCLK/1000;
	Count1msInc++;
}


void Delay_ms(unsigned char Val_ms)

{
	Count1msInc=0;
	TR0=1;
	while((Count1msInc<Val_ms)==1);	
	TR0=0;
}

void Delay_s(unsigned char Val_s)
{
	unsigned int temp,count_s;
	temp=Val_s*1000;
	Count1msInc=0;
	TR0=1;
	while((Count1msInc<temp)==1) 
	{
		count_s=Count1msInc/1000;
		write(1,count_s%10);
		write(2,count_s/10);
	}	
	TR0=0;
}

ON_4004()
{
	CS=0;
	DELAY(50000);

}

OFF_4004()
{
	DELAY(50000);
	CS=1;
}

void SPIWrOne(unsigned char ch)
{
	ON_4004();
	SPIF = 0;
	K=ch;
	exchg();
	SPI0DAT=K;
	while(SPIF==0);
	OFF_4004();
}

void SPIWrTwo(unsigned char ch)
{
	SPIF = 0;
	K=ch;
	exchg();
	SPI0DAT=K;
	while(SPIF==0);
}

void SPIWrThree(unsigned char addrl,unsigned char addrh,unsigned char COM)
{
	ON_4004();
	SPIWrTwo(addrl);
	SPIWrTwo(addrh);
	SPIWrTwo(COM);
	OFF_4004();

}

void SYSCLK_Init(void)
{
   OSCICN |= 0x03;                     
   OSCXCN=0xff;  
}

void PORT_Init()
{
   PRT1CF  |= 0xff;                     
   PRT2CF  |= 0xff;                     

   PRT0CF  |= 0xff;                     
	XBR0=0x02;
	XBR1=0x00;
	XBR2=0x40;
}

void SPI0_Init()
{
	SPI0CFG=0x07;
	SPI0CFG|=0x0C0;
	SPI0CN=0x03;
	SPI0CKR=SYSCLK/2/100000-1;
}

exchg()
{
	unsigned char temp;
	//*
	temp=K%2; 		temp=temp<<1;
	temp+=(K>>1) %2;	temp=temp<<1;
	temp+=(K>>2) %2;	temp=temp<<1;
	temp+=(K>>3) %2;	temp=temp<<1;
	temp+=(K>>4) %2;	temp=temp<<1;
	temp+=(K>>5) %2;	temp=temp<<1;
	temp+=(K>>6) %2;	temp=temp<<1;
	temp+=(K>>7) %2;	

	K=temp;
}

BELL1()
{
	write(8,1);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	write(8,0);
}

BELL2()
{
	write(8,1);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	write(8,0);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	write(8,1);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	DELAY(65000);
	write(8,0);
}


RECORD(unsigned int addrl,unsigned int addrh,unsigned char time)
{
    
	BELL1();
	SPIF=0;
	
	SPIWrOne(0x20);
	DELAY(10000);
	EA=1;
	SPIWrOne(0x20);
	DELAY(10000);
	//BELL2();

	SPIWrThree(addrl,addrh,0x0a0);
    

	SPIWrOne(0xb0);
	//Delay_s(10);
	Delay_s(time);
    
	SPIWrOne(0x30);
	BELL1();
	

}

PLAY0096H(unsigned int addrl,unsigned int addrh,unsigned char time)
{
    

	SPIWrOne(0x20);
	DELAY(60000);
	SPIWrOne(0x20);
	DELAY(60000);
	BELL2();
	SPIWrThree(addrl,addrh,0x0E0);

	SPIWrOne(0xf0);
	//Delay_s(10);
	Delay_s(time);
	
	SPIWrOne(0x30);
	SPIWrOne(0x10);
	DELAY(1000);
	BELL1();
	
	
}

/*RECORD1(int addrl,int addrh)
{
    int pl,ph;
	pl=addrl;
	ph=addrh;
	BELL1();
	SPIF=0;
	//write(1,2);
	SPIWrOne(0x20);
	DELAY(10000);
	EA=1;
	SPIWrOne(0x20);
	DELAY(10000);
	//BELL2();
	//write(2,3);
	//SPIWrThree(addrl,addrh,0x0a0);
    SPIWrThree(pl,ph,0x0a0);

	SPIWrOne(0xb0);
	//Delay_s(30);
	Delay_s(4);
     //Delay_s(100);
	SPIWrOne(0x30);
	BELL1();
	//write(0,13);
	//flag=2;
}*/

/*PLAY0000H()
{
	SPIWrOne(0x20);
	DELAY(60000);
	SPIWrOne(0x20);
	DELAY(60000);
	BELL2();
	SPIWrThree(0x08,0xea,0x0E0);

	SPIWrOne(0xf0);
	Delay_s(30);
	//Delay_s(10);
	//Delay_s(100);
	SPIWrOne(0x30);
	SPIWrOne(0x10);
	DELAY(1000);
	BELL1(); 
	//write(3,1);
	
}*/



void main(void)
{
	unsigned int temp1,temp2,xor1,wei1,wei2,temp_wei;
	unsigned int pl,ph;
	unsigned int templ,temph;
	unsigned int k;
	unsigned int flag=0;
    unsigned int i,i1,j,n;

	WDTCN=0xde;
	WDTCN=0xad;
	SYSCLK_Init();
	PORT_Init();
	SPI0_Init();
	Timer0_Init();
	EA=1;
	write(8,0);
	write(0,15);	
	write(1,0);
	write(2,0);
	write(3,0);
	write(4,0);
	write(5,0);
	write(6,0);
	flag=0;
    temp2=read();

	while(1)
	{
	    temp1=read();
		if (temp1!=temp2) 
		{
    		xor1=temp1^temp2;
			DELAY(10);
    		temp2=read();
    		if (temp1==temp2) 
			{   
	    		wei1=xor1%2;
				temp_wei=temp2;
	    		if(wei1!=0)
				{
		   			wei2=temp_wei%2;
		   			if(wei2==0) 
                               { if(flag==0)
							      {
								   for(j=1;j<=i;j++)        // 录音全播放
								    {	
									 write(0,14);
									 write(3,j);
									 write(4,0);
									  
									 if(j>=10) {write(3,j%10); write(4,j/10);}
								     //if (j==17) {j=1;write(3,1); write(4,0);}
									 if (j==16) {j=1;write(3,1); write(4,0);}	 
									 pl=adl[j-1];
									 ph=adh[j-1]; 
									       
									 Delay_s(3);
									 PLAY0096H(pl,ph,10);
									 if(j==i) PLAY0096H(0x0a,0x09,4); //放音结束通知
									 }
									}
								  if(flag==1)// 选择段播放录音
								      {
									  Delay_s(3);
                                      PLAY0096H(pl,ph,10);    
								      flag=0;
                                      }
									 write(0,15);
									 
								}
				}
				xor1>>=1;
	    		wei1=xor1%2;
	    		if(wei1!=0)
				{
           			temp_wei>>=1;
		   			wei2=temp_wei%2;
		   			if(wei2==0)  
                                 { 
                                     write(0,13);
									 if(flag==0) // 正常录音
									 {
									 
									 i++;
									 i1++;
                                     write(3,i);
									 write(4,0);

									 if(i1==16) {i1=0;PLAY0096H(0xea,0x08,4);} //空间存储满通知
									  else {
									       PLAY0096H(0xca,0x08,5); // 录音开始通知
									       pl=adl[i-1];
									       ph=adh[i-1];
								     
									       Delay_s(1);
									       RECORD(pl,ph,10);
									  	   }
									 
                         
								    if(i>=10) {write(3,i%10); write(4,i/10);}
								    if (i==16) {i=0;write(3,i%10); write(4,0);}	  
									
									
									 write(0,15);
									  
									 }
									if(flag==1) //选择段录音
                                       {
									   write(0,13);									   
									   
									   templ=pl;
									   temph=ph;   
									   
									     
	                         	       Delay_s(3);
                                       RECORD(templ,temph,10);
                                     for(k=n-1;k<i;k++)  //排序
                                      { 
									    adl[k]=adl[k+1];
									    adh[k]=adh[k+1];
                                       
									    adl[i]=templ;
									    adh[i]=temph;
									   }                                 									   
									   write(0,15);
									   flag=0; 
    									}
										
                                    
								    }
				 }
                xor1>>=1;
	    		wei1=xor1%2;
	    		if(wei1!=0)
				{
           			temp_wei>>=2;
		   			wei2=temp_wei%2;
		   			if(wei2==0)  {	   
                                      if(flag==1)
									  {    
									   	write(0,11);
                                     for(k=n-1;k<i;k++)  //排序//段删除
                                      { 
									    adl[k]=adl[k+1];
									    adh[k]=adh[k+1];
                         
										//adl[i]=0x34;
										//adh[i]=0x08;
										i=i-1;
										if(i<=1)i=1;
									   }
									   //write(0,15);                                 									   									   
									   flag=0; 
    								  }	
								 //Delay_s(3);
							    //RECORD(0x0a,0x09,4);
								//PLAY0096H(0x0a,0x09,4);
								//PLAY0096H(0x34,0x08,30);
										                             
								 } 
                 }
                xor1>>=1;
	    		wei1=xor1%2;
	    		if(wei1!=0)
				{
           			temp_wei>>=3;
		   			wei2=temp_wei%2;
		   			if(wei2==0) { 
					             n++;    // 段选				  
                                 write(5,n);
                                 write(6,0);
								 if(n>=10) {write(5,n%10); write(6,n/10);}
								 if (n==16) {n=1;write(5,1); write(6,0);}    
								 pl=adl[n-1];		            	                            
								 ph=adh[n-1];
								 	 
								 flag=1;	                                                   
                                 write(0,7);
								 
					              } 	     
				}
			}
		} 

	}//end while
}// end main		
void write (int addr,int datain)
{
    char pp;
    PRT2CF  |= 0xff; 
    pp=addr;
	pp|= 0xA0;
	P1 =pp;
 
	P2=datain;
	wr=1;	
	wr=0;

}

unsigned int read()
{
   unsigned int data_rd;
   PRT2CF  &= 0xf0; 
   P2=0xff;
   rd=0;
   data_rd=P2;
   data_rd &=0x0f;

   rd=1;
   PRT2CF  |= 0xff; 

   return data_rd;

}


/*addrl
     0x00,0x96,0x2c,0xc2,0x58,0xee,0x84,0x1a
     0xb0,0x46,0xdc,0x72,0x08,0x9e,0x34,0xca,0x60

addrh
     0x00,0x00,0x01,0x01,0x02,0x02,0x03,0x04
	 0x04,0x05,0x05,0x06,0x07,0x07,0x08,0x08,0x09*/

⌨️ 快捷键说明

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