OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2528|回复: 3

别人的这两行代码干什么用的?

[复制链接]

22

主题

87

帖子

0

精华

高级会员

Rank: 4

积分
807
金钱
807
注册时间
2016-5-16
在线时间
118 小时
发表于 2018-3-9 16:18:18 | 显示全部楼层 |阅读模式
1金钱
[mw_shl_code=applescript,true]/*
* FILE                : led.h
* DESCRIPTION         : This file is for led.c
* Author              : ysloveivy
* Copyright           :
*
* History
* --------------------
* Rev                 : 0.00
* Date                : 11/21/2015
*
* create.
* --------------------
*/
#ifndef __led_h__
#define __led_h__

//--------------------------- Define ---------------------------//
//红灯<----->PI5
#define LED_RED_OFF GPIO_SetBits(GPIOI,GPIO_Pin_5)
#define LED_RED_ON  GPIO_ResetBits(GPIOI,GPIO_Pin_5)
//绿灯<----->PI6
#define LED_GREEN_OFF GPIO_SetBits(GPIOI,GPIO_Pin_6)
#define LED_GREEN_ON  GPIO_ResetBits(GPIOI,GPIO_Pin_6)
//蓝灯<----->PI7
#define LED_BLUE_OFF GPIO_SetBits(GPIOI,GPIO_Pin_7)
#define LED_BLUE_ON  GPIO_ResetBits(GPIOI,GPIO_Pin_7)

//----------------------- Include files ------------------------//

//-------------------------- Typedef----------------------------//
typedef struct {
        int (* initialize)(void);
}LED_T;

//--------------------------- Extern ---------------------------//[mw_shl_code=applescript,true]/*
* FILE                : led.c
* DESCRIPTION         : This file is led driver.
* Author              : ysloveivy
* Copyright           :
*
* History
* --------------------
* Rev                 : 0.00
* Date                : 11/21/2015
*
* create.
* --------------------
*/
//--------------------------- Include ---------------------------//
#include "..\include\led.h"
#include "..\fwlib\inc\stm32f4xx_gpio.h"
#include "..\fwlib\inc\stm32f4xx_rcc.h"

//--------------------- Function Prototype ----------------------//
static int initialize(void);

//--------------------------- Variable --------------------------//
LED_T led = {
        .initialize = initialize
};
//--------------------------- Function --------------------------//
/*
* Name                : initialize
* Description         : ---
* Author              : ysloveivy.
*
* History
* --------------------
* Rev                 : 0.00
* Date                : 11/21/2015
*
* create.
* --------------------
*/
static int initialize(void)
{
        GPIO_InitTypeDef   GPIO_uInitStructure;

        //LED IO初始化
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI,ENABLE);
        GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;    //设置连接三色LED灯的IO端口
        GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_OUT;                          //设置端口为输出模式
        GPIO_uInitStructure.GPIO_OType = GPIO_OType_PP;                         //推挽输出
        GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_UP;                           //上拉
        GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_100MHz;                     //设置速度为第三级

        GPIO_Init(GPIOI,&GPIO_uInitStructure);

        //PI5、PI6、PI7接三色LED灯,PI5、PI6、PI7置高电位,灯熄灭
        GPIO_SetBits(GPIOI,GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);

        return 0;
}
[/mw_shl_code]
extern LED_T led;

#endif //__led_h__
[/mw_shl_code]
这几个代码什么意思?
typedef struct {
        int (* initialize)(void);
}LED_T;

//--------------------------- Extern ---------------------------//
extern LED_T led;

1$YYHL{RA7[EBSCC94OZVG1.png

最佳答案

查看完整内容[请看2#楼]

1、结构体内的的成员定义叫函数指针,跟平常的指针一样,只是带参数;需要初始化一个函数实体,并使得这个函数指针指向该函数实体,然后通过指针调用函数; 2、操作系统的驱动常见这样的写法,看看Linux、RTOS的开源程序等等。
团战可以不可以输,TM也必须死
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

2

主题

685

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
3448
金钱
3448
注册时间
2017-7-4
在线时间
869 小时
发表于 2018-3-9 16:18:19 | 显示全部楼层
1、结构体内的的成员定义叫函数指针,跟平常的指针一样,只是带参数;需要初始化一个函数实体,并使得这个函数指针指向该函数实体,然后通过指针调用函数;
2、操作系统的驱动常见这样的写法,看看Linux、RTOS的开源程序等等。
回复

使用道具 举报

2

主题

20

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
348
金钱
348
注册时间
2017-10-13
在线时间
39 小时
发表于 2018-3-9 19:40:33 | 显示全部楼层
百度下: C语言 结构体 函数指针,你就明白了
回复

使用道具 举报

6

主题

107

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
374
金钱
374
注册时间
2012-12-30
在线时间
48 小时
发表于 2018-3-9 22:10:39 | 显示全部楼层
这是C++的语法吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-8 18:02

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表