Sei sulla pagina 1di 8

//###########################################################################

// Description:
//! \addtogroup f2833x_example_list
//!
//! <h1>GPIO Toggle (gpio_toggle)</h1>
//!
//! \note ALL OF THE I/O'S TOGGLE IN THIS PROGRAM. MAKE SURE
//! THIS WILL NOT DAMAGE YOUR HARDWARE BEFORE RUNNING THIS
//! EXAMPLE.
//!
//! Three different examples are included. Select the example
//! (data, set/clear or toggle) to execute before compiling using
//! the macros found at the top of the code.
//!
//! Each example toggles all the GPIOs in a different way, the first
//! through writing values to the GPIO DATA registers, the second through
//! the SET/CLEAR registers and finally the last through the TOGGLE register
//!
//! The pins can be observed using Oscilloscope.
//
//###########################################################################
// $TI Release: F2833x/F2823x Header Files and Peripheral Examples V141 $
// $Release Date: November 6, 2015 $
// $Copyright: Copyright (C) 2007-2015 Texas Instruments Incorporated -
// http://www.ti.com/ ALL RIGHTS RESERVED $
//###########################################################################

#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 0 // 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

// add for example


#define EXAMPLE4 1 // Example for SMC150 B/D

// Prototype statements for functions found within this file.


void delay_loop(void);
void Gpio_select(void);
void Gpio_example1(void);
void Gpio_example2(void);
void Gpio_example3(void);
void Gpio_example4(void); // add for example

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. Initialize 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();

#endif // - EXAMPLE1

#if EXAMPLE2

// This example uses SET/CLEAR registers to toggle I/O's


Gpio_example2();

#endif

#if EXAMPLE3

// This example uses TOGGLE registers to toggle I/O's


Gpio_example3();

#endif

// add for example


#if EXAMPLE4

// This example uses TOGGLE registers to toggle I/O's


Gpio_example4();
#endif
}

void delay_loop()
{
volatile long i;
for (i = 0; i < 1000000; i++) {}
}

void Gpio_example1(void)
{
// Example 1:
// Toggle I/Os using DATA registers

for(;;)
{
GpioDataRegs.GPADAT.all =0xAAAAAAAA;
GpioDataRegs.GPBDAT.all =0x0000000A;

delay_loop();

GpioDataRegs.GPADAT.all =0x55555555;
GpioDataRegs.GPBDAT.all =0x00000005;

delay_loop();
}
}

void Gpio_example2(void)
{
// Example 2:
// Toggle I/Os using SET/CLEAR registers
for(;;)
{

GpioDataRegs.GPASET.all =0xAAAAAAAA;
GpioDataRegs.GPACLEAR.all =0x55555555;

GpioDataRegs.GPBSET.all =0x0000000A;
GpioDataRegs.GPBCLEAR.all =0x00000005;

delay_loop();

GpioDataRegs.GPACLEAR.all =0xAAAAAAAA;
GpioDataRegs.GPASET.all =0x55555555;

GpioDataRegs.GPBCLEAR.all =0x0000000A;
GpioDataRegs.GPBSET.all =0x00000005;

delay_loop();
}
}

void Gpio_example3(void)
{
// Example 2:
// Toggle I/Os using TOGGLE registers
// Set pins to a known state

GpioDataRegs.GPASET.all =0xAAAAAAAA;
GpioDataRegs.GPACLEAR.all =0x55555555;

GpioDataRegs.GPBSET.all =0x0000000A;
GpioDataRegs.GPBCLEAR.all =0x00000005;

// Use TOGGLE registers to flip the state of


// the pins.
// Any bit set to a 1 will flip state (toggle)
// Any bit set to a 0 will not toggle.

for(;;)
{
GpioDataRegs.GPATOGGLE.all =0xFFFFFFFF;
GpioDataRegs.GPBTOGGLE.all =0xFFFFFFFF; //0x0000000F;
delay_loop();
}
}

// add for example


void Gpio_example4(void)
{
GpioDataRegs.GPCSET.all =0xAAAAAAAA;
GpioDataRegs.GPCCLEAR.all =0x55555555;

for(;;)
{
GpioDataRegs.GPCTOGGLE.all =0xFFFFFFFF;
delay_loop();
}
}

void Gpio_select(void)
{
EALLOW;
GpioCtrlRegs.GPAMUX1.all = 0x00000000; // All GPIO
GpioCtrlRegs.GPAMUX2.all = 0x00000000; // All GPIO
GpioCtrlRegs.GPAMUX1.all = 0x00000000; // All GPIO
GpioCtrlRegs.GPADIR.all = 0xFFFFFFFF; // All outputs
GpioCtrlRegs.GPBDIR.all = 0xFFFFFFFF; // All outputs

// add for example


GpioCtrlRegs.GPCMUX1.all = 0x00000000;
GpioCtrlRegs.GPCDIR.all = 0xFFFFFFFF;
EDIS;
}
//===========================================================================
// No more.
//===========================================================================
Hi,
I have programmed to GPIO pins from GPIO-0/1/2/3/4/5 which are PWM1A/1B, PWM2A/2B and PWM3A/3B.
1. When I want to disable the PWM's 1/2/3, I am setting the following GPIO pins to LOW that is GPIO0/1, GPIO2/3 and
GPIO4/5 bit to 0. I am not sure whether I need to set the
GPIO control registers everytime to perform. Can I use
GpioDataRegs.GPACLEAR.bit.GPIO0 = 0; // SET INPUT TO LOW
GpioDataRegs.GPASET.bit.GPIO1 = 1; // SET INPUT TO HIGH. without performing on GPIO control registers?
Please clarify?
Code:
{
// PWM1a & PWM 1B are set to ZERO in GPIO Pin 0 and 1.
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO0 = 1; // Enable pulldown on GPIO0 PWM-1A
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0; // GPIO0 = GPIO0
GpioCtrlRegs.GPADIR.bit.GPIO0 = 1; // GPIO0 = output
EDIS;
GpioDataRegs.GPACLEAR.bit.GPIO0 = 0; // SET INPUT TO LOW.

EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO1 = 1; // Enable pulldown on GPIO1 PWM-1B
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0; // GPIO1 = GPIO1
GpioCtrlRegs.GPADIR.bit.GPIO1 = 1; // GPIO1 = output
EDIS;
GpioDataRegs.GPACLEAR.bit.GPIO1 = 0; // SET INPUT TO LOW.

// PWM2A & PWM 2B are set to ZERO in GPIO Pin 2 and 3


EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO2 = 1; // Enable pulldown on GPIO2 PWM-2A
GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 0; // GPIO2 = GPIO2
GpioCtrlRegs.GPADIR.bit.GPIO2 = 1; // GPIO2 = output
EDIS;
GpioDataRegs.GPACLEAR.bit.GPIO2 = 0; // SET INPUT TO LOW

EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO3 = 1; // Enable pulldown on GPIO2 PWM-2B
GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 0; // GPIO3 = GPIO3
GpioCtrlRegs.GPADIR.bit.GPIO3 = 1; // GPIO3 = output
EDIS;
GpioDataRegs.GPACLEAR.bit.GPIO3 = 0; // SET INPUT TO LOW

// PWM3A & PWM 3B are set to ZERO in GPIO Pin 4 and 5


EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO4 = 1; // Enable pulldown on GPIO2 PWM-3A
GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 0; // GPIO4 = GPIO4
GpioCtrlRegs.GPADIR.bit.GPIO4 = 1; // GPIO4 = output
EDIS;
GpioDataRegs.GPACLEAR.bit.GPIO4 = 0; // SET INPUT TO LOW

EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO5 = 1; // Enable pulldown on GPIO2 PWM-3B
GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 0; // GPIO5 = GPIO5
GpioCtrlRegs.GPADIR.bit.GPIO5 = 1; // GPIO4 = output
EDIS;
GpioDataRegs.GPACLEAR.bit.GPIO5 = 0; // SET INPUT TO LOW
}
2. Secondly, I want to set PWM1A to LOW and PWM1B to HIGH. Similary for PWM2A/3A to LOW and PWM2B/3B to
HIGH. Below is the code.
function:
// PWM1A is set to ZERO and PWM1B is set to HIGH on GPIO Pin 0 and 1.
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO0 = 1; // Enable pulldown on GPIO1 PWM-1A
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0; // GPIO0 = GPIO0
GpioCtrlRegs.GPADIR.bit.GPIO0 = 1; // GPIO0 = output
EDIS;
GpioDataRegs.GPACLEAR.bit.GPIO0 = 0; // SET INPUT TO LOW.

EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO1 = 1; // Enable pulldown on GPIO1 PWM-1B
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0; // GPIO1 = GPIO1
GpioCtrlRegs.GPADIR.bit.GPIO1 = 1; // GPIO1 = output
EDIS;
GpioDataRegs.GPASET.bit.GPIO1 = 1; // SET INPUT TO HIGH.

// PWM2A is set to ZERO and PWM2B to HIGH in GPIO Pin 2 and 3


EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO2 = 1; // Enable pulldown on GPIO2 PWM-2A
GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 0; // GPIO2 = GPIO2
GpioCtrlRegs.GPADIR.bit.GPIO2 = 1; // GPIO2 = output
EDIS;
GpioDataRegs.GPACLEAR.bit.GPIO2 = 0; // SET INPUT TO LOW

EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO3 = 1; // Enable pulldown on GPIO3 PWM-2B
GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 0; // GPIO3 = GPIO3
GpioCtrlRegs.GPADIR.bit.GPIO3 = 1; // GPIO3 = output
EDIS;
GpioDataRegs.GPASET.bit.GPIO3 = 0; // SET INPUT TO HIGH

// PWM3A is set to ZERO and PWM3B is set to HIGH in GPIO Pin 4 and 5
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO4 = 1; // Enable pulldown on GPIO4 PWM-3A
GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 0; // GPIO4 = GPIO4
GpioCtrlRegs.GPADIR.bit.GPIO4 = 1; // GPIO4 = output
EDIS;
GpioDataRegs.GPACLEAR.bit.GPIO4 = 0; // SET INPUT TO LOW

EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO5 = 1; // Enable pulldown on GPIO5 PWM-3B
GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 0; // GPIO5 = GPIO5
GpioCtrlRegs.GPADIR.bit.GPIO5 = 1; // GPIO5 = output
EDIS;
GpioDataRegs.GPASET.bit.GPIO5 = 0; // SET INPUT TO HIGH

Kuldeep,
The main issue that I see with your code is that:
GpioCtrlRegs.GPACLEAR.bit.GPIO0 = 0;
does nothing to the GPIO. If instead you do:
GpioCtrlRegs.GPACLEAR.bit.GPIO0 = 1;
GPIO0 will become low.
In general, to change the output of a GPIO, you only need the GPxCLEAR, GPxSET, or GPxDAT instruction. The GPIO
control registers only need to be edited during initialization (or whenever you want the basic functionality of a pin to
change (ie GPIOoutput to PWM output) )
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
#include "lab2.h"
long int i,k,l;
long int retardo=5000000;
long int test=0; // Variable para buscar errores
void main(void) {
DINT; //deshabilita interrupciones
DRTM; //deshabilita interrupciones real time mode
InitSysCtrl(); //inicializa el sistema como PLL,clocks,watcgdog
InitPieCtrl(); //inicializa el apoyo de interrupción de periféricos
IER = 0x0000; //limpia máscara de interrupciones
IFR = 0x0000; //limpia bandera de interrupciones
InitPieVectTable(); //inicializa tabla de interrupciones por defecto
EINT; //habilita interrupciones
ERTM;
EALLOW;
//Pull up y Multiplexor de los pines
GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0; // Habilitar pullup en GPIO0
GpioCtrlRegs.GPAPUD.bit.GPIO1 = 0; // Habilitar pullup en GPIO1
GpioCtrlRegs.GPAPUD.bit.GPIO2 = 0; // Habilitar pullup en GPIO2
GpioCtrlRegs.GPAPUD.bit.GPIO3 = 0; // Habilitar pullup en GPIO3
GpioCtrlRegs.GPAPUD.bit.GPIO4 = 0; // Habilitar pullup en GPIO4
GpioCtrlRegs.GPAPUD.bit.GPIO5 = 0; // Habilitar pullup en GPIO5
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0; // GPIO0 = GPIO0
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0; // GPIO1 = GPIO1
GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1; // GPIO2 = PWM2A
GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 2; // GPIO3 = ECAP5
GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 0; // GPIO4 = GPIO4
GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 3; // GPIO5 = ECAP1

// Habilitar un GPIO como salida en el GPIO6, usar el "set" para puesta a uno
GpioCtrlRegs.GPAPUD.bit.GPIO6 = 0; // Habilitar pullup en GPIO6
GpioDataRegs.GPASET.bit.GPIO6 = 1; // cargar latch de salida = 1
GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 0; // GPIO6 = GPIO6
GpioCtrlRegs.GPADIR.bit.GPIO6 = 1; // GPIO6 = es salida

// Habilitar eCAP2 en GPIO7 con calificador sincronizado con el Sysclout (defecto)


GpioCtrlRegs.GPAPUD.bit.GPIO7 = 0; // Habilitar pullup en GPIO7
GpioCtrlRegs.GPAQSEL1.bit.GPIO7 = 0; // Sincronizar con reloj principal SYSCLOUT
GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 3; // GPIO7 = ECAP2

// Habilitar GPIO como entrada en GPIO8 con pull up


GpioCtrlRegs.GPAPUD.bit.GPIO8 = 0; // Habilitar pullup en GPIO8
GpioCtrlRegs.GPAQSEL1.bit.GPIO8 = 2; // Selector de calificador a 6 muestras
GpioCtrlRegs.GPAMUX1.bit.GPIO8 = 0; // GPIO8 = GPIO8
GpioCtrlRegs.GPADIR.bit.GPIO8 = 0; // GPIO8 = es entrada

// Habilitar GPIO como entrada en GPIO9 sin pull up


GpioCtrlRegs.GPAPUD.bit.GPIO9 = 1; // Deshabilitar el pullup en GPIO9
GpioCtrlRegs.GPAQSEL1.bit.GPIO9 = 3; // Selector de calificador a modo asíncrono
GpioCtrlRegs.GPAMUX1.bit.GPIO9 = 0; // GPIO9 = GPIO9
GpioCtrlRegs.GPADIR.bit.GPIO9 = 1; // GPIO9 = es entrada

// Habilitar un GPIO como salida en el GPIO10, usar el "clear" para puesta a cero
GpioCtrlRegs.GPAPUD.bit.GPIO10 = 0; // Habilitar pullup en GPIO10
GpioDataRegs.GPACLEAR.bit.GPIO10 = 1; // cargar latch de salida = 1
GpioCtrlRegs.GPAMUX1.bit.GPIO10 = 0; // GPIO10 = GPIO10
GpioCtrlRegs.GPADIR.bit.GPIO10 = 1; // GPIO10 = es salida

// Habilitar un GPIO como salida en el GPIO11, luego se hará un TOGGLE


GpioCtrlRegs.GPAPUD.bit.GPIO11 = 0; // Habilitar pullup en GPIO11
GpioCtrlRegs.GPAMUX1.bit.GPIO11 = 0; // GPIO11 = GPIO11
GpioCtrlRegs.GPADIR.bit.GPIO11 = 1; // GPIO11 = es salida

// Otros GPIO12 - GPIO15


GpioCtrlRegs.GPAPUD.bit.GPIO12 = 0; // Habilitar pullup en GPIO12
GpioCtrlRegs.GPAPUD.bit.GPIO13 = 0; // Habilitar pullup en GPIO13
GpioCtrlRegs.GPAPUD.bit.GPIO14 = 0; // Habilitar pullup en GPIO14
GpioCtrlRegs.GPAPUD.bit.GPIO15 = 0; // Habilitar pullup en GPIO15
GpioDataRegs.GPASET.bit.GPIO12 = 1; // cargar latch de salida = 1
GpioDataRegs.GPASET.bit.GPIO13 = 1; // cargar latch de salida = 1

GpioCtrlRegs.GPAQSEL1.bit.GPIO14 = 3; // Selector de calificador a modo asíncrono


GpioCtrlRegs.GPAQSEL1.bit.GPIO15 = 3; // Selector de calificador a modo asíncrono
GpioCtrlRegs.GPAMUX1.bit.GPIO12 = 0; // GPIO12 = GPIO12
GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 0; // GPIO13 = GPIO13
GpioCtrlRegs.GPADIR.bit.GPIO12 = 1; // GPIO12 = es salida
GpioCtrlRegs.GPADIR.bit.GPIO13 = 1; // GPIO12 = es salida
GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 0; // GPIO13 = GPIO13
GpioCtrlRegs.GPAMUX1.bit.GPIO14 = 1; // GPIO14 = TZ3
GpioCtrlRegs.GPAMUX1.bit.GPIO15 = 1; // GPIO15 = TZ4
// Luces en el control Card
GpioCtrlRegs.GPAPUD.bit.GPIO31 = 0; // Habilitar pullup en GPIO31
GpioDataRegs.GPADAT.bit.GPIO31 = 1; // Latch a 1
GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 0; // GPIO31 = GPIO31
GpioCtrlRegs.GPADIR.bit.GPIO31 = 1; // GPIO31 = es salida
GpioCtrlRegs.GPBPUD.bit.GPIO34 = 0; // Habilitar pullup en GPIO34
GpioDataRegs.GPBDAT.bit.GPIO34 = 0; // Latch a 0
GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0; // GPIO34 = GPIO34
GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1; // GPIO34 = es salida
EDIS;
//Datos a poner en los pines
GpioDataRegs.GPATOGGLE.bit.GPIO11 = 1; // Modificar alternando estado de GPIO11
GpioDataRegs.GPADAT.bit.GPIO12 = 0; // GPIO12 es salida a 0
GpioDataRegs.GPADAT.bit.GPIO13 = 1; // GPIO13 es salida a 1
EINT;
ERTM;

//XF bandera externa


for (i=0;i<60;i++) //Los retardo son proporcionales a la velocidad de la memoria
{
GpioDataRegs.GPATOGGLE.bit.GPIO11 =1; //Alternar estado del pin GPIO11
for (k=0;k<retardo;k++); // bloque de retardo
GpioDataRegs.GPATOGGLE.bit.GPIO31 = 1;
GpioDataRegs.GPBTOGGLE.bit.GPIO34 = 1;
};
GpioDataRegs.GPADAT.bit.GPIO31 =0; //Al terminar dejar pin OFF
GpioDataRegs.GPBDAT.bit.GPIO34 = 1; //Al terminar dejar pin ON
botones.all=0;
while(1){ //hacer loop por lo menos una vez
test++; // test debería cambiar en cada pasada
*(long int *)0x8000=test; // La memoria =0x8000 debe cambiar cada vez
botones.bit.boton0=GpioDataRegs.GPADAT.bit.GPIO0; //Leer pin con boton[0]
botones.bit.boton1=GpioDataRegs.GPADAT.bit.GPIO1; //Leer pin con boton[1]
botones.bit.boton2=GpioDataRegs.GPADAT.bit.GPIO8; //Leer pin con boton[2]
botones.bit.boton3=GpioDataRegs.GPADAT.bit.GPIO9; //Leer pin con boton[3]

GpioDataRegs.GPADAT.bit.GPIO10= botones.bit.boton0 && botones.bit.boton1; //Copiar boton1&&boton0 en salida


GPIO10
}; //Itera infinitamente
}