uint16_t motor_encoder_read(){ uint8_t msb; uint8_t lsb; //enable output motor_encoder_output_enable(1); //read MSB to data motor_encoder_select_byte(0); _delay_us(20); msb = reverse_bits(motor_encoder_byte_read()); //read LSB to data motor_encoder_select_byte(1); _delay_us(20); lsb = reverse_bits(motor_encoder_byte_read()); //disable output motor_encoder_output_enable(0); uint16_t data = (msb << 8) + lsb; return data; }
// Initializes the motor in port C void motor_init() { max520_init(MAX520_TWI_ADDRESS); DDRC |= (1<<PC3) | (1<<PC4) | (1<<PC5) | (1<<PC6) | (1<<PC7); DDRK = 0; motor_output_enable(ENABLE); motor_encoder_reset(); motor_encoder_select_byte(ENCODER_HIGH); motor_speed(0); motor_enable(ENABLE); }