单片机写程序的思路

单片机写程序的思路

一,先读芯片手册

STM32F1中文参考手册
链接:https://pan.baidu.com/s/1THQdvlptnFDkxzD4E9D42g?pwd=llil
提取码:llil

1.GPIO的概念

GPIO其实指的是通用的输入输出端口,I指的是input,O指的是output,指的是用户可以通过软件的方式控制芯片的引脚输出电平以及可以利用引脚检测外部电压。一般单片机中采用的电平协议都是TTL电平协议,TTL电平规定高电平(5V)使用逻辑1表示,低电平(0V)使用逻辑0表示。

2.引脚的布局

在这里插入图片描述

在这里插入图片描述
可以看到,、芯片除了一些特殊功能引脚之外,剩余的引脚的命名是有规律的,ST公司规定:每个IO口的命名是以P开头,把IO口分为很多组,组(端口)以字母AH进行命名,每组有16个引脚,以数字015进行区分,比如 PA0 ~ PA15 、 PB0 ~ PB15 …

注意:每个IO口都有对应的功能,一般情况下IO口都具有输入输出(GPIO)功能,部分引脚也具有其他功能,其他功能一般也被称为“第二功能”。
在这里插入图片描述

3.GPIO的使用

一般情况下使用GPIO作为输入输出引脚时,都会和外设以及传感器打交道,以测温枪的RGB灯为例

1.分析原理图----查找原理图,找到需要控制的硬件对应的引脚,并且根据原理图理解硬件的控制原理!!!

在这里插入图片描述

2. 进行程序设计,为了降低开发难度,ST公司提供了很多的函数接口给用户,可以参考!!!

在这里插入图片描述
链接:https://pan.baidu.com/s/1nxv4_R_6xNvOxKH8D2tV4A?pwd=umr1
提取码:umr1
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

可以通过ST公司提供的代码思路再结合ST公司提供的代码例程,从而进行代码的设计,如
在这里插入图片描述
参考移植代码

