你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【NUCLEO-L476RG开发】1、自制的简易LED库

[复制链接]
JackieLaura 提问时间:2015-12-1 14:06 /
     之前活动最后一天抢了一块L476的开发板,开发板早就收到了。     不过前段时间有点小忙,一直没有分享学习笔记。现在在这里献丑了,也要感谢ST给的这次体验的机会。


main.c
  1. #include "stm32l476xx.h"
  2. #include "led.h"

  3. static int disable_jtag(void);

  4. int main(void)
  5. {
  6.         disable_jtag();
  7.         GreenLED.initialize(GreenLEDGPIO);
  8.         while(1)
  9.         {
  10.                 GreenLED.set(GreenLEDGPIO, LED_ON);
  11.                 GreenLED.set(GreenLEDGPIO, LED_OFF);
  12.         }
  13. }

  14. static int
  15.         disable_jtag(void)
  16. {
  17.         RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN | RCC_AHB2ENR_GPIOBEN;
  18.         //PA15 AF5
  19.         GPIOA->AFR[1] &= ~(0xf0000000);       
  20.         GPIOA->AFR[1] |=   0x50000000 ;
  21.         //PB3 PB4 PB5 AF5       
  22.         GPIOB->AFR[0] &= ~(0x00fff000);       
  23.         GPIOB->AFR[0] |=   0x00555000 ;
  24.         return 0;
  25. }
复制代码
led.h
  1. #ifndef _LED_H
  2. #define _LED_H

  3. #include "stm32l476xx.h"

  4. #define RCC_AHB2Periph_GPIOA               RCC_AHB2ENR_GPIOAEN
  5. #define RCC_AHB2Periph_GPIOB               RCC_AHB2ENR_GPIOBEN
  6. #define RCC_AHB2Periph_GPIOC               RCC_AHB2ENR_GPIOCEN
  7. #define RCC_AHB2Periph_GPIOD               RCC_AHB2ENR_GPIODEN
  8. #define RCC_AHB2Periph_GPIOE               RCC_AHB2ENR_GPIOEEN
  9. #define RCC_AHB2Periph_GPIOF               RCC_AHB2ENR_GPIOFEN
  10. #define RCC_AHB2Periph_GPIOG               RCC_AHB2ENR_GPIOGEN
  11. #define RCC_AHB2Periph_GPIOH               RCC_AHB2ENR_GPIOHEN

  12. typedef enum {
  13.         LED_ON = 0,
  14.         LED_OFF
  15. }LED_STATUS;

  16. typedef enum {       
  17.         GPIO_Pin_0 = 0,
  18.         GPIO_Pin_1,
  19.         GPIO_Pin_2,
  20.         GPIO_Pin_3,
  21.         GPIO_Pin_4,
  22.         GPIO_Pin_5,
  23.         GPIO_Pin_6,
  24.         GPIO_Pin_7,
  25.         GPIO_Pin_8,
  26.         GPIO_Pin_9,
  27.         GPIO_Pin_10,
  28.         GPIO_Pin_11,
  29.         GPIO_Pin_12,
  30.         GPIO_Pin_13,
  31.         GPIO_Pin_14,
  32.         GPIO_Pin_15,
  33.         GPIO_Pin_All,               
  34. }GPIO_PIN_E;

  35. typedef struct {       
  36.         GPIO_TypeDef* GPIO_PORT;
  37.         GPIO_PIN_E GPIO_PIN;
  38.         uint32_t GPIO_CLK;
  39. }LED_GPIO_T;

  40. typedef struct {
  41.         int (* initialize)(LED_GPIO_T);
  42.         int (* set)(LED_GPIO_T, LED_STATUS);
  43.         LED_STATUS (* get)(LED_GPIO_T);
  44.         int (* toggle)(LED_GPIO_T);
  45. }LED_T;

  46. extern LED_T GreenLED;
  47. extern LED_GPIO_T GreenLEDGPIO;

  48. #endif
