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

📄 frequsb.c

📁 单片机89c51对usb操作的全过程,初始化
💻 C
字号:

#include <AT89x51.h>
#include <Intrins.h>


#define enable  IE=IE|0x80;         //enable interrupt
#define disable IE=IE&0x7F;     	//ptrdisable interrupt 

#define rd      P3_2       			//USB100 model control pins
#define wr      P3_3
#define txe     P3_4
#define rxf     P3_5    

#define st1	P0_0       				//switch 1_4 pins
#define st2	P0_1
#define st3	P0_2
#define st4	P0_3

#define ld1	P0_4      				//led 1_4 pins
#define ld2	P0_5
#define ld3	P0_6
#define ld4	P0_7

unsigned char receive_byte;
unsigned char stdata;
unsigned char firstdata;
bit flag=0;

/*	
函数:	delay_ms(unsigned int x)
参数:	将要延时的ms数
功能:	延时xms,x的范围为1~65025
*/
void delay_ms(unsigned int x)
{
	unsigned int i,j;
	i=0;
	for(i=0;i<x;i++)
	{
		j=108;                                                                                                                                                                                                                                                                                                                               ;
		while(j--);
	}
}

/*	
函数:	initial()
参数:   无
功能: 	初始化各种参数
*/
void initial(void)
{
	txe=1;
	rxf=1;
	
	
	firstdata=0x00;      //the NO_ of the received data
	ld1=1;               //put off all of the light
	ld2=1;
	ld3=1;
	ld4=1;      
	   
	wr=0; 	             //set wr equal zero and rd equal 1 before write or read USB100 _         
	rd=1;  
	P1=0xff;
}

/*	
函数:	getfrompc()
参数:   无
功能: 	查询USB100端口看是否PC有数据送出,如有,则接收PC数据
*/
void getfrom_pc(void)
{
	if(rxf==0)	                  //the buffer of the USB100 have data
	{
		firstdata+=0x01;
		rd=0;
		receive_byte=P1;
		rd=1;
		flag=1;
	}
}

/*	
函数:	get_switch()
参数:   无
功能: 	轮循法扫描按键端口,如有键按下则将相应的按键值存入stdata中
*/       
void get_switch(void)
{
	if (st1==0)                       //switch 1 haveing been pushed 
	{       
		delay_ms(5);	              //elimanate shake
		if (st1==0)
		{
			stdata=0x01;
			flag=1;
			ld1=0;
			ld2=1;
			ld3=1;
			ld4=1;
			delay_ms(200);			  //waiting for the button shake
		}
	}   
   	if (st2==0)                       //switch 2 haveing been pushed  
	{       
		delay_ms(5);
		if (st2==0)
		{
			stdata=0x02;
			flag=1;
			ld1=1;
			ld2=0;
			ld3=1;
			ld4=1;
			delay_ms(200);
		}
	}   	 
	if (st3==0)                       //switch 3 haveing been pushed      
	{       
		delay_ms(5);
		if (st3==0)
		{
			stdata=0x03;
			flag=1;
			ld1=1;
			ld2=1;
			ld3=0;
			ld4=1;			
			delay_ms(200);
		}
	}   			   
	if (st4==0)                       //switch 4 haveing been pushed      
	{       
		delay_ms(5);
		if (st4==0)
		{
			stdata=0x04;
			flag=1;
			ld1=1;
			ld2=1;
			ld3=1;
			ld4=0;			
			delay_ms(200);
		}
	} 
}    

 /*	
函数:	puton_led()
参数:   无
功能: 	根据接收到的PC数据点亮相应的LED
*/
void puton_led(void)
{
	if (firstdata==0x01)
	{  	
	      	if(receive_byte==0x80)
		{
			if(txe==0)
			{                                
				wr=1;
				P1=0x80;
				wr=0;
			}
			P1=0xff;
			firstdata=0x00;
		} 
		else
		{
			if (receive_byte!=0xff)    //judge the first data whether it is FFH_
			{
				firstdata=0x00;	       //if not then don't receive the second data 
			}
		}
	}       
	if (firstdata==0x02)              //if yes then receive the second data
	{
		firstdata=0x00;
		if (receive_byte==0xf0)
		{
			ld1=1;            //if the value equel f0H then put off all of the led  
			ld2=1;
			ld3=1;
			ld4=1;
		}
		if (receive_byte==0x01)     
		{
			ld2=1;            //if the value equel 01H then put on the led1
			ld3=1;
			ld4=1;
			ld1=0;              
		}		
		if (receive_byte==0x02)
		{
			ld1=1;            //if the value equel 02H then put on the led2
			ld3=1;
			ld4=1;
			ld2=0;
		}				
		if (receive_byte==0x03)
		{
			ld1=1;            //if the value equel 03H then put on the led3
			ld2=1;
			ld4=1;
			ld3=0;
		}	   
		if (receive_byte==0x04)
		{
			ld1=1;           //if the value equel 04H then put on the led4
			ld2=1;
			ld3=1;
			ld4=0;
		}  
		if (receive_byte==0x0f)
		{
			ld1=0;          //if the value equel 0fH then put on all of the led
			ld2=0;
			ld3=0;
			ld4=0;
		}		

	}
}

 /*	
函数:	sendto_pc()
参数:   无
功能: 	将接收到的按键数据送入PC中
*/ 
void sendto_pc(void)
{
		if(txe==0)
		{                                
			wr=1;
			P1=0xff;
			wr=0;
			wr=1;
			P1=stdata;
			wr=0;
		}	
}

void main()
{       
	initial();
	while(1)
	{         
		while(flag==0)
		{
			getfrom_pc();		//scan the USB and switch port
	        get_switch();
	  	}
	  	flag=0;
	  	if(receive_byte!=0)		//if there have received a valid data from pc 
	  	{
			puton_led();		//then put on the led 
			receive_byte=0;
		}
		if (stdata!=0)	 		//if there have a switch haveing been pushed 
		{	         			//then send its value to the computer
			sendto_pc();                                         
			stdata=0;
			P1=0xff;
		}		
	}
}    

⌨️ 快捷键说明

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