1 #define ALLOCATE_EXTERN // Allocate explicit register addresses here
  2 
  3 #include "Fx2regs.h"

  4 #include "Fx2sdly.h"
  5 
  6 #define BLOCKSIZE 64
  7 

  8 unsigned char time1_L;
  9 unsigned char time1_H;

 10 unsigned char time2_L;
 11 unsigned char time2_H;

 12 unsigned char old_time1_L;
 13 unsigned char old_time1_H;

 14 unsigned char old_time2_L;
 15 unsigned char old_time2_H;

 16 
 17 void EZUSB_Delay1ms (void)
 18 {

 19 	_asm
 20 
 21 	mov	A, #0           

 22 	mov _DPS,A   
 23 	mov DPTR,#(0xffff-602)

 24 	mov R4,#5
 25 
 26 	loop:           

 27 	inc     DPTR            
 28 	mov     A,DPL           
 29 	orl     A,DPH           

 30 	jnz     loop                      
 31 
 32 	er_end:     
 33 	ret

 34 
 35 	_endasm;
 36 
 37 }

 38 
 39 void EZUSB_Delay(WORD ms)
 40 {

 41 
 42 	//
 43 	// Adjust the delay based on the CPU clock

 44 	// EZUSB_Delay1ms() assumes a 24MHz clock

 45 	//
 46 	if ((CPUCS & bmCLKSPD) == 0)			  // 12Mhz

 47 		ms = (ms + 1) / 2;					   // Round up before dividing so we can accept 1.

 48 	else if ((CPUCS & bmCLKSPD) == bmCLKSPD1)	// 48Mhz

 49 		ms = ms * 2;
 50 

 51 	while (ms--)
 52 		EZUSB_Delay1ms();

 53 }
 54 
 55 void init_fx2 (void)

 56 {
 57 	CPUCS = bmCLKSPD1; // CPU runs @ 48 MHz

 58 
 59 	SYNCDELAY;
 60 	REVCTL = 0x03; // is this needed?

 61 	SYNCDELAY;
 62 
 63 	EP1OUTCS &= ~bmEPSTALL; //reset stall bit on EP1 OUT 

 64 	EP1OUTBC = 0x40;	//re-arm for EP1 out transfer

 65 
 66 	EP1INCS &= ~bmEPSTALL;
 67 

 68 	IFCONFIG=0xC0;
 69 	PORTACFG=0x00;
 70 	PORTECFG=0x00;

 71 
 72 	OEA=0x01;
 73 	IOA=0x01;

 74 
 75 	OEE=0x02;
 76 	IOE=0x02;

 77 }
 78 
 79 /*------------------------------------------------
 80 MAIN C Function
 81 ------------------------------------------------*/

 82 
 83 void main (void)
 84 {

 85 	time1_L = 250;
 86 	time1_H = 0;

 87 	time2_L = 250;
 88 	time2_H = 0;

 89 
 90 	old_time1_L = 0;
 91 	old_time1_H = 0;

 92 	old_time2_L = 0;
 93 	old_time2_H = 0;

 94 
 95 	init_fx2();
 96 
 97 	while (1)

 98 	{
 99 		if (!(EP1OUTCS & bmEPBUSY))  //check for data available in EP1 out

100 		{
101 			if (EP1OUTBUF[0] == 1) //set time 1

102 			{
103 				time1_L = EP1OUTBUF[1];
104 				time1_H = EP1OUTBUF[2];

105 				// time1 = EP1OUTBUF[1] + (EP1OUTBUF[2] << 8);

106 			}
107 			if (EP1OUTBUF[0] == 2) //set time 2

108 			{
109 				time2_L = EP1OUTBUF[1];
110 				time2_H = EP1OUTBUF[2];	

111 				// time2 = EP1OUTBUF[1] + (EP1OUTBUF[2] << 8);

112 			}
113 			EP1OUTCS &= ~bmEPSTALL; //reset stall bit on EP1 OUT

114 			EP1OUTBC = 0x40;	//re-arm for EP1 out transfer

115 		}
116 
117 		if (!(EP1INCS & bmEPBUSY))

118 		{
119 			if ((time1_L != old_time1_L) || (time1_H != old_time1_H))

120 			{
121 				EP1INBUF[0] = time1_L;
122 				EP1INBUF[1] = time1_H;

123 				EP1INBUF[2] = time2_L;
124 				EP1INBUF[3] = time2_H;

125 				EP1INCS &= ~bmEPSTALL;
126 				EP1INBC = 0x04; // 4 bytes to transfer, initates send on EP1 IN

127 				old_time1_L = time1_L;
128 				old_time1_H = time1_H;

129 				old_time2_L = time2_L;
130 				old_time2_H = time2_H;

131 			}
132 			else if ((time2_L != old_time2_L) || (time2_H != old_time2_H))

133 			{
134 				EP1INBUF[0] = time1_L;
135 				EP1INBUF[1] = time1_H;

136 				EP1INBUF[2] = time2_L;
137 				EP1INBUF[3] = time2_H;

138 				EP1INCS &= ~bmEPSTALL;
139 				EP1INBC = 0x04; // 4 bytes to transfer, initates send on EP1 IN

140 				old_time1_L = time1_L;
141 				old_time1_H = time1_H;

142 				old_time2_L = time2_L;
143 				old_time2_H = time2_H;

144 			}
145 		}
146 
147 		IOA = 0x01;

148 		IOE = 0x02;
149 		EZUSB_Delay(time1_L + (time1_H << 8));

150 		IOA = 0x00;
151 		IOE = 0x00; 

152 		EZUSB_Delay(time2_L + (time2_H << 8));   

153 	}
154 }


syntax highlighted by Code2HTML, v. 0.9.1