TI中文支持网
TI专业的中文技术问题搜集分享网站

F28027程序

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

Green Deng:主要作用就是给函数一个初始值吧。应该程序运行时需要用到的函数都需要初始化。
至于不需要初始化的函数,你方便举个例程中的例子吗?可能得具体问题具体分析了

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

user6102121:

回复 Green Deng:

这是一个关于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 _FLASHmemcpy(&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 interruptsDINT;

// 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 pinoutGpio_setup1();

#endif// – EXAMPLE1

#if EXAMPLE2
// This example is a communications pinoutGpio_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-GPIO5EALLOW;GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0;// Enable pullup on GPIO0GpioCtrlRegs.GPAPUD.bit.GPIO1 = 0;// Enable pullup on GPIO1GpioCtrlRegs.GPAPUD.bit.GPIO2 = 0;// Enable pullup on GPIO2GpioCtrlRegs.GPAPUD.bit.GPIO3 = 0;// Enable pullup on GPIO3GpioCtrlRegs.GPAPUD.bit.GPIO4 = 0;// Enable pullup on GPIO4GpioCtrlRegs.GPAPUD.bit.GPIO5 = 0;// Enable pullup on GPIO5GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;// GPIO0 = PWM1AGpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1;// GPIO1 = PWM1BGpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1;// GPIO2 = PWM2AGpioCtrlRegs.GPAMUX1.bit.GPIO3 = 1;// GPIO3 = PWM2BGpioCtrlRegs.GPAMUX1.bit.GPIO4 = 1;// GPIO4 = PWM3AGpioCtrlRegs.GPAMUX1.bit.GPIO5 = 1;// GPIO5 = PWM3B
// Enable an GPIO output on GPIO6&7, set it highGpioCtrlRegs.GPAPUD.bit.GPIO6 = 0;// Enable pullup on GPIO6GpioDataRegs.GPASET.bit.GPIO6 = 1;// Load output latchGpioCtrlRegs.GPAMUX1.bit.GPIO6 = 0;// GPIO6 = GPIO6GpioCtrlRegs.GPADIR.bit.GPIO6 = 1;// GPIO6 = output
GpioCtrlRegs.GPAPUD.bit.GPIO7 = 0;// Enable pullup on GPIO7GpioDataRegs.GPASET.bit.GPIO7 = 1;// Load output latchGpioCtrlRegs.GPAMUX1.bit.GPIO7 = 0;// GPIO7 = GPIO7GpioCtrlRegs.GPADIR.bit.GPIO7 = 1;// GPIO7 = output
// Enable Trip Zone input on GPIO12GpioCtrlRegs.GPAPUD.bit.GPIO12 = 0;// Enable pullup on GPIO12GpioCtrlRegs.GPAQSEL1.bit.GPIO12 = 3; // asynch inputGpioCtrlRegs.GPAMUX1.bit.GPIO12 = 1;// GPIO12 = TZ1
// Enable SPI-A on GPIO16 – GPIO19GpioCtrlRegs.GPAPUD.bit.GPIO16 = 0;// Enable pullup on GPIO16GpioCtrlRegs.GPAPUD.bit.GPIO17 = 0;// Enable pullup on GPIO17GpioCtrlRegs.GPAPUD.bit.GPIO18 = 0;// Enable pullup on GPIO18GpioCtrlRegs.GPAPUD.bit.GPIO19 = 0;// Enable pullup on GPIO19GpioCtrlRegs.GPAQSEL2.bit.GPIO16 = 3; // asynch inputGpioCtrlRegs.GPAQSEL2.bit.GPIO17 = 3; // asynch inputGpioCtrlRegs.GPAQSEL2.bit.GPIO18 = 3; // asynch inputGpioCtrlRegs.GPAQSEL2.bit.GPIO19 = 3; // asynch inputGpioCtrlRegs.GPAMUX2.bit.GPIO16 = 1;// GPIO16 = ***GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 1;// GPIO17 = SPIS0MIAGpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1;// GPIO18 = ***GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1;// GPIO19 = SPISTEA
// Enable SCI-A on GPIO28 – GPIO29GpioCtrlRegs.GPAPUD.bit.GPIO28 = 0;// Enable pullup on GPIO28GpioCtrlRegs.GPAQSEL2.bit.GPIO28 = 3; // Asynch inputGpioCtrlRegs.GPAMUX2.bit.GPIO28 = 1;// GPIO28 = SCIRXDAGpioCtrlRegs.GPAPUD.bit.GPIO29 = 0;// Enable pullup on GPIO29GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 1;// GPIO29 = SCITXDA
// Make GPIO34 an inputGpioCtrlRegs.GPBPUD.bit.GPIO34 = 0;// Enable pullup on GPIO34GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0; // GPIO34 = GPIO34GpioCtrlRegs.GPBDIR.bit.GPIO34 = 0;// GPIO34 = inputEDIS;
}

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-GPIO5EALLOW;GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0;// Enable pullup on GPIO0GpioCtrlRegs.GPAPUD.bit.GPIO1 = 0;// Enable pullup on GPIO1GpioCtrlRegs.GPAPUD.bit.GPIO2 = 0;// Enable pullup on GPIO2GpioCtrlRegs.GPAPUD.bit.GPIO3 = 0;// Enable pullup on GPIO3GpioCtrlRegs.GPAPUD.bit.GPIO4 = 0;// Enable pullup on GPIO4GpioCtrlRegs.GPAPUD.bit.GPIO5 = 0;// Enable pullup on GPIO5GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;// GPIO0 = PWM1AGpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1;// GPIO1 = PWM1BGpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1;// GPIO2 = PWM2AGpioCtrlRegs.GPAMUX1.bit.GPIO3 = 1;// GPIO3 = PWM2BGpioCtrlRegs.GPAMUX1.bit.GPIO4 = 1;// GPIO4 = PWM3AGpioCtrlRegs.GPAMUX1.bit.GPIO5 = 1;// GPIO5 = PWM3B
// Enable an GPIO output on GPIO6&7GpioCtrlRegs.GPAPUD.bit.GPIO6 = 0;// Enable pullup on GPIO6GpioDataRegs.GPASET.bit.GPIO6 = 1;// Load output latchGpioCtrlRegs.GPAMUX1.bit.GPIO6 = 0;// GPIO6 = GPIO6GpioCtrlRegs.GPADIR.bit.GPIO6 = 1;// GPIO6 = output
GpioCtrlRegs.GPAPUD.bit.GPIO7 = 0;// Enable pullup on GPIO7GpioDataRegs.GPASET.bit.GPIO7 = 1;// Load output latchGpioCtrlRegs.GPAMUX1.bit.GPIO7 = 0;// GPIO7 = GPIO7GpioCtrlRegs.GPADIR.bit.GPIO7 = 1;// GPIO7 = output
// Enable SPI-A on GPIO16 – GPIO19GpioCtrlRegs.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 inputGpioCtrlRegs.GPAQSEL2.bit.GPIO17 = 3; // asynch inputGpioCtrlRegs.GPAQSEL2.bit.GPIO18 = 3; // asynch inputGpioCtrlRegs.GPAQSEL2.bit.GPIO19 = 3; // asynch inputGpioCtrlRegs.GPAMUX2.bit.GPIO16 = 1;// GPIO16 = ***GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 1;// GPIO17 = SPIS0MIAGpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1;// GPIO18 = ***GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1;// GPIO19 = SPISTEA
// Enable SCI-A on GPIO28 – GPIO29GpioCtrlRegs.GPAPUD.bit.GPIO28 = 0;// Enable pullup on GPIO28GpioCtrlRegs.GPAQSEL2.bit.GPIO28 = 3; // asynch inputGpioCtrlRegs.GPAMUX2.bit.GPIO28 = 1;// GPIO28 = SCIRXDAGpioCtrlRegs.GPAPUD.bit.GPIO29 = 0;// Enable pullup on GPIO29GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 1;// GPIO29 = SCITXDA
// Enable CAN-A on GPIO30 – GPIO31GpioCtrlRegs.GPAPUD.bit.GPIO30 = 0;// Enable pullup on GPIO30GpioCtrlRegs.GPAMUX2.bit.GPIO30 = 1;// GPIO30 = CANTXAGpioCtrlRegs.GPAPUD.bit.GPIO31 = 0;// Enable pullup on GPIO31GpioCtrlRegs.GPAQSEL2.bit.GPIO31 = 3; // asynch inputGpioCtrlRegs.GPAMUX2.bit.GPIO31 = 1;// GPIO31 = CANRXA

