微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 基于STM32神舟系列开发板的读取绝对式编码器源码

基于STM32神舟系列开发板的读取绝对式编码器源码

时间:11-27 来源:互联网 点击:
#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)

{

//串口 PA9 PA10

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //管脚9

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure); //TX初始化

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //管脚10

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure); //RX初始化

//绝对值码盘 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; //波特率9600

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无流控制

USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位

USART_InitStructure.USART_StopBits = USART_StopBits_1; //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]; // CMD_BUFFER_LEN长度自己定义吧

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 >if (

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top