参考:http://www.openedv.com/posts/list/21179.htm
https://blog.csdn.net/wuyuzun/article/details/72851940
源码:
《main.c》
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "timer.h"
#include "math.h"
u32 SIN_LUT_index = 0;
u32 Cnt_Dir = 0;
s32 Sin_Lut[360]= {0};
u8 LowRangeFlag= 0;
u8 HighRangeFlag= 0;
u16 SetUpPara= 1;
u16 Amp1= 500;
u16 Amp2= 1000;
vu8 sign=0;
uint64_t Inverter_i;
uint64_t Inverter_V;
uint64_t bridge_i;
uint64_t Photovoltaic_i;
uint64_t Photovoltaic_V;
ErrorStatus HSEStartUpStatus;
void RCC_Configuration(void);
void TIM_APP_Config(void);
void ADC_APP_Config(void);
void PWM_Init( void )
{
for( SIN_LUT_index=0; SIN_LUT_index<360; SIN_LUT_index++)
{
Sin_Lut[SIN_LUT_index] = 1000*sin(2*3.1415926*(2*SIN_LUT_index+1)/720.0);
}
}
int main(void)
{
#ifdef DEBUG
debug();
#endif
PWM_Init();
/* System Clocks Configuration */
//RCC_Configuration();
TIM_APP_Config();
while(1)
{
}
}
#if 0
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if (HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
// FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
// FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while (RCC_GetSYSCLKSource() != 0x08)
{}
}
}
#endif
#ifdef DEBUG
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
while (1)
{}
}
#endif
《timer.c》
#include "timer.h"
#include "led.h"
#include "usart.h"
vu16 Pulse_A=0,Pulse_B=0;
vu16 Counter_sine=0;
//static uint32_t temp_index= 0;
extern u32 SIN_LUT_index;
extern s32 Sin_Lut[];
extern u8 LowRangeFlag;
extern u8 HighRangeFlag;
extern u16 SetUpPara;
extern u16 Amp1;
extern u16 Amp2;
extern uint64_t Inverter_i;
extern uint64_t