#include "stm32f10x.h"
#include "lcd.h"
#include "led.h"
#include "key.h"
#include "stdio.h"
#include "i2c.h"
#include "pwm.h"
#include "usart.h"
**第一部分**
u32 TimingDelay = 0;
u8 RXCOUNT = 0;
u8 RXBUF[20];
u8 RXOVER = 0;
u8 count = 0;
u8 i;
u8 usart_flag =0;
u8 text1[20];
u8 text2[20];
u8 text3[20];
u8 text4[20];
u16 fre_1 = 0;
u16 fre_2 = 0;
u8 per_1 = 2;
u8 per_2 = 2;
u8 key1_temp = 0;
u8 key2_temp = 0;
extern u8 TIM2_CH2_CAPTURE_MODE;
extern u32 TIM2_CH2_CAPTURE_HL;
extern u8 TIM2_CH3_CAPTURE_MODE;
extern u32 TIM2_CH3_CAPTURE_HL;
void Delay_Ms(u32 nTime);
void RCC_Configuration(void);
void Scan_Key(void);
void LCD_Init(void);
void LCD_Display(void);
void PWM_IN_Frequency(void);
void _24c02_Write(u16 adr,u16 data);
u16 _24c02_Read(u16 adr);
int main(void)
{
i2c_init();
LCD_Init();
RCC_Configuration();
USART2_Configuration();
TIM2_Configuration();
TIM3_Configuration(1000,300);
KEY_Configuration();
LED_Configuration();
while(1)
{
PWM_IN_Frequency();
LCD_Display();
Scan_Key();
}
}
void LCD_Init(void)
{
SysTick_Config(SystemCoreClock/1000);
STM3210B_LCD_Init();
LCD_Clear(Blue);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
per_1 = _24c02_Read(0x01);
Delay_Ms(10);
per_2 = _24c02_Read(0x02);
Delay_Ms(10);
}
void LCD_Display(void)
{
sprintf((char*)text1,"Channel(1): %dHz ",fre_1);
LCD_DisplayStringLine(Line2,text1);
sprintf((char*)text2,"N(1): %d ",per_1);
LCD_DisplayStringLine(Line3,text2);
sprintf((char*)text3,"Channel(2): %dHz ",fre_2);
LCD_DisplayStringLine(Line4,text3);
sprintf((char*)text4,"N(2): %d ",per_2);
LCD_DisplayStringLine(Line5,text4);
if(usart_flag == 1)
{
if(RXOVER == 1)
{
RXOVER = 0;
switch(RXBUF[4] - 48)
{
case 1:LED_Control(LED1,0);
LED_Control(LED2,1);
break;
case 2:LED_Control(LED1,1);
LED_Control(LED2,0);
break;
default: break;
}
if(count == 7)
{
if((RXBUF[4] - 48) == 1)
{
switch(RXBUF[6] - 48)
{
case 2:per_1 = 2;break;
case 3:per_1 = 3;break;
case 4:per_1 = 4;break;
case 5:per_1 = 5;break;
case 6:per_1 = 6;break;
case 7:per_1 = 7;break;
case 8:per_1 = 8;break;
case 9:per_1 = 9;break;
default:break;
}
_24c02_Write(0x01,per_1);
LCD_DisplayStringLine(Line7,(u8*)" CH1 ");
}
else if((RXBUF[4] - 48) == 2)
{
switch(RXBUF[6] - 48)
{
case 2:per_2 = 2;break;
case 3:per_2 = 3;break;
case 4:per_2 = 4;break;
case 5:per_2 = 5;break;
case 6:per_2 = 6;break;
case 7:per_2 = 7;break;
case 8:per_2 = 8;break;
case 9:per_2 = 9;break;
default:break;
}
_24c02_Write(0x02,per_2);
Delay_Ms(10);
LCD_DisplayStringLine(Line7,(u8*)" CH2 ");
}
}
else if(count == 8)
{
if((RXBUF[4] - 48) == 1)
{
per_1 = 10;
_24c02_Write(0x01,per_1);
LCD_DisplayStringLine(Line7,(u8*)" CH1 ");
}
else if((RXBUF[4] - 48) == 2)
{
per_2 = 10;
_24c02_Write(0x02,per_2);
LCD_DisplayStringLine(Line7,(u8*)" CH2 ");
}
}
for(i=0;i<20;i++)
{
RXBUF[i]=0;
}
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
}
}
}
void Delay_Ms(u32 nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
void Scan_Key(void)
{
if(K1 == 0)
{
Delay_Ms(10);
if(K1 == 0)
{
key1_temp++;
if(key1_temp == 1)
{
usart_flag = 1;
LED_Control(LED1,1);
LED_Control(LED2,1);
LED_Control(LED3,0);
}
else
{
key1_temp = 0;
usart_flag = 0;
LED_Control(LED3,1);
}
}
while(K1 == 0);
}
if((K2 == 0) && (key1_temp == 0))
{
Delay_Ms(10);
if((K2 == 0) && (key1_temp == 0))
{
if(key2_temp == 0)
{
key2_temp =1;
LED_Control(LED1,1);
LED_Control(LED2,0);
}
else if(key2_temp == 1)
{
key2_temp =0;
LED_Control(LED1,0);
LED_Control(LED2,1);
}
}
while(K2 == 0);
}
if((K3 == 0) && (key1_temp == 0))
{
Delay_Ms(10);
if((K3 == 0) && (key1_temp == 0))
{
if(key2_temp == 0)
{
if(1<per_1)
per_1--;
else
per_1 = 1;
_24c02_Write(0x01,per_1);
}
else if(key2_temp == 1)
{
if(per_2>1)
per_2--;
else
per_2 = 1;
_24c02_Write(0x02,per_2);
}
}
while(K3 == 0);
}
if((K4 == 0) && (key1_temp == 0))
{
Delay_Ms(10);
if((K4 == 0) && (key1_temp == 0))
{
if(key2_temp == 0)
{
if(per_1<10)
per_1++;
else
per_1 = 10;
_24c02_Write(0x01,per_1);
}
else if(key2_temp == 1)
{
if(per_2<10)
per_2++;
else
per_2 = 10;
_24c02_Write(0x02,per_2);
}
}
while(K4 == 0);
}
}
void PWM_IN_Frequency(void)
{
if(TIM2_CH2_CAPTURE_MODE == 3)
{
fre_1 = 1000000 / TIM2_CH2_CAPTURE_HL;
if((fre_1 < 50) || (fre_1 > 50000))
{
Ch1_Enable = 1;
sprintf((char*)text1,"Channel(1): %dHz ",fre_1);
LCD_DisplayStringLine(Line2,text1);
TIM3_Configuration(fre_1 * per_1,fre_2);
}
else
{
Ch1_Enable = 0;
}
TIM2_CH2_CAPTURE_MODE = 0;
}
else if(TIM2_CH3_CAPTURE_MODE == 3)
{
fre_2 = 1000000 / TIM2_CH3_CAPTURE_HL;
if((fre_2 < 50) || (fre_2 > 50000))
{
Ch2_Enable = 1;
sprintf((char*)text3,"Channel(2): %dHz ",fre_2);
LCD_DisplayStringLine(Line4,text3);
TIM3_Configuration(fre_1,fre_2 * per_2);
}
else
{
Ch2_Enable = 0;
}
TIM2_CH2_CAPTURE_MODE = 0;
}
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}
void _24c02_Write(u16 adr,u16 data)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(adr);
I2CWaitAck();
I2CSendByte(data);
I2CWaitAck();
I2CStop();
}
u16 _24c02_Read(u16 adr)
{
u16 temp;
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(adr);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
temp = I2CReceiveByte();
I2CWaitAck();
I2CStop();
return temp;
}
``
**第二部分**
#include "led.h"
#define LED1 GPIO_Pin_8
#define LED2 GPIO_Pin_9
#define LED3 GPIO_Pin_10
#define LED4 GPIO_Pin_11
#define LED5 GPIO_Pin_12
#define LED6 GPIO_Pin_13
#define LED7 GPIO_Pin_14
#define LED8 GPIO_Pin_15
#define LEDALL GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15
void LED_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = LEDALL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
GPIO_SetBits(GPIOC,LEDALL);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}
void LED_Control(u16 ledx,u16 led_sta)
{
if(led_sta == 0)
{
GPIO_SetBits(GPIOD,GPIO_Pin_2);
GPIO_ResetBits(GPIOC,ledx);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}
else
{
GPIO_SetBits(GPIOD,GPIO_Pin_2);
GPIO_SetBits(GPIOC,ledx);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}
}
#include "key.h"
#define K1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
#define K2 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)
#define K3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)
#define K4 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)
void KEY_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
#include "usart.h"
void USART2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx;
USART_Init(USART2, &USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_Cmd(USART2, ENABLE);
}
#include "pwm.h"
u32 TIM3_Ch1_Val =0;
u32 TIM3_Ch2_Val =0;
u8 Ch1_Enable = 1;
u8 Ch2_Enable = 1;
void TIM2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2 ;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
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 = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_TimeBaseStructure.TIM_Period = 0xffff;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision =0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_CC2 | TIM_IT_CC3, ENABLE);
}
void TIM3_Configuration(u32 ch2_fre,u32 ch3_fre)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 0xffff;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM3_Ch1_Val = 1000000 / ch2_fre;
TIM3_Ch2_Val = 1000000 / ch3_fre;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = TIM3_Ch1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
if(Ch1_Enable)
{
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
}
else
{
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
}
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
if(Ch2_Enable)
{
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
}
else
{
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
}
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_Pulse = TIM3_Ch2_Val;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM3, TIM_IT_CC1|TIM_IT_CC2 , ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
**第三部分**
#include "stm32f10x_it.h"
#include "pwm.h"
extern u32 TimingDelay;
u8 CAPTURE_Flag=0;
u16 capture = 0;
u8 TIM2_CH2_CAPTURE_MODE = 0;
u32 TIM2_CH2_CAPTURE_H = 0;
u32 TIM2_CH2_CAPTURE_HL = 0;
u8 TIM2_CH3_CAPTURE_MODE = 0;
u32 TIM2_CH3_CAPTURE_H = 0;
u32 TIM2_CH3_CAPTURE_HL = 0;
extern u8 count;
extern u8 RXCOUNT;
extern u8 RXBUF[20];
extern u8 RXOVER;
void SysTick_Handler(void)
{
static u8 CAPTURE_sum = 0;
TimingDelay--;
if(++CAPTURE_sum == 200)
{
CAPTURE_sum = 0;
CAPTURE_Flag ^=1;
}
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_CC2) == 1)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_CC2);
if(CAPTURE_Flag)
{
switch(TIM2_CH2_CAPTURE_MODE)
{
case 0:
TIM2_CH2_CAPTURE_HL = 0;
TIM2_CH2_CAPTURE_H = 0;
TIM2_CH2_CAPTURE_MODE = 1;
TIM_SetCounter(TIM2,0x0);
TIM_OC2PolarityConfig(TIM2,TIM_ICPolarity_Falling);
break;
case 1:
TIM2_CH2_CAPTURE_H = TIM_GetCounter(TIM2);
TIM2_CH2_CAPTURE_MODE = 2;
TIM_OC2PolarityConfig(TIM2,TIM_ICPolarity_Rising);
break;
case 2:
TIM2_CH2_CAPTURE_HL = TIM_GetCounter(TIM2);
TIM2_CH2_CAPTURE_MODE = 3;
TIM_OC2PolarityConfig(TIM2,TIM_ICPolarity_Rising);
break;
default: break;
}
}
else
{
TIM2_CH2_CAPTURE_MODE = 0;
}
}
if(TIM_GetITStatus(TIM2,TIM_IT_CC3) == 1)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_CC3);
if(!CAPTURE_Flag)
{
switch(TIM2_CH3_CAPTURE_MODE)
{
case 0:
TIM2_CH3_CAPTURE_HL = 0;
TIM2_CH3_CAPTURE_H = 0;
TIM2_CH3_CAPTURE_MODE = 1;
TIM_SetCounter(TIM2,0x0);
TIM_OC3PolarityConfig(TIM2,TIM_ICPolarity_Falling);
break;
case 1:
TIM2_CH3_CAPTURE_H = TIM_GetCounter(TIM2);
TIM2_CH3_CAPTURE_MODE = 2;
TIM_OC3PolarityConfig(TIM2,TIM_ICPolarity_Rising);
break;
case 2:
TIM2_CH3_CAPTURE_HL = TIM_GetCounter(TIM2);
TIM2_CH3_CAPTURE_MODE = 3;
TIM_OC3PolarityConfig(TIM2,TIM_ICPolarity_Rising);
break;
default: break;
}
}
else
{
TIM2_CH3_CAPTURE_MODE = 0;
}
}
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_CC1) == 1)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);
capture = TIM_GetCapture1(TIM3);
TIM_SetCompare1(TIM3,capture + TIM3_Ch1_Val *1/2);
}
if(TIM_GetITStatus(TIM3,TIM_IT_CC2) == 1)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC2);
capture = TIM_GetCapture2(TIM3);
TIM_SetCompare2(TIM3,capture + TIM3_Ch2_Val *1/2);
}
}
void USART2_IRQHandler(void)
{
u8 temp;
if(USART_GetFlagStatus(USART2, USART_IT_RXNE) == SET)
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
temp = USART_ReceiveData(USART2);
if((temp == '\n') || (RXCOUNT == 20))
{
count = RXCOUNT-1;
RXCOUNT =0;
RXOVER = 1;
USART_ITConfig(USART2,USART_IT_RXNE,DISABLE);
}
else
{
RXBUF[RXCOUNT] = temp;
RXCOUNT++;
}
}
}
逻辑分析仪结果如下:杜邦线连接PA1/PA2 ----PA7 / PA6即可观察输入频率
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200221135257339.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDIzNjMwMg==,size_16,color_FFFFFF,t_70)
输入捕获主要借鉴大佬的思想
[详细可见](https://blog.csdn.net/qq_34952376/article/details/81172774)