oled显示的代码是移植过来的。
经过红色那部分的配置之后,oled的清屏函数OLED_CLS,显示函数OLED_ShowStr都没用了。
#include "stm32f10x.h"
#include "OLED_I2C.h"
#include "delay.h"
#include "stdio.h"
float a1,a2,period=0,frequency;
uint8_t flag=0;
void GPIO_INIT()
{
GPIO_InitTypeDef GPIOS;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA,ENABLE);
GPIOS.GPIO_Pin=GPIO_Pin_0;
GPIOS.GPIO_Mode=GPIO_Mode_AF_PP;
GPIOS.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIOS);
GPIOS.GPIO_Pin=GPIO_Pin_2;
GPIOS.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIOS);
}
void TIM3_INIT()
{
TIM_TimeBaseInitTypeDef TIMEBASE;
TIM_OCInitTypeDef TIM_OC;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIMEBASE.TIM_Period=9999;
TIMEBASE.TIM_Prescaler=71;
TIMEBASE.TIM_CounterMode=TIM_CounterMode_Up;
TIMEBASE.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIMEBASE);
TIM_OC.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OC.TIM_OutputState=TIM_OutputState_Enable;
TIM_OC.TIM_Pulse=9900;
TIM_OC.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC3Init(TIM3,&TIM_OC);
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3,ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
void TIM2_INIT()
{
TIM_ICInitTypeDef TIM_IC;
TIM_TimeBaseInitTypeDef TIMEBASE;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIMEBASE.TIM_Period=0xffff;
TIMEBASE.TIM_Prescaler=72-1;
TIMEBASE.TIM_CounterMode=TIM_CounterMode_Up;
TIMEBASE.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIMEBASE);
TIM_IC.TIM_Channel=TIM_Channel_3;
TIM_IC.TIM_ICFilter=0;
TIM_IC.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_IC.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_IC.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInit(TIM2,&TIM_IC);
TIM_Cmd(TIM2,ENABLE);
TIM_ARRPreloadConfig(TIM2,ENABLE);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_ITConfig(TIM2,TIM_IT_CC3,ENABLE);
}
void NVIC_INIT()
{
NVIC_InitTypeDef NVICS;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVICS.NVIC_IRQChannel=TIM2_IRQn;
NVICS.NVIC_IRQChannelPreemptionPriority=0;
NVICS.NVIC_IRQChannelSubPriority=0;
NVICS.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVICS);
}
void TIM2_IRQHandler()
{
if(TIM_GetITStatus(TIM2,TIM_IT_CC3)!=0)
{
if(flag==0)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_CC3);
a1=TIM_GetCapture3(TIM2);
flag=1;
}
else if(flag==1)
{
if(TIM_GetFlagStatus(TIM2,TIM_FLAG_Update) != SET)
{
a2=TIM_GetCapture3(TIM2);
period=a2-a1;
}
else
{
TIM_ClearITPendingBit(TIM2,TIM_IT_CC3);
period=0xffff-a1+TIM_GetCapture3(TIM2)+1;
}
flag=0;
frequency=(float)1000000/period;
}
}
}
void init()
{
GPIO_INIT();
TIM3_INIT();
TIM2_INIT();
NVIC_INIT();
}
int main(void)
{
char num[10];
init();
DelayInit();
I2C_Configuration();
OLED_Init();
OLED_CLS();
OLED_ShowStr(0,0,(unsigned char*)"wsw",2);
while(1)
{
sprintf(num,"%f",frequency);
OLED_ShowStr(0,0,(unsigned char*)num,2);
}
} |