基于STM32神舟系列开发板的读取绝对式编码器源码
#include "stm32f10x_usart.h"
#include
#include
#include
#define CMD_BUFFER_LEN 100
GPIO_InitTypeDef GPIO_InitStructure;
void Delay(__IO uint32_t 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)
{
//串口 PA9 PA10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//绝对值码盘 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; //浮空输入,TIM3的ch1和ch2通道
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; //浮空输入,TIM3的ch1和ch2通道
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
//模拟激光测试时改波特率为 38400
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无流控制
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//打开Rx接收和Tx发送功能
USART_Init(USART1, &USART_InitStructure);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);//允许DMA
USART_Cmd(USART1, ENABLE);
}
void myprintf(USART_TypeDef* USARTx, char *fmt, ...)
{
char buffer[CMD_BUFFER_LEN + 1];
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
)
{
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 >if (
STM32神舟系列编码 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)