stm32單片機源程序:
- /************************************************************
- ************************************************************/
- #include "bsp_TB6600_TIM.h"
- #include "stm32f10x.h"
- #include "stm32f10x_usart.h"
- #include <stdio.h>
- char RX=0;
- extern __IO uint16_t prescaler;//這個參數控制pwm頻率
- /////////////////////////串口函數///////////////////////////////
- void Printf_Configuration(void);
- void Usart_GPIO_Configuration(void);
- ////////////////////////////////////////////////////////////////
- void RCC_Configuration(void);
- void Limit(uint16_t F);//速度限制
- ////////////////////////////////////////////////////////////////
- //////////////////////printf函數////////////////////////////////
- int fputc(int ch,FILE*f)
- {
- while(USART_GetFlagStatus( USART1, USART_FLAG_TC)==RESET);//等待發送完畢
- USART_SendData(USART1,ch);//將ch送給usart1
- return(ch);//返回ch
- }
- //因為printf()之類的函數,使用了半主機模式。使用標準庫會導致程序無法運行。有兩種解決方法
- //1.使用微庫。魔術棒——Target——Code Generation選擇使用Use MicroLIB
- //2.添加如下程序代碼
- struct __FILE
- {
- int handle;
- /* Whatever you require here. If the only file you are using is */
- /* standard output using printf() for debugging, no file handling */
- /* is required. */
- };
- FILE __stdout; // FILE is typedefed in stdio.h.
- ///////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////
- int main(void)
- {
- RCC_Configuration();
- TB6600_TIMx_PWM_Init();
- Usart_GPIO_Configuration();
- Printf_Configuration();
- printf("start");
- while(1) //必須有while(1)否側程序就會跑飛
- {
- while(USART_GetFlagStatus( USART1, USART_FLAG_RXNE)==RESET);
- RX=USART_ReceiveData(USART1);
- if(RX=='Z')//原速300r/s
- {
- prescaler=125;
- printf("speed:300r/s\n\r");
- }
- if(RX=='A')//加速600r/s
- {
- prescaler=62;
- printf("speed:600/s\n\r");
- }
- if(RX=='E')//減速150r/s
- {
- prescaler=250;
- printf("speed:150/s\n\r");
- }
- }
- }
- void RCC_Configuration(void)
- {
- RCC_DeInit(); //初始化缺省值
- RCC_HSEConfig(RCC_HSE_ON);//使能外部高速時鐘
- while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
- FLASH_SetLatency(FLASH_Latency_2);
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- RCC_HCLKConfig(RCC_SYSCLK_Div1); //HCLK=SYSCLK
- RCC_PCLK2Config(RCC_HCLK_Div1); //PCLK2=HCLK
- RCC_PCLK1Config(RCC_HCLK_Div2); //PCLK1=HCLK/2
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- RCC_PLLCmd(ENABLE);
- 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);
- /* Enable GPIOB, GPIOC and AFIO clocks */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE);
- }
- void Usart_GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* Configure USARTX_TX as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* Configure USARTX_RX as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA, ENABLE);
- }
- void Printf_Configuration(void)
- {
- USART_InitTypeDef USART_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , 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(USART1, &USART_InitStructure);
- USART_Cmd(USART1, ENABLE);
- }
- void USART2_IRQHandler(void)
- {
- if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)//判斷中斷來源標志
- {
- USART_ClearFlag(USART2,USART_IT_RXNE);//清除中斷標志
- while(USART_GetFlagStatus( USART2,USART_FLAG_RXNE)==RESET);//判斷接收標志,否則數據會丟失
-
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
完整代碼下載:
57步進電機測試.zip
(2.06 MB, 下載次數: 125)
2017-5-9 14:43 上傳
點擊文件名下載附件
|