#include "stm32f10x.h"
void NVIC_Configuration();
void initializ(void)
{
int CNT=200;
while(CNT--);
(RCC->CR) = (RCC->CR) | RCC_CR_HSEON;//使能外部振荡器
CNT=200;
while(CNT--);
while(!((RCC->CR) & RCC_CR_HSERDY));//确认外部时钟
(RCC->CFGR) = 0x0413640A;//PLL输出作为系统时钟;(AHB)SYSCLK不分频 ;(低速APB1)HCLK 2分频(输出24M);
//高速(APB2) 2分频(24M);ADC4分频(12M);HSE时钟作为PLL输入时钟,PLL输出48M
(RCC->CFGR2) = 0x00000001;//PREDIV1对输入时钟2分频;HSE振荡器时钟作为PREDIV1的时钟源;
(RCC->CR) = (RCC->CR) | RCC_CR_PLLON;//使能PLL
CNT=2000;
while(CNT--);
while(!((RCC->CR) & RCC_CR_PLLRDY));//确认
(RCC->CR) = (RCC->CR) & ~RCC_CR_HSION;//关闭HSI
/****GPIO端口设置*****/
(RCC->APB2ENR) = 0x0000003C;//端口A,B,C,D时钟使能
(GPIOC->CRL) = 0x44440300;//PC0,PC1,PC3设为(ADC12)模拟输入,PC2设为通用推挽输出
(GPIOA->CRL) = 0x4B444B33;//PA0,PA1设为通用推挽输出,PA2,PA6设为复用推挽输出,PA3复用输入
(GPIOA->CRH) = 0x444444B4;
(GPIOB->CRH) = 0x444444BB;
//(GPIOA->ODR) |= 0X0200;//PA9置1
/****系统中断配置*****/
/*NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
SCB->AIRCR = 0x05FA0500;//采用中断分组模式2
NVIC->ISER[TIM2_IRQn/32] |= 1<<(TIM2_IRQn % 32);//开启TIM2中断
NVIC->IP[TIM2_IRQn] = 0xA << 4;//抢占优先级2,响应优先级2
NVIC->ISER[TIM4_IRQn/32] |= 1<<(TIM4_IRQn % 32);//开启TIM4中断
NVIC->IP[TIM4_IRQn] = 0x5 << 4;//抢占优先级1,响应优先级1
*/
NVIC_Configuration();
//NVIC_SETPRIMASK(); // Disable Interrupts
//NVIC_RESETPRIMASK(); // Enable Interrupts
/****TIM3产生500K输出信号*****/
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;//TIM3时钟开启
TIM3->ARR=95;//设定计数器自动重装值
TIM3->PSC=0;//不预分频
TIM3->CCMR1|=6<<4;//CH1 PWM1模式
TIM3->CCMR1|=1<<3;//CH1 预装载使能
TIM3->CR1|=1<<4;//向下计数模式
TIM3->CCER|=3<<0;//OC1输出使能
TIM3->CCR1=47;//设定占空比50%
TIM3->EGR|=1<<0;//初始化计数器
TIM3->CR1|=1<<7;//ARPE使能 自动重装载预装载允许位
TIM3->CR1|=1<<0;//使能定时器TIM3
//TIM3->DIER |= 0x0002;//TIM3_DIER_CC1IE;
TIM3->SR = 0x0000;
/****TIM4产生500uS定时*****/
RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; //TIM4时钟开启
TIM4->ARR=960;//设定计数器自动重装值
TIM4->PSC=0;//不预分频
TIM4->CCMR2|=6<<4;//CH3 PWM1模式
TIM4->CCMR2|=6<<12;//CH4翻转模式
TIM4->CCMR2|=1<<11;//CH4预装载使能
TIM4->CR1|=1<<4;//向下计数模式
TIM4->CCER|=3<<8;//OC3输出使能
TIM4->CCER|=3<<12;//OC4输出使能
TIM4->CCR3=480;//设定占空比50%
TIM4->CCR4=480;//设定占空比50%
//TIM4->DIER |= TIM_DIER_CC4IE;//允许捕获/比较4中断
TIM4->DIER |= TIM_DIER_UIE;//允许TIM4中断更新
TIM4->EGR|=1<<0;//初始化计数器
TIM4->CR1|=1<<7;//ARPE使能 自动重装载预装载允许位
TIM4->CR1|=1<<0;//使能定时器TIM4
TIM4->SR = 0x0000;
/****TIM2定时*****/
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; //TIM2时钟开启
TIM2->ARR=9600;//设定计数器自动重装值
TIM2->PSC=0;//不预分频
TIM2->CCMR1|=3<<4;//CH1翻转模式
//TIM2->CCMR1|=3<<12;//CH2翻转模式
TIM2->CCMR1|=1<<3;//CH1预装载使能
TIM2->CR1|=1<<4;//向下计数模式
TIM2->CCER|=3<<0;//OC1输出使能
TIM2->CCER|=3<<4;//OC2输出使能
TIM2->CCR1=4800;//设定占空比50%
//TIM2->DIER |= TIM_DIER_CC1IE;//允许捕获/比较1中断
TIM2->DIER |= TIM_DIER_UIE;//允许TIM2中断更新
//TIM2->EGR|=1<<0;//初始化计数器
//TIM2->SR = 0x0000;
TIM2->CR1|=1<<7;//ARPE使能 自动重装载预装载允许位
//NVIC_TIM2Configuration();
TIM2->CR1|=1<<0;//TIM_CR1_CEN使能定时器TIM2
/****ADC设置*****/
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;//ADC1时钟开启
ADC1->CR1 |= ADC_CR1_SCAN;//开启ADC1扫描模式
//ADC1->CR2 |= ADC_CR2_TSVREFE;//启用温度传感器和VREFINT
//ADC1->CR2 |= ADC_CR2_EXTTRIG;//使用外部事件启动转换
//ADC1->CR2 |= 5<<17;//TIM4的CC4事件启动规则转换
ADC1->CR2 |= ADC_CR2_DMA;//使用DMA
ADC1->CR2 |= ADC_CR2_RSTCAL;//初始化校准寄存器
//ADC1->SMPR1 |= 1 | 1<<3 | 1<<6 | 1<<9;//ADC10,11,12,13采样时间7.5周期
ADC1->CR2 |= ADC_CR2_ADON;//把ADC从断电模式下唤醒(第一次写入“1”)
ADC1->SQR1 |= 2<<20;//扫描转换4个规则通道
ADC1->SQR3 = 11 | 13<<5 | 10<<10;//ADC序列AD11,AD12,AD13,AD10
//ADC1->CR2 |= ADC_CR2_ADON;//启动转换
ADC1->CR2 |= ADC_CR2_RSTCAL;//开启复位校准
while(ADC1->CR2 & ADC_CR2_RSTCAL);//等待复位校准结束
ADC1->CR2 |= ADC_CR2_CAL;//开AD位校准
while(ADC1->CR2 & ADC_CR2_CAL);//等待AD校准结束
/****DMA设置*****/
RCC->AHBENR |= RCC_AHBENR_DMA1EN;//DMA1时钟开启
DMA1_Channel1->CCR |= 1<<12; //PL 通道优先级中
DMA1_Channel1->CCR |= 1<<10; //MSIZE存储器数据宽度16位
DMA1_Channel1->CCR |= 1<<8; //PSIZE外设数据宽度16位
DMA1_Channel1->CCR |= 1<<7; //MINC执行存储器地址增量操作
DMA1_Channel1->CCR |= 1<<5; //CURC执行循环操作
DMA1_Channel1->CNDTR = 300; //传输数量
DMA1_Channel1->CPAR = (uint32_t)&ADC1->DR;//外设地址
DMA1_Channel1->CMAR = (uint32_t)&AD_C;//存储器起始地址
DMA1_Channel1->CCR |= DMA_CCR1_EN;//开启DMA
/****USART设置*****/
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;//使能USART1时钟
USART1->CR1 |= USART_CR1_UE;//USART模块使能
USART1->CR1 |= USART_CR1_PEIE;//PE中断使能
USART1->CR1 |= USART_CR1_TXEIE;//发送缓冲区空中断使
USART1->CR1 |= USART_CR1_RXNEIE;//接收缓冲区非空中断使能
USART1->CR2 |= 0<<12;//一个停止位
USART1->BRR = 5000;//波特率4.8KHz
}
void NVIC_Configuration(void)//配置TIM的优先级
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}