• 大小: 2KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-11
  • 语言: 其他
  • 标签: 驱动  

资源简介

曼彻斯特编码的驱动代码,基于stm32F4xx上的代码。将字节转换为曼彻斯特编码,并通过gpio输出。编码具有前导码(高电平),和stop结束标志码(低电平)

资源截图

代码片段和文件信息


#include “string.h“
#include “rtthread.h“
#include “stm32f4xx.h“
#include “common.h“
#include “manchester.h“


uint32_t counter_us = 0;

static bool even_parity_select(uint8_t ch)//fase eventrue uneven
{
bool parity = false;
while (ch)
{
parity = !parity;
ch = ch & (ch - 1);
}
return parity;
}

static void udelay(uint32_t us)
{
counter_us = 0;

while(us != counter_us);

}

void TIM3_IRQHandler(void)
{
if ( TIM_GetITStatus(TIM3 TIM_IT_Update) != RESET ) 
{
TIM_ClearITPendingBit(TIM3  TIM_IT_Update);  
rt_interrupt_enter();
counter_us ++;
rt_interrupt_leave();
}
}

static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure; 
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}


static void TIM_Configuration(void)
{
TIM_TimebaseInitTypeDef  TIM_TimebaseStructure;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 ENABLE); 

NVIC_Configuration();

  TIM_TimebaseStructure.TIM_Period = 10 - 1;       
  TIM_TimebaseStructure.TIM_Prescaler = 90 - 1;//10us
  TIM_TimebaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimebaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimebaseStructure.TIM_RepetitionCounter = 0;

TIM_TimebaseInit(TIM3 &TIM_TimebaseStructure);

TIM_ClearFlag(TIM3 TIM_FLAG_Update);
TIM_ITConfig(TIM3 TIM_IT_Update ENABLE);

TIM_Cmd(TIM3 DISABLE);
}

static void RCC_Configuration(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA ENABLE);
}

static void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

/* TIM3 channel 1 pin (PA6) configuration */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

GPIO_ResetBits(GPIOA GPIO_Pin_6);
GPIO_Init(GPIOA &GPIO_InitStructure);
}

void manchester_init(void)
{
RCC_Configuration();

GPIO_Configuration();

TIM_Configuration();
}

static void Manchester_Enable(void)
{
TIM_Cmd(TIM3 ENABLE);
}

static void Manchester_Disable(void)
{
TIM_Cmd(TIM3 DISABLE);
}

static void manchester_high(void)
{
GPIO_SetBits(GPIOA GPIO_Pin_6);
}

static void manchester_low(void)
{
GPIO_ResetBits(GPIOA GPIO_Pin_6);
}

/**
* 1. I/O output low level.
* 2. Wait for half frame time T.
* 3. I/O output high level.
* 4. Wait for half frame time T.
* 5. return
*/
static void ManchesterOne(void)
{
manchester_low();
udelay(HALF_BIT_TIME);
manchester_high();
udelay(HALF_BIT_TIME);
}

/*
* 1. I/O output high level.
* 2. Wait for half frame time T.
* 3. I/O output low l

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        339  2019-06-06 16:06  manchester.h

     文件       3769  2019-06-06 16:06  manchester.c

----------- ---------  ---------- -----  ----

                 4108                    2


评论

共有 条评论