2017-03-09 91 views
1

我有一個關於「未知的儲存大小」的問題。我檢查了stackoverflow,我發現了一些答案。但是,這些解決方案無效,或者我無法很好地使用這些答案。「變量」的儲存大小未知

請幫我解決我的問題。 感謝您的幫助和愉快的一天。

main.c文件,

#include <avr/io.h> 
#include <PORT.h> 

int main(void) 
{ 

    // Insert code 
    PORT a; 

    while(1) 
    ; 

    return 0; 
} 

PORT.h文件

#ifndef PORT_H_INCLUDED 
#define PORT_H_INCLUDED 

#include <config.h> 

    typedef enum PORT_TYPE{ 
    // All port in ATmega328P 
     PORT_B, 
     PORT_C, 
     PORT_D 
    }PORT_TYPE; 

    typedef enum PORT_PIN{ 
    // All pins in ATmega328P 
     PIN_0, 
     PIN_1, 
     PIN_2, 
     PIN_3, 
     PIN_4, 
     PIN_5, 
     PIN_6, 
     PIN_7 
    }PORT_PIN; 

    typedef struct PORT PORT; 

    void PORT_init(PORT * const me, 
        void (* setDirectionFunction)(PORT * const me, PORT_PIN pinNumber), 
        void (* setStatusFunction)(PORT * const me, PORT_PIN pinNumber), 
        void (* enablePullResistorFunction)(PORT * const me, PORT_PIN pinNumber), 
        void (* disablePullResistorFunction)(PORT * const me, PORT_PIN pinNumber), 
        void (* notifyPinChangeFunction)(PORT * const me, PORT_PIN pinNumber), 
        unsigned char (*readPINFunction)(PORT * const me, PORT_PIN pinNumber), 
        unsigned char (*readPortFunction)(PORT * const me)); 

    void PORT_setDirection(PORT * const me, PORT_PIN pinNumber); 
    void PORT_setStatus(PORT * const me, PORT_PIN pinNumber); 
    void PORT_enablePullResistor(PORT * const me, PORT_PIN pinNumber); 
    void PORT_disablePullResistor(PORT * const me, PORT_PIN pinNumber); 
    void PORT_notifyPinChange(PORT * const me, PORT_PIN pinNumber); 
    unsigned char PORT_readPIN(PORT * const me, PORT_PIN pinNumber); 
    unsigned char PORT_readPort(PORT * const me); 

    PORT * PORT_create(PORT_TYPE whichPort); 
    void PORT_destroy(PORT * const me); 



#endif // PORT_H_INCLUDED 

PORT.c文件

#include <avr/io.h> 
#include <PORT.h> 

#define ADDR_PORTB (0x0023) 
#define ADDR_PORTC (0x0026) 
#define ADDR_PORTD (0x0029) 

typedef volatile struct{ 
// it is used for manipulating registers. 
    unsigned char pin; 
    unsigned char ddr; 
    unsigned char port; 
}PORT_hw; 

struct PORT{ 
    unsigned char changePIN; 
    PORT_hw volatile *p_hw; 
    void (* setDirection)(PORT * const me, PORT_PIN pinNumber); 
    void (* setStatus)(PORT * const me, PORT_PIN pinNumber); 
    void (* enablePullResistor)(PORT * const me, PORT_PIN pinNumber); 
    void (* disablePullResistor)(PORT * const me, PORT_PIN pinNumber); 
    void (* notifyPinChange)(PORT * const me, PORT_PIN pinNumber); 
    unsigned char (*readPIN)(PORT * const me, PORT_PIN pinNumber); 
    unsigned char (*readPort)(PORT * const me); 
}; 

static PORT g_PORT[3]; 
g_PORT[0].p_hw = (PORT_hw volatile *)ADDR_PORTB; 
g_PORT[1].p_hw = (PORT_hw volatile *)ADDR_PORTC; 
g_PORT[2].p_hw = (PORT_hw volatile *)ADDR_PORTD; 

void PORT_init(PORT * const me, 
        void (* setDirectionFunction)(PORT * const me, PORT_PIN pinNumber), 
        void (* setStatusFunction)(PORT * const me, PORT_PIN pinNumber), 
        void (* enablePullResistorFunction)(PORT * const me, PORT_PIN pinNumber), 
        void (* disablePullResistorFunction)(PORT * const me, PORT_PIN pinNumber), 
        void (* notifyPinChangeFunction)(PORT * const me, PORT_PIN pinNumber), 
        unsigned char (*readPINFunction)(PORT * const me, PORT_PIN pinNumber), 
        unsigned char (*readPortFunction)(PORT * const me)){ 
    me->changePIN = 0x00; 
    me->pinStatus = 0x00; 
    me->setDirection = setDirectionFunction; 
    me->setStatus = setStatusFunction; 
    me->enablePullResistor = enablePullResistorFunction; 
    me->disablePullResistor = disablePullResistorFunction; 
    me->notifyPinChange = notifyPinChangeFunction; 
    me->readPIN = readPINFunction; 
    me->readPort = readPortFunction; 
} 

void PORT_setDirection(PORT * const me, PORT_PIN pinNumber){} 
void PORT_setStatus(PORT * const me, PORT_PIN pinNumber){} 
void PORT_enablePullResistor(PORT * const me, PORT_PIN pinNumber){} 
void PORT_disablePullResistor(PORT * const me, PORT_PIN pinNumber){} 
void PORT_notifyPinChange(PORT * const me, PORT_PIN pinNumber){} 
unsigned char PORT_readPIN(PORT * const me, PORT_PIN pinNumber){} 
unsigned char PORT_readPort(PORT * const me){} 


PORT * PORT_create(PORT_TYPE whichPort){ 
    PORT *p_PORT = &(g_PORT[whichPort]); 
    PORT_init(p_PORT, PORT_setDirection, PORT_setStatus, PORT_enablePullResistor, PORT_disablePullResistor, PORT_notifyPinChange, PORT_readPIN, PORT_readPort); 
    return p_PORT; 
} 
void PORT_destroy(PORT * const me){} 

回答

2

的PORT.h頭文件聲明struct PORT存在,但沒有按沒有定義它:

typedef struct PORT PORT; 

實際定義在PORT.c中,在main.c中不可見。

因此,您無法創建PORT類型的變量。但是,您可以創建一個PORT *類型。由於定義未知,因此將其稱爲不透明指針

根據PORT.h中定義的函數判斷,您可以使用PORT_create函數返回一個PORT *,隨後可以將其傳遞給其他函數。