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)
|