高级会员

- 积分
- 807
- 金钱
- 807
- 注册时间
- 2016-5-16
- 在线时间
- 118 小时
|
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;
|
-
最佳答案
查看完整内容[请看2#楼]
1、结构体内的的成员定义叫函数指针,跟平常的指针一样,只是带参数;需要初始化一个函数实体,并使得这个函数指针指向该函数实体,然后通过指针调用函数;
2、操作系统的驱动常见这样的写法,看看Linux、RTOS的开源程序等等。
|