00001 /**
00002   ******************************************************************************
00003   * @file    GPIO/IOToggle/main.c 
00004   * @author  MCD Application Team
00005   * @version V3.5.0
00006   * @date    08-April-2011
00007   * @brief   Main program body.
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
00019   ******************************************************************************
00020   */ 
00021 
00022 /* Includes ------------------------------------------------------------------*/
00023 #include "stm32f10x.h"
00024 #include "stm32_eval.h"
00025 
00026 /** @addtogroup STM32F10x_StdPeriph_Examples
00027   * @{
00028   */
00029 
00030 /** @addtogroup GPIO_IOToggle
00031   * @{
00032   */
00033 
00034 /* Private typedef -----------------------------------------------------------*/
00035 /* Private define ------------------------------------------------------------*/
00036 /* Private macro -------------------------------------------------------------*/
00037 /* Private variables ---------------------------------------------------------*/
00038 GPIO_InitTypeDef GPIO_InitStructure; //1.定义外设的结构体变量
00039 
00040 /* Private function prototypes -----------------------------------------------*/
00041 /* Private functions ---------------------------------------------------------*/
00042 
00043 /**
00044   * @brief  Main program.
00045   * @param  None
00046   * @retval None
00047   */
00048 int main(void)
00049 {
00050   /*!< At this stage the microcontroller clock setting is already configured, 
00051        this is done through SystemInit() function which is called from startup
00052        file (startup_stm32f10x_xx.s) before to branch to application main.
00053        To reconfigure the default setting of SystemInit() function, refer to
00054        system_stm32f10x.c file
00055      */     
00056        
00057   /* GPIOD Periph clock enable */
00058   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //2.打开外设的时钟
00059 
00060   /* Configure PD0 and PD2 in output pushpull mode */   //3.对外设结构成员进行配置
00061   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2;//引脚定义
00062   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     //晶振
00063   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      //输出模式 
00064   GPIO_Init(GPIOD, &GPIO_InitStructure);                //初始化外设
00065 
00066   /* To achieve GPIO toggling maximum frequency, the following  sequence is mandatory. 
00067      You can monitor PD0 or PD2 on the scope to measure the output signal. 
00068      If you need to fine tune this frequency, you can add more GPIO set/reset 
00069      cycles to minimize more the infinite loop timing.
00070      This code needs to be compiled with high speed optimization option.  */
00071   while (1)
00072   {
00073     /* Set PD0 and PD2 */ //直接访问硬件寄存器的程序   优点:运行效率高,缺点:可读性差
00074     GPIOD->BSRR = 0x00000005; //0000 0000 0000 ... 1000 0000 复位 设置为低电平
00075     /* Reset PD0 and PD2 */
00076     GPIOD->BRR  = 0x00000005; //置位 设置位高电平
00077 
00078     /* Set PD0 and PD2 */
00079     GPIOD->BSRR = 0x00000005;
00080     /* Reset PD0 and PD2 */
00081     GPIOD->BRR  = 0x00000005;
00082 
00083     /* Set PD0 and PD2 */
00084     GPIOD->BSRR = 0x00000005;
00085     /* Reset PD0 and PD2 */
00086     GPIOD->BRR  = 0x00000005;
00087 
00088     /* Set PD0 and PD2 */
00089     GPIOD->BSRR = 0x00000005;
00090     /* Reset PD0 and PD2 */
00091     GPIOD->BRR  = 0x00000005;
00092 
00093     /* Set PD0 and PD2 */
00094     GPIOD->BSRR = 0x00000005;
00095     /* Reset PD0 and PD2 */
00096     GPIOD->BRR  = 0x00000005;
00097 
00098     /* Set PD0 and PD2 */
00099     GPIOD->BSRR = 0x00000005;
00100     /* Reset PD0 and PD2 */
00101     GPIOD->BRR  = 0x00000005;
00102 
00103     /* Set PD0 and PD2 */
00104     GPIOD->BSRR = 0x00000005;
00105     /* Reset PD0 and PD2 */
00106     GPIOD->BRR  = 0x00000005;
00107 
00108     /* Set PD0 and PD2 */
00109     GPIOD->BSRR = 0x00000005;
00110     /* Reset PD0 and PD2 */
00111     GPIOD->BRR  = 0x00000005;
00112 
00113     /* Set PD0 and PD2 */
00114     GPIOD->BSRR = 0x00000005;
00115     /* Reset PD0 and PD2 */
00116     GPIOD->BRR  = 0x00000005;
00117 
00118     /* Set PD0 and PD2 */
00119     GPIOD->BSRR = 0x00000005;
00120     /* Reset PD0 and PD2 */
00121     GPIOD->BRR  = 0x00000005;
00122   }
00123 }
00124 
00125 #ifdef  USE_FULL_ASSERT
00126 
00127 /**
00128   * @brief  Reports the name of the source file and the source line number
00129   *         where the assert_param error has occurred.
00130   * @param  file: pointer to the source file name
00131   * @param  line: assert_param error line source number
00132   * @retval None
00133   */
00134 void assert_failed(uint8_t* file, uint32_t line)
00135 { 
00136   /* User can add his own implementation to report the file name and line number,
00137      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00138 
00139   /* Infinite loop */
00140   while (1)
00141   {
00142   }
00143 }
00144 
00145 #endif
00146 
00147 /**
00148   * @}
00149   */
00150 
00151 /**
00152   * @}
00153   */
00154 
00155 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

固件包的使用
固件包里面的函数可读性高
在这里插入图片描述可以看到通过访问硬件的寄存器也可以实现硬件的控制,只不过导致程序的可读性变低,为了提高开发效率以及提高程序可读性和可靠性,可以采用ST公司提供的函数接口,一般外设的函数接口的声明都在外设对应的头文件中,函数对应的定义都在外设对应的源文件中

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
分析原理图
在这里插入图片描述
**
在这里插入图片描述
可以知道,芯片IO口如果配置为输入模式,则可以有三种选择:浮空输入、上拉输入、下拉输入,通过原理图可以知道测量按键外部以及接了一个强上拉电阻,就可以选择浮空输入。

最终实现程序
在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

太极不打烊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值