⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 ARM_CORTEX-M3应用实例开发详解光盘
💻 C
字号:
/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* 文件名          : main.c
* 描述          : 主程序
********************************************************************************/

/* 头文件 ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
#include <stdio.h>

ErrorStatus HSEStartUpStatus;

/* 函数及引用外部函数 -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
extern  void I2C_LM75_Init(void);
extern u32  TempCelsius_Value ;
extern u16 I2C_LM75_Temp_Read(void);
extern ErrorStatus I2C_LM75_Status(void);
extern void Thermometer_Temperature(void)  ;

int main(void)
{  u16 i=0;
   
   
#ifdef DEBUG
  debug();
#endif

  /* 系统时钟配置 */
  RCC_Configuration();
    
  /* 中断向量配置 */
  NVIC_Configuration();

  /* GPIO配置 */
  GPIO_Configuration();
  
  /* USART1配置 */
  USART_Configuration();

  I2C_LM75_Init();/*I2C初始化*/

 Thermometer_Temperature();
 
while(1	)
	{	
		if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)
		{	           
			i = USART_ReceiveData(USART1);
			printf("  %c",i&0xFF);	   /* 打印输入字符 */
					
		}
	}

}

#ifdef  DEBUG
/*******************************************************************************
* 函数名  : assert_failed
* 描述    : 报告出错
* 输入         : - 文件指针
*                 - 行指针
* 输出         : 无
* 返回         : 无
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{ 
  /* 当STLM75异常时输出报错信息 */

  printf("\n\r Wrong parameter value detected on\r\n");
  printf("       file  %s\r\n", file);
  printf("       line  %d\r\n", line);
    
  
}
#endif

/*******************************************************************************
* 函数名  : RCC_Configuration
* 描述    : 报告出错
* 输入         : 配置系统时钟
*                
* 输出         : 无
* 返回         : 无
*******************************************************************************/
void RCC_Configuration(void)
{
  /* 复位 */
  RCC_DeInit();

  /* 使能 HSE */
  RCC_HSEConfig(RCC_HSE_ON);

  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();

  if(HSEStartUpStatus == SUCCESS)
  {
    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1); 
  
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1); 

    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);

    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    /* 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)
    {
    }
  }
   
  /* 使能USART1与GPIOA  */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
}

/*******************************************************************************
* 函数名  : NVIC_Configuration
* 描述    : 报告出错
* 输入         :  配置向量地址
* 输出         : 无
* 返回         : 无
*******************************************************************************/

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
}

/*******************************************************************************
* 函数名  : GPIO_Configuration
* 描述    : GPIO配置
* 输入         : 无
* 输出         : 无
* 返回         : 无
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef 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);
}


/*******************************************************************************
* 函数名  : USART_Configuration
* 描述    : 串口配置
* 输入         : 无
* 输出         : 无
* 返回         : 无
*******************************************************************************/

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);
}


/*******************************************************************************
* 函数名  : fputc
* 描述    : 串口输出函数
* 输入         : 无
* 输出         : 无
* 返回         : 无
*******************************************************************************/
int fputc(int ch, FILE *f)
{
  
  USART_SendData(USART1, (u8) ch);

  /* 循环只到传送结束 */
  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {
  }

  return ch;
}

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -