|
#include "stm32f10x.h"
#include "OLED12864.h"
#include "comm.h"
#include "keyled.h"
Systemtypedef System;
ADCtypedef ADResult[2];
extern u32 ADC_BUF[2];
u32 temp=0; //电压数据
//unsigned short ADResult;
/**
* @brief STM32时钟系统初始化
* SYSClk= 72MHz
* APB1 = 36MHz
* APB2 = 72MHz
* AHB = 72MHz
* ADC_Clk = 18MHz
* @参数 None
* @返回值 None
*/
void Stm32_Clock_Init(void)
{
RCC->CR|=0x00010000; //外部高速时钟使能HSEON
while(!(RCC->CR>>17)); //等待外部时钟就绪
/* PLL=9*HSE OSC, APB1=AHB/DIV2, APB2=AHB/DIV1, ADC_Clk = APB2/4 */
RCC->CFGR=0X1C4400;
RCC->CFGR|=1<<16; //HSE as PLLSRC
FLASH->ACR|=0x32; //FLASH 2个延时周期
RCC->CR|=0x01000000; //PLLON
while(!(RCC->CR>>25)); //等待PLL锁定
RCC->CFGR|=0x00000002; //PLL作为系统时钟
while((RCC->CFGR & 0x0c) != 0x08); //wait,PLL used as system clock
}
void SystemVar_Init(void)
{
System.HPriTime = 0;
System.LPriTime = 0;
}
/**
* @brief 用户任务调度定时器初始化.
* 周期1ms.
* 使能更新中断.
* @参数 None
* @返回值 None
*/
void UserTaskTimer_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //???
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_Period = 1000;//1ms时间设定
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
TIM_Cmd(TIM3, ENABLE);
}
/**
* @brief 用户任务调度定时器更新中断.
* 启动ADC转换.
* 软定时器更新.
* @参数 None
* @返回值 None
*/
void TIM3_IRQHandler(void)
{
static u32 TimeCount = 0; //???
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
TimeCount++;
if(TimeCount % HPRITASK_TIME_MS == 0) System.HPriTime = 1;
if(TimeCount % LPRITASK_TIME_MS == 0) System.LPriTime = 1;
}
}
int main(void)
{
u32 wd;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Stm32_Clock_Init();
SystemVar_Init();
LED_Init();
AD_init();
ADvar_init();
UserTaskTimer_Init();
OLED_Init();
OLED_Fill(0);
// OLED_P8x16Str(0,0,"BLDC CLOSED LOOP");
// OLED_P8x16Str(5,2,"Targe: rpm");
//// OLED_P8x16Str(5,4,"Real : rpm");
//// OLED_P8x16Str(5,6,"VBUS : mV");
// OLED_P8x16Str(5,6,"D_Y : mV");
// OLED_P8x16Str(5,4,"W_D : mV");
OLED_ShowCHinese(0,4,19);//运
OLED_ShowCHinese(18,4,20);//行
OLED_ShowCHinese(36,4,21);//时
OLED_ShowCHinese(54,4,22);//间
OLED_ShowCHinese(72,4,13);//
LED1 = LED2 = LED3 = 0;
LED4 = LED5 = 1;
while(1)
{
if(System.HPriTime)
{
System.HPriTime = 0;
// do something
LED2 = !LED2;
}
else if(System.LPriTime)
{
//刷新HMI信息
System.LPriTime = 0;
temp = ((ADResult[0].avg*3300)/4096); //电压数据转换
wd = ((ADResult[1].avg*3300)/4096);
Display_data(50,6,temp,0); //OLED打印电压数据
Display_data(50,4,wd,0); //OLED打印电压数据
}
}
}
|
|