Reading absolute encoder source code based on STM32 Shenzhou series development board

Publisher:wmghyuLatest update time:2015-09-22 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include "stm32f10x.h"
#include "stm32f10x_usart.h"
#include
#include
#include
 
#define CMD_BUFFER_LEN 100
 
GPIO_InitTypeDef GPIO_InitStructure;
void Delay(__IO uint32_t nCount)
{
  for(; nCount != 0; nCount--);
}
void RCC_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}
 
void GPIO_Config(void)
{
//Serial port PA9 PA10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;                       //管脚9 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;      //Multiplex push-pull output 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);                              //TX initialization 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;                     //管脚10 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Floating input 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);                               //RX initialization
 
//Absolute value encoder PC1/clk, PC2/cs, PC3/data
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Floating input, ch1 and ch2 channels of TIM3
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Floating input, ch1 and ch2 channels of TIM3
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
 
void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
//Change the baud rate to 38400 when simulating laser testing
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_InitStructure.USART_BaudRate = 115200;                                   //Baud rate 9600 
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无流控制 
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //Word length 8 bits 
USART_InitStructure.USART_StopBits = USART_StopBits_1;                   //1-bit stop byte 
USART_InitStructure.USART_Parity = USART_Parity_No;                     //No parity check 
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //Turn on Rx receiving and Tx sending functions 
USART_Init(USART1, &USART_InitStructure);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);//Enable DMA                  //Initialization 
USART_Cmd(USART1, ENABLE);
}
 
 
void myprintf(USART_TypeDef* USARTx, char *fmt, ...)
{
char buffer[CMD_BUFFER_LEN + 1];   // Define the length of CMD_BUFFER_LEN yourself
u8 i = 0;
va_list arg_ptr;
va_start(arg_ptr, fmt);
vsnprintf(buffer, CMD_BUFFER_LEN + 1, fmt, arg_ptr);
while ((i < CMD_BUFFER_LEN) && buffer[i])
{
USART_SendData(USARTx, (u8)buffer[i++]);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);
}
va_end(arg_ptr);
}
 
int main(void)
{
s32 b = 0;
int c = 0;
RCC_Config();
GPIO_Config();
USART_Config();
GPIO_SetBits(GPIOC, GPIO_Pin_2);
GPIO_ResetBits(GPIOC, GPIO_Pin_1);
while (1)
{
int i = 0;
u32 abs_encoder_data = 0; //u16
s32 a = 0;
u32 g_abs_encoder_data_new = 0, g_abs_encoder_data_old, g_abs_encoder_data;
GPIO_ResetBits(GPIOC, GPIO_Pin_2);
//b=a;
for(i = 0; i < 72; i ++)
{
//clk
if(i%4==0)
GPIO_SetBits(GPIOC, GPIO_Pin_1);
else
GPIO_ResetBits(GPIOC, GPIO_Pin_1);
//read
if(i%4==2)
abs_encoder_data |= (GPIOC->IDR&GPIO_Pin_3)>>3;
//myprintf(USART1,"%d%d " ,GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3),(GPIOC->IDR&GPIO_Pin_3)>>3);
abs_encoder_data <<= 1; 
//myprintf(USART1,"%d",GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3));
}
GPIO_SetBits(GPIOC, GPIO_Pin_2);
g_abs_encoder_data_old = g_abs_encoder_data_new;
if
(
((abs_encoder_data & 0x003E) == 0x0020)//111110  
|| ((abs_encoder_data & 0x003E) == 0x0022) //34 100010
|| ((abs_encoder_data & 0x003E) == 0x0024) //36   100100
)
{
g_abs_encoder_data_new = (abs_encoder_data & 0x3FFFF) >> 6;
a = g_abs_encoder_data_new - g_abs_encoder_data_old;
if ((a - b)>900)
c -= 1;
if ((b - a)>900)
c += 1;
g_abs_encoder_data += a;
b=a;
//myprintf(USART1, "%d ", a);
//myprintf(USART1, "%d ", b);
if (c >= 0)
{
myprintf(USART1, "%d圈 ", c);
myprintf(USART1, "%d格 ", a);
}
else if (c<0)
{
myprintf(USART1, "%d圈 ", c + 1);
myprintf(USART1, "%d格 ", a - 4096);
//g_abs_encoder_data = 7;
}
// myprintf(USART1, "%d ", a); 
}
}
}
Reference address:Reading absolute encoder source code based on STM32 Shenzhou series development board

Previous article:Serial communication source code based on STM32 Shenzhou series development board
Next article:Detailed explanation of 8 modes of GPIO in STM32

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号