您的位置 首页 国产IC

根据STM32神舟系列开发板的读取肯定式编码器源码

includestm32f10xhincludestm32f10x_usarthincludeincludeincludedefineCMD_BUFFER_LEN100GPIO_InitTypeDef

#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 >= 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);
}
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/bandaoti/ic/257048.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部