Hi all,
I'm trying to get an encoder like this:
http://www.aliexpress.com/item/New-High-Quality-Incremental-optical-rotary-encoder-400-600-360-pulses-Rev-AB-2-phase-DC5/2048956011.html
working on a Maple mini, which uses a stm32f103rc. I connected the
phases of the encoder to PB6 and PB7. I wrote a program which reads the
changes by hand, this program works, which means for me the encoder is
not broken and the ports are also ok.
What I want to do is using the internal encoder interface which also
uses, as far as I understand, PB6 and PB7 as input. And I just fail to
get it working.
My code is:
#include <stdarg.h>
#include <stdio.h>
#include <libopencm3/stm32/timer.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/scb.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
void p(char *fmt, ... );
void p(char *fmt, ... ){
static uint8_t c='\0';
char tmp[256];
va_list args;
va_start (args, fmt );
vsnprintf(tmp, 256, fmt, args);
va_end (args);
int i=0;
while (tmp[i] != '\0' )
{
c = tmp[i];
usart_send_blocking(USART2, c);
i++;
}
}
static void clock_setup(void)
{
rcc_clock_setup_in_hse_8mhz_out_72mhz();
/* Enable GPIOA clock. */
rcc_periph_clock_enable(RCC_GPIOA);
/* Enable clocks for GPIO port A (for LED GPIO_USART2_TX) and
USART2. */
rcc_periph_clock_enable(RCC_USART2);
}
static void usart_setup(void)
{
/* Setup GPIO pin GPIO_USART2_RE_TX on GPIO port A for transmit. */
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
/* Setup GPIO pin GPIO_USART2_RE_RX on GPIO port A for receive. */
gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX);
/* Setup UART parameters. */
usart_set_baudrate(USART2, 38400);
usart_set_databits(USART2, 8);
usart_set_stopbits(USART2, USART_STOPBITS_1);
usart_set_parity(USART2, USART_PARITY_NONE);
usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);
usart_set_mode(USART2, USART_MODE_TX_RX);
/* Enable USART2 Receive interrupt. */
USART_CR1(USART2) |= USART_CR1_RXNEIE;
/* Finally enable the USART. */
usart_enable(USART2);
}
static void encoder_setup(void)
{
// Use PB6 + PB7 for encoder input (5v)
gpio_set_mode (GPIOB, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_PULL_UPDOWN, GPIO6);
gpio_set_mode (GPIOB, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_PULL_UPDOWN, GPIO7);
timer_reset(TIM4);
rcc_periph_clock_enable(RCC_TIM4);
timer_set_period(TIM4, 0xFFFF);
timer_slave_set_mode(TIM4, TIM_SMCR_SMS_EM3); // encoder
timer_ic_set_input(TIM4, TIM_IC1, TIM_IC_IN_TI1);
timer_ic_set_input(TIM4, TIM_IC2, TIM_IC_IN_TI2);
timer_enable_counter(TIM4);
}
int main(void)
{
int j = 0, c = 0;
int i;
uint16_t pos;
SCB_VTOR = (uint32_t) 0x08005000;
clock_setup();
usart_setup();
p("usart setup done... \r\n");
encoder_setup();
p("encoder setup done... \r\n");
while (1) {
pos = timer_get_counter(TIM4);
p(" pos: %ld \r\n", pos);
usart_send_blocking(USART2, c + '0'); /* USART2: Send byte. */
c = (c == 9) ? 0 : c + 1; /* Increment c. */
if ((j++ % 80) == 0) { /* Newline after line full. */
usart_send_blocking(USART2, '\r');
usart_send_blocking(USART2, '\n');
}
for (i = 0; i < 8000000; i++) /* Wait a bit. */
__asm__("nop");
}
return 0;
}
Any hits? Solutions?
Thanks
Thorsten
------------------------------------------------------------------------------
_______________________________________________
libopencm3-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libopencm3-devel