打印
[STM32L1]

求STM32L151 485串口的代码

[复制链接]
1516|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
归来|  楼主 | 2015-11-3 19:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
弄了好久就是出不来,求大神
沙发
cornrn| | 2016-1-13 09:19 | 只看该作者
/**********************************Copyright (c)***********************************************
*                Jiangsu Zhihai Electronic Technology Co., Ltd.
*                      Research & Development Department
*
*                                                                                                        www.smarthealth-tech.com
*
-----------------------------------------------------------------------------------------------
* @file    main.c
* @author  Gu Dongdong QQ858860583         21IC账号:cornrn  开源电子网:cornrn 邮箱:kuhill@qq.com
* @date    2015-12-16  
***********************************************************************************************/
#ifndef __SYS_H
#define __SYS_H       
#include "HAL_bsp.h"
#include "includes.h"       

/*********************************************************************************************************
** 函数名称: HAL_BSP_Configuration
** 功能描述: MCU驱动配置
** 输   入: 无
** 输   出: 无
** 全局变量: 无
** 调用模块: 由官方固件库提供配置API
*********************************************************************************************************/
void HAL_BSP_Configuration(void)
{
        RCC_Configuration();
        NVIC_Configuration();
        GPIO_Configuration();
        USART_Configuration();       
  SysTick_Configuration();       
}
/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{
        RCC_HSICmd(ENABLE);
        while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1|RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOC, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2|RCC_APB1Periph_TIM2, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG|RCC_APB2Periph_ADC1|RCC_APB2Periph_USART1 ,ENABLE);
  
}
/*********************************************************************************************************
** 函数名称: NVIC_Configuration
** 功能描述: 中断响应配置
** 输   入: 无
** 输   出: 无
** 全局变量: 无
** 调用模块: 无
*********************************************************************************************************/
void NVIC_Configuration(void)
{
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
        NVIC_SetPriority(SysTick_IRQn,NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
}

/*********************************************************************************************************
** 函数名称: GPIO_Configuration
** 功能描述: GPIO功能配置
** 输   入: 无      
** 输   出: 无
** 返    回: 无
** 全局变量: 无
** 调用模块: 无
********************************************************************************************************/
void GPIO_Configuration(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        /*PWR_CTL,LCD_RS,BT_PWR_CTL,LCD_CTL*/
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_11|GPIO_Pin_12;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);       
        GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_11|GPIO_Pin_12);
        /*LED&LCD*/
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_4 |GPIO_Pin_5 |GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14 | GPIO_Pin_15;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
        GPIO_SetBits(GPIOB,GPIO_Pin_4  | GPIO_Pin_5);
        /* KEY1,PWR_STATE,KEY2*/
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_4| GPIO_Pin_6| GPIO_Pin_7;  
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
        /*KEY3,BT_LINK,LOD+,LOD-*/
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_0 | GPIO_Pin_1|GPIO_Pin_6| GPIO_Pin_7;  
  GPIO_Init(GPIOB, &GPIO_InitStructure);       
        /*ECG_TO_ADC,PWR_MONITOR,VREF_IN*/
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
        /*USART*/
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource9,GPIO_AF_USART1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10,GPIO_AF_USART1);  
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_9 | GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
        /*I2C2*/
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_I2C2);
        GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_I2C2);
        GPIO_InitStructure.GPIO_Pin         = GPIO_Pin_10 | GPIO_Pin_11;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
         /* BZ_CTL */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_TIM2);
}

/********************************************************************************************************
** 函数名称: USART_Configuration
** 功能描述: 串口初始化
** 输   入:      
** 输   出:
** 返    回: 无                                 
********************************************************************************************************/
void USART_Configuration(void)
{
  USART_InitTypeDef USART_InitStructure;
  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_Init(USART1, &USART_InitStructure);
  USART_Cmd(USART1, ENABLE);
}
/********************************************************************************************************
** 函数名称: SysTick_Configuration
** 功能描述: 系统时钟滴答
** 输   入: 无      
** 输   出: 无
** 返    回: 无
** 全局变量: 无
** 调用模块: 无
********************************************************************************************************/
void SysTick_Configuration(void)
{
        if(SysTick_Config(8000))//8MHZ/8k=1k
        {
                while (1);
        }               
        SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
}   
/*******************************************************************************
* Function Name  : SysTick_Handler
* Description    : SysTick_Handler
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SysTick_Handler(void)
{               
        if(OSRunning==1)                                                //OS开始跑了,才执行正常的调度处理
        {
                OSIntEnter();                                                             //进入中断
                OSTimeTick();                                               //调用ucos的时钟服务程序               
                OSIntExit();                                                        //触发任务切换软中断
        }
}

/*******************************************************************************
* Function Name  : fputc
* Description    : Retargets the C library printf function to the USART.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int fputc(int ch, FILE *f)
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (unsigned char) ch);

  /* Loop until the end of transmission */
  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {
  }
  return ch;
}
/*******************************************************************************
* Function Name  : fgetc
* Description    : Retargets the C library scanf function to the USART.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int fgetc(FILE *f)
{
        int ch;  
        while((USART1->SR&0x20)==0);
        ch = USART_ReceiveData(USART1);
        return ch;
}

/******************************* (C) COPYRIGHT SmartHealth *****END OF FILE*******************************/



//THUMB指令不支持汇编内联
//采用如下方法实现执行汇编指令WFI  
void WFI_SET(void)
{
        __ASM volatile("wfi");                  
}
//关闭所有中断
void INTX_DISABLE(void)
{                  
        __ASM volatile("cpsid i");
}
//开启所有中断
void INTX_ENABLE(void)
{
        __ASM volatile("cpsie i");                  
}
//设置栈顶地址
//addr:栈顶地址
__asm void MSR_MSP(unsigned int addr)
{
    MSR MSP, r0                         //set Main Stack value
    BX r14
}



#endif

使用特权

评论回复
板凳
cornrn| | 2016-1-13 09:23 | 只看该作者
这个是我编写的STM32L151 UCOSIII模板例程,里面包含了USART printf mpu6050等

SmartHealth内部移植模板:例6-1 UCOSIII任务创建和删除.rar

709.51 KB

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

36

帖子

1

粉丝