📄 main.c
字号:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include <util/delay.h> /* for _delay_ms() */
#include "oddebug.h" /* This is also an example for using debug macros */
#include "usbdrv.h"
#define SET_LED_RED 1
#define SET_LED_GREEN 2
#define GET_DATA 3
#define START 4
#define Set_Bit(val, bitn) (val |=(1<<(bitn)))
#define Clr_Bit(val, bitn) (val&=~(1<<(bitn)))
#define Get_Bit(val, bitn) (val &(1<<(bitn)) )
#define Rsv_Bit(val, bitn) (val ^=(1<<(bitn)))
#define ledGreenOn() PORTD &= ~(1 << PD3)
#define ledGreenOff() PORTD |= (1 << PD3)
#define ledGreenRsv() PORTD ^= (1 << PD3)
#define ledRedOn() PORTD &= ~(1 << PD4)
#define ledRedOff() PORTD |= (1 << PD4)
#define ledRedRsv() PORTD ^= (1 << PD4)
#define IRBUFSIZE 100
uchar start,prt,last;
uchar irbuf[IRBUFSIZE];
PROGMEM char usbDescriptorConfiguration[25] = { /* USB configuration descriptor */
9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
USBDESCR_CONFIG, /* descriptor type */
18 + 7, 0,
/* total length of data returned (including inlined descriptors) */
1, /* number of interfaces in this configuration */
1, /* index of this configuration */
0, /* configuration name string index */
(char)USBATTR_BUSPOWER, /* attributes */
USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
/* interface descriptor follows inline: */
9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
USBDESCR_INTERFACE, /* descriptor type */
0, /* index of this interface */
0, /* alternate setting for this interface */
2, /* endpoints excl 0: number of endpoint descriptors to follow */
USB_CFG_INTERFACE_CLASS,
USB_CFG_INTERFACE_SUBCLASS,
USB_CFG_INTERFACE_PROTOCOL,
0, /* string index for interface */
/* endpoint descriptor for endpoint 1 */
7, /* sizeof(usbDescrEndpoint) */
USBDESCR_ENDPOINT, /* descriptor type = endpoint */
(char)0x81, /* IN endpoint number 1 */
0x03, /* attrib: Interrupt endpoint */
8, 0, /* maximum packet size */
USB_CFG_INTR_POLL_INTERVAL, /* in ms */
};
unsigned char usbFunctionSetup(unsigned char data[8])
{
uchar len = 0;
usbRequest_t *rq = (void *)data;
switch(rq->bRequest)
{
case SET_LED_RED:
if(rq->wValue.bytes[0]==0)
ledRedOff();
else
ledRedOn();
break;
case SET_LED_GREEN:
if(rq->wValue.bytes[0]==0)
ledGreenOff();
else
ledGreenOn();
break;
case START:
start=rq->wValue.bytes[0];
if(start)
{
ledRedOn();
prt=0;
last=1<<(PC2);
}
else ledRedOff();
break;
case GET_DATA:
len=0xff;
ledRedRsv();
break;
default:
break;
}
return len;
}
//==================================================
//定时器T0初始化
void timer0_init(void)
{
TCCR0 = 0x00;//停止定时器
TCNT0 = 0x00;//初始值定时时间15MS
TIMSK=TIMSK & (~(1<<TOIE0)); //禁止产生中断
TIFR=TIFR | (1<<TOV0); //清除中断标志
TCCR0 = 0x05;//启动定时器1024分频
}
//端口初始化
void port_init(void)
{
PORTD = 0;
PORTB = 0; /* no pullups on USB and ISP pins */
DDRD = ~(1 << 2); /* all outputs except PD2 = INT0 */
DDRB = 0; /* all USB and ISP pins inputs */
}
int main(void)
{
uchar i;
cli(); //禁止所有中断
port_init();
wdt_enable(WDTO_1S);
DBG1(0x00, 0, 0); /* debug output: main starts */
start=0;
prt=0;
last=1<<2;
usbInit();
usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
i = 0;
while(--i){ /* fake USB disconnect for > 250 ms */
wdt_reset();
_delay_ms(1);
}
usbDeviceConnect();
ledRedOff();
ledGreenOff();
sei();
for(;;)
{ /* main event loop */
usbPoll();
wdt_reset();
if(start==1)
{
uchar t=Get_Bit(PINC,2);
if(t!=last)
{
irbuf[prt]=TCNT0;
timer0_init();
last=t;
prt++;
}
if(prt==IRBUFSIZE||(Get_Bit(TIFR,TOV0)&&prt>3))
{
i=0;
start=2;
ledRedOff();
ledGreenOn();
timer0_init();
TCCR0=0;
prt--;
}
}
if(start==2)
{
if(usbInterruptIsReady())
{
if(i>=prt)
{
usbSetInterrupt((void *)&irbuf[i],prt+8-i);
start=1;
prt=0;
last=1<<(PC2);
ledRedOn();
ledGreenOff();
}
else
usbSetInterrupt((void *)&irbuf[i], 8);
i+=8;
}
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -