1.这个例子是用于光学器件中,MEMS加电压调转镜角度的工程例子;
2.此例中,用到的技术点有: STM32F103C8, 国内先进的调压芯片GP8401,PWM,FLASH,USART;
3.电路图中有使用MP2359降压芯片(12VL转5V),与LTC3872升压芯片(5V转60V),GP8401的PAC应用;
4.供学习参考,也推荐一下,上海益客电子;
电路原理图如下:
单片机源程序如下:
- /********************************* MDK-ARM Project ****************************
- * Project Name:
- * Firmware Library version: V3.5.0
- * Author:
- * Date:
- * Brief: (mcu--64kflash,20kram,72mhz,26IO)
- * 使用内部时钟,64M, APB1_PCLK1=32M
- ******************************************************************************/
- //----------------------project header flie ------------------------------------------
- #include "stm32f10x.h"
- #include "userconfig.h"
- //#include <string.h>
- //************************* project functions *********************************
- /*******************************************************************************
- * Function Name : Variables_Init(void)
- * Description : This function .
- * Input : None
- * Output : None
- * Return : None
- * Attention : None
- *******************************************************************************/
- static void Variables_Init(void)
- { u8 cnt=0;
-
- GPIO_InitTypeDef GPIO_InitStructure;
-
- //IO口控制MEMS切换的端口初始化
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOA,GPIO_Pin_6|GPIO_Pin_12);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0| GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOB,GPIO_Pin_0|GPIO_Pin_1);
-
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3| GPIO_Pin_4| GPIO_Pin_5| GPIO_Pin_7| GPIO_Pin_8| GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOA,GPIO_Pin_3|GPIO_Pin_4| GPIO_Pin_5| GPIO_Pin_7| GPIO_Pin_11);
- GPIO_SetBits(GPIOA,GPIO_Pin_8);
-
-
- //变量初始化
- for(cnt=0;cnt<12;cnt++) usart1_rx_buffer[cnt]=0;
- usart1_rx_point=0;sign_rxn_cnt=0;
- usart1_rx_count=0;
- data_usart1_rx=0;
- sign_rxn_usart1=0;
- for(cnt=0;cnt<12;cnt++) usart1_tx_buffer[cnt]=0;
- usart1_tx_point=0;
- usart1_tx_count=0;
-
- sign_debug=0;
- sign_clear_all_correct_vol=0;
- sign_x_correct_vol=0;
- set_vol_XZ=0; //最高65.535V
- set_vol_XF=0;
- set_vol_YZ=0;
- set_vol_YF=0;
- current_vol_XZ=0;
- current_vol_XF=0;
- current_vol_YZ=0;
- current_vol_YF=0;
-
- current_ch=0; //开机默认为通道0 各通道电压为0MV
- current_xz=0;
- current_xf=0;
- current_yz=0;
- current_yf=0;
-
-
- // //每次上电将在MCU的FLASH中的各通道及电数MV读取SRAM中,以方使快速切换通道时输出电压到加电端子上
- // if(read_CHx_VOLx_to_SRAMx()) ;
- // else
- // {
- // usart1_tx_buffer[0]='e';
- // usart1_tx_buffer[1]='r';
- // usart1_tx_buffer[2]='r';
- // usart1_tx_buffer[3]='o';
- // usart1_tx_buffer[4]='r';
- // usart1_tx_ptr(usart1_tx_buffer,5);//返回ERROR信息
- // }
- //
- //
- // //最大通道
- // max_ch=STMFLASH_ReadHalfWord(MAX_CH_FLASH_ADDR);
- //
-
- // //定时器PWM初始
- // TIM_SelectOCxM(TIM4, TIM_Channel_1, TIM_OCMode_PWM1); //初始中强置低后,要再重恢复到PWM
- // TIM_SelectOCxM(TIM4, TIM_Channel_2, TIM_OCMode_PWM1);
- // TIM_CCxCmd(TIM4, TIM_Channel_1, TIM_CCx_Enable);
- // TIM_CCxCmd(TIM4, TIM_Channel_2, TIM_CCx_Enable);
- TIM_Cmd(TIM4, ENABLE); //开启PWM输出波形
- flag_mems_busy=IDLE;
- }
- /*******************************************************************************
- * Function Name : int main(void)
- * Description : This function .
- * Input : None
- * Output : None
- * Return : None
- * Attention : None
- *******************************************************************************/
- int main(void)
- {
- //u8 i;
- //uint32_t n=0;
-
- MemsReset:
- NVIC_Configuration();
- RCC_Configuration();
- GPIO_Configuration();
- EXTI_Configuration();
- TIM_Configuration(); //在PB.7上有上升沿时,将在PB.6上产生一个脉冲;
- USART_Configuration();
- USART_ClearFlag(USART1,USART_FLAG_RXNE);
- Variables_Init();//状态初始
-
- mems_IDLE();
- TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Disable); //立即生效
- TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Disable); //立即生效
-
- while(1)
- {
- //IWDG_Feed();
- //if(mems_ALARM );
- //if(!mems_RESET()) goto MemsReset;
-
- if(sign_rxn_usart1==1) // 标记接收到了1帧数据
- {
- receive_info();
- sign_rxn_usart1=0;
- mems_IDLE();
- }
- }
- }
- #ifdef USE_FULL_ASSERT
- /*******************************************************************************
- * Function Name : assert_failed(uint8_t* file, uint32_t line)
- * Description : This function .Reports the name of the source file and
- the source line number where the assert_param error has
- occurred
- * Input : file: pointer to the source file name
- line: assert_param error line source number
- * Output : None
- * Return : None
- * Attention : None
- *******************************************************************************/
- void assert_failed(uint8_t* file, uint32_t 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) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
复制代码
Keil5代码下载(仅供参考):
代码.7z
(369.96 KB, 下载次数: 16)
|