/////////////////////////////////////////////////////////////////////////////// // encoder_skel.c // Tests functionality of both the encoders using the bar graph // Name: // Date: /////////////////////////////////////////////////////////////////////////////// // //PORT D connections // PORT D: 7 6 5 4 3 2 1 0 // SCK (SCK)--| | | | | | | |--unused // MISO (MISO)----| | | | | |-----unused // MOSI (MOSI)-------| | | |--------unused // (unused) SS_n----------| |-----------unused // *SS_n is unused but must be configred as an output in master mode // //PORT (E) connections // PORT E: 7 6 5 4 3 2 1 0 // not present--| | | | | | | |--RCLK // not present-----| | | | | |-----SH/LD_n (encoder bd) // not present--------| | |--------unused // not present-----------| |-----------unused // //PORT (C) connections // PORT C: 7 6 5 4 3 2 1 0 // not present--| | | | | | | |--OE_n (PWM dimming) // not present-----| | | | | |-----unused // not present--------| | |--------unused // not present-----------| |-----------unused // #include #include /////////////////////////////////////////////////////////////////////////////// // Initalize Port D SPI void init_SPID(void){} /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// void init_32Mhz_RC_clk(void){ //setup 32Mhz internal RC clock OSC.CTRL = OSC_RC32MEN_bm; //enable 32MHz clock while (!(OSC.STATUS & OSC_RC32MRDY_bm)); //wait for clock to be ready CCP = CCP_IOREG_gc; //enable protected register change CLK.CTRL = CLK_SCLKSEL_RC32M_gc; //switch to 32MHz clock } /////////////////////////////////////////////////////////////////////////////// //****************************************************************************** // encoder_chk //****************************************************************************** // //Checks for encoder movement using the 4-state state machine. //Tested with Bournes 24 pulse encoder PEC12R-3217F-S0024. //Clockwise movement returns 1 //Counterclockwise movement returns 0 //No encoder movement returns -1 //Detent position is expected to assert A and B inputs both to logic true. // //expects encoder pin states to be in the arg "encoder_var" as follows: // encoder S2 encoder S1 // array index 1 array index 0 // b_pin a_pin b_pin a_pin // bit6 bit5 bit1 bit0 // bits 3,4 not used; bit2 is switch for S1, bit7 is for switch S2 int8_t encoder_chk(uint8_t encoder_var, uint8_t encoder_sel){} /////////////////////////////////////////////////////////////////////////////// //ISR for TC0 ISR( ){ //capture encoder pin states into shift register //send data in thermomether code to bar graph while shifing in bits from encoder //update bar graph display //see if the encoders moved //adjust count based on encoder movement }//ISR /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// //setup timer for normal mode overflows every 1mS void init_TC0_normal(void){ //setup timer for normal mode and overflow interrupt // TCC0_CTRLA |= TC_CLKSEL_DIV64_gc; //TC0 runs on 500Khz TCC0_CTRLB |= TC_WGMODE_NORMAL_gc; TCC0_PER = 0x0200; //512 decimal TCC0_INTCTRLA = TC0_OVFINTLVL0_bm; //interrupt level bit zero set } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // segment_sum void segsum(uint16_t sum){ }//seg_sum /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // main() int main(void){ init_32Mhz_RC_clk(); //set clock to 32Mhz init_SPID(); //setup SPI on PORT D init_TC0_normal(); //setup timer TC0 //configure all PORT A pins as WIREDANDPULL OPC[2:0]=111 //configure Port C upper nibble as all totem pole outputs //configure other PORT pins as needed //enable low level interrupts and global interrupt enable while(1){ //Turn on each digit by incrementing the count to PORTC bits 4-6. _delay_loop_2(LOOP_DELAY); //loop to make LEDs visible }//while }//main