用的是STM32107vc外部晶振本人把原来的25M换成了8M的
#include <stm32f10x_lib.h>
#include "usart.h"
#include "delay.h"
vu16 T1_H,T2_H,T3_H,T4_H;
//unsigned char counter=0;
//unsigned char sign,Temp[11];
//float w[3],angle[3],T;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/******************配置RCC服务函数************************/
void RCC_Config(void)
{
ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
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)
{
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOE,ENABLE); //开启GPIOA,USART1时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //开启TIM3时钟
}
/******************配置GPIOA服务函数************************/
void GPIOA_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Tx (PA9) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//PA9时钟速度50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//PA9时钟速度50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
/******************配置USART1服务函数************************/
void USART1_Config(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate =115200; //波特率115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1个停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //奇偶失能
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制失能
USART_InitStructure.USART_Mode = USART_Mode_Rx ; //接收使能
USART_Init(USART1, &USART_InitStructure); //初始化结构体
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 使能指定的USART1接收中断
USART_Cmd(USART1, ENABLE); //使能串口1
}
/******************配置TIM2_PWM服务函数************************/
void TIM2_PWM_Config(void)
{
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Output Compare Toggle Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = T1_H;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
/* Output Compare Toggle Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = T2_H;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
/* Output Compare Toggle Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState =TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = T3_H;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
/* Output Compare Toggle Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState =TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = T4_H;
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
/* TIM enable counter */
TIM_Cmd(TIM2, ENABLE);
}
/******************配置串口1中断服务函数************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure; //定义数据结构体
//NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);//将中断矢量放到Flash的0地址
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//设置优先级配置的模式,详情请阅读原材料中的文章
//使能串口中断,并设置优先级
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); //将结构体丢到配置函数,即写入到对应寄存器中
}
unsigned char USART2_ReceiveChar(void)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
return(USART_ReceiveData(USART1));
}
void PWM_word_processing(void)
{
// delay_init(72);
// sign=1;
// while(sign)
// {
// if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断有效,若接收数据寄存器满
// {
// Temp[counter] = USART_ReceiveData(USART1); //接收数据
// USART_GetFlagStatus(USART1, USART_FLAG_ORE|USART_FLAG_RXNE );
// delay_ms(1);
// if(counter == 0 && Temp[0] != 0x55) break; //第 0 号数据不是帧头,跳过
// counter++;
// if(counter==11) //接收到 11 个数据
// {
// GPIO_ResetBits(GPIOE,GPIO_Pin_7);
// counter=0; //重新赋值,准备下一帧数据的接收
// sign=0;
// delay_ms(10);
// GPIO_SetBits(GPIOE,GPIO_Pin_7);
// }
// }
// }
//
// while(sign)
// {
// //PWM_word_processing ();
// USART2_ReceiveChar();
// Temp[counter++]=USART_ReceiveData(USART1);
// if(counter==11)
// {
// counter=0;
// sign=0;
// }
// }
// if(Temp[0]==0x55) //检查帧头
// {
// switch(Temp[1])
// {
// case 0x52: //标识这个包是角速度包
// w[0] = ((short)(Temp[3]<<8| Temp[2]))/32768.0*2000; //X轴角速度
// w[1] = ((short)(Temp[5]<<8| Temp[4]))/32768.0*2000; //Y轴角速度
// w[2] = ((short)(Temp[7]<<8| Temp[6]))/32768.0*2000; //Z轴角速度
// T = ((short)(Temp[9]<<8| Temp[8]))/340.0+36.25; //温度
// break;
// case 0x53: //标识这个包是角度包
// angle[0] = ((short)(Temp[3]<<8| Temp[2]))/32768.0*180; //X轴滚转角(x 轴)
// angle[1] = ((short)(Temp[5]<<8| Temp[4]))/32768.0*180; //Y轴俯仰角(y 轴)
// angle[2] = ((short)(Temp[7]<<8| Temp[6]))/32768.0*180; //Z轴偏航角(z 轴)
// break;
// default: break;
// }
// }
}
这是主函数
#include "stm32f10x_lib.h"
#include "usart.h"
#include "delay.h"
extern float w[3],angle[3],T;
extern vu16 T1_H,T2_H,T3_H,T4_H;
unsigned int cs[111];
unsigned int io=0;
void PWM_adjustment(void)
{
;
}
void main(void)
{
RCC_Config();
GPIOA_Config();
USART1_Config();
TIM2_PWM_Config();
delay_init(72);
NVIC_Configuration() ; // 0x08000000
while(1)
{
PWM_word_processing ();
// usart2_receivechar();
// cs[io++]=usart_receivedata(usart1);
// if(io==111)
// {
// io=0;
// usart1->sr&=0<<3;
// delay_ms(50);
// }
}
}
这是中断服务函数
void USART1_IRQHandler(void)
{
sign=1;
while(sign)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断有效,若接收数据寄存器满
{
USART_ClearFlag(USART1,USART_FLAG_RXNE);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
Temp[counter] = USART_ReceiveData(USART1); //接收数据
if(counter == 0 && Temp[0] != 0x55) break; //第 0 号数据不是帧头,跳过
counter++;
if(counter==11) //接收到 11 个数据
{
GPIO_ResetBits(GPIOE,GPIO_Pin_7);
delay_ms(10);
GPIO_SetBits(GPIOE,GPIO_Pin_7);
counter=0; //重新赋值,准备下一帧数据的接收
sign=0;
}
}
}
if(Temp[0]==0x55) //检查帧头
{
switch(Temp[1])
{
case 0x52: //标识这个包是角速度包
w[0] = ((short)(Temp[3]<<8| Temp[2]))/32768.0*2000; //X轴角速度
w[1] = ((short)(Temp[5]<<8| Temp[4]))/32768.0*2000; //Y轴角速度
w[2] = ((short)(Temp[7]<<8| Temp[6]))/32768.0*2000; //Z轴角速度
T = ((short)(Temp[9]<<8| Temp[8]))/340.0+36.25; //温度
break;
case 0x53: //标识这个包是角度包
angle[0] = ((short)(Temp[3]<<8| Temp[2]))/32768.0*180; //X轴滚转角(x 轴)
angle[1] = ((short)(Temp[5]<<8| Temp[4]))/32768.0*180; //Y轴俯仰角(y 轴)
angle[2] = ((short)(Temp[7]<<8| Temp[6]))/32768.0*180; //Z轴偏航角(z 轴)
break;
default: break;
}
}
}
此帖出自
小平头技术问答
一周热门 更多>