基于stm32f103最小使用code

总共有4个文件,其中led.c和debug.c可以不需要 

         - main.c

         - system.c

         - debug.c

         - led.c

1. main.c

主要实现系统的初始化,led端口,串口的初始化,此处如果发现1S灯闪烁,打印出"hello world",就说明初步环境好了,可以在这基础上添加自己的代码了。

#include "stm32f10x.h"
#include "includes.h"

int main(void)
{
	system_init();
	
	led_init();
	
	uart1_init();
	
	led_on(0);
	sys_delay_ms(1000);
	led_off(0);
	sys_delay_ms(1000);
	
	while(1)
	{
		sys_delay_ms(2000);
		led_toggle(0);
		printf("hello world\r\n");
	}	
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  while (1)
  {	
   			
  }
}

#endif


/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

2. system.c

   此文件实现系统时钟配置,中断向量表配置和systick配置。

#include "system.h"
#include "stm32f10x_it.h"


ErrorStatus HSEStartUpStatus;
//1. clock config
void rcc_configuration(void)
{
	RCC_DeInit();  

  	RCC_HSEConfig(RCC_HSE_ON); 
	
	HSEStartUpStatus = RCC_WaitForHSEStartUp();
	
  	if(SUCCESS == HSEStartUpStatus)
	{

	    FLASH_SetLatency(FLASH_Latency_2);
	    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
	
    	    RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);	
	    RCC_PLLCmd(ENABLE); 
	    while(RESET == RCC_GetFlagStatus(RCC_FLAG_PLLRDY))
   	    {   
	     }
	    // HCLK  up to 72MHz
	    RCC_HCLKConfig(RCC_SYSCLK_Div1);   //AHBÍâÉè
	    RCC_PCLK1Config(RCC_HCLK_Div2);    //APB1ÍâÉè
	    RCC_PCLK2Config(RCC_HCLK_Div1);    //APB2ÍâÉè   
			
	    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);	
	    while(0x08 != RCC_GetSYSCLKSource())
	    {
	    }	
	}  	
}
//2.nvic config
void nvic_configuration(void)
{
		
#ifdef  VECT_TAB_RAM 	
	NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);	
#else 
	NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
//3. systick config/  1ms 
void systick_configuration(void)
{
	//clk configuration
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
	SysTick_Config(72000);	//1ms arise interrupt
}
//4. systick ms_delay  
void sys_delay_ms(uint16_t ms)
{
	uint32_t _t = systick_ms_counter+ms;

	while(systick_ms_counter < _t);			
}
//5. systick s_delay
void sys_delay_1s(uint16_t s)
{
	uint32_t _t = systick_s_counter + s;
	while(systick_s_counter < _t);
}
//6. sys_init
void system_init(void)
{
	rcc_configuration();

	nvic_configuration();

	systick_configuration();	
}
//7. uncertain_delay
void delay(uint16_t dly)
{
	int i,j;
	for(i = 0;i<dly;i++)
	for(j = 0;j<1;j++);
}

3. debug.c

   该文件用于实现打印串口驱动,只提供打印输出,利用printf函数的重映射,还需要在Keil软件中进行设置,否则打印不出来滴

#include "includes.h"

void usart1_gpio_config(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	//DBTX  PA9
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	//DBRX  PA10
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
}
//2.
void usart1_func_config(void)
{
	USART_InitTypeDef  USART_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	
	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_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	
	USART_Init(USART1,&USART_InitStructure);
	
	USART_Cmd(USART1,ENABLE);
	
//	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
}
//3.
void uart1_nvic_config(void)
{
	NVIC_InitTypeDef  NVIC_InitStructure;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
	
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	
	NVIC_Init(&NVIC_InitStructure);
}
//4.串口初始化,对外的接口.
void uart1_init(void)
{
	usart1_gpio_config();
	
	usart1_func_config();
	
//	uart1_nvic_config();
}
//5.printf函数的重映射实现
int fputc(int ch, FILE * f)
{
    //rs232 Debug
    USART_SendData(USART1, (uint8_t)ch);
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); 
	
    return ch;
}

4. led.c

    该文件实现板子上led  GPIO口的配置初始化,灯的开,关,闪烁功能。

#include "includes.h"

//led config
//pe8 pe9
void led_init(void)
{
	 GPIO_InitTypeDef GPIO_InitStructure;
		
	 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
	 
	 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
	 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	 GPIO_Init(GPIOE,&GPIO_InitStructure);
	
	 GPIO_SetBits(GPIOE,GPIO_Pin_8|GPIO_Pin_9); 
}
//
void led_on(uint8_t ledid)
{
	switch(ledid)
	{
		case 0:GPIO_ResetBits(GPIOE,GPIO_Pin_8|GPIO_Pin_9);break;
		case 1:GPIO_ResetBits(GPIOE,GPIO_Pin_8);break;
		case 2:GPIO_ResetBits(GPIOE,GPIO_Pin_9);break;
		default:break;
	}
}
//
void led_off(uint8_t ledid)
{
	switch(ledid)
	{
		case 0:GPIO_SetBits(GPIOE,GPIO_Pin_8|GPIO_Pin_9);break;
		case 1:GPIO_SetBits(GPIOE,GPIO_Pin_8);break;
		case 2:GPIO_SetBits(GPIOE,GPIO_Pin_9);break;
		default:break;
	}
}

void led_toggle(uint8_t led_id){
	switch(led_id)
	{
		case 0:GPIO_WriteBit(GPIOE,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_8)));
					 GPIO_WriteBit(GPIOE,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_8)));
					 break;
		case 1:GPIO_WriteBit(GPIOE,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_8)));break;
		case 2:GPIO_WriteBit(GPIOE,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_8)));break;
		default:break;
	}
}

5. stm32f10x_it.c

    感觉也有必要贴出中断服务函数文件,这里面最开始配置了systick的中断服务函数

#include "stm32f10x_it.h"

uint32_t systick_ms_counter = 0;
uint32_t systick_s_counter = 0;
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval : None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval : None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval : None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval : None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval : None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval : None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval : None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval : None
  */
void EXTI1_IRQHandler()
{
}

/* ¹¦ÄÜ£º µ±ÓнÓÊÕÖжÏʱ£¬½«½ÓÊÕµÄ×Ö·û´®´æ·ÅÔÚRxBuff[]ÖÐ
          µ±Óз¢ËÍÖжÏʱ£¬½«TxBuff[]ÖеÄÊý¾Ý·¢Ë͵½PC¶Ë
*/
void UART4_IRQHandler()
{
	
}
void SysTick_Handler()
{	
	systick_ms_counter++;   //ms
	if(systick_ms_counter%1000==0){
		systick_s_counter++; //s
	}
}
void USART1_IRQHandler()
{

}
/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval : None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */ 


/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值