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

📄 mainloop.c

📁 D12的驱动程序
💻 C
字号:
/*
   //*************************************************************************
   //
   //                  		ZLGMCU
   //				www.zlgmcu.com
   // File Name:	MAINLOOP.C
   // Revision:		V0.2(2002-04-08)
   // Use Library:	USB51S.LIB
   // Note:		USB51S.LIB不带DMA控制功能
   //*************************************************************************
   */

#include <stdio.h>
#include <string.h>
#include <reg51.h>                /* special function register declarations   */
#include "mainloop.h"
#include "isr.h"
#include "protozlg.h"
#include "absacc.h"
#define IN0 XBYTE[0xfef8]    //设置AD0809通道0的地址

#include "d12ci.h"
/*
//*************************************************************************
//  Public static data
//*************************************************************************
*/
unsigned char Data;
static unsigned int i_st;	//add by zjl

extern EPPFLAGS bEPPflags;
extern unsigned char idata GenEpBuf[];
extern unsigned char idata EpBuf[];
extern unsigned char xdata *ad_addr;
extern unsigned char xdata *da_addr;

//D12中断服务
usb_isr() interrupt 0
{
	DISABLE;
//	fn_usb_isr();//调用D12中断服务子程序
//----------------------code below copied from fn_usb_isr()---------
//	bEPPflags.bits.in_isr = 1;
	i_st = D12_ReadInterruptRegister();
	if(i_st != 0) 
		{

		if(i_st & D12_INT_ENDP2IN)	//	ep2_txdone();
			{
				D12_COMMAND = 0x45;		//clear last treat status
				D12_COMMAND = 0x05;
				D12_COMMAND = 0xFA;		//enable buffer sothat command in will be respond
			}
		else if(i_st & D12_INT_ENDP2OUT)		ep2_rxdone();
		else if(i_st & D12_INT_ENDP0IN) 		ep0_txdone();
		else if(i_st & D12_INT_ENDP0OUT)		ep0_rxdone();
//		if(i_st & D12_INT_EOT)
//			dma_eot();
		else if(i_st & D12_INT_BUSRESET) 
				{
					bus_reset();
					bEPPflags.bits.bus_reset = 1;
				}
		else if(i_st & D12_INT_SUSPENDCHANGE)
			bEPPflags.bits.suspend = 1;
//		if(i_st & D12_INT_ENDP1IN)
//			ep1_txdone();
//		if(i_st & D12_INT_ENDP1OUT)
//			ep1_rxdone();
	}
	bEPPflags.bits.in_isr = 0;	
	ENABLE;
}
void inserv(void) interrupt 2
{

	Data=*ad_addr;		//在这里得到A/D转化的值
	D12_WriteEndpoint(3, 1, &Data);//数据写入D12缓冲区
		Data&=0xbf;
	P1=0xb0|Data;
				//设置为66,看PC机接收到数据是否符合。

}
void delay(int i){
	int a;
	for(a=0;a<=i;a++)  ;
}

void main(void)
{
//	unsigned char i;	

	P0 = 0xFF;//初始化I/O口
//	P1 = 0xAA;
//	P1 = 0x55;
//	delay(20000);
//	delay(20000);
	P1 = 0xFF;
	P2 = 0xFF;
	P3 = 0xFF;
	MCU_D12CS = 0x0;//gaidong
	D12SUSPD = 0;

	IT0 = 0;//初始化中断
	EX0 = 1;
	PX0 = 0;
	//IT1=0;//设置INT1低电平中断触发//gaidong
	//EX1=1;//允许外部中断1//gaidong
	EA =1;//允许使用片里ROM

	MCU_D12CS = 0x1;//gaidong
	MCU_D12CS =0x0 ;//gaidong
	D12_SetDMA(0x0);//gaidong
	bEPPflags.value = 0;

	//reconnect_USB();//联接USB总线
//	while(bEPPflags.bits.configuration==0)	
		reconnect_USB();
    
	/* Main program loop */

//	for(i=0;i<64;i++) EpBuf[i] = 1; //init EpBuf by zjl
//	D12_WriteEndpoint(5, 64, EpBuf);

	while( TRUE ){
		if(bEPPflags.bits.configuration)
		  check_key_LED();//连接正常,调用按键和LED控制处理
		  delay(10000);
	   	  usbserve();//USB服务数据处理
	} // Main Loop
}

void check_key_LED(void)
{
	static unsigned char c, last_key = 0x1,b;

/*	if(MCU_SWM0&MCU_SWM1)	c=0x40;
    
	else	c=0x80;*/
	

     c=(MCU_SWM0&MCU_SWM1) ;
	  
     
    if(c==1) {b=2;}
	else {b=16;}
 
	if (c != last_key) {
	
		D12_WriteEndpoint(3, 1, &b);//按键状态改变,发送信息给主机/	
	}
	last_key = c;

	if(bEPPflags.bits.ep1_rxdone) {
		DISABLE;//接收到主机发来的LED控制信息
		bEPPflags.bits.ep1_rxdone = 0;
		ENABLE;
		MCU_LED0 = !(GenEpBuf[3] & 0x1);//控制LED状态
		MCU_LED1 = !(GenEpBuf[3] & 0x2);
	}
}



⌨️ 快捷键说明

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