/* Applicable only on those packages with GPIO32 and GPIO33 pinned out// Enable I2C-A on GPIO32 – GPIO33GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0;// Enable pullup on GPIO32GpioCtrlRegs.GPBPUD.bit.GPIO33 = 0;// Enable pullup on GPIO33GpioCtrlRegs.GPBQSEL1.bit.GPIO32 = 3; // asynch inputGpioCtrlRegs.GPBQSEL1.bit.GPIO32 = 3; // asynch inputGpioCtrlRegs.GPBMUX1.bit.GPIO32 = 1;// GPIO32 = SDAAGpioCtrlRegs.GPBMUX1.bit.GPIO33 = 1;// GPIO33 = SCLA
*/// Make GPIO34 an inputGpioCtrlRegs.GPBPUD.bit.GPIO34 = 0;// Enable pullup on GPIO34GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0;// GPIO34 = GPIO34GpioCtrlRegs.GPBDIR.bit.GPIO34 = 0;// GPIO34 = input
EDIS;
}

//===========================================================================
// No more.
//===========================================================================

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

笨鸟:一般来说,这里还要分为两点,初始化可以分为库函数中官方的初始化和我们自己写的初始化函数。
1.官方的初始化函数,一般是开启外设的时钟,给外设一个确切的状态。
2.一般来说,MCU复位的时候,都会有个默认状态的。自己写初始化函数的目的在于,再次给需要使用的外设一个确切的状态。

赞(0)
未经允许不得转载:TI中文支持网 » F28027程序
分享到: 更多 (0)