stm32中引脚和一些功能模块的一般配置方法

注意,初始化任意东西,都得最先配置这个GPIO或者模块的时钟,不然会失败

/*************************************************
Function: void USART_Config(void)      
Description: USART配置函数             
Input: 无                              
Output:无                              
Return:无                              
*************************************************/  
void USART_Config(void)
{
    //需要初始化的三个结构体
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    USART_ClockInitTypeDef USART_ClockInitStruct;
    

    
    //----使能模块时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); //开启USART6时钟
    //----使能GPIO引脚时钟
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);  
//------------------------------配置串口模块的功能 //----选择模块利用引脚 GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);//这相当于M3的开启复用时钟?只配置复用的引脚, GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6); //------------------------------配置GPIOC引脚 //----缺省值填入 GPIO_StructInit(&GPIO_InitStructure); //----配置GPIOC_Pin6为TX输出 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //设置为复用,必须为AF,OUT不行 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC,&GPIO_InitStructure); //--------配置GPIOC_Pin7为RX输入 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //这也必须为复用,与M3不同! GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC,&GPIO_InitStructure); //----------------------------------------------- //IO引脚复用功能设置,与之前版本不同 //----------配置USART6 //---------------------缺省值填入 USART_StructInit(&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(USART6, &USART_InitStructure); //--------缺省值填入 USART_ClockStructInit(&USART_ClockInitStruct); USART_ClockInit(USART6, &USART_ClockInitStruct); //配置中断,使能相应模块 USART_ITConfig(USART6, USART_IT_RXNE, ENABLE); //使能 USART6中断 USART_Cmd(USART6, ENABLE); //使能 USART6 }

/**名称:USART6中断服务程序
  *作用:USART6收到数据后产生中断,并将收到的内容发回给上位机
  */
void USART6_IRQHandler(void)
{
    //判断中断状态
    if (USART_GetITStatus(USART6, USART_IT_RXNE) != RESET) 
    { 
        //--清除中断标志位
        USART_ClearITPendingBit(USART6, USART_IT_RXNE);
        //USART_SendData(USART6, USART_ReceiveData(USART6)); //发送收到的数据
        USART_SendData(USART6, 'n'); //发送收到的数据
        STM_EVAL_LEDOn(LED3); //点亮LED,起到中断指示作用
    }
}


void MSD0_SPI_Configuration(void)
{		
  GPIO_InitTypeDef GPIO_InitStructure;

  //SPI模块时钟使能
  SPIX_CLK_INIT(SPIX_CLK, ENABLE);
  //IO口时钟使能
  RCC_AHB1PeriphClockCmd(SPIX_SCK_GPIO_CLK | SPIX_MISO_GPIO_CLK | 
                         SPIX_MOSI_GPIO_CLK | sFLASH_CS_GPIO_CLK, ENABLE);

 //--------------------------模块设置    
  //设置SPI引脚利用功能
  GPIO_PinAFConfig(SPIX_SCK_GPIO_PORT, SPIX_SCK_SOURCE, SPIX_SCK_AF);
  GPIO_PinAFConfig(SPIX_MISO_GPIO_PORT, SPIX_MISO_SOURCE, SPIX_MISO_AF);
  GPIO_PinAFConfig(SPIX_MOSI_GPIO_PORT, SPIX_MOSI_SOURCE, SPIX_MOSI_AF);
  
  //--------------------------相应GPIO设置
  //每个GPIO引脚配置
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;    
  //--SPI SCK pin configuration
  GPIO_InitStructure.GPIO_Pin = SPIX_SCK_PIN;
  GPIO_Init(SPIX_SCK_GPIO_PORT, &GPIO_InitStructure);
  //--SPI MOSI pin configuration
  GPIO_InitStructure.GPIO_Pin =  SPIX_MOSI_PIN;
  GPIO_Init(SPIX_MOSI_GPIO_PORT, &GPIO_InitStructure);
  //--SPI MISO pin configuration 
  GPIO_InitStructure.GPIO_Pin =  SPIX_MISO_PIN;
  GPIO_Init(SPIX_MISO_GPIO_PORT, &GPIO_InitStructure);
  //--片选引脚得单独配置
  //--Configure sFLASH Card CS pin in output pushpull mode 
  GPIO_InitStructure.GPIO_Pin = sFLASH_CS_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(sFLASH_CS_GPIO_PORT, &GPIO_InitStructure);
  
  //关闭片选
  MSD0_card_disable(); 

  //设置SPI接口
  MSD0_SPIHighSpeed(0);		

  //使能SPI模块
  SPI_Cmd(SPIX, ENABLE);
}


/*******************************************************************************
* Function Name  : MSD0_SPIHighSpeed
* Description    : SD Card Speed Set
* Input          : - b_high: 1 = 18MHz, 0 = 281.25Hz
* Output         : None
* Return         : None
* Attention		 : None
*******************************************************************************/
void MSD0_SPIHighSpeed(uint8_t b_high)
{
    SPI_InitTypeDef SPI_InitStructure;

    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;

    /* Speed select */
    if(b_high == 0)
    {
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
    }
    else
    {
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
    }

    SPI_Init(SPIX, &SPI_InitStructure);
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值