Rev 24 | Rev 27 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#include <avr/io.h>#include <avr/pgmspace.h>#include <avr/interrupt.h>#define F_CPU 12000000#include <util/delay.h>#include <avr/wdt.h>#include <usbdrv.h>#include <stdlib.h>#include <string.h>#include "config.h"#include "hiddesc.h"void doInt(void);uint8_t getKey(void);volatile uint8_t pcIntCurr = 0;volatile uint8_t pcIntLast = 0;volatile uint8_t pcIntMask = 0;volatile uint8_t rot_stat = 0;volatile uint8_t rot_sent = 0;struct{union {uint8_t data[5]; // Rotariesstruct {uint8_t rx:8;uint8_t ry:8;uint8_t b00:1;uint8_t b01:1;uint8_t b02:1;uint8_t b03:1;uint8_t b04:1;uint8_t b05:1;uint8_t b06:1;uint8_t b07:1;uint8_t b08:1;uint8_t b09:1;uint8_t b10:1;uint8_t b11:1;uint8_t b12:1;uint8_t b13:1;uint8_t b14:1;uint8_t b15:1;};};} report;usbMsgLen_t usbFunctionSetup(uchar data[8]) {usbRequest_t *rq = (void *)data;if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {if(rq->bRequest == USBRQ_HID_GET_REPORT) {return sizeof(report);} else if(rq->bRequest == USBRQ_HID_GET_IDLE) {return 1;}}return 0;}void hadUsbReset(void) {}int main(void) {ACSR |= (1<<ACD); // Disable analog comparator/*DDR : 1 = Output, 0 = InputPORT: 1 = Pullup for Input, otherwise set outputPIN : Read input pin*//*PB0 - Input - Keypad 5PB1 - Input - Keypad 6PB2 - Input - Keypad 7PB3 - Input - Keypad 8PB4 - Input, Pullup - Function select*/DDRB = 0B00000000;PORTB = 0B00010000;/*PD0 - Input, Pullup, PCINT16 - Rotary 1aPD1 - Input, Pullup, PCINT17 - Rotary 1bPD4 - Output - Keypad 1PD5 - Output - Keypad 2PD6 - Output - Keypad 3PD7 - Output - Keypad 4*/DDRD = 0B11110000;PORTD = 0B00000011;PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sourcesPCICR |= ( 1 << PCIE2 ); //enable pin change interupts// Timers not used for the moment// Setup timer0 - Enable overflow, 8 times prescaler//TIMSK0 = (1<<TOIE0); // Eable timer overflow for Timer0//TCNT0 = 0x00; // Set Timer0 to 0//TCCR0B = (1<< CS01) ; // /8 prescalerusbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */_delay_ms(500);usbDeviceConnect();wdt_enable(WDTO_1S);usbInit();sei();for(;;) {wdt_reset();usbPoll();if(usbInterruptIsReady()){report.data[2] |= (1 << getKey());// Now work out what rotary to send, if any// Also record if we sent a positive response,// so we can send a '0' next time (if selected on PD4)/*if (rot_stat == 0x01 && rot_sent == 0) {report.rot1a = 1;rot_sent = 1;} else if (rot_stat == 0x02 && rot_sent == 0) {report.rot1b = 1;rot_sent = 1;} else {rot_sent = 0;}*/// Reset our stat so ready for next turnrot_stat = 0;// If our function select is set, dont bother// sending a 'o' between consequtive 1's.if (rbi(PINB, PB4))rot_sent = 0;/* called after every poll of the interrupt endpoint */usbSetInterrupt(&report, sizeof(report));}}}uint8_t getKey() {uint8_t col, row = 0;uint8_t key = 0;uint8_t n = 0;for (col=4; col<=7; col++) {sbi(PORTD, col);for (row=0; row<=3; row++); {if (rbi(PINB, row))key = n;n++;}cbi(PORTD, col);}return key;}void doInt() {// If rot_stat is not 0, we havn't sent// our last results yet. Skip this click.if (rot_stat != 0) {pcIntMask = 0;return;}// Check which pin caused the interrupt. If they both// equal 0, the pin that interrupted is the directionif (rbi(pcIntCurr, PCINT17) == 0&& rbi(pcIntCurr, PCINT17) == 0&& rbi(pcIntMask, PCINT16) ) {rot_stat = 1;} else if (rbi(pcIntCurr, PCINT16) == 0&& rbi(pcIntCurr, PCINT17) == 0&& rbi(pcIntMask, PCINT17) ) {rot_stat = 2;}// Clear the mask so we know we've delth with itpcIntMask = 0;}/* Not used for the momentISR(TIMER0_OVF_vect) {timer0_ovf++;}*/ISR(PCINT2_vect){// Save the state and work out which pin caused// the interrupt to occurpcIntCurr = PIND;pcIntMask = pcIntCurr ^ pcIntLast;pcIntLast = pcIntCurr;doInt();}