注意需要进行定时器的引脚复用,不然不得。
#include "al422b.h"
#include "usart.h"
u8 TIM3_CAPTURE_STA=0;
u16 TIM3_CAPTURE_VAL;
u8 TIM4_CAPTURE_STA=0;
u16 TIM4_CAPTURE_VAL;
void AL422B_wen_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = 0;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM3,&TIM_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearITPendingBit(TIM3,TIM_IT_Update|TIM_IT_CC3);
TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC3,ENABLE);
TIM_Cmd(TIM3,ENABLE );
}
void TIM3_IRQHandler(void)
{
if((TIM3_CAPTURE_STA & 0X80) == 0)
{
if((TIM_GetITStatus(TIM3,TIM_IT_Update) != RESET))
{
if(TIM3_CAPTURE_STA & 0X40)
{
if((TIM3_CAPTURE_STA & 0X3F) == 0X3F)
{
TIM3_CAPTURE_STA |= 0X80;
TIM3_CAPTURE_VAL = 0XFFFF;
}
else TIM3_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM3,TIM_IT_CC3) != RESET)
{
if(TIM3_CAPTURE_STA & 0X40)
{
TIM3_CAPTURE_STA |= 0X80;
TIM3_CAPTURE_VAL = TIM_GetCapture3(TIM3);
TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Rising);
}
else
{
TIM3_CAPTURE_STA = 0;
TIM3_CAPTURE_VAL = 0;
TIM_SetCounter(TIM3,0);
TIM3_CAPTURE_STA |= 0X40;
TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Falling);
}
TIM_ClearFlag(TIM3, TIM_FLAG_CC3);
}
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update|TIM_IT_CC3);
}
void AL422B_sh_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = 0;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM4,&TIM_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearITPendingBit(TIM4,TIM_IT_Update|TIM_IT_CC4);
TIM_ITConfig(TIM4,TIM_IT_Update|TIM_IT_CC4,ENABLE);
TIM_Cmd(TIM4,ENABLE );
printf("dddd\r\n");
}
void TIM4_IRQHandler(void)
{
if((TIM4_CAPTURE_STA & 0X80) == 0)
{
if((TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET))
{
if(TIM4_CAPTURE_STA & 0X40)
{
if((TIM4_CAPTURE_STA & 0X3F) == 0X3F)
{
TIM4_CAPTURE_STA |= 0X80;
TIM4_CAPTURE_VAL = 0XFFFF;
}
else TIM4_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM4,TIM_IT_CC4) != RESET)
{
if(TIM4_CAPTURE_STA & 0X40)
{
TIM4_CAPTURE_STA |= 0X80;
TIM4_CAPTURE_VAL = TIM_GetCapture4(TIM4);
TIM_OC4PolarityConfig(TIM4,TIM_ICPolarity_Rising);
}
else
{
TIM4_CAPTURE_STA = 0;
TIM4_CAPTURE_VAL = 0;
TIM_SetCounter(TIM4,0);
TIM4_CAPTURE_STA |= 0X40;
TIM_OC4PolarityConfig(TIM4,TIM_ICPolarity_Falling);
}
TIM_ClearFlag(TIM4, TIM_FLAG_CC4);
}
}
TIM_ClearITPendingBit(TIM4,TIM_IT_Update|TIM_IT_CC4);
}
#ifndef __AL422B_H
#define __AL422B_H
#include "stm32f10x.h"
extern u8 TIM3_CAPTURE_STA;
extern u16 TIM3_CAPTURE_VAL;
extern u8 TIM4_CAPTURE_STA;
extern u16 TIM4_CAPTURE_VAL;
#define WRST1_PIN GPIO_Pin_11
#define WRST1_PORT GPIOD
#define WRST1_CLK RCC_APB2Periph_GPIOD
#define WRST1_High GPIO_ResetBits(WRST1_PORT,WRST1_PIN);
#define WRST1_Low GPIO_SetBits(WRST1_PORT,WRST1_PIN);
#define OE1_PIN GPIO_Pin_10
#define OE1_PORT GPIOD
#define OE1_CLK RCC_APB2Periph_GPIOD
#define OE1_High GPIO_ResetBits(OE1_PORT,OE1_PIN);
#define OE1_Low GPIO_SetBits(OE1_PORT,OE1_PIN);
#define RRST1_PIN GPIO_Pin_9
#define RRST1_PORT GPIOD
#define RRST1_CLK RCC_APB2Periph_GPIOD
#define RRST1_High GPIO_ResetBits(RRST1_PORT,RRST1_PIN);
#define RRST1_Low GPIO_SetBits(RRST1_PORT,RRST1_PIN);
#define RCK1_PIN GPIO_Pin_8
#define RCK1_PORT GPIOD
#define RCK1_CLK RCC_APB2Periph_GPIOD
#define RCK1_High GPIO_ResetBits(RCK1_PORT,RCK1_PIN);
#define RCK1_Low GPIO_SetBits(RCK1_PORT,RCK1_PIN);
#define WRST2_PIN GPIO_Pin_15
#define WRST2_PORT GPIOB
#define WRST2_CLK RCC_APB2Periph_GPIOB
#define WRST2_High GPIO_ResetBits(WRST2_PORT,WRST2_PIN);
#define WRST2_Low GPIO_SetBits(WRST2_PORT,WRST2_PIN);
#define OE2_PIN GPIO_Pin_14
#define OE2_PORT GPIOB
#define OE2_CLK RCC_APB2Periph_GPIOB
#define OE2_High GPIO_ResetBits(OE2_PORT,OE2_PIN);
#define OE2_Low GPIO_SetBits(OE2_PORT,OE2_PIN);
#define RRST2_PIN GPIO_Pin_13
#define RRST2_PORT GPIOB
#define RRST2_CLK RCC_APB2Periph_GPIOB
#define RRST2_High GPIO_ResetBits(RRST2_PORT,RRST2_PIN);
#define RRST2_Low GPIO_SetBits(RRST2_PORT,RRST2_PIN);
#define RCK2_PIN GPIO_Pin_12
#define RCK2_PORT GPIOB
#define RCK2_CLK RCC_APB2Periph_GPIOB
#define RCK2_High GPIO_ResetBits(RCK2_PORT,RCK2_PIN);
#define RCK2_Low GPIO_SetBits(RCK2_PORT,RCK2_PIN);
#define WRST3_PIN GPIO_Pin_15
#define WRST3_PORT GPIOE
#define WRST3_CLK RCC_APB2Periph_GPIOE
#define WRST3_High GPIO_ResetBits(WRST3_PORT,WRST3_PIN);
#define WRST3_Low GPIO_SetBits(WRST3_PORT,WRST3_PIN);
#define OE3_PIN GPIO_Pin_14
#define OE3_PORT GPIOE
#define OE3_CLK RCC_APB2Periph_GPIOE
#define OE3_High GPIO_ResetBits(OE3_PORT,OE3_PIN);
#define OE3_Low GPIO_SetBits(OE3_PORT,OE3_PIN);
#define RRST3_PIN GPIO_Pin_13
#define RRST3_PORT GPIOE
#define RRST3_CLK RCC_APB2Periph_GPIOE
#define RRST3_High GPIO_ResetBits(RRST3_PORT,RRST3_PIN);
#define RRST3_Low GPIO_SetBits(RRST3_PORT,RRST3_PIN);
#define RCK3_PIN GPIO_Pin_12
#define RCK3_PORT GPIOE
#define RCK3_CLK RCC_APB2Periph_GPIOE
#define RCK3_High GPIO_ResetBits(RCK3_PORT,RCK3_PIN);
#define RCK3_Low GPIO_SetBits(RCK3_PORT,RCK3_PIN);
#define WRST4_PIN GPIO_Pin_11
#define WRST4_PORT GPIOE
#define WRST4_CLK RCC_APB2Periph_GPIOE
#define WRST4_High GPIO_ResetBits(WRST4_PORT,WRST4_PIN);
#define WRST4_Low GPIO_SetBits(WRST4_PORT,WRST4_PIN);
#define OE4_PIN GPIO_Pin_10
#define OE4_PORT GPIOE
#define OE4_CLK RCC_APB2Periph_GPIOE
#define OE4_High GPIO_ResetBits(OE4_PORT,OE4_PIN);
#define OE4_Low GPIO_SetBits(OE4_PORT,OE4_PIN);
#define RRST4_PIN GPIO_Pin_9
#define RRST4_PORT GPIOE
#define RRST4_CLK RCC_APB2Periph_GPIOE
#define RRST4_High GPIO_ResetBits(RRST4_PORT,RRST4_PIN);
#define RRST4_Low GPIO_SetBits(RRST4_PORT,RRST4_PIN);
#define RCK4_PIN GPIO_Pin_8
#define RCK4_PORT GPIOE
#define RCK4_CLK RCC_APB2Periph_GPIOE
#define RCK4_High GPIO_ResetBits(RCK4_PORT,RCK4_PIN);
#define RCK4_Low GPIO_SetBits(RCK4_PORT,RCK4_PIN);
void AL422B_Init(void);
void AL422B_wen_Init(u16 arr,u16 psc);
void AL422B_sh_Init(u16 arr,u16 psc);
#endif
main.c
#include "stm32f10x.h"
#include "led.h"
#include "key.h"
#include "usart.h"
#include "al422b.h"
unsigned char os1[1920];
unsigned char os2[1920];
unsigned char os3[1920];
unsigned char os4[1920];
unsigned char osall[7700];
void gpioA0A7_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 |GPIO_Pin_2 |GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void gpioB0B7_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 |GPIO_Pin_2 |GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void gpioC0C7_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 |GPIO_Pin_2 |GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void gpioD0D7_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 |GPIO_Pin_2 |GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
void gpioE0E7_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 |GPIO_Pin_2 |GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
void B3_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_3);
RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_2);
}
u8 RxBuf0[1000];
u8 SendBuf0[5000];
void USART1_RecDMA(uint8_t* buf,int len)
{
DMA_InitTypeDef DMA_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel4);
DMA_DeInit(DMA1_Channel5);
DMA_Cmd(DMA1_Channel5,DISABLE);
DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
DMA_InitStruct.DMA_MemoryBaseAddr = (u32)RxBuf0;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStruct.DMA_BufferSize = 5000;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStruct.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
DMA_InitStruct.DMA_Priority = DMA_Priority_High;
DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5,&DMA_InitStruct);
DMA_Cmd(DMA1_Channel5,ENABLE);
DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel5_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
}
void USART1_SendDMA()
{
DMA_InitTypeDef DMA_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);
DMA_Cmd(DMA1_Channel4,DISABLE);
DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
DMA_InitStruct.DMA_MemoryBaseAddr = (u32)SendBuf0;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStruct.DMA_BufferSize = 5000;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStruct.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
DMA_InitStruct.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4,&DMA_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
DMA_Cmd(DMA1_Channel4,ENABLE);
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
}
void DMA1_Channel4_IRQHandler()
{
if(DMA_GetITStatus(DMA1_IT_TC4))
{
DMA_ClearITPendingBit(DMA1_IT_TC4);
printf("send ok...\r\n");
}
}
int main(void)
{
u32 temp=0;
int i=0;
int inputvalue;
Delay_Init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
Usart_Config();
LED_Init();
printf("******************\r\n");
gpioA0A7_Init();
gpioB0B7_Init();
gpioC0C7_Init();
gpioD0D7_Init();
gpioE0E7_Init();
AL422B_wen_Init(0xFFFF,72-1);
AL422B_sh_Init(0xFFFF,72-1);
while(1)
{
if(TIM3_CAPTURE_STA & 0X80)
{
temp = TIM3_CAPTURE_STA&0X3F;
temp *= 65536;
temp += TIM3_CAPTURE_VAL;
printf("T:%d us\r\n",temp);
TIM3_CAPTURE_STA = 0;
}
if(TIM4_CAPTURE_STA & 0X80)
{
temp = TIM4_CAPTURE_STA&0X3F;
temp *= 65536;
temp += TIM4_CAPTURE_VAL;
printf("F:%d us\r\n",temp);
TIM4_CAPTURE_STA = 0;
}
}
}
定时器1us定时一次
|