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

📄 fm.c

📁 C51写的Zi2020 FM发射模块控制代码!
💻 C
字号:
#include <stdio.h>
#include <intrins.h>
#include <math.h>
#include <reg52.h>

#define CHIPADD_W 0xa0  	 	
#define CHIPADD_R 0xa1
#define CHIPFM_W  0x6c              
#define CHIPFM_R  0x6D
#define CH_LOW   75.9

sfr  P4  = 0xe8;
sbit P43 = P4^3;
sbit P42 = P4^2;
sbit P41 = P4^1;
sbit P40 = P4^0;

sbit  DOTt=P4^1;

sbit  SDA=P3^5;
sbit  SCL=P3^6;

//sbit  SDA=P3^7;    		
//sbit  SCL=P3^6;

sbit  SW=P2^4;
sbit  RSTB=P2^3;

sbit  seg3=P0^4;
sbit  seg2=P0^5;
sbit  seg1=P0^6;           
sbit  seg0=P0^7;

// for key press
sbit  key_up=P3^2;
sbit  key_down=P3^3;
sbit  key_pow=P3^4;
sbit  key_sleep=P3^5;
bit 	bPressKey = 0;					
bit     chpo=0;                         
bit 	bWriteChip = 0;

unsigned int  	channel = 1;            
unsigned int    channel_high=184;
unsigned int    channel_low=1;
unsigned char    power=5;             
unsigned char  	iLed = 0;
unsigned char  	LedCount[4] = {0xff,0xff,0xff,0xff};


unsigned char	    ucKey = -1;
unsigned char	    ucTempKey = 0;
unsigned char   	ucDelayCount = 0;
unsigned int 		counter=0;       				

void SOMENOP(void)
{
  _nop_ ();_nop_ ();

}

void delay(unsigned char ucMs)
{
	int i,j;
	for(i=0;i<ucMs;i++)
	{
		for(j=0;j<100;j++)
		 SOMENOP();
	}
}


void display(unsigned int ucData,unsigned char ucType)		
{
	char i;
	unsigned char ucTemp;
		switch(ucType)
		{
			case 0:	//dec
				LedCount [0] =ucData%10;
				LedCount [1] =ucData/10-ucData/100*10;
				LedCount [2] =ucData/100-ucData/1000*10;
				LedCount [3] =ucData/1000;
			break;
			case 1:	//hex
				LedCount [0] = ucData/16;
				LedCount [1] = ucData%16;
				LedCount [3] = ucData%256;
				LedCount [4] = ucData%4096;
			break;
			case 2:	//str
			break;
			case 3:
				LedCount [0] = 0xff;
				LedCount [1] = 0xff;
				LedCount [2] = 0xff;
				LedCount [3] = 0xff;
			break;
		}

		for(i=0;i<4;i++)
		{
			ucTemp = LedCount[i];
			switch(ucTemp)
			{
				case 0:
					ucTemp =0x7E;
				break;
				case 1:	
					ucTemp =0x18;
				break;
				case 2:
					ucTemp =0x6D;
				break;
				case 3:
					ucTemp =0x3D;	
				break;
				case 4:	
					ucTemp =0x1B;
				break;
				case 5:
					ucTemp =0x37;
				break;
				case 6:	
					ucTemp =0X77;
				break;
				case 7:	
					ucTemp =0x1C;
				break;
				case 8:
					ucTemp =0x7F;
				break;
				case 9:
					ucTemp =0x3F;
				break;
				case 'A':
					ucTemp =0x5F;
				break;
				case 'E':
					ucTemp =0x67;
				break;
				case 'U':
					ucTemp =0x7A;
				break;
				default:
					ucTemp = 0xff;
				break;
			}
			LedCount[i] = ucTemp;	
			
		}

}


