GPIO
In any microcontroller there is at least one general purpose input-output port. STM32 is a not different breed and as expected it also has several GPIO ports. These ports are usually named GPIOA, GPIOB, etc. but unlike most 8/16-bit micros these ports are 16 bit wide. Thus, in general, every port has 16 IO pins. Port pins have several modes of operation and this is what that makes them both robust and complex at first. In development boards the IO port pin naming is cut short and so wersquo;ll find PA0, PB12, etc. instead of GPIOA0, GPIOB12, etc. Even in the reference manuals this short naming is used widely. In the end every I/O pin is general purpose in nature.
Key points with STM32rsquo;s GPIO
·Upon any reset event all GPIOs are floating inputs. This prevents any accidental
damage to GPIOs in the event of emergency.
·All GPIO registers are needed to be accessed with 32 bit words. This is mandatory.
·Reserved bits for any GPIO register are kept at reset values i.e. 0.
·For every GPIO port there are seven registers but the only the first four are the most important ones. The rest can be ignored most of the times.
·These important GPIO registers per port are CRL, CRH, IDR and ODR. The rest three registers for GPIO ports can be avoided at the beginning.
·For bit set and for bit clear, operations REGx |= (1 lt;lt; bit) and REGx amp;= ~ (1 lt;lt; bit) respectively are very handy and efficient. However MikroC compiler provides bit level access though its internal header file coding like this GPIOB_BSRRbits.BS15. We can use this method or the other one.
·Some IOs are 5V tolerant and are label “FT” in datasheets and other docs. The rest of the I/O pins are not that tolerant and so 3.3V operation should always be ensured. STM32 has a maximum VDD tolerance of 4V but this value shouldnrsquo;t be used in any situation for safe operation. Best is to avoid 5V use with GPIOs whenever possible. This will help in avoiding accidental damage to the micro due to common mistakes.
·Unused GPIO pins should be kept at reset state or tied to ground with 10kΩ resistors so that they remain either as floating inputs or safely connected to ground though itrsquo;s not a must.
·I/O pins can source or sink currents up to 25mA. Thus direct LED drive is possible with them. To drive loads that need more than this value, we must use external BJTs, MOSFETs, optocouplers, transistor arrays or other driver devices.
·I/O pins should not directly drive inductive or capacitive loads.
·Care should be taken to avoid driving outputs beyond 50MHz. This is the maximum I/O pin frequency rating.
·CRL/H registers essential set GPIO pin operating mode independently as according to the table below:
·CRL sets GPIOs from 0 – 7 while CRH sets from 8 – 15. All IO ports are 16 bit wide unlike common 8 bit micros like PIC or AVR.
CRL Register:
CRH Register:
·As can be seen from port configuration table, right after reset CRL and CRH both have a value of 0x44444444. This value means floating input mode.
·IDR registers are responsible for digital inputs while ODR registers are responsible for digital outputs. In both registers the upper 16 bits are always kept 0 because GPIOs are 16 bits wide while their representative registers are 32 bit wide. Thus only the lower 16 nibbles are of concern. The code writing convention will be 0x0000xxxx for IDRs and ODRs. The upper nibbles are reserved and thus set as zeroes.
·Just like CRL and CRH, IDR and ODR should be accessed as 32 words.
·CRL and CRH should be initialized first before using ODR or IDR registers of a GPIO port.
ODR Register:
IDR Register:
·Reset values of IDR and ODR are both zero. This ensures initial logic low state for ODR and float state for IDR.
·After each device reset, all peripheral clocks are disabled (except for the SRAM and FLITF). Before using a peripheral you have to enable its clock in the RCC_AHBENR, RCC_APB1ENR or RCC_APB2ENR register. These registers are important and should always be set before using an internal peripheral.
APB2 peripheral clock enable register (RCC_APB2ENR):
APB1 peripheral clock enable register (RCC_APB1ENR):
AHB peripheral clock enable register (RCC_AHBENR):
·The above registers should be set first before anything else.
MikroC for ARM has a built-in library for GPIO. Though such libraries are easy to get started with even without getting an insight of the internal registers, they are both space and execution time consuming things. Thus for simple works best is avoid them unless their use makes a big program easy to understand and deal with.
INTERRUPT
In my earlier post on STM32 GPIOs I showed how to flash a LED with variable delay times. That example was based on polling method where the code continuously monitored the logic state of a GPIO input pin attached to a push button to determine the delay amount. Obviously that wonrsquo;t be an efficient technique when a program will be of a considerable size and complexity. This is simply so because the CPU will have to check the GPIOrsquo;s logic state every time the super-loop (while (1) loop in the main function) repeats and the push button will also not be responsive during the software delay function calls. Thus the overall performance is poor and not real-time. To get rid of these issues, wersquo;ll need to use external interrupts – a vital feature in every common microcontroller.
STM32F1xx series are ARM Cortex M3 based MCUs. The Cortex M3 based MCUs have a sophisticated and yet easy to use interrupt system called the Nested Vectored Interrupt Controller (NVIC). It ensures low latency and high performance. There are several features of the NVIC and these are handled by the compiler. Our job is simply to enjoy the lightning fast interrupt responses owing to the NVI
剩余内容已隐藏,支付完成后下载完整资料
目录
STM32 Analog-to-Digital Conversion 21
STM32 Digital-to-Analog Conversion 50
中文翻译一:
STM32 GPIO端口解析
在任何微控制器中,至少有一个通用输入输出端口。 STM32并不是一个不同的类型,所以正如预期的那样,它也有几个GPIO端口。 这些端口通常被命名为GPIOA,GPIOB等,但是与大多数8/16位微控制器不同,这些端口为16位字宽。 因此,通常每个端口都有16个IO引脚。 端口引脚具有几种工作模式,这就是使它们一开始既稳固又复杂的原因。 在开发板上,IO端口引脚的命名被缩短了,因此我们会找到PA0,PB12等,而不是GPIOA0,GPIOB12等。即使在参考手册中,这种简短的命名也得到了广泛使用。 最后,每个I / O引脚本质上都是通用的。
STM32的GPIO的特点
·在发生复位时,所有的GPIO端口均为浮空输入,这样可防止任何紧急情况造成意外破坏。
·所有的GPIO寄存器均需要使用32位字进行访问。这是强制性的。
·任何GPIO寄存器的保留位均保持在复位值即0。
·每个GPIO端口都有七个寄存器,但只有前四个是最重要的。其余大多数时间可以忽略。
·每个端口上这些重要的GPIO寄存器是CRL,CRH,IDR和ODR。剩下的三个GPIO端口的寄存器刚开始可以避免使用。(注:开始指初学时)
·对于位设置和位清除,操作REGx | =(1 lt;lt;bit)和REGx&=〜(1 lt;lt;bit)非常方便和有效。但是,MikroC编译器也可通过其内部头文件编码(如GPIOB_BSRRbits.BS15)提供位级别访问。 我们可以使用这种方法或其他方法。
·某些IO可以承受5V电压,并在数据表和其他文档中标记为“ FT”。 I / O引脚的其余部分容差不大,因此应始终确保3.3V操作。STM32的最大VDD容限为4V,但在任何情况下都不应使用此值以确保安全操作。最好是尽可能避免使用5V的GPIO。 这将有助于避免由于常见错误而导致的意外损坏。
·虽然不是必须但未使用的GPIO引脚应保持复位状态,或通过10kOmega;电阻接地,以使它们保持为浮动输入或安全接地。
·I / O引脚可提供或吸收高达25mA的电流。因此,它们可以直接驱动LED。 为了驱动需要超过该值的负载,我们必须使用外部BJT,MOSFET,光耦合器,晶体管阵列或其他驱动器设备。
·应注意避免将输出驱动到50MHz以上。这是最大的I / O引脚频率额定值。
·CRL/H根据下表独立地设置GPIO引脚基本操作模式:
·CRL将设置GPIO的0 – 7位,而CRH则将设置其8 –15位。与PIC或AVR等常见的8位单片机不同,所有IO端口均为16位宽。
CRL寄存器:
CRH寄存器:
·从端口配置表中可以看出,复位后立CRL和CRH的值立即变为0x44444444。 该值表示浮空输入模式。
·IDR寄存器负责数字输入,而ODR寄存器负责数字输出。在这两个寄存器中,其代表寄存器为32位宽,因为GPIO为16位宽,故高16位始终保持为0。 因此,仅关注较低的16位。 IDR和ODR的代码编写约定为0x0000xxxx。因为被设置为零,高位将被保留。
·就像CRL和CRH一样,IDR和ODR应该以32个字来访问。
·在使用GPIO端口的ODR或IDR寄存器之前,应首先初始化CRL和CRH。
ODR寄存器:
IDR寄存器:
·IDR和ODR的复位值均为零。这样可确保ODR的初始逻辑低电平状态和IDR的浮点空状态。
·每次器件复位后,所有外设时钟均被禁用(SRAM和FLITF除外)。使用外设之前,必须在RCC_AHBENR,RCC_APB1ENR或RCC_APB2ENR寄存器中启用其时钟。 这些寄存器很重要,应始终在使用内部外设之前进行设置。
APB2外设时钟使能寄存器(RCC_APB2ENR):
APB1外设时钟使能寄存器(RCC_APB1ENR):
APHB外设时钟使能寄存器(RCC_AHBENR):
·上述寄存器应首先设置。
用于ARM的MikroC具有用于GPIO的内置库。 尽管不了解内部寄存器也很容易上手这种库,但它们在空间和执行时间上都很费时。因此,对于简单的作品,最好避免使用它们,除非它们的使用使大型程序易于理解和处理。
外文原文一:
STM32 GPIO PORTS INSIGHTS
In any microcontroller there is at least one general purpose input-output port. STM32 is a not different breed and as expected it also has several GPIO ports. These ports are usually named GPIOA, GPIOB, etc. but unlike most 8/16-bit micros these ports are 16 bit wide. Thus, in general, every port has 16 IO pins. Port pins have several modes of operation and this is what that makes them both robust and complex at first. In development boards the IO port pin naming is cut short and so wersquo;ll find PA0, PB12, etc. instead of GPIOA0, GPIOB12, etc. Even in the reference manuals this short naming is used widely. In the end every I/O pin is general purpose in nature.
Key points with STM32rsquo;s GPIO
·Upon any reset event all GPIOs are floating inputs. This prevents any accidental
damage to GPIOs in the event of emergency.
·All GPIO registers are needed to be accessed with 32 bit words. This is mandatory.
·Reserved bits for any GPIO register are kept at reset values i.e. 0.
·For every GPIO port there are seven registers but the only the first four are the most important ones. The rest can be ignored most of the times.
·These important GPIO registers per port are CRL, CRH, IDR and ODR. The rest three registers for GPIO ports can be avoided at the beginning.
·For bit set and for bit clear, operations REGx |= (1 lt;lt; bit) and REGx amp;= ~ (1 lt;lt; bit) respectively are very handy and efficient. However MikroC compiler provides bit level access though its internal header file coding like this GPIOB_BSRRbits.BS15. We can use this method or the other one.
·Some IOs are 5V tolerant and are label “FT” in datasheets and other docs. The rest of the I/O pins are not that tolerant and so 3.3V operation should always be ensured. STM32 has a maximum VDD tolerance of 4V but this value shouldnrsquo;t be used in any situation for safe operation. Best is to avoid 5V use with GPIOs whenever possible. This will help in avoiding accidental damage to the micro due to common mistakes.
·Unused GPIO pins should be kept at reset state or tied to ground with 10kΩ resistors so that they remain either as floating inputs or safely connected to ground though itrsquo;s not a must.
·I/O pins can source or sink currents up to 25mA. Thus direct LED drive is possible with them. To drive loads that need more than this value, we must use external BJTs, MOSFETs, optocouplers, transistor arrays or other driver devices.
·I/O pins should not directly drive inductive or capacitive loads.
·Care should be taken to avoid driving
剩余内容已隐藏,支付完成后下载完整资料
资料编号:[235503],资料为PDF文档或Word文档,PDF文档可免费转换为Word
以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。