DSP 源程序(PWMDAC)中这样定义结构体的好处?
时间:10-02
整理:3721RD
点击:
最近在学习TMS320F2808,刚接触SPI,最初目的是先实现SPIDAC,就是与外部的DAC实现通信,所以在编写程序的时候,借鉴了PWMDAC的源码,结构体什么的都是仿照PWMDAC的源码编写的,但是有关结构体及指针的基础不是很好,不是很清楚在这个地方,结构体这样编写定义,以及这个地方指针的引用所带来的好处,希望有大神可以详细解释一下。下面是PWMDAC的部分源码:
/*-----------------------------------------------------------------------------
Define the structure of the PWMDAC Driver Object
-----------------------------------------------------------------------------*/
typedef struct {
int16 *PwmDacInPointer0; // Input: Pointer to source data output on PWMDAC channel 0
int16 *PwmDacInPointer1; // Input: Pointer to source data output on PWMDAC channel 1
int16 *PwmDacInPointer2; // Input: Pointer to source data output on PWMDAC channel 2
Uint16 PeriodMax; // Parameter: PWMDAC half period in number of clocks (Q0)
void (*init)(); // Pointer to the init function
void (*update)(); // Pointer to the update function
} PWMDAC ;
/*-----------------------------------------------------------------------------
Define a PWMDAC_handle
-----------------------------------------------------------------------------*/
typedef PWMDAC *PWMDAC_handle;
/*------------------------------------------------------------------------------
Default Initializers for the F280X PWMGEN Object
------------------------------------------------------------------------------*/
#define F280X_PWMDAC_DEFAULTS { (int16 *)0x300, \
(int16 *)0x300, \
(int16 *)0x300, \
500, \
(void (*)(Uint32))F280X_PWMDAC_Init, \
(void (*)(Uint32))F280X_PWMDAC_Update \
}
#define PWMDAC_DEFAULTS F280X_PWMDAC_DEFAULTS
/*------------------------------------------------------------------------------
Prototypes for the functions in F280XPWMDAC.C
------------------------------------------------------------------------------*/
void F280X_PWMDAC_Init(PWMDAC_handle);
void F280X_PWMDAC_Update(PWMDAC_handle);
#endif // __F280X_PWMDAC_H__
上面这几段代码是头文件中的,具体在主函数中还会有所涉及,主要想问的已经在前面说了,希望有大神可以帮忙解释一下这样的结构体编写及指针调用有什么好处,或者说意义?
/*-----------------------------------------------------------------------------
Define the structure of the PWMDAC Driver Object
-----------------------------------------------------------------------------*/
typedef struct {
int16 *PwmDacInPointer0; // Input: Pointer to source data output on PWMDAC channel 0
int16 *PwmDacInPointer1; // Input: Pointer to source data output on PWMDAC channel 1
int16 *PwmDacInPointer2; // Input: Pointer to source data output on PWMDAC channel 2
Uint16 PeriodMax; // Parameter: PWMDAC half period in number of clocks (Q0)
void (*init)(); // Pointer to the init function
void (*update)(); // Pointer to the update function
} PWMDAC ;
/*-----------------------------------------------------------------------------
Define a PWMDAC_handle
-----------------------------------------------------------------------------*/
typedef PWMDAC *PWMDAC_handle;
/*------------------------------------------------------------------------------
Default Initializers for the F280X PWMGEN Object
------------------------------------------------------------------------------*/
#define F280X_PWMDAC_DEFAULTS { (int16 *)0x300, \
(int16 *)0x300, \
(int16 *)0x300, \
500, \
(void (*)(Uint32))F280X_PWMDAC_Init, \
(void (*)(Uint32))F280X_PWMDAC_Update \
}
#define PWMDAC_DEFAULTS F280X_PWMDAC_DEFAULTS
/*------------------------------------------------------------------------------
Prototypes for the functions in F280XPWMDAC.C
------------------------------------------------------------------------------*/
void F280X_PWMDAC_Init(PWMDAC_handle);
void F280X_PWMDAC_Update(PWMDAC_handle);
#endif // __F280X_PWMDAC_H__
上面这几段代码是头文件中的,具体在主函数中还会有所涉及,主要想问的已经在前面说了,希望有大神可以帮忙解释一下这样的结构体编写及指针调用有什么好处,或者说意义?
这样做的话,你只需要一个该结构体,就可以对整个DAC操作了;
如果你有多个DAC需要操作,可以将不同DAC对应结构体的指针传给你的子程序,这样就可以用同一个子程序对不同的DAC完成相同操作;否则你是不是需要对每一个DAC写一个子程序呢?这些子程序的区别仅仅是DAC的地址不一样。