void timer0 (void) interrupt 1  
{ 
	//char i;
	unsigned char ucDigCode,ucKeyPress,uctemp;

	TH0=0xfa;	  //4.25ms interval
//	TL0=0x91;	
	TL0=0XFF;			
	if(counter<40000)
	{
			counter++;
		   

			seg0=1;		
		 	seg1=1;
			seg2=1;
			seg3=1;
			DOTt=0;
   
	  	if(iLed >= 4)
				iLed = 0;
			
			ucDigCode = LedCount[iLed];
			
			P1=ucDigCode;                     
		
			switch(iLed)
				{
					case 0:
					 
						seg0 = 0;						
					break;
					case 1:
					    if(chpo==1)
						seg1 = 1;
						else
						{
						seg1 = 0;					
					   	DOTt=1;
						}
					break;
					case 2:
						if(chpo==1)
						seg2 = 1;
						else
						seg2 = 0;				
					break;
					case 3:
				       if(chpo==1)
						seg3 = 1;
						else
						seg3 = 0;
					break;
					default:
					break;
				}

			iLed++;
		
	}
			else
				{
				counter=40000;
          	 
				seg0=1;		 
		     		seg1=1;
				seg2=1;
				seg3=1;       
		
				}

    key_up=0;
  	key_down=0;
  	key_pow=0;
   	key_sleep=0;
	
    uctemp=P3;            			 
	ucKeyPress=uctemp | 0x0c3;            

	if(ucKeyPress !=0xc3)			
	{
		if(ucKeyPress == ucTempKey)
		{
			ucDelayCount++;
			if( ucDelayCount > 5 )
			{
				ucDelayCount = 0;
				ucKey = ucTempKey;
				bPressKey = 1;
			}
		}
		else
		{
			ucTempKey = ucKeyPress;
			ucKey = -1;
			ucDelayCount = 0;
		}
	}
	else							
	{
		if(ucDelayCount>4)			
		{
			ucKey = ucTempKey;
			bPressKey = 1;
			ucDelayCount = 0;
		}
		else
		{
			ucDelayCount = 0;
			ucKey = -1;
		}

	}

}

void starti2c(void)
{	
	 SCL=1;
 	 SOMENOP();
   	 SDA=1;
 	 SOMENOP();
	 SOMENOP();
 	 SDA=0;
 	 SOMENOP();
 	 SOMENOP();
 	 SCL=0;
 	 SOMENOP();
} 


void stop_i2c()
{  	
	SDA=0;
   	SOMENOP();
  	SCL=1;
   	SOMENOP();
   	SOMENOP();
   	SDA=1;
}
void mack(void)
{
	SCL=0;
	SOMENOP();
	SDA=0;
	SOMENOP();
	SCL=1;
	SOMENOP();
	SCL=0;
	SOMENOP();
	SDA=1;
	SOMENOP();
}

void sendbyte(int c)
{
 	int bitcn;
 	for(bitcn=0;bitcn<8;bitcn++)
 	{
   		if((c<<bitcn)&0x80)
   		SDA=1;
   		else SDA=0;
   		 SOMENOP();
  		SCL=1;
   		 SOMENOP();
   		 SOMENOP();
   		 SCL=0;
   		 SOMENOP();
  	}
}

unsigned char recbyte()
{
	unsigned char retc=0;
 	unsigned char bitcn;
 	SDA=1;
 	for(bitcn=0;bitcn<8;bitcn++)
  	{
  		 SOMENOP();
   		SCL=0;
   		 SOMENOP();
   		 SOMENOP();
   		SCL=1;
   		 SOMENOP();
   		retc=retc<<1;
   		if(SDA==1)
   		retc=retc+1;
   		 SOMENOP();
   	}
  	    SCL=0;
  	    SOMENOP();
  		return(retc);
 }

unsigned char read24c(char byteadd)
{  
   	unsigned char val;
 
   	starti2c();
   	sendbyte(CHIPADD_W);
   	mack();
   	sendbyte(byteadd);
   	mack();
   	starti2c();
 	sendbyte(CHIPADD_R);
   	mack();
   	val=recbyte();
   	stop_i2c();

   	return(val);
}