复制代码
led.c
  1. #include "led.h"

  2. static int led_init(LED_GPIO_T);
  3. static int led_set(LED_GPIO_T, LED_STATUS);
  4. static LED_STATUS led_get(LED_GPIO_T);
  5. static int led_toggle(LED_GPIO_T);

  6. LED_GPIO_T GreenLEDGPIO = {       
  7.         .GPIO_PORT = GPIOA,
  8.         .GPIO_PIN = GPIO_Pin_5,
  9.         .GPIO_CLK = RCC_AHB2Periph_GPIOA,
  10. };

  11. LED_T GreenLED = {
  12.         .initialize = led_init,
  13.         .set = led_set,
  14.         .get = led_get,
  15.         .toggle = led_toggle,
  16. };

  17. static int
  18.         led_init(LED_GPIO_T gpio)
  19. {
  20.         int pincount = gpio.GPIO_PIN;
  21.        
  22.         RCC->AHB2ENR |= gpio.GPIO_CLK;
  23.        
  24.         gpio.GPIO_PORT->MODER &= \
  25.                                                                                                 ~(gpio.GPIO_PORT == GPIOA ? \
  26.                                                                                                 0x03ffffff : \
  27.                                                                                                 0xffffffff);                                                                                //reset values:0xabff ffff A                0xffff ffff B-G         0x0000 0000 H
  28.         gpio.GPIO_PORT->MODER |= \
  29.                                                                                                 (pincount == 16) ? \
  30.                                                                                                 (gpio.GPIO_PORT == GPIOA ? 0x01555555 : 0x55555555) : \
  31.                                                                                                 (1 << (pincount * 2));
  32.         gpio.GPIO_PORT->OTYPER &= \
  33.                                                                                                 (pincount == 16) ? \
  34.                                                                                                 0x0 : \
  35.                                                                                                 ~(1 << pincount);                                                                //reset values:0x0000 0000
  36.         gpio.GPIO_PORT->PUPDR &= \
  37.                                                                                                 (pincount == 16) ? \
  38.                                                                                                 ((gpio.GPIO_PORT == GPIOA || gpio.GPIO_PORT == GPIOB) ? 0xfc000000 : 0x0) : \
  39.                                                                                                 ~(1 << (pincount * 2));                                        //reset values:0x6400 0000 A/B        0x0000 0000 others
  40.         gpio.GPIO_PORT->OSPEEDR |= \
  41.                                                                                                 (pincount == 16) ? \
  42.                                                                                                 (gpio.GPIO_PORT == GPIOA ? 0xadaaaaaa : 0xaaaaaaaa) : \
  43.                                                                                                 (0x02 << (pincount * 2));                                //reset values:0x0c00 0000 A                0x0000 0000 others
  44.         return 0;
  45. }

  46. static int
  47.         led_set(LED_GPIO_T gpio, LED_STATUS ledStatus)
  48. {
  49.         switch(ledStatus)
  50.         {
  51.                 case LED_ON:
  52.                         gpio.GPIO_PORT->BSRR = gpio.GPIO_PIN == 16 ? 0xffff : 1 << gpio.GPIO_PIN;
  53.                         break;
  54.                 case LED_OFF:
  55.                         gpio.GPIO_PORT->BRR  = gpio.GPIO_PIN == 16 ? 0xffff : 1 << gpio.GPIO_PIN;
  56.                         break;
  57.         }
  58.         return 0;
  59. }

  60. static LED_STATUS
  61.         led_get(LED_GPIO_T gpio)
  62. {
  63.         int input = gpio.GPIO_PORT->ODR;
  64.         input >>= gpio.GPIO_PIN;
  65.         return  input == 1 ? LED_ON : LED_OFF;
  66. }

  67. static int
  68.         led_toggle(LED_GPIO_T gpio)
  69. {
  70.         switch(led_get(gpio))
  71.         {
  72.                 case LED_ON:
  73.                         led_set(gpio, LED_OFF);
  74.                         break;
  75.                 case LED_OFF:
  76.                         led_set(gpio, LED_ON);
  77.                         break;
  78.         }
  79.         return 0;
  80. }
复制代码


工程源码:
led.rar (28.55 KB, 下载次数: 6)

评分

参与人数 1 ST金币 +20 收起 理由
沐紫 + 20

查看全部评分

收藏 1 评论17 发布时间:2015-12-1 14:06

举报

17个回答
qianfan 回答时间:2015-12-1 16:30:51
JackieLaura 发表于 2015-12-1 15:41
标准库中的GPIOInitStructure不是每个IO占用一个?

那个是可以反复利用的。像GPIO_InitTypeDef ioIt;
ioIt.GPIO_Pin=GPIO_Pin_5;
GPIO_Init(GPIOA,&ioIt);
...

ioIt.GPIO_Pin=GPIO_Pin_6;
GPIO_Init(GPIOB,&ioIt);

而这个LED_GPIO_T是需要提前将所有的引脚定义,放到flash中的。不可能重复利用。一个引脚需要占用一个LED_GPIO_T的。如果你有100个引脚,需要占用100×sizeof(LED_GPIO_T),而使用GPIO_InitTypeDef只需要1×sizeof(GPIO_InitTypeDef)。
JackieLaura 回答时间:2015-12-1 15:41:23
QianFan 发表于 2015-12-1 14:47
不得不说,虽然很高大上,但是没啥用。每一个引脚占用一个LED_GPIO_T,占用了太多的资源。 ...

标准库中的GPIOInitStructure不是每个IO占用一个?
stmcu.org.png
JackieLaura 回答时间:2015-12-2 08:47:48
QianFan 发表于 2015-12-1 16:30
那个是可以反复利用的。像GPIO_InitTypeDef ioIt;
ioIt.GPIO_Pin=GPIO_Pin_5;
GPIO_Init(GPIOA,&ioIt);

作为全局变量。。。那我再改改。。。
stmcu.org.png
wamcncn 回答时间:2015-12-1 14:12:46
谢谢分享,真复杂
沐紫 回答时间:2015-12-1 14:19:18
谢谢你哦
JackieLaura 回答时间:2015-12-1 14:19:47
wambob 发表于 2015-12-1 14:12
谢谢分享,真复杂

是模仿别人方式写的。。。
stmcu.org.png
JackieLaura 回答时间:2015-12-1 14:20:23

谢谢沐紫的金币
stmcu.org.png
qianfan 回答时间:2015-12-1 14:47:43
不得不说,虽然很高大上,但是没啥用。每一个引脚占用一个LED_GPIO_T,占用了太多的资源。
yanhaijian 回答时间:2015-12-1 14:56:44
呵呵,来劲的。
stary666 回答时间:2015-12-1 16:52:26
看看,,,,,,,,,,,,,,,,,
黑皮男 回答时间:2015-12-1 16:53:13
不错,学习了,比我自己整的好多了
Paderboy 回答时间:2015-12-1 17:13:29
多谢分享。。。不错。。。、、
STMCU-Logo.png
orima 回答时间:2015-12-1 18:24:40
感谢分享。
JackieLaura 回答时间:2015-12-2 08:49:02
stary666 发表于 2015-12-1 16:52
看看,,,,,,,,,,,,,,,,,


stmcu.org.png
12下一页

所属标签

相似问题

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版