用了原子哥的代码 还是有问题,不知道是不是配置的问题,所以希望把工程贴上来看看
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
uint16_t CCR3_Val;
uint16_t flag1;
typedef union taguiDATA{
uint16_t uidata;
struct{
unsigned char data_Hi;
unsigned char data_Lo;
}bytes;
}uiDATA;
uiDATA COM_data;
uint16_t USART_RX_BUF[64]; //接收缓冲,最大64个字节.
//接收状态
//bit7,接收完成标志
//bit6,接收到0x0d
//bit5~0,接收到的有效字节数目
uint16_t USART_RX_STA=0; //接收状态标记
/* Private functions ---------------------------------------------------------*/
void GPIO_Configuration(void); //
void PWM_Configuration(void);
void USART_Configuration(void);
/*******************************************************************************
* Function Name : Delay
* Description : Delay Time
* Input : - nCount: Delay Time
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void Delay (uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
/*******************************************************************************
* Function Name : main
* Description : Main Programme
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
int main(void)
{
uint16_t len;
GPIO_Configuration();
PWM_Configuration();
USART_Configuration();
/* Infinite loop */
while (1)
{
if(USART_RX_STA&0x80)
{
len=USART_RX_STA&0x3f;//得到此次接收到的数据长度
if(len == 2)
{
COM_data.bytes.data_Lo =USART_RX_BUF[0];
COM_data.bytes.data_Hi =USART_RX_BUF[1];
USART_RX_STA = 0;
CCR3_Val = COM_data.uidata;
TIM_SetCompare4(TIM4,CCR3_Val);
Delay(5000);
 WM_Configuration();
}
if(len > 2)
USART_RX_STA = 0;
}
}
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configure GPIO Pin
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO结构体 打开时钟总线,D端口
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD , ENABLE);
/**
* LED1 -> PD8 , LED2 -> PD9 , LED3 -> PD10 , LED4 -> PD11
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_0; //选手端口
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //标志速度
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; ; //配置模式 推免输出
GPIO_Init(GPIOD, &GPIO_InitStructure); //让数据生效,把那些初始化数据写到寄存器里面去,
}
void PWM_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//打开时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
//配置PWM波输出端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 4095; //周期
TIM_TimeBaseStructure.TIM_Prescaler = 0; //分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //初始TIM3
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE); //
TIM_Cmd(TIM4, ENABLE);
}
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//打开时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//配置USART1,USART2引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 Rx
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 Tx
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置USART1,
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_ITConfig(USART1, USART_IT_RXNE, ENABLE);//接收中断使能
//USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
//配置NVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //使能USART1中断
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE); //使能USART1
}
#ifdef USE_FULL_ASSERT
中断服务函数是
extern uint8_t USART_RX_BUF[64]; //接收缓冲,最大64个字节.
//接收状态
//bit7,接收完成标志
//bit6,接收到0x0d
//bit5~0,接收到的有效字节数目
extern uint8_t USART_RX_STA; //接收状态标记
void USART1_IRQHandler(void)
{
uint8_t Res;
//STM_EVAL_LEDToggle(LED2);
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //读取接收到的数据
if((USART_RX_STA&0x80)==0)//接收未完成
{
if(USART_RX_STA&0x40)//接收到了0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始
else USART_RX_STA|=0x80; //接收完成了
}
else //还没收到0X0D
{
if(Res==0x0d)USART_RX_STA|=0x40;
else
{
USART_RX_BUF[USART_RX_STA&0X3F]=Res ;
USART_RX_STA++;
if(USART_RX_STA>63)USART_RX_STA=0;//接收数据错误,重新开始接收
}
}
}
这里实在是不知道对不对,那个没有去控制这个PWM
|