void writ24c(char byaddress,char wridat)
{
   	starti2c();
   	sendbyte(CHIPADD_W);
   	mack();
   	sendbyte(byaddress);
	mack();
   	sendbyte(wridat);
   	mack();
   	stop_i2c();
}

void readdata(void)
{

	unsigned char chanl,chanh;
	chanl=read24c(1);
	delay(200);
	chanh=read24c(0);
	delay(200);
	power=read24c(3);
	delay(200);
	channel=chanh*256+chanl;
}



void writedata(void)
{
	unsigned char senddata1=0,senddata2=0,senddata3=0;
	unsigned int temp=CH_LOW+channel;                     

    senddata1=temp&0x0ff;
	senddata2=((temp>>8)&0x07)+(((power-1)&0x03)*64);

	if(power>=5)
		senddata3=0x80;
	else 
		senddata3=0x00;  
  
	//----------------------------------
	starti2c();
   	sendbyte(CHIPFM_W);
   	mack();
   	sendbyte(0x00);
   	mack();
   	sendbyte(senddata1);
   	mack();
	stop_i2c();
	delay(20);
	//----------------------------------
	starti2c();
   	sendbyte(CHIPFM_W);
   	mack();
   	sendbyte(0x01);
   	mack();
   	sendbyte(senddata2);
   	mack();
	stop_i2c();
	delay(20);	
	//-----------------------------------
	starti2c();
   	sendbyte(CHIPFM_W);
   	mack();
   	sendbyte(0x13);
   	mack();
   	sendbyte(senddata3);
   	mack();
  	stop_i2c();
}

void displayled(void)
{
	display((chpo==0)?(channel+CH_LOW):(power),0);

}


void writdata24c(void)
{

	unsigned char chanl,chanh;
	unsigned int channel_temp;
	channel_temp=channel;
	if ( chpo==0)
		{
		  	chanl=channel_temp%256;
		    chanh=channel_temp/256;
	        writ24c(0, chanh);
			delay(30);
		  	writ24c(1, chanl);
		}
	else
   			writ24c(3,power);
   		    delay(200);
}

void judge(int drec)
{
	if(chpo==0)
	{		
		channel+=drec;
		if(channel>channel_high)
			channel=channel_low;
		if(channel<channel_low)
			channel=channel_high;
	}
	else
	{
		power+=drec;
		if(power>8)
			power=1;
		if(power<=0)
			power=8;
	}

}

void main(void)
{  

	unsigned char in=0;
    SDA = 0;
	SCL = 0;
	DOTt = 0;
	power = 3;
	EA=0;
	TMOD=0x01;
	TH0=0xfa;	 
	TL0=0x91;
	ET0=1;
   	TR0=1;
	delay(30);
	readdata();
	delay(100);
	writedata();
	EA=1;
	chpo=1;
	displayled();
	delay(80000);
	delay(80000);
	chpo=0;
	displayled();
    delay(400);  

while(1)
    {   
 		if((ucKey !=-1) && (bPressKey ==1))
		{	
			
		    if(counter>=4000)
			counter=0;
			else
			{
				counter=0;
				switch(ucKey)	
				{
					case 0xC7:		
					{
	
		             judge(1);
					displayled();
					delay(20);
					writdata24c();
					delay(10);
					writedata();
					delay(20);
					}
			
					break;

					case 0xCB:		
    	   				{
						judge(-1);
						displayled();
						writdata24c();
						delay(10);
						writedata();
						delay(20);
				
					}
					break;

					case 0xD3:		
					
						if(chpo==0)
						chpo=1;
						else
						chpo=0;
						displayled();
						delay(4000);
					break;
	
						case 0xE3:		
						SW=!SW;
					
					break;
					default:
					break;
					}
				}
			
			ucKey = -1;
			bPressKey = 0;
			}
          
	 }

}	
 


⌨️ 快捷键说明

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