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

📄 stm32f10x_usart.lst

📁 STM32利用正交编码器实现电机的控制
💻 LST
📖 第 1 页 / 共 4 页
字号:
    205          
    206            /* Determine the integer part */
    207            integerdivider = ((0x19 * apbclock) / (0x04 * (USART_InitStruct->USART_BaudRate)));
    208            tmpreg = (integerdivider / 0x64) << 0x04;
    209          
    210            /* Determine the fractional part */
    211            fractionaldivider = integerdivider - (0x64 * (tmpreg >> 0x04));
    212            tmpreg |= ((((fractionaldivider * 0x10) + 0x32) / 0x64)) & ((u8)0x0F);
    213          
    214            /* Write to USART BRR */
    215            USARTx->BRR = (u16)tmpreg;
    216          }
    217          
    218          /*******************************************************************************
    219          * Function Name  : USART_StructInit
    220          * Description    : Fills each USART_InitStruct member with its default value.
    221          * Input          : - USART_InitStruct: pointer to a USART_InitTypeDef structure
    222          *                    which will be initialized.
    223          * Output         : None
    224          * Return         : None
    225          *******************************************************************************/
    226          void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
    227          {
    228            /* USART_InitStruct members default value */
    229            USART_InitStruct->USART_BaudRate = 0x2580; /* 9600 Baud */
    230            USART_InitStruct->USART_WordLength = USART_WordLength_8b;
    231            USART_InitStruct->USART_StopBits = USART_StopBits_1;
    232            USART_InitStruct->USART_Parity = USART_Parity_No ;
    233            USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    234            USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    235            USART_InitStruct->USART_Clock = USART_Clock_Disable;
    236            USART_InitStruct->USART_CPOL = USART_CPOL_Low;
    237            USART_InitStruct->USART_CPHA = USART_CPHA_1Edge;
    238            USART_InitStruct->USART_LastBit = USART_LastBit_Disable;
    239          }
    240          
    241          /*******************************************************************************
    242          * Function Name  : USART_Cmd
    243          * Description    : Enables or disables the specified USART peripheral.
    244          * Input          : - USARTx: where x can be 1, 2 or 3 to select the USART
    245          *                    peripheral.
    246          *                : - NewState: new state of the USARTx peripheral.
    247          *                    This parameter can be: ENABLE or DISABLE.
    248          * Output         : None
    249          * Return         : None
    250          *******************************************************************************/
    251          void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
    252          {
    253            /* Check the parameters */
    254            assert(IS_FUNCTIONAL_STATE(NewState));
    255            
    256            if (NewState != DISABLE)
    257            {
    258              /* Enable the selected USART by setting the RUN bit in the CR1 register */
    259              USARTx->CR1 |= CR1_RUN_Set;
    260            }
    261            else
    262            {
    263              /* Disable the selected USART by clearing the RUN bit in the CR1 register */
    264              USARTx->CR1 &= CR1_RUN_Reset;
    265            }
    266          }
    267          
    268          /*******************************************************************************
    269          * Function Name  : USART_ITConfig
    270          * Description    : Enables or disables the specified USART interrupts.
    271          * Input          : - USARTx: where x can be 1, 2 or 3 to select the USART
    272          *                    peripheral.
    273          *                  - USART_IT: specifies the USART interrupt sources to be
    274          *                    enabled or disabled.
    275          *                    This parameter can be one of the following values:
    276          *                       - USART_IT_PE
    277          *                       - USART_IT_TXE
    278          *                       - USART_IT_TC
    279          *                       - USART_IT_RXNE
    280          *                       - USART_IT_IDLE
    281          *                       - USART_IT_LBD
    282          *                       - USART_IT_CTS
    283          *                       - USART_IT_ERR
    284          *                  - NewState: new state of the specified USARTx interrupts.
    285          *                    This parameter can be: ENABLE or DISABLE.
    286          * Output         : None
    287          * Return         : None
    288          *******************************************************************************/
    289          void USART_ITConfig(USART_TypeDef* USARTx, u16 USART_IT, FunctionalState NewState)
    290          {
    291            u32 usartreg = 0x00, itpos = 0x00, itmask = 0x00;
    292            u32 address = 0x00;
    293          
    294            /* Check the parameters */
    295            assert(IS_USART_CONFIG_IT(USART_IT));  
    296            assert(IS_FUNCTIONAL_STATE(NewState));
    297            
    298            /* Get the USART register index */
    299            usartreg = (((u8)USART_IT) >> 0x05);
    300          
    301            /* Get the interrupt position */
    302            itpos = USART_IT & USART_IT_Mask;
    303          
    304            itmask = (((u32)0x01) << itpos);
    305            address = *(u32*)&(USARTx);
    306          
    307            if (usartreg == 0x01) /* The IT  is in CR1 register */
    308            {
    309              address += 0x0C;
    310            }
    311            else if (usartreg == 0x02) /* The IT  is in CR2 register */
    312            {
    313              address += 0x10;
    314            }
    315            else /* The IT  is in CR3 register */
    316            {
    317              address += 0x14; 
    318            }
    319            if (NewState != DISABLE)
    320            {
    321              *(u32*)address  |= itmask;
    322            }
    323            else
    324            {
    325              *(u32*)address &= ~itmask;
    326            }
    327          }
    328          
    329          /*******************************************************************************
    330          * Function Name  : USART_DMACmd
    331          * Description    : Enables or disables the USART抯 DMA interface.
    332          * Input          : - USARTx: where x can be 1, 2 or 3 to select the USART
    333          *                    peripheral.
    334          *                  - USART_DMAReq: specifies the DMA request.
    335          *                    This parameter can be any combination of the following values:
    336          *                       - USART_DMAReq_Tx
    337          *                       - USART_DMAReq_Rx
    338          *                  - NewState: new state of the DMA Request sources.
    339          *                   This parameter can be: ENABLE or DISABLE.
    340          * Output         : None
    341          * Return         : None
    342          *******************************************************************************/
    343          void USART_DMACmd(USART_TypeDef* USARTx, u16 USART_DMAReq, FunctionalState NewState)
    344          {
    345            /* Check the parameters */
    346            assert(IS_USART_DMAREQ(USART_DMAReq));  
    347            assert(IS_FUNCTIONAL_STATE(NewState)); 
    348            
    349            if (NewState != DISABLE)
    350            {
    351              /* Enable the DMA transfer for selected requests by setting the DMAT and/or
    352              DMAR bits in the USART CR3 register */
    353              USARTx->CR3 |= USART_DMAReq;
    354            }
    355            else
    356            {
    357              /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
    358              DMAR bits in the USART CR3 register */
    359              USARTx->CR3 &= (u16)~USART_DMAReq;
    360            }
    361          }
    362          
    363          /*******************************************************************************
    364          * Function Name  : USART_SetAddress
    365          * Description    : Sets the address of the USART node.
    366          * Input          : - USARTx: where x can be 1, 2 or 3 to select the USART
    367          *                    peripheral.
    368          *                  - USART_Address: Indicates the address of the USART node.
    369          * Output         : None
    370          * Return         : None
    371          *******************************************************************************/
    372          void USART_SetAddress(USART_TypeDef* USARTx, u8 USART_Address)
    373          {
    374            /* Check the parameters */
    375            assert(IS_USART_ADDRESS(USART_Address)); 
    376              
    377            /* Clear the USART address */
    378            USARTx->CR2 &= CR2_Address_Mask;
    379            /* Set the USART address node */
    380            USARTx->CR2 |= USART_Address;
    381          }
    382          
    383          /*******************************************************************************
    384          * Function Name  : USART_WakeUpConfig
    385          * Description    : Selects the USART WakeUp method.
    386          * Input          : - USARTx: where x can be 1, 2 or 3 to select the USART
    387          *                    peripheral.
    388          *                  - USART_WakeUp: specifies the USART wakeup method.
    389          *                    This parameter can be one of the following values:
    390          *                        - USART_WakeUp_IdleLine
    391          *                        - USART_WakeUp_AddressMark
    392          * Output         : None
    393          * Return         : None
    394          *******************************************************************************/
    395          void USART_WakeUpConfig(USART_TypeDef* USARTx, u16 USART_WakeUp)
    396          {
    397            /* Check the parameters */
    398            assert(IS_USART_WAKEUP(USART_WakeUp));
    399            
    400            USARTx->CR1 &= CR3_WAKE_Mask;
    401            USARTx->CR1 |= USART_WakeUp;
    402          }
    403          
    404          /*******************************************************************************
    405          * Function Name  : USART_ReceiverWakeUpCmd
    406          * Description    : Determines if the USART is in mute mode or not.
    407          * Input          : - USARTx: where x can be 1, 2 or 3 to select the USART
    408          *                    peripheral.
    409          *                  - NewState: new state of the USART mode.
    410          *                    This parameter can be: ENABLE or DISABLE.
    411          * Output         : None
    412          * Return         : None
    413          *******************************************************************************/
    414          void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
    415          {
    416            /* Check the parameters */
    417            assert(IS_FUNCTIONAL_STATE(NewState)); 
    418            
    419            if (NewState != DISABLE)
    420            {
    421              /* Enable the mute mode USART by setting the RWU bit in the CR1 register */
    422              USARTx->CR1 |= CR1_RWU_Set;
    423            }
    424            else
    425            {
    426              /* Disable the mute mode USART by clearing the RWU bit in the CR1 register */
    427              USARTx->CR1 &= CR1_RWU_Reset;
    428            }
    429          }
    430          
    431          /*******************************************************************************
    432          * Function Name  : USART_LINBreakDetectLengthConfig
    433          * Description    : Sets the USART LIN Break detection length.
    434          * Input          : - USARTx: where x can be 1, 2 or 3 to select the USART
    435          *                    peripheral.
    436          *                  - USART_LINBreakDetectLength: specifies the LIN break
    437          *                    detection length.
    438          *                    This parameter can be one of the following values:
    439          *                       - USART_LINBreakDetectLength_10b
    440          *                       - USART_LINBreakDetectLength_11b
    441          * Output         : None
    442          * Return         : None
    443          *******************************************************************************/
    444          void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, u16 USART_LINBreakDetectLength)
    445          {
    446            /* Check the parameters */
    447            assert(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
    448            
    449            USARTx->CR2 &= CR3_LBDL_Mask;
    450            USARTx->CR2 |= USART_LINBreakDetectLength;  
    451          }
    452          
    453          /*******************************************************************************

⌨️ 快捷键说明

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