This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

F28027程序



小白,例程中有些函数需要初始化,有些不需要,请问初始化的目的及原则是什么??

  • 主要作用就是给函数一个初始值吧。应该程序运行时需要用到的函数都需要初始化。
    至于不需要初始化的函数,你方便举个例程中的例子吗?可能得具体问题具体分析了
  • 这是一个关于GPIO口的例程,但是他没有初始化 InitGpio(); ,我以前的理解是用到那个就初始化哪个,现在有点不太明白了,求指教


    #include "DSP28x_Project.h" // Device Headerfile and Examples Include File

    // Select the example to compile in. Only one example should be set as 1
    // the rest should be set as 0.

    #define EXAMPLE1 1 // Basic pinout configuration example
    #define EXAMPLE2 0 // Communication pinout example

    // Prototype statements for functions found within this file.
    void Gpio_setup1(void);
    void Gpio_setup2(void);

    void main(void)
    {
    // WARNING: Always ensure you call memcpy before running any functions from RAM
    // InitSysCtrl includes a call to a RAM based function and without a call to
    // memcpy first, the processor will go "into the weeds"
    #ifdef _FLASH
    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
    #endif

    // Step 1. Initialize System Control:
    // PLL, WatchDog, enable Peripheral Clocks
    // This example function is found in the f2802x_SysCtrl.c file.
    InitSysCtrl();

    // Step 2. Initialize GPIO:
    // This example function is found in the f2802x_Gpio.c file and
    // illustrates how to set the GPIO to it's default state.
    // InitGpio(); Skipped for this example

    // Step 3. Clear all interrupts and initialize PIE vector table:
    // Disable CPU interrupts
    DINT;

    // Initialize PIE control registers to their default state.
    // The default state is all PIE interrupts disabled and flags
    // are cleared.
    // This function is found in the f2802x_PieCtrl.c file.
    InitPieCtrl();

    // Disable CPU interrupts and clear all CPU interrupt flags:
    IER = 0x0000;
    IFR = 0x0000;

    // Initialize the PIE vector table with pointers to the shell Interrupt
    // Service Routines (ISR).
    // This will populate the entire table, even if the interrupt
    // is not used in this example. This is useful for debug purposes.
    // The shell ISR routines are found in f2802x_DefaultIsr.c.
    // This function is found in f2802x_PieVect.c.
    InitPieVectTable();

    // Step 4. Initialize all the Device Peripherals:
    // Not required for this example

    // Step 5. User specific code:

    #if EXAMPLE1

    // This example is a basic pinout
    Gpio_setup1();

    #endif // - EXAMPLE1

    #if EXAMPLE2

    // This example is a communications pinout
    Gpio_setup2();

    #endif

    }

    void Gpio_setup1(void)
    {
    // Example 1:
    // Basic Pinout.
    // This basic pinout includes:
    // PWM1-3, TZ1-TZ4, SPI-A, EQEP1, SCI-A, I2C
    // and a number of I/O pins

    // These can be combined into single statements for improved
    // code efficiency.

    // Enable PWM1-3 on GPIO0-GPIO5
    EALLOW;
    GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0; // Enable pullup on GPIO0
    GpioCtrlRegs.GPAPUD.bit.GPIO1 = 0; // Enable pullup on GPIO1
    GpioCtrlRegs.GPAPUD.bit.GPIO2 = 0; // Enable pullup on GPIO2
    GpioCtrlRegs.GPAPUD.bit.GPIO3 = 0; // Enable pullup on GPIO3
    GpioCtrlRegs.GPAPUD.bit.GPIO4 = 0; // Enable pullup on GPIO4
    GpioCtrlRegs.GPAPUD.bit.GPIO5 = 0; // Enable pullup on GPIO5
    GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1; // GPIO0 = PWM1A
    GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1; // GPIO1 = PWM1B
    GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1; // GPIO2 = PWM2A
    GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 1; // GPIO3 = PWM2B
    GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 1; // GPIO4 = PWM3A
    GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 1; // GPIO5 = PWM3B

    // Enable an GPIO output on GPIO6&7, set it high
    GpioCtrlRegs.GPAPUD.bit.GPIO6 = 0; // Enable pullup on GPIO6
    GpioDataRegs.GPASET.bit.GPIO6 = 1; // Load output latch
    GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 0; // GPIO6 = GPIO6
    GpioCtrlRegs.GPADIR.bit.GPIO6 = 1; // GPIO6 = output

    GpioCtrlRegs.GPAPUD.bit.GPIO7 = 0; // Enable pullup on GPIO7
    GpioDataRegs.GPASET.bit.GPIO7 = 1; // Load output latch
    GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 0; // GPIO7 = GPIO7
    GpioCtrlRegs.GPADIR.bit.GPIO7 = 1; // GPIO7 = output

    // Enable Trip Zone input on GPIO12
    GpioCtrlRegs.GPAPUD.bit.GPIO12 = 0; // Enable pullup on GPIO12
    GpioCtrlRegs.GPAQSEL1.bit.GPIO12 = 3; // asynch input
    GpioCtrlRegs.GPAMUX1.bit.GPIO12 = 1; // GPIO12 = TZ1

    // Enable SPI-A on GPIO16 - GPIO19
    GpioCtrlRegs.GPAPUD.bit.GPIO16 = 0; // Enable pullup on GPIO16
    GpioCtrlRegs.GPAPUD.bit.GPIO17 = 0; // Enable pullup on GPIO17
    GpioCtrlRegs.GPAPUD.bit.GPIO18 = 0; // Enable pullup on GPIO18
    GpioCtrlRegs.GPAPUD.bit.GPIO19 = 0; // Enable pullup on GPIO19
    GpioCtrlRegs.GPAQSEL2.bit.GPIO16 = 3; // asynch input
    GpioCtrlRegs.GPAQSEL2.bit.GPIO17 = 3; // asynch input
    GpioCtrlRegs.GPAQSEL2.bit.GPIO18 = 3; // asynch input
    GpioCtrlRegs.GPAQSEL2.bit.GPIO19 = 3; // asynch input
    GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 1; // GPIO16 = ***
    GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 1; // GPIO17 = SPIS0MIA
    GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1; // GPIO18 = ***
    GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1; // GPIO19 = SPISTEA

    // Enable SCI-A on GPIO28 - GPIO29
    GpioCtrlRegs.GPAPUD.bit.GPIO28 = 0; // Enable pullup on GPIO28
    GpioCtrlRegs.GPAQSEL2.bit.GPIO28 = 3; // Asynch input
    GpioCtrlRegs.GPAMUX2.bit.GPIO28 = 1; // GPIO28 = SCIRXDA
    GpioCtrlRegs.GPAPUD.bit.GPIO29 = 0; // Enable pullup on GPIO29
    GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 1; // GPIO29 = SCITXDA

    // Make GPIO34 an input
    GpioCtrlRegs.GPBPUD.bit.GPIO34 = 0; // Enable pullup on GPIO34
    GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0; // GPIO34 = GPIO34
    GpioCtrlRegs.GPBDIR.bit.GPIO34 = 0; // GPIO34 = input
    EDIS;
    }

    void Gpio_setup2(void)
    {
    // Example 1:
    // Communications Pinout.
    // This basic communications pinout includes:
    // PWM1-3, SPI-A, SCI-A
    // and a number of I/O pins

    // Enable PWM1-3 on GPIO0-GPIO5
    EALLOW;
    GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0; // Enable pullup on GPIO0
    GpioCtrlRegs.GPAPUD.bit.GPIO1 = 0; // Enable pullup on GPIO1
    GpioCtrlRegs.GPAPUD.bit.GPIO2 = 0; // Enable pullup on GPIO2
    GpioCtrlRegs.GPAPUD.bit.GPIO3 = 0; // Enable pullup on GPIO3
    GpioCtrlRegs.GPAPUD.bit.GPIO4 = 0; // Enable pullup on GPIO4
    GpioCtrlRegs.GPAPUD.bit.GPIO5 = 0; // Enable pullup on GPIO5
    GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1; // GPIO0 = PWM1A
    GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1; // GPIO1 = PWM1B
    GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1; // GPIO2 = PWM2A
    GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 1; // GPIO3 = PWM2B
    GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 1; // GPIO4 = PWM3A
    GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 1; // GPIO5 = PWM3B

    // Enable an GPIO output on GPIO6&7
    GpioCtrlRegs.GPAPUD.bit.GPIO6 = 0; // Enable pullup on GPIO6
    GpioDataRegs.GPASET.bit.GPIO6 = 1; // Load output latch
    GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 0; // GPIO6 = GPIO6
    GpioCtrlRegs.GPADIR.bit.GPIO6 = 1; // GPIO6 = output

    GpioCtrlRegs.GPAPUD.bit.GPIO7 = 0; // Enable pullup on GPIO7
    GpioDataRegs.GPASET.bit.GPIO7 = 1; // Load output latch
    GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 0; // GPIO7 = GPIO7
    GpioCtrlRegs.GPADIR.bit.GPIO7 = 1; // GPIO7 = output

    // Enable SPI-A on GPIO16 - GPIO19
    GpioCtrlRegs.GPAPUD.bit.GPIO16 = 0; // Enable pullup on GPIO16 (***)
    GpioCtrlRegs.GPAPUD.bit.GPIO17 = 0; // Enable pullup on GPIO17 (SPIS0MIA)
    GpioCtrlRegs.GPAPUD.bit.GPIO18 = 0; // Enable pullup on GPIO18 (***)
    GpioCtrlRegs.GPAPUD.bit.GPIO19 = 0; // Enable pullup on GPIO19 (SPISTEA)
    GpioCtrlRegs.GPAQSEL2.bit.GPIO16 = 3; // asynch input
    GpioCtrlRegs.GPAQSEL2.bit.GPIO17 = 3; // asynch input
    GpioCtrlRegs.GPAQSEL2.bit.GPIO18 = 3; // asynch input
    GpioCtrlRegs.GPAQSEL2.bit.GPIO19 = 3; // asynch input
    GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 1; // GPIO16 = ***
    GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 1; // GPIO17 = SPIS0MIA
    GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1; // GPIO18 = ***
    GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1; // GPIO19 = SPISTEA

    // Enable SCI-A on GPIO28 - GPIO29
    GpioCtrlRegs.GPAPUD.bit.GPIO28 = 0; // Enable pullup on GPIO28
    GpioCtrlRegs.GPAQSEL2.bit.GPIO28 = 3; // asynch input
    GpioCtrlRegs.GPAMUX2.bit.GPIO28 = 1; // GPIO28 = SCIRXDA
    GpioCtrlRegs.GPAPUD.bit.GPIO29 = 0; // Enable pullup on GPIO29
    GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 1; // GPIO29 = SCITXDA

    // Enable CAN-A on GPIO30 - GPIO31
    GpioCtrlRegs.GPAPUD.bit.GPIO30 = 0; // Enable pullup on GPIO30
    GpioCtrlRegs.GPAMUX2.bit.GPIO30 = 1; // GPIO30 = CANTXA
    GpioCtrlRegs.GPAPUD.bit.GPIO31 = 0; // Enable pullup on GPIO31
    GpioCtrlRegs.GPAQSEL2.bit.GPIO31 = 3; // asynch input
    GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 1; // GPIO31 = CANRXA

    /* Applicable only on those packages with GPIO32 and GPIO33 pinned out
    // Enable I2C-A on GPIO32 - GPIO33
    GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0; // Enable pullup on GPIO32
    GpioCtrlRegs.GPBPUD.bit.GPIO33 = 0; // Enable pullup on GPIO33
    GpioCtrlRegs.GPBQSEL1.bit.GPIO32 = 3; // asynch input
    GpioCtrlRegs.GPBQSEL1.bit.GPIO32 = 3; // asynch input
    GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 1; // GPIO32 = SDAA
    GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 1; // GPIO33 = SCLA
    */
    // Make GPIO34 an input
    GpioCtrlRegs.GPBPUD.bit.GPIO34 = 0; // Enable pullup on GPIO34
    GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0; // GPIO34 = GPIO34
    GpioCtrlRegs.GPBDIR.bit.GPIO34 = 0; // GPIO34 = input

    EDIS;
    }

    //===========================================================================
    // No more.
    //===========================================================================
  • 一般来说,这里还要分为两点,初始化可以分为库函数中官方的初始化和我们自己写的初始化函数。
    1.官方的初始化函数,一般是开启外设的时钟,给外设一个确切的状态。
    2.一般来说,MCU复位的时候,都会有个默认状态的。自己写初始化函数的目的在于,再次给需要使用的外设一个确切的状态。
  • 这个例程只是作为一个GPIO口配置的例程,也就是说它并没有真正使用GPIO口,所以并没有(或者说跳过)初始化。
    其中,Gpio_setup1是将GPIO口作为基本输出引脚需要进行的配置示例。Gpio_setup2是将GPIO作为通讯引脚需要进行的配置示例。这些说明其实程序中都有写明,你可以仔细看一下程序和程序中注释掉的说明。