我用的是mdk4.14
程序是仿 mdk下面带的printf的例子写的
还加了点自己的创新!就是点亮LED,每点亮一次从USART1发送已经是第几次点亮了!上源码!求指教!
USART1的时钟初始化在 RCC_Configuration()函数的最后一行
并且在我的工程里包含了stm库函数的那个.lib库
附件里是我的整个工程
#include <stm32f10x_lib.h>
#include"stm32f10x_it.c"
#include<stdio.h>
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
void RCC_Configuration(void);
void NVIC_Configuration(void);
void Delay(vu32 nCount);
ErrorStatus HSEStartUpStatus;
int main(void)
{
int x=0;
#ifdef DEBUG
debug();
#endif
RCC_Configuration();
NVIC_Configuration();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOA , ENABLE);
//LED初始化
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOD , &GPIO_InitStructure);
//串口GPIO初始化,先初始化TXD
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);
//初始化RXD
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化串口
USART_DeInit(USART1);
USART_StructInit(&USART_InitStructure);
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
//开始闪烁
while (1)
{
GPIO_SetBits(GPIOD,GPIO_Pin_2);
Delay(0xaffff);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
Delay(0xaffff);
printf("abc%d",x++);
}
}
void RCC_Configuration(void)
{
RCC_DeInit();
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if( HSEStartUpStatus == SUCCESS )
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08)
{}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
}
void Delay(vu32 nCount)
{
for( ; nCount !=0 ; nCount-- );
}
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
return ch;
}
#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
|