哪位大神帮小弟弟看看程序,程序烧进去后串口的数据是:F6 F4 ,小弟不解请求大神帮忙看看 ,谢谢!
程序如下:
#include "
STM32f10x.h"
#include
#define GPIO_LED1 GPIO_Pin_2
#define GPIO_LED2 GPIO_Pin_3
#define GPIO_LED3 GPIO_Pin_4
#define GPIO_LED4 GPIO_Pin_7
#define GPIO_LED_ALL GPIO_LED1 |GPIO_LED2 |GPIO_LED3 |GPIO_LED4
#define UART2_TX_IO GPIO_Pin_5
#define UART2_RX_IO GPIO_Pin_6
void Init(void);
void InitClock(void);
void InitGPIO(void);
void InitUart(void);
void Delayms(u32 Z);
void Delayus(u32 Z);
void FailCLKInit(void);
void UartSendChar(u8 TXBuf);
void UartSendString(const u8 *ADDPointer);
void UartReceiveChar(u8 RXBuf) ;
int main(void)
{
Init();
while(1)
{
GPIO_SetBits(GPIOD , GPIO_LED_ALL);
UartSendChar(0x38);
Delayms(800);
GPIO_ResetBits(GPIOD , GPIO_LED_ALL);
UartSendChar(0x31);
Delayms(800);
}
}
void Init(void)
{
InitClock() ;
InitGPIO();
InitUart();
}
void InitClock(void)
{
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON); // HSE = 25MHz
if(SUCCESS == RCC_WaitForHSEStartUp())
{
// FLASH_PrefetchBufferCmd(ENABLE); // ??
// FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1); // AHB CLOCK(HCLK) = SYSTEM CLOCK /1
RCC_PCLK1Config(RCC_HCLK_Div2); // APB1 CLOCK = HCK/2 max 36MHz
RCC_PCLK2Config(RCC_HCLK_Div1); // APB2 CLOCK = HCK/1 max 72MHz
// debug
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_2); // PLLSOURCECLK = HSECLK/1 , PLLCLKOUT = SOURCECLK *2
RCC_PLLCmd(ENABLE); // START PLL
while(RESET == RCC_GetFlagStatus(RCC_FLAG_PLLRDY)); // WAIT PLLCLK READY
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //SYSTEMCLK = PLLCLKOUT 25*2 = 50 MHz
while(RCC_GetSYSCLKSource() != 0x08) ; // WAIT SYSTEMCLK = PLLCLKOUT SUCCESFUL
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
else
FailCLKInit();
}
void InitGPIO(void)
{
GPIO_InitTypeDef GPIOD_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE); // ENABLE GPIOD CLOCK
GPIOD_InitStructure.GPIO_Pin = GPIO_LED_ALL;
GPIOD_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIOD_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // PUSH-PULL OUTPUT
GPIO_Init(GPIOD, &GPIOD_InitStructure);
GPIOD_InitStructure.GPIO_Pin = UART2_TX_IO;
GPIOD_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIOD_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // PUSH-PULL OUTPUT
GPIO_Init(GPIOD, &GPIOD_InitStructure);
GPIOD_InitStructure.GPIO_Pin = UART2_RX_IO;
GPIOD_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIOD_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOA
tiNG; // PUSH-PULL INPUT
GPIO_Init(GPIOD, &GPIOD_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE) ; // REMAP GPIO
GPIO_SetBits(GPIOD , GPIO_LED_ALL); // LED OFF
}
void FailCLKInit(void)
{
}
void InitUart(void)
{
USART_InitTypeDef USART2_InitStructure;
float temp;
u16 mantissa;
u16 fraction;
temp=(float)(25*1000000)/(115200*16);//得到USARTDIV
mantissa=temp; //得到整数部分
fraction=(temp-mantissa)*16; //得到小数部分
mantissaDR = TXBuf ;
while(RESET == USART_GetFlagStatus(USART2,USART_FLAG_TC));
// USART_ClearFlag(USART2,USART_FLAG_TC);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
}
void UartSendString(const u8 *ADDPointer)
{
}
void UartReceiveChar(u8 RXBuf)
{
}
void Delayms(u32 Z)
{
while(Z--)
Delayus(1000);
}
void Delayus(u32 Z) // SYSCLK = HCLK = 25MHz *2 , MAX Z= 0X00FFFFFF/50 = 335 544
{
SysTick->LOAD=50*Z; //装载计数值,因为时钟50M,50次才1μs
SysTick->CTRL=0x00000005; //时钟来源设为HCLK=50MHz,禁止异常中断请求,打开定时器
while(!(SysTick->CTRL&0x00010000)); //等待计数到0
SysTick->CTRL=0x00000000; //关闭定时器
}
/****************************** Interrupt **************************/
一周热门 更多>