Created
November 4, 2011 02:30
-
-
Save kevinmehall/1338529 to your computer and use it in GitHub Desktop.
Quadrature Encoder with the Xmega event system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "usb.h" | |
// PORT C | |
#define QDEC1 (1<<2) | |
#define QDEC2 (1<<3) | |
int main(void){ | |
sei(); | |
USB_ConfigureClock(); | |
USB_Init(); | |
PORTC.DIRSET = PWM1 | PWM2 | (1<<4); | |
PORTC.DIRCLR = QDEC1 | QDEC2; | |
PORTC.PIN1CTRL = PORT_INVEN_bm; | |
//Set-up quadrature decoding; see p. 75 | |
PORTC.PIN2CTRL = PORT_ISC_LEVEL_gc | PORT_OPC_PULLUP_gc; | |
PORTC.PIN3CTRL = PORT_ISC_LEVEL_gc | PORT_OPC_PULLUP_gc; | |
EVSYS.CH0MUX = EVSYS_CHMUX_PORTC_PIN2_gc; //P. 77 | |
EVSYS.CH0CTRL = EVSYS_QDEN_bm | EVSYS_DIGFILT_2SAMPLES_gc; //p. 78 | |
TCC1.CTRLD = TC_EVACT_QDEC_gc | TC_EVSEL_CH0_gc; //pp. 180-1 | |
TCC1.PER = 4799; //set period register of the timer/counter to numLines*4 -1 == 4799 | |
TCC1_CTRLA=TC_CLKSEL_DIV1_gc; | |
TCC1.CNT = 0; | |
while (1){ | |
USB_Task(); | |
} | |
} | |
/** Event handler for the library USB Control Request reception event. */ | |
bool EVENT_USB_Device_ControlRequest(USB_Request_Header_t* req){ | |
if ((req->bmRequestType & CONTROL_REQTYPE_TYPE) == REQTYPE_VENDOR){ | |
if (req->bRequest == 0x10){ | |
*((uint16_t*) ep0_buf_in) = TCC1.CNT; | |
USB_ep0_send(2); | |
return true; | |
}else if (req->bRequest == 0xBB){ | |
USB_ep0_send(0); | |
USB_ep0_wait_for_complete(); | |
_delay_us(10000); | |
USB_Detach(); | |
_delay_us(100000); | |
void (*enter_bootloader)(void) = (void *) 0x47fc /*0x8ff8/2*/; | |
enter_bootloader(); | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment