stm32和pc串口无法通信问题,代码如下 #include "stm32f10x_lib.h" #include <stdio.h>
#define LOW Bit_RESET #define HIGH Bit_SET void Put_String(u8 *p);
#ifdef __GNUC__ /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf set to 'Yes') calls __io_putchar() */ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif /* __GNUC__ */
union LongData { unsigned long word ; unsigned char byte[4]; };
#define setBit(pin,val) GPIO_WriteBit(GPIOA,pin,val) #define getBit(pin) GPIO_ReadInputDataBit(GPIOA,pin) typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
void USART_Configuration(void); void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); static vu32 TimingDelay; ErrorStatus HSEStartUpStatus; void delay(void); void delay1(void); void delay2(u32 nTime); void delay_50us(void);
void delay() { ; }
void delay1(void) { int i; for (i=0; i<0xfffff; i++) ; }
void delay2(u32 nTime) { SysTick_CounterCmd(SysTick_Counter_Enable); TimingDelay = nTime; while(TimingDelay != 0); SysTick_CounterCmd(SysTick_Counter_Disable); SysTick_CounterCmd(SysTick_Counter_Clear); }
void delay_50us(void) { unsigned char i; for (i=0; i<16; i++) ; }
int main(void) { #ifdef DEBUG debug(); #endif
/* System clocks configuration ---------------------------------------------*/ RCC_Configuration();
/* NVIC configuration ------------------------------------------------------*/ NVIC_Configuration();
/* GPIO configuration ------------------------------------------------------*/ GPIO_Configuration(); USART_Configuration(); // use below to disable read-protection and this will trigger a mass-erase of flash block //FLASH_Unlock(); //FLASH_ReadOutProtection(DISABLE); /* SysTick end of count event each 1ms with input clock equal to 9MHz (HCLK/8, default) */ SysTick_SetReload(9000);
/* Enable SysTick interrupt */ SysTick_ITConfig(ENABLE); while(1) { GPIO_SetBits(GPIOB, GPIO_Pin_5); delay1(); GPIO_ResetBits(GPIOB, GPIO_Pin_5);
GPIO_SetBits(GPIOB, GPIO_Pin_6); delay1(); GPIO_ResetBits(GPIOB, GPIO_Pin_6);
GPIO_SetBits(GPIOB, GPIO_Pin_7); delay1(); GPIO_ResetBits(GPIOB, GPIO_Pin_7);
GPIO_SetBits(GPIOB, GPIO_Pin_8); delay1(); GPIO_ResetBits(GPIOB, GPIO_Pin_8); delay1(); Put_String("\r\n in DEBUG mode... \n\r"); } }
/******************************************************************************* * Function Name : RCC_Configuration * Description : Configures the different system clocks. * Input : None * Output : None * Return : None *******************************************************************************/ void RCC_Configuration(void) { /* 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/2 */ RCC_PCLK2Config(RCC_HCLK_Div2);
/* 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) { } }
/* Enable peripheral clocks --------------------------------------------------*/ /* Enable USART1 and GPIOA clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1, ENABLE); // for small target (48pin) in mini-kit //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // for DIV's EVAL-board //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); }
/******************************************************************************* * Function Name : GPIO_Configuration * Description : Configures the different GPIO ports. * Input : None * Output : None * Return : None *******************************************************************************/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE); // for target(48pin) of mini-kit /* Configure PB.5/6/7/8 for LR3/4/5/6 ---------------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure);
// for DIV's EVL-board GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); //test GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); }
/******************************************************************************* * Function Name : NVIC_Configuration * Description : Configures NVIC and Vector Table base location. * Input : None * Output : None * Return : None *******************************************************************************/ void NVIC_Configuration(void) { #ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif }
void USART_Configuration(void) { USART_InitTypeDef USART_InitStructure;
/* USART1 configuration ------------------------------------------------------*/ /* USART1 configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled - USART Clock disabled - USART CPOL: Clock is active low - USART CPHA: Data is captured on the middle - USART LastBit: The clock pulse of the last data bit is not output to the SCLK pin */ USART_InitStructure.USART_BaudRate = 115200; 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_InitStructure.USART_Clock = USART_Clock_Disable; USART_InitStructure.USART_CPOL = USART_CPOL_Low; USART_InitStructure.USART_CPHA = USART_CPHA_2Edge; USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
USART_Init(USART1, &USART_InitStructure); /* Enable USART1 */ USART_Cmd(USART1, ENABLE); }
#ifdef DEBUG /******************************************************************************* * Function Name : assert_failed * Description : Reports the name of the source file and the source line number * where the assert_param error has occurred. * Input : - file: pointer to the source file name * - line: assert_param error line source number * Output : None * Return : None *******************************************************************************/ void assert_failed(u8* file, u32 line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */ while (1) { } } #endif /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
/******************************************************************************* * Function Name : PUTCHAR_PROTOTYPE * Description : Retargets the C library printf function to the USART. * Input : None * Output : None * Return : None *******************************************************************************/ PUTCHAR_PROTOTYPE { //FlagStatus flag; /* Place your implementation of fputc here */ /* e.g. write a character to the USART */ USART_SendData(USART1, (u8) ch);
/* Loop until the end of transmission */ while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) { }
return ch; }
void Put_String(u8 *p) { while(*p) { USART_SendData(USART1, *p++); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) { ; } } }
|
|