DSP28335代码学习 下载本文

}

//--------------------------------------------------------------------------- // ConfigCpuTimer:

//--------------------------------------------------------------------------- // This function initializes the selected timer to the period specified // by the \// and the period in \// after configuration. //

void ConfigCpuTimer(struct CPUTIMER_VARS *Timer, float Freq, float Period) {

Uint32 temp;

// Initialize timer period: Timer->CPUFreqInMHz = Freq; Timer->PeriodInUSec = Period; temp = (long) (Freq * Period); Timer->RegsAddr->PRD.all = temp;

// Set pre-scale counter to divide by 1 (SYSCLKOUT): Timer->RegsAddr->TPR.all = 0; Timer->RegsAddr->TPRH.all = 0;

// Initialize timer control register:

Timer->RegsAddr->TCR.bit.TSS = 1; // 1 = Stop timer, 0 = Start/Restart Timer Timer->RegsAddr->TCR.bit.TRB = 1; // 1 = reload timer Timer->RegsAddr->TCR.bit.SOFT = 1;

Timer->RegsAddr->TCR.bit.FREE = 1; // Timer Free Run

Timer->RegsAddr->TCR.bit.TIE = 1; // 0 = Disable/ 1 = Enable Timer Interrupt

// Reset interrupt counter: Timer->InterruptCount = 0; }

//=========================================================================== // End of file.

3.通用输入/输出(GPIO) 3.1、GPIO概述

有3个32位I/O端口。端口A由GPIO0~GPIO31组成,端口B由GPIO32~GPIO63组成,端口C由GPIO64~GPIO87组成。 3.1.1、GPIO寄存器

1、控制寄存器:F2833x共有3个通用I/O端口。每个端口都有与之相对应的控制寄存器。下面列出端口A的寄存器 GPACTRL GPIO A控制寄存器(GPIO0~GPIO31) GPAQSEL1 GPIO A选择限制寄存器1 (GPIO0~GPIO15) GPAQSEL2 GPIO A选择限制寄存器2 (GPIO16~GPIO31) GPAMUX1 GPIO A多路开关控制寄存器1 (GPIO0~GPIO15) GPAMUX2 GPIO A多路开关控制寄存器2 (GPIO16~GPIO31) GPADIR GPIO A方向控制寄存器(GPIO0~GPIO31) GPAPUD GPIO A上拉禁止寄存器(GPIO0~GPIO31) 2、数据寄存器:下面给出端口A的数据寄存器,B,C端口一样。 GPADAT GPIO A数据寄存器(GPIO0~GPIO31) GPASET GPIO A置位寄存器(GPIO0~GPIO31) GPACLEAR GPIO A清楚寄存器(GPIO0~GPIO31) GPATOGGLE GPIO A切换寄存器(GPIO0~GPIO31) 3、GPIO中断和低功耗选择寄存器

3.1.2、寄存器功能介绍

1、GPxDAT寄存器(x=A,B,C)

每个I/O端口都有1个数据寄存器,它是一个读/写寄存器。读入的寄存器的值反映了输入限制后输入引脚当前的电平,写寄存器可设置输出引脚为相应的电平。

当GPxDAT.bit=0,并且引脚设置为输出,则把引脚拉成低电平; 当GPxDAT.bit=1,并且引脚设置为输出,则把引脚拉成高电平。 2、GPxSET寄存器(x=A,B,C)

每个I/O端口都有1个置位寄存器,它是一个只写寄存器,读为0。如果对应的引脚配置为输出,则向置位寄存器的该位写1,将对应的引脚电平拉高。写0无效。 当GPxSET.bit=0,无效;

当GPxDAT.bit=1,并且引脚设置为输出,则把引脚拉成高电平。 3、GPxCLEAR寄存器(x=A,B,C)

每个I/O端口都有1个清除寄存器,它是一个只写寄存器,读为0。如果对应的引脚配置为输出,则向置位寄存器的该位写1,将对应的引脚电平拉低。写0无效。 当GPxSET.bit=0,无效;

当GPxDAT.bit=1,并且引脚设置为输出,则把引脚拉成低电平。 4、GPxTOGGLE寄存器(x=A,B,C)

每个I/O端口都有1个切换寄存器,它是一个只写寄存器,读为0。如果对应的引脚配置为输出,则向置位寄存器的该位写1,将对应的引脚拉成反向电平。写0无效。 当GPxSET.bit=0,无效;

当GPxDAT.bit=1,并且引脚设置为输出,则把引脚拉成反向电平。既输出高电平变低电平,输出低电平变高电平。 小结:通过以上方式可以将引脚设置为高电平或者低电平。 3.1.3输入限制

用户可以通过配置GPAQSEL1、 GPAQSEL2、GPBQSEL1、GPBQSEL2寄存器来选择GPIO引脚的输入限制类型。对于一个GPIO输入引脚,输入限制可以仅被指定为与SY_SCLKOUT同步或采样窗限制;而对于配置为外设输入的引脚,除同步于

SYSCLKOUT或采样窗限制之外,还可以是异步的。 1、 输入异步

该模式仅用于不需要输入同步的外设或外设自身具有信号同步功能,如通信端口SCI、SPI、eCAN和IIC。如果引脚是GPIO引脚,则异步功能失效。 2、 仅与SYSCLKOUT同步

这是所有引脚复位时默认的输入限制模式。在该模式中,输入信号仅与SYSCLKOUT同步。因为引入的信号不同步,所以需要1个SYSCLKOUT的延迟。 3、 采用采样窗限制

在该模式中,输入信号首先与系统时钟同步(SYSCLKOUT同步),然后在允许输入改变之前通过特定的数个周期进行限制。需要指定两种参数:采样周期、采样数。 3.2程序实例

3.2.1、GPIO切换测试程序(GpioToggle.c) 1、GpioToggle.c文件要点 (1)内容:GPIO切换测试程序

(2)条件:本程序需要DSP2833Xv1.00头文件支持,另外还需要配置成“boot to H0”操作方式。除了boot方式配置之外,不需要其他的硬件配置。

(3)说明:这里包含了(数据寄存器方式、置位/清除寄存器方式、切换寄存器方式),可在编译前通过对程序开头#define声明选择执行其中的一个,并通过示波器观察各个引脚的输出波形。 2、GpioToggle.c文件 Example_2833xGpioToggle.c //

// TITLE: DSP2833x Device GPIO toggle test program.

#include \ // 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 // Use DATA registers to toggle I/O's #define EXAMPLE2 0 // Use SET/CLEAR registers to toggle I/O's #define EXAMPLE3 0 // Use TOGGLE registers to toggle I/O's

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

void Gpio_select(void); //Gpio配置函数

void Gpio_example1(void); //用数据寄存器设置I/O引脚电平函数 void Gpio_example2(void); //用置位/清除寄存器设置I/O引脚电平函数 void Gpio_example3(void); //用切换寄存器设置I/O引脚电平函数

void main(void) {

// Step 1. Initialize System Control: // PLL, WatchDog, enable Peripheral Clocks

// This example function is found in the DSP2833x_SysCtrl.c file.

InitSysCtrl();

// Step 2. Initalize GPIO:

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

// For this example use the following configuration: Gpio_select();

// 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 DSP2833x_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 DSP2833x_DefaultIsr.c. // This function is found in DSP2833x_PieVect.c. InitPieVectTable();

// Step 4. Initialize all the Device Peripherals:

// This function is found in DSP2833x_InitPeripherals.c // InitPeripherals(); // Not required for this example

// Step 5. User specific code:

#if EXAMPLE1

// This example uses DATA registers to toggle I/O's Gpio_example1();