Hi!
I try read encoder connected to xplain board.
Encoder connected to pins 0 and 1 of PORTD.
Inputs of PORTD.0 and PORTD.1 are oututs to leds connected to PORTE. Leds are flashes when encoder rotating.
I output timer counter values into LCD.
Probles is that counter values such as if they are random :(
I use codevision compiler.
................. // Event System initialization void event_system_init(void) { // Event System Channel 0 source: Port D, Pin0 EVSYS.CH0MUX=EVSYS_CHMUX_PORTD_PIN0_gc; // Event System Channel 1 source: None EVSYS.CH1MUX=EVSYS_CHMUX_OFF_gc; ............... // PORTD initialization // OUT register PORTD.OUT=0x00; // Bit0:7: Input PORTD.DIR=0x00; // Bit0 Output/Pull configuration: Totempole/No // Bit0 Input/Sense configuration: Sense low level // Bit0 inverted: Off // Bit0 slew rate limitation: Off PORTD.PIN0CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_LEVEL_gc; // Bit1 Output/Pull configuration: Totempole/No // Bit1 Input/Sense configuration: Sense low level // Bit1 inverted: Off // Bit1 slew rate limitation: Off PORTD.PIN1CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_LEVEL_gc; // Bit2 Output/Pull configuration: Totempole/No // Bit2 Input/Sense configuration: Sense both edges // Bit2 inverted: Off // Bit2 slew rate limitation: Off ....................... // Disable and reset the timer/counter just to be sure tc0_disable(&TCC0); // Clock source: Peripheral Clock/1 TCC0.CTRLA=(TCC0.CTRLA & (~TC0_CLKSEL_gm)) | TC_CLKSEL_DIV64_gc; // Mode: Normal Operation, Overflow Int./Event on TOP // Compare/Capture on channel A:D: Off TCC0.CTRLB=(TCC0.CTRLB & (~(TC0_CCAEN_bm | TC0_CCBEN_bm | TC0_CCCEN_bm | TC0_CCDEN_bm | TC0_WGMODE_gm))) | TC_WGMODE_NORMAL_gc; // Capture event source: Event Channel 0 // Capture event action: Quadrature Decode TCC0.CTRLD=(TCC0.CTRLD & (~(TC0_EVACT_gm | TC0_EVSEL_gm))) | TC_EVACT_QDEC_gc | TC_EVSEL_CH0_gc; // Overflow interrupt: Disabled // Error interrupt: Disabled TCC0.INTCTRLA=(TCC0.INTCTRLA & (~(TC0_ERRINTLVL_gm | TC0_OVFINTLVL_gm))) | TC_ERRINTLVL_OFF_gc | TC_OVFINTLVL_OFF_gc; // Compare/Capture channel A:D interrupt: Disabled TCC0.INTCTRLB=(TCC0.INTCTRLB & (~(TC0_CCDINTLVL_gm | TC0_CCCINTLVL_gm | TC0_CCBINTLVL_gm | TC0_CCAINTLVL_gm))) | TC_CCDINTLVL_OFF_gc | TC_CCCINTLVL_OFF_gc | TC_CCBINTLVL_OFF_gc | TC_CCAINTLVL_OFF_gc; // High resolution extension: Off HIRESC.CTRL&= ~HIRES_HREN0_bm; // Advanced Waveform Extension initialization // Optimize for speed #pragma optsize- // Disable locking the AWEX configuration registers just to be sure n=MCU.AWEXLOCK & (~MCU_AWEXCLOCK_bm); CCP=CCP_IOREG_gc; MCU.AWEXLOCK=n; // Restore optimization for size if needed #pragma optsize_default // Pattern generation: Off // Dead time insertion: Off AWEXC.CTRL&= ~(AWEX_PGM_bm | AWEX_CWCM_bm | AWEX_DTICCDEN_bm | AWEX_DTICCCEN_bm | AWEX_DTICCBEN_bm | AWEX_DTICCAEN_bm); // Fault protection initialization // Fault detection on OCD Break detection: On // Fault detection restart mode: Latched Mode // Fault detection action: None (Fault protection disabled) AWEXC.FDCTRL=(AWEXC.FDCTRL & (~(AWEX_FDDBD_bm | AWEX_FDMODE_bm | AWEX_FDACT_gm))) | AWEX_FDACT_NONE_gc; // Fault detect events: // Event channel 0:7: Off AWEXC.FDEVMASK=0b00000000; // Make sure the fault detect flag is cleared AWEXC.STATUS|=AWEXC.STATUS & AWEX_FDF_bm; // Clear the interrupt flags TCC0.INTFLAGS=TCC0.INTFLAGS; // Set counter register TCC0.CNT=0x0000; // Set period register TCC0.PER=3999; // Set channel A:D Compare/Capture register TCC0.CCA=0x0000; TCC0.CCB=0x0000; TCC0.CCC=0x0000; TCC0.CCD=0x0000; // Restore interrupts enabled/disabled state SREG=s; } ......................... EN=TCC0.CNT; //var int EN
My encoder 1000 puls/revolution.
Thank for help :)