我是靠谱客的博主 淡然萝莉,最近开发中收集的这篇文章主要介绍蓝桥杯基础模块初始化初始化main.c  main.hled.c led.h key.c key.h tick.ctimer.c timer.h  usart.cusart.hadc.cadc.hpwm.c pwm.h i2c.c pwm.c(simple out) 可调duty和frequency  pwm.c(simple input)可调duty和frequencyRTC.c,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录

初始化

main.c 

main.h

led.c 

led.h 

key.c 

key.h 

tick.c

timer.c 

timer.h 

usart.c

usart.h

adc.c

adc.h

pwm.c 

pwm.h

i2c.c

pwm.c(simple out) 可调duty和frequency

pwm.c(simple input)可调duty和frequency

RTC.c


初始化

对STM32板上的基础模块的初始化,每次必须要用的一些基础模块如下:

main.c 

#include "main.h"
int main(void)
{
SysTick_Config(SystemCoreClock/1000);
Delay_Ms(10);
STM3210B_LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
while(1);
}

 main.h

#ifndef __MAIN_H
#define __MAIN_H
#include "stm32f10x.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "led.h"
#include "lcd.h"
#include "adc.h"
#include "tick.h"
#include "timer.h"
#include "key.h"
#include "usart.h"
#endif

led.c 

#include "led.h"
u8 ledstate=0x00;
void LEDInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &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);
LEDcontrol(LEDALL,0);
}
void LEDcontrol(u8 LED,u8 state)
{
if(state==0){
GPIO_SetBits(GPIOC,LED);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}
else {
GPIO_ResetBits(GPIOC,LED);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}
}
void LEDswitch(u8 ledstate)
{
LEDcontrol(LEDALL,0);
LEDcontrol(8<<ledstate,1);
}

led.h 

#ifndef __LED_H
#define __LED_H
#include "main.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 0xff
void LEDInit(void);
void LEDcontrol(u8 LED,u8 state);
void LEDswitch(u8 ledstate);
extern u8 ledstate;
#endif

key.c 

#include "key.h"
void KEYInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
_Bool KEYflag=1;
u8 KEYScan(void)
{
if((!key1||!key2||!key3||!key4)&&KEYflag)
{
Delay_Ms(10);
KEYflag=0;
if
(key1==0) return 1;
else if(key2==0) return 2;
else if(key3==0) return 3;
else if(key4==0) return 4;
}
else if(key1==1&&key2==1&&key3==1&&key4==1)
{
KEYflag=1;
}
return 0;
}

key.h 

#ifndef __KEY_H
#define __KEY_H
#include "main.h"
extern _Bool KEYflag;
#define key1
GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)
#define key2
GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)
#define key3
GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)
#define key4
GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2)
void KEYInit(void);
u8 KEYScan(void);
#endif

tick.c

#include "tick.h"
u32 TimingDelay = 0;
void Delay_Ms(u32 nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}

timer.c 

#include "timer.h"
void TIM2Init(void)
{
TIM_TimeBaseInitTypeDef
TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
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);
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
}
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}

timer.h 

#ifndef __TIMER_H
#define __TIMER_H
#include "main.h"
void TIM2Init(void);
void TIM2_IRQHandler(void);
#endif

 usart.c

#include "usart.h"
uint8_t RxBuffer[20];
__IO uint8_t RxCounter=0 ;
_Bool RXflag=0;
void USART2Init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
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_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART2_IRQHandler(void)
{
char tmp = 0;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
tmp = USART_ReceiveData(USART2);
if( (RxCounter >= 1 && tmp == 'n' && RxBuffer[RxCounter-1] == 'r') || RxCounter >= 20)
{
RxBuffer[RxCounter-1] = 0;
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
RXflag = 1;
}
else{
RxBuffer[RxCounter++] = tmp;
}
}
}
int fputc(int ch, FILE *f)
{
USART_SendData(USART2, (uint8_t) ch);
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
return ch;
}

usart.h

#ifndef __USART_H
#define __USART_H
#include "main.h"
extern uint8_t RxBuffer[20];
extern __IO uint8_t RxCounter ;
extern _Bool RXflag;
void USART2Init(void);
int fputc(int ch, FILE *f);
#endif

adc.c

#include "adc.h"
void ADC1Init(void)
{
ADC_InitTypeDef
ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//NVIC_InitTypeDef NVIC_InitStructure;
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1 , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_239Cycles5);
//多通道模式下把上面这个写到get(通道)函数里,然后加上software软件转换
//ADC_ITConfig(ADC1, ADC_IT_JEOC, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
//	NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn;
//	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//	NVIC_Init(&NVIC_InitStructure);
}
float Adc_GetVal(void)
{
float temp=0;
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
temp = ADC_GetConversionValue(ADC1)*3.3/4095;
return temp;
}

adc.h

#ifndef __ADC_H
#define __ADC_H
#include "main.h"
void ADC1Init(void);
float Adc_GetVal(void);
#endif

pwm.c 

#include "pwm.h"
u16 IC2Value = 0;
u16 IC3Value = 0;
u16 Frequency_1 = 0;
u16 Frequency_2 = 0;
//u16 DutyCycle = 0;
//u16 Frequency = 0;
u16 CCR1_Val=200;
u16 CCR1_ZKB=0.3;
void Timer2_PwmIn(void)
//void Timer3_PwmOut(u16 arr,u16 psc);
{
TIM_ICInitTypeDef
TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef
TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
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);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 0xffff;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
//psc
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
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_PWMIConfig(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_PWMIConfig(TIM2, &TIM_ICInitStructure);
//	TIM3CH1_set(0);
//
TIM3CH2_set(0);
//TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
测频率要关掉,测占空比打开
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
TIM_Cmd(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_CC2|TIM_IT_CC3, ENABLE);
}
void TIM2_IRQHandler(void)
{
__IO uint16_t temp;
static __IO uint16_t IC1Value_temp,IC2Value_temp;
if(TIM_GetITStatus(TIM2, TIM_IT_CC2)!= RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
IC2Value = TIM_GetCapture2(TIM2);
temp = IC2Value - IC1Value_temp;
if(temp==0)
Frequency_1=0;
else{
Frequency_1 = SystemCoreClock/72/temp;
}
IC1Value_temp = IC2Value;
}
if(TIM_GetITStatus(TIM2, TIM_IT_CC3)!= RESET)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_CC3);
IC3Value = TIM_GetCapture3(TIM2);
temp = IC3Value - IC2Value_temp;
if(temp==0)
Frequency_2=0;
else{
Frequency_2 = SystemCoreClock/72/temp;
}
IC2Value_temp = IC3Value;
}
//
void TIM3_IRQHandler(void)
//
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
//
IC2Value = TIM_GetCapture1(TIM2);
//
if (IC2Value != 0)
//
{
//
DutyCycle = (TIM_GetCapture2(TIM3) * 100) / IC2Value;
//
Frequency = SystemCoreClock/72/IC2Value;
//
}
//
else
//
{
//
DutyCycle = 0;
//
Frequency = 0;
//
}
}
uint16_t capture = 0;
_Bool pa6_state=0,pa7_state=0;
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 );
capture = TIM_GetCapture1(TIM3);
if(pa6_state==0)
{
TIM_SetCompare1(TIM3,(u16)(capture + CCR1_Val*CCR1_ZKB));
pa6_state=1;
}
else if(pa6_state==1)
{
TIM_SetCompare1(TIM3,(u16)(capture + CCR1_Val*(1-CCR1_ZKB)));
pa6_state=0;
}
}
}
void TIM3CH1_set(u8 status)
{
TIM_OCInitTypeDef
TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
if(status)
{
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_ITConfig(TIM3, TIM_IT_CC1 , ENABLE);
}
else
{
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
TIM_ITConfig(TIM3, TIM_IT_CC1 , DISABLE);
}
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
}

pwm.h

#ifndef __PWM_H
#define __PWM_H
#include "main.h"
extern u16 IC2Value;
extern u16 IC3Value;
extern u16 Frequency_1;
extern u16 Frequency_2;
//extern u16 DutyCycle = 0;
//extern u16 Frequency = 0;
extern
u16
capture ;
extern _Bool pa6_state;
extern _Bool pa7_state;
extern u16 CCR1_Val;
extern u16 CCR1_ZKB;
void Timer2_PwmIn(void);
void TIM2_IRQHandler(void);
#endif

 i2c.c

void At24c02_U8write(u8 add,u8 data)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(add);
I2CWaitAck();
I2CSendByte(data);
I2CWaitAck();
I2CStop();
delay1(500);
}
void At24c02_U32write(u8 add,u32 data)
{
At24c02_U8write(add+0,(data>>0)&0xff);
At24c02_U8write(add+1,(data>>8)&0xff);
At24c02_U8write(add+2,(data>>16)&0xff);
At24c02_U8write(add+3,(data>>24)&0xff);
delay1(500);
}
u8 At24c02_U8read(u8 add)
{
u8 Tmp = 0;
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(add);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
Tmp=I2CReceiveByte();
I2CStop();
delay1(500);
return Tmp;
}
u32 At24c02_U32read(u32 add)
{
u32 tmp=0;
tmp |=((u32)At24c02_U8read(add+0))<<0;
tmp |=((u32)At24c02_U8read(add+1))<<8;
tmp |=((u32)At24c02_U8read(add+2))<<16;
tmp |=((u32)At24c02_U8read(add+3))<<24;
delay1(500);
return tmp;
}

 pwm.c(simple out) 可调duty和frequency

#include "timer.h"
uint16_t CCR1_Val=2000;
uint16_t CCR2_Val=1000;
float CH1duty=0.3;
float CH2duty=0.6;
void TIM3_Init()
{
TIM_TimeBaseInitTypeDef
TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_OCInitTypeDef
TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
//用timing无法软件仿真
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Disable);
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Disable);
TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 , ENABLE);
TIM_Cmd(TIM3, ENABLE);
GPIO_SetBits(GPIOA,GPIO_Pin_6 | GPIO_Pin_7 );
}
_Bool CH1flag=1;
_Bool CH2flag=1;
uint16_t capture=0;
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
{
capture = TIM_GetCapture1(TIM3);
if(CH1flag)
{
//看情况选择是否乘以2,会导致频率不一样
TIM_SetCompare1(TIM3, capture + (u16)(2*CCR1_Val*CH1duty));
CH1flag=0;
}
else
{
TIM_SetCompare1(TIM3, capture + (u16)(2*CCR1_Val*(1-CH1duty)));
CH1flag=1;
}
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
GPIO_WriteBit(GPIOA, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_6)));
}
else if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
{
capture = TIM_GetCapture2(TIM3);
if(CH2flag)
{
TIM_SetCompare1(TIM3, capture + (u16)(2*CCR2_Val*CH2duty));
CH2flag=0;
}
else
{
TIM_SetCompare1(TIM3, capture + (u16)(2*CCR2_Val*(1-CH2duty)));
CH2flag=1;
}
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
GPIO_WriteBit(GPIOA, GPIO_Pin_7, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_7)));
}
}


 pwm.c(simple input)可调duty和frequency

__IO uint16_t IC3ReadValue1 = 0, IC3ReadValue2 = 0;
__IO uint16_t CaptureNumber = 0;
__IO uint32_t Capture = 0;
__IO uint32_t TIM3Freq = 0;
void TIM3_inputInit()
{
TIM_TimeBaseInitTypeDef
TIM_TimeBaseStructure;
TIM_ICInitTypeDef
TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//只捕获频率
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(TIM3, &TIM_ICInitStructure);
/* TIM enable counter */
TIM_Cmd(TIM3, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
//捕获频率和占空比
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_PWMIConfig(TIM3, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2); //CC2为主模式,则CC1就为从模式
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
TIM_Cmd(TIM3, ENABLE);
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
}
//只捕获频率
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_CC2) == SET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
if(CaptureNumber == 0)
{
IC3ReadValue1 = TIM_GetCapture2(TIM3);
CaptureNumber = 1;
}
else if(CaptureNumber == 1)
{
IC3ReadValue2 = TIM_GetCapture2(TIM3);
Capture = (uint16_t)(IC3ReadValue2 - IC3ReadValue1);
TIM3Freq = (uint32_t) SystemCoreClock / Capture;
CaptureNumber = 0;
}
}
}
//捕获频率和占空比
void TIM3_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
IC2Value = TIM_GetCapture2(TIM3);
//TIM_GetCapture2是主模式
if (IC2Value != 0)
{
DutyCycle = (TIM_GetCapture1(TIM3) * 100) / IC2Value;
//TIM_GetCapture1是从模式
Frequency = SystemCoreClock / IC2Value;
}
else
{
DutyCycle = 0;
Frequency = 0;
}
}

RTC.c

#include "rtc.h"
u32 TimingDelay=0;
__IO uint32_t TimeDisplay = 0;
void Delay_Ms(u32 nTime)
{
TimingDelay=nTime;
while(TimingDelay != 0);
}
void RTC_Init()
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Reset Backup Domain */
BKP_DeInit();
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait till LSI is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
/* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(40000);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* To output second signal on Tamper pin, the tamper functionality
must be disabled (by default this functionality is disabled) */
BKP_TamperPinCmd(DISABLE);
//
/* Enable the RTC Second Output on Tamper Pin */
//
BKP_RTCOutputConfig(BKP_RTCOutputSource_Second);
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Time_Adjust(uint32_t THH,uint32_t TMM,uint32_t TSS)
{
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Change the current time */
RTC_SetCounter(THH*3600+TMM*60+TSS);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
/**
* @brief
Displays the current time.
* @param
TimeVar: RTC counter value.
* @retval None
*/
void Time_Display(uint32_t TimeVar)
{
uint32_t THH = 0, TMM = 0, TSS = 0;
/* Reset RTC Counter when Time is 23:59:59 */
if (RTC_GetCounter() == 0x0001517F)
{
RTC_SetCounter(0x0);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
/* Compute
hours */
THH = TimeVar / 3600;
/* Compute minutes */
TMM = (TimeVar % 3600) / 60;
/* Compute seconds */
TSS = (TimeVar % 3600) % 60;
printf("Time: %0.2d:%0.2d:%0.2dr", THH, TMM, TSS);
}
/**
* @brief
Shows the current time (HH:MM:SS) on the Hyperterminal.
* @param
None
* @retval None
*/
void Time_Show(void)
{
printf("nr");
/* Infinite loop */
while (1)
{
/* If 1s has been elapsed */
if (TimeDisplay == 1)
{
/* Display current time */
Time_Display(RTC_GetCounter());
TimeDisplay = 0;
}
}
}
void RTC_IRQHandler(void)
{
if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
{
RTC_ClearITPendingBit(RTC_IT_SEC);
TimeDisplay = 1;
RTC_WaitForLastTask();
}
}

最后

以上就是淡然萝莉为你收集整理的蓝桥杯基础模块初始化初始化main.c  main.hled.c led.h key.c key.h tick.ctimer.c timer.h  usart.cusart.hadc.cadc.hpwm.c pwm.h i2c.c pwm.c(simple out) 可调duty和frequency  pwm.c(simple input)可调duty和frequencyRTC.c的全部内容,希望文章能够帮你解决蓝桥杯基础模块初始化初始化main.c  main.hled.c led.h key.c key.h tick.ctimer.c timer.h  usart.cusart.hadc.cadc.hpwm.c pwm.h i2c.c pwm.c(simple out) 可调duty和frequency  pwm.c(simple input)可调duty和frequencyRTC.c所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部