Sunteți pe pagina 1din 27

/*

* semaforo.c
*
*
*/
#include "F28x_Project.h"
void main(void)
{
//
// Paso 1. Inicialice el control del sistema:
// PLL, WatchDog, habilita Peripheral Clocks
// Esta funcin de ejemplo se encuentra en el archivo F2837xS_SysCtrl.c.
//
InitSysCtrl();
//
// Paso 2. Inicie GPIO:
// Esta funcin de ejemplo se encuentra en el archivo F2837xS_Gpio.c y
// ilustra cmo configurar el GPIO en su estado predeterminado.
//
InitGpio();
//
// Paso 3. Borrar todas las interrupciones e inicializar la tabla de vector PIE:
// Deshabilitar interrupciones de la CPU
//
Gpio_select();
DINT;
//
// Inicializar los registros de control PIE a su estado predeterminado.
// El estado predeterminado es que todas las interrupciones PIE estn deshabilitadas y los indicadores
se borran.
// Esta funcin se encuentra en el archivo F2837xS_PieCtrl.c.
//
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 F2837xS_DefaultIsr.c.
// This function is found in F2837xS_PieVect.c.
//
InitPieVectTable();
//
// Enable global Interrupts and higher priority real-time debug events:
//
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
//
// Step 6. IDLE loop. Just sit and loop forever (optional):
//
for(;;)
{
//
// Turn on LED
//
GpioDataRegs.GPADAT.all = 0x00021000;//verde y Rojo primer semaforo
DELAY_US(1000*500);
GpioDataRegs.GPADAT.bit.GPIO12=0;
DELAY_US(1000*500);
GpioDataRegs.GPADAT.bit.GPIO12=1;
DELAY_US(1000*500);
GpioDataRegs.GPADAT.bit.GPIO12=0;
DELAY_US(1000*500);
GpioDataRegs.GPADAT.bit.GPIO13=1;//Naranja primer semaforo
DELAY_US(1000*500);
GpioDataRegs.GPADAT.all = 0x0000c000;//Rojo y verde segundo semaforo
DELAY_US(1000*500);
GpioDataRegs.GPADAT.bit.GPIO15=0;
DELAY_US(1000*500);
GpioDataRegs.GPADAT.bit.GPIO15=1;
DELAY_US(1000*500);
GpioDataRegs.GPADAT.bit.GPIO15=0;
DELAY_US(1000*500);
GpioDataRegs.GPADAT.bit.GPIO16=1;//Naranja segundo semaforo
DELAY_US(1000*500);
}
}
void Gpio_select(void)
{
EALLOW;

GpioCtrlRegs.GPAMUX1.all = 0x00000000; // All GPIO


GpioCtrlRegs.GPAMUX2.all = 0x00000000; // All GPIO
GpioCtrlRegs.GPBMUX1.all = 0x00000000; // All GPIO Mux para configurar el Pin si es GPIO, si
es PWM etc
GpioCtrlRegs.GPADIR.all = 0xFFFFFFFF; // All outputs Dir indicar quienes son entradas y
quienes son salidas
GpioCtrlRegs.GPBDIR.all = 0x00001FFF; // All outputs
EDIS;
}
//
+*********************************************************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************
//Prende y apaga LEDS
#include "F28x_Project.h"
void main(void)
{
InitSysCtrl();
InitGpio();
GPIO_SetupPinMux(12, GPIO_MUX_CPU1, 0);
GPIO_SetupPinOptions(12, GPIO_OUTPUT, GPIO_PUSHPULL);
GPIO_SetupPinMux(13, GPIO_MUX_CPU1, 0);
GPIO_SetupPinOptions(13, GPIO_OUTPUT, GPIO_PUSHPULL);
GPIO_SetupPinMux(14, GPIO_MUX_CPU1, 0);
GPIO_SetupPinOptions(14, GPIO_OUTPUT, GPIO_PUSHPULL);
GPIO_SetupPinMux(15, GPIO_MUX_CPU1, 0);
GPIO_SetupPinOptions(15, GPIO_OUTPUT, GPIO_PUSHPULL);
GPIO_SetupPinMux(16, GPIO_MUX_CPU1, 0);
GPIO_SetupPinOptions(16, GPIO_OUTPUT, GPIO_PUSHPULL);
GPIO_SetupPinMux(17, GPIO_MUX_CPU1, 0);
GPIO_SetupPinOptions(17, GPIO_OUTPUT, GPIO_PUSHPULL);
DINT;
InitPieCtrl();
IER = 0x0000;

IFR = 0x0000;
InitPieVectTable();
EINT;
ERTM;

// Enable Global interrupt INTM


// Enable Global realtime interrupt DBGM

for(;;)
{
GPIO_WritePin(12, 0);
DELAY_US(10000*10);
GPIO_WritePin(12, 1);
DELAY_US(10000*10);
GPIO_WritePin(13, 0);
DELAY_US(10000*10);
GPIO_WritePin(13, 1);
DELAY_US(10000*10);
GPIO_WritePin(14, 0);
DELAY_US(10000*10);
GPIO_WritePin(14, 1);
DELAY_US(10000*10);
GPIO_WritePin(15, 0);
DELAY_US(10000*10);
GPIO_WritePin(15, 1);
DELAY_US(10000*10);
GPIO_WritePin(16, 0);
DELAY_US(10000*10);
GPIO_WritePin(16, 1);
DELAY_US(10000*10);
GPIO_WritePin(17, 0);
DELAY_US(10000*10);
GPIO_WritePin(17, 1);
DELAY_US(10000*10);

}
}

// Display de 7 segmentos
#include "F28x_Project.h"

void Gpio_setup1(void); // Basic pinout configuration example


void leepin(void);
void delay_loop(void);

void main(void)
{
InitSysCtrl();
InitGpio();
//Declaracion de salidas.
//Configuracion del GPIO12 como un salida (PIN 40) LED A
GPIO_SetupPinMux(12, GPIO_MUX_CPU1, 0);
GPIO_SetupPinOptions(12, GPIO_OUTPUT, GPIO_PUSHPULL);
//Configuracion del GPIO13 como un salida (PIN 39) LED B
GPIO_SetupPinMux(13, GPIO_MUX_CPU1, 0);
GPIO_SetupPinOptions(13, GPIO_OUTPUT, GPIO_PUSHPULL);
//Configuracion del GPIO14 como un salida (PIN 38) LED C
GPIO_SetupPinMux(14, GPIO_MUX_CPU1, 0);
GPIO_SetupPinOptions(14, GPIO_OUTPUT, GPIO_PUSHPULL);
//Configuracion del GPIO15 como un salida (PIN 37) LED D
GPIO_SetupPinMux(15, GPIO_MUX_CPU1, 0);
GPIO_SetupPinOptions(15, GPIO_OUTPUT, GPIO_PUSHPULL);
//Configuracion del GPIO16 como un salida (PIN 36) LED E
GPIO_SetupPinMux(16, GPIO_MUX_CPU1, 0);
GPIO_SetupPinOptions(16, GPIO_OUTPUT, GPIO_PUSHPULL);
//Configuracion del GPIO17 como un salida (PIN 35) LED F
GPIO_SetupPinMux(17, GPIO_MUX_CPU1, 0);
GPIO_SetupPinOptions(17, GPIO_OUTPUT, GPIO_PUSHPULL);
//Configuracion del GPIO20 como un salida (PIN 34) LED G
GPIO_SetupPinMux(20, GPIO_MUX_CPU1, 0);
GPIO_SetupPinOptions(20, GPIO_OUTPUT, GPIO_PUSHPULL);

DINT;
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
Gpio_setup1();
EINT;
ERTM;

// Enable Global interrupt INTM


// Enable Global realtime interrupt DBGM

for(;;)
{
void leepin(void);

//Funcion para leer el pin

}
}
//------------------------------------------------------------------------------------------------------void Gpio_setup1(void)
{
//declaracion de entradas (BOTON 1)
//aqui configuro el pin 33 (GPIO21) como entrada
EALLOW; // Make GPIO12 an input
GpioCtrlRegs.GPAPUD.bit.GPIO21 = 0; // Enable pullup on GPIO21
GpioCtrlRegs.GPAMUX2.bit.GPIO21 = 0; // GPIO21 = GPIO21
GpioCtrlRegs.GPADIR.bit.GPIO21 = 0; // GPIO21 = input
//declaracion de entradas (BOTON 2)
//aqui configuro el pin 15 (GPIO58) como entrada
GpioCtrlRegs.GPBPUD.bit.GPIO58 = 0; // Enable pullup on GPIO58
GpioCtrlRegs.GPBMUX2.bit.GPIO58 = 0; // GPIO58 = GPIO58
GpioCtrlRegs.GPBDIR.bit.GPIO58 = 0; // GPIO58 = input
//declaracion de entradas (BOTON 3)

//aqui configuro el pin 14 (GPIO59) como entrada


GpioCtrlRegs.GPBPUD.bit.GPIO59 = 0; // Enable pullup on GPIO59
GpioCtrlRegs.GPBMUX2.bit.GPIO59 = 0; // GPIO59 = GPIO59
GpioCtrlRegs.GPBDIR.bit.GPIO59 = 0; // GPIO59 = input
//declaracion de entradas (BOTON 4)
//aqui configuro el pin 13 (GPIO72)
GpioCtrlRegs.GPCPUD.bit.GPIO72 = 0;
GpioCtrlRegs.GPCMUX1.bit.GPIO72 = 0;
GpioCtrlRegs.GPCDIR.bit.GPIO72 = 0;

como entrada
// Enable pullup on GPIO72
// GPIO72 = GPIO72
// GPIO72 = input

/*
//declaracion de entradas
//aqui configuro el pin 12 (GPIO73)
GpioCtrlRegs.GPCPUD.bit.GPIO73 = 0;
GpioCtrlRegs.GPCMUX1.bit.GPIO73 = 0;
GpioCtrlRegs.GPCDIR.bit.GPIO73 = 0;

como entrada
// Enable pullup on GPIO73
// GPIO73 = GPIO73
// GPIO73 = input

GpioCtrlRegs.GPAPUD.bit.GPIO15 = 0;
GpioDataRegs.GPASET.bit.GPIO15 = 1;
GpioCtrlRegs.GPAMUX1.bit.GPIO15 = 0;
GpioCtrlRegs.GPADIR.bit.GPIO15 = 1;
*/
EDIS;
leepin();

//
//
//
//

Enable pullup on GPIO8


Load output latch
GPIO8 = GPIO8
GPIO8 = output

}
void leepin(void)
{
for(;;)
{
if (GpioDataRegs.GPADAT.bit.GPIO21==0)
{
//1 es encendido, 0 es apagado.
//Con este arreglo de estados se forman las
figuras en el display
//CERO
GPIO_WritePin(12, 0);
//a
GPIO_WritePin(13, 0);
//b
GPIO_WritePin(14, 0);
//c
GPIO_WritePin(15, 0);
//d
GPIO_WritePin(16, 0);
//e
GPIO_WritePin(17, 0);
//f
GPIO_WritePin(20, 1);
//g
//DELAY_US(1000*2000);

}
else if (GpioDataRegs.GPBDAT.bit.GPIO58==0)
{
// -------------------------------//nueve
GPIO_WritePin(12, 0);
//a
GPIO_WritePin(13, 0);
//b
GPIO_WritePin(14, 0);
//c
GPIO_WritePin(15, 1);
//d
GPIO_WritePin(16, 1);
//e
GPIO_WritePin(17, 0);
//f
GPIO_WritePin(20, 0);
//g
DELAY_US(1000*20);
}
else if (GpioDataRegs.GPBDAT.bit.GPIO59==0)
{
// -------------------------------//ocho
GPIO_WritePin(12, 0);
//a
GPIO_WritePin(13, 0);
//b
GPIO_WritePin(14, 1);
//c
GPIO_WritePin(15, 0);
//d
GPIO_WritePin(16, 0);
//e
GPIO_WritePin(17, 1);
//f
GPIO_WritePin(20, 0);
//g
DELAY_US(1000*20);
}
else if (GpioDataRegs.GPCDAT.bit.GPIO72==0)
{
//Letra a
GPIO_WritePin(12, 0);
GPIO_WritePin(13, 0);
GPIO_WritePin(14, 0);
GPIO_WritePin(15, 1);
GPIO_WritePin(16, 0);
GPIO_WritePin(17, 0);
GPIO_WritePin(20, 0);

//a
//b
//c
//d
//e
//f
//g

// DELAY_US(1000*2000);
}
else
{
// -------------------------------//Limpia el display

GPIO_WritePin(12, 1);
//a
GPIO_WritePin(13, 1);
//b
GPIO_WritePin(14, 1);
//c
GPIO_WritePin(15, 1);
//d
GPIO_WritePin(16, 1);
//e
GPIO_WritePin(17, 1);
//f
GPIO_WritePin(20, 1);
//g
}
}
}

ADC
#include "F28x_Project.h"
//////////////////////////
Uint16 valorpwm=0;
#define EPWM2_TIMER_TBPRD
#define EPWM2_MAX_CMPA
#define EPWM2_MIN_CMPA
#define EPWM2_MAX_CMPB
#define EPWM2_MIN_CMPB
#define EPWM_CMP_UP
#define EPWM_CMP_DOWN

8000
4016
0
2950
50
1
0

typedef struct
{
volatile struct EPWM_REGS *EPwmRegHandle;
Uint16 EPwm_CMPA_Direction;
Uint16 EPwm_CMPB_Direction;
Uint16 EPwmTimerIntCount;
Uint16 EPwmMaxCMPA;
Uint16 EPwmMinCMPA;
Uint16 EPwmMaxCMPB;
Uint16 EPwmMinCMPB;
}EPWM_INFO;
EPWM_INFO epwm2_info;

void InitEPwm2Example(void);
__interrupt void epwm2_isr(void);
void update_compare(EPWM_INFO*);
///////////////
void ConfigureADC(void);
void SetupADCContinuous(Uint16 channel);
#define RESULTS_BUFFER_SIZE 256
Uint16 AdcaResults[RESULTS_BUFFER_SIZE];
Uint16 resultsIndex;

void Gpio_select(void);

void main(void)
{
InitSysCtrl();
///Gpio_select();
CpuSysRegs.PCLKCR2.bit.EPWM2=1;
InitEPwm2Gpio();
DINT;
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.EPWM2_INT = &epwm2_isr;
EDIS;
// This is needed to disable write to EALLOW protected
registers
EALLOW;
CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 0;
EDIS;
InitEPwm2Example();
EALLOW;
CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 1;
EDIS;
IER |= M_INT3;
PieCtrlRegs.PIEIER3.bit.INTx1 = 1;
PieCtrlRegs.PIEIER3.bit.INTx2 = 1;
PieCtrlRegs.PIEIER3.bit.INTx3 = 1;

ConfigureADC();
SetupADCContinuous(0);
EINT;
ERTM;
for(resultsIndex = 0; resultsIndex < RESULTS_BUFFER_SIZE;
resultsIndex++)
{
AdcaResults[resultsIndex] = 0;
}
resultsIndex = 0;

for(;;)
{
EALLOW;
AdcaRegs.ADCINTSEL1N2.bit.INT1E = 1;
AdcaRegs.ADCINTSEL1N2.bit.INT2E = 1;
AdcaRegs.ADCINTSEL3N4.bit.INT3E = 1;
AdcaRegs.ADCINTSEL3N4.bit.INT4E = 1;
AdcaRegs.ADCINTFLGCLR.all = 0x000F;
resultsIndex = 0;
AdcaRegs.ADCSOCFRC1.all = 0x00FF;
while(resultsIndex < RESULTS_BUFFER_SIZE)
{
while(0 == AdcaRegs.ADCINTFLG.bit.ADCINT3);
AdcaRegs.ADCINTFLGCLR.bit.ADCINT3 = 1;
AdcaResults[resultsIndex++] =
AdcaResultRegs.ADCRESULT0;
AdcaRegs.ADCSOCFRC1.all = 0x00FF;
}
AdcaRegs.ADCINTSEL1N2.bit.INT1E = 0;

AdcaRegs.ADCINTSEL1N2.bit.INT2E = 0;
AdcaRegs.ADCINTSEL3N4.bit.INT3E = 0;
AdcaRegs.ADCINTSEL3N4.bit.INT4E = 0;
EDIS;
valorpwm=AdcaResultRegs.ADCRESULT0*2;
}

}
void ConfigureADC(void)
{
EALLOW;
AdcaRegs.ADCCTL2.bit.PRESCALE = 6;
AdcSetMode(ADC_ADCA, ADC_RESOLUTION_12BIT,
ADC_SIGNALMODE_SINGLE);
AdcaRegs.ADCCTL1.bit.INTPULSEPOS = 1;
AdcaRegs.ADCCTL1.bit.ADCPWDNZ = 1;
DELAY_US(1000);
EDIS;
}

void SetupADCContinuous(Uint16 channel)


{
Uint16 acqps=14;
EALLOW;
AdcaRegs.ADCSOC0CTL.bit.CHSEL
channel
AdcaRegs.ADCSOC1CTL.bit.CHSEL
channel
AdcaRegs.ADCSOC2CTL.bit.CHSEL
channel
AdcaRegs.ADCSOC3CTL.bit.CHSEL
channel
AdcaRegs.ADCSOC4CTL.bit.CHSEL
channel

= channel;

//SOC will convert on

= channel;

//SOC will convert on

= channel;

//SOC will convert on

= channel;

//SOC will convert on

= channel;

//SOC will convert on

AdcaRegs.ADCSOC5CTL.bit.CHSEL
channel
AdcaRegs.ADCSOC6CTL.bit.CHSEL
channel
AdcaRegs.ADCSOC7CTL.bit.CHSEL
channel
AdcaRegs.ADCSOC8CTL.bit.CHSEL
channel
AdcaRegs.ADCSOC9CTL.bit.CHSEL
channel
AdcaRegs.ADCSOC10CTL.bit.CHSEL
channel
AdcaRegs.ADCSOC11CTL.bit.CHSEL
channel
AdcaRegs.ADCSOC12CTL.bit.CHSEL
channel
AdcaRegs.ADCSOC13CTL.bit.CHSEL
channel
AdcaRegs.ADCSOC14CTL.bit.CHSEL
channel
AdcaRegs.ADCSOC15CTL.bit.CHSEL
channel

= channel;

//SOC will convert on

= channel;

//SOC will convert on

= channel;

//SOC will convert on

= channel;

//SOC will convert on

= channel;

//SOC will convert on

= channel;

//SOC will convert on

= channel;

//SOC will convert on

= channel;

//SOC will convert on

= channel;

//SOC will convert on

= channel;

//SOC will convert on

= channel;

//SOC will convert on

AdcaRegs.ADCSOC0CTL.bit.ACQPS
acqps +

= acqps;

//sample window is

AdcaRegs.ADCSOC1CTL.bit.ACQPS
acqps +

= acqps;

//1 SYSCLK cycles


//sample window is

AdcaRegs.ADCSOC2CTL.bit.ACQPS
acqps +

= acqps;

//1 SYSCLK cycles


//sample window is

AdcaRegs.ADCSOC3CTL.bit.ACQPS
acqps +

= acqps;

//1 SYSCLK cycles


//sample window is

AdcaRegs.ADCSOC4CTL.bit.ACQPS
acqps +

= acqps;

//1 SYSCLK cycles


//sample window is

AdcaRegs.ADCSOC5CTL.bit.ACQPS
acqps +

= acqps;

//1 SYSCLK cycles


//sample window is

AdcaRegs.ADCSOC6CTL.bit.ACQPS
acqps +

= acqps;

//1 SYSCLK cycles


//sample window is

AdcaRegs.ADCSOC7CTL.bit.ACQPS
acqps +

= acqps;

//1 SYSCLK cycles


//sample window is
//1 SYSCLK cycles

AdcaRegs.ADCSOC9CTL.bit.ACQPS
acqps +

= acqps;

AdcaRegs.ADCSOC10CTL.bit.ACQPS = acqps;
acqps +
AdcaRegs.ADCSOC11CTL.bit.ACQPS = acqps;
acqps +
AdcaRegs.ADCSOC12CTL.bit.ACQPS = acqps;
acqps +
AdcaRegs.ADCSOC13CTL.bit.ACQPS = acqps;
acqps +
AdcaRegs.ADCSOC14CTL.bit.ACQPS = acqps;
acqps +
AdcaRegs.ADCSOC15CTL.bit.ACQPS = acqps;
acqps +

//sample window is
//1 SYSCLK cycles
//sample window is
//1 SYSCLK cycles
//sample window is
//1 SYSCLK cycles
//sample window is
//1 SYSCLK cycles
//sample window is
//1 SYSCLK cycles
//sample window is
//1 SYSCLK cycles
//sample window is
//1 SYSCLK cycles

AdcaRegs.ADCINTSEL1N2.bit.INT1E
AdcaRegs.ADCINTSEL1N2.bit.INT2E
AdcaRegs.ADCINTSEL3N4.bit.INT3E
AdcaRegs.ADCINTSEL3N4.bit.INT4E

=
=
=
=

AdcaRegs.ADCINTSEL1N2.bit.INT1CONT
AdcaRegs.ADCINTSEL1N2.bit.INT2CONT
AdcaRegs.ADCINTSEL3N4.bit.INT3CONT
AdcaRegs.ADCINTSEL3N4.bit.INT4CONT
AdcaRegs.ADCINTSEL1N2.bit.INT1SEL
INT1 flag
AdcaRegs.ADCINTSEL1N2.bit.INT2SEL
INT2 flag
AdcaRegs.ADCINTSEL3N4.bit.INT3SEL
INT3 flag
AdcaRegs.ADCINTSEL3N4.bit.INT4SEL
INT4 flag

0;
0;
0;
0;
=
=
=
=

//disable
//disable
//disable
//disable

INT1
INT2
INT3
INT4

flag
flag
flag
flag

0;
0;
0;
0;

= 6;

//end of SOC6 will set

= 14; //end of SOC14 will set


= 7;

//end of SOC7 will set

= 15; //end of SOC15 will set

//
//ADCINT2 will trigger first 8 SOCs
//
AdcaRegs.ADCINTSOCSEL1.bit.SOC0 = 2;
AdcaRegs.ADCINTSOCSEL1.bit.SOC1 = 2;
AdcaRegs.ADCINTSOCSEL1.bit.SOC2 = 2;
AdcaRegs.ADCINTSOCSEL1.bit.SOC3 = 2;

AdcaRegs.ADCINTSOCSEL1.bit.SOC4
AdcaRegs.ADCINTSOCSEL1.bit.SOC5
AdcaRegs.ADCINTSOCSEL1.bit.SOC6
AdcaRegs.ADCINTSOCSEL1.bit.SOC7

=
=
=
=

2;
2;
2;
2;

//
//ADCINT1 will trigger second 8 SOCs
//
AdcaRegs.ADCINTSOCSEL2.bit.SOC8 = 1;
AdcaRegs.ADCINTSOCSEL2.bit.SOC9 = 1;
AdcaRegs.ADCINTSOCSEL2.bit.SOC10 = 1;
AdcaRegs.ADCINTSOCSEL2.bit.SOC11 = 1;
AdcaRegs.ADCINTSOCSEL2.bit.SOC12 = 1;
AdcaRegs.ADCINTSOCSEL2.bit.SOC13 = 1;
AdcaRegs.ADCINTSOCSEL2.bit.SOC14 = 1;
AdcaRegs.ADCINTSOCSEL2.bit.SOC15 = 1;
EDIS;
}
__interrupt void epwm2_isr(void)
{
update_compare(&epwm2_info);
EPwm2Regs.ETCLR.bit.INT = 1;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
}

void InitEPwm2Example()
{
//
// Setup TBCLK
//
EPwm2Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP;
EPwm2Regs.TBPRD = EPWM2_TIMER_TBPRD;
EPwm2Regs.TBCTL.bit.PHSEN = TB_DISABLE;
loading
EPwm2Regs.TBPHS.bit.TBPHS = 0x0000;
EPwm2Regs.TBCTR = 0x0000;
EPwm2Regs.TBCTL.bit.HSPCLKDIV = TB_DIV2;
SYSCLKOUT
EPwm2Regs.TBCTL.bit.CLKDIV = TB_DIV2;
//
// Setup shadow register load on ZERO

// Count up
// Set timer period
// Disable phase
// Phase is 0
// Clear counter
// Clock ratio to

//
EPwm2Regs.CMPCTL.bit.SHDWAMODE
EPwm2Regs.CMPCTL.bit.SHDWBMODE
EPwm2Regs.CMPCTL.bit.LOADAMODE
EPwm2Regs.CMPCTL.bit.LOADBMODE

=
=
=
=

CC_SHADOW;
CC_SHADOW;
CC_CTR_ZERO;
CC_CTR_ZERO;

//
// Set Compare values
//
EPwm2Regs.CMPA.bit.CMPA = EPWM2_MIN_CMPA;
value
EPwm2Regs.CMPB.bit.CMPB = EPWM2_MAX_CMPB;
value
//
// Set actions
//
EPwm2Regs.AQCTLA.bit.PRD = AQ_CLEAR;
Period
EPwm2Regs.AQCTLA.bit.CAU = AQ_SET;
event A,

// Set compare A
// Set Compare B

// Clear PWM2A on
// Set PWM2A on
// up count

EPwm2Regs.AQCTLB.bit.PRD = AQ_CLEAR;
Period
EPwm2Regs.AQCTLB.bit.CBU = AQ_SET;
event B,

// Clear PWM2B on
// Set PWM2B on
// up count

//
// Interrupt where we will change the Compare Values
//
EPwm2Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO;
// Select INT on
Zero event
EPwm2Regs.ETSEL.bit.INTEN = 1;
// Enable INT
EPwm2Regs.ETPS.bit.INTPRD = ET_3RD;
// Generate INT
on 3rd event
//
// Information this example uses to keep track
// of the direction the CMPA/CMPB values are
// moving, the min and max allowed values and
// a pointer to the correct ePWM registers
//
epwm2_info.EPwm_CMPA_Direction = EPWM_CMP_UP;
increasing CMPA

// Start by

epwm2_info.EPwm_CMPB_Direction = EPWM_CMP_DOWN; // and decreasing


CMPB
epwm2_info.EPwmTimerIntCount = 0;
// Zero the
interrupt
// counter
epwm2_info.EPwmRegHandle = &EPwm2Regs;
// Set the pointer
to the
// ePWM module
epwm2_info.EPwmMaxCMPA = EPWM2_MAX_CMPA;
// Setup min/max
// CMPA/CMPB
values
epwm2_info.EPwmMinCMPA = EPWM2_MIN_CMPA;
epwm2_info.EPwmMaxCMPB = EPWM2_MAX_CMPB;
epwm2_info.EPwmMinCMPB = EPWM2_MIN_CMPB;
}
void update_compare(EPWM_INFO *epwm_info)
{
//
// Every 10'th interrupt, change the CMPA/CMPB values
//
if(epwm_info->EPwmTimerIntCount == 10)
{
epwm_info->EPwmTimerIntCount = 0;
//
// If we were increasing CMPA, check to see if
// we reached the max value. If not, increase CMPA
// else, change directions and decrease CMPA
//
if(epwm_info->EPwm_CMPA_Direction == EPWM_CMP_UP)
{
// if(epwm_info->EPwmRegHandle->CMPA.bit.CMPA < epwm_info>EPwmMaxCMPA)
//{
epwm_info->EPwmRegHandle->CMPA.bit.CMPA=valorpwm;
// }
/*
else
{
epwm_info->EPwm_CMPA_Direction = EPWM_CMP_DOWN;
epwm_info->EPwmRegHandle->CMPA.bit.CMPA=50;
}
*/
}

//
// If we were decreasing CMPA, check to see if
// we reached the min value. If not, decrease CMPA
// else, change directions and increase CMPA
//
/*
else
{
if(epwm_info->EPwmRegHandle->CMPA.bit.CMPA == epwm_info>EPwmMinCMPA)
{
epwm_info->EPwm_CMPA_Direction = EPWM_CMP_UP;
epwm_info->EPwmRegHandle->CMPA.bit.CMPA=1950;
}
else
{
epwm_info->EPwmRegHandle->CMPA.bit.CMPA=50;
}
}
*/
//
// If we were increasing CMPB, check to see if
// we reached the max value. If not, increase CMPB
// else, change directions and decrease CMPB
//
if(epwm_info->EPwm_CMPB_Direction == EPWM_CMP_UP)
{
if(epwm_info->EPwmRegHandle->CMPB.bit.CMPB < epwm_info>EPwmMaxCMPB)
{
epwm_info->EPwmRegHandle->CMPB.bit.CMPB++;
}
else
{
epwm_info->EPwm_CMPB_Direction = EPWM_CMP_DOWN;
epwm_info->EPwmRegHandle->CMPB.bit.CMPB--;
}
}
//
// If we were decreasing CMPB, check to see if
// we reached the min value. If not, decrease CMPB
// else, change directions and increase CMPB
//
else
{

if(epwm_info->EPwmRegHandle->CMPB.bit.CMPB == epwm_info>EPwmMinCMPB)
{
epwm_info->EPwm_CMPB_Direction = EPWM_CMP_UP;
epwm_info->EPwmRegHandle->CMPB.bit.CMPB++;
}
else
{
epwm_info->EPwmRegHandle->CMPB.bit.CMPB--;
}
}
}
else
{
epwm_info->EPwmTimerIntCount++;
}
return;
}

//###################################################################
########
//
// FILE:
Example_2837xSSci_Echoback.c
//
// TITLE:
SCI Echoback.
//
//! \addtogroup cpu01_example_list
//! <h1>SCI Echoback (sci_echoback)</h1>
//!
//! This test receives and echo-backs data through the SCI-A port.
//!
//! The PC application 'hyperterminal' or another terminal
//! such as 'putty' can be used to view the data from the SCI and
//! to send information to the SCI. Characters received
//! by the SCI port are sent back to the host.
//!
//! \b Running \b the \b Application
//! -# Configure hyperterminal or another terminal such as putty:
//!
//! For hyperterminal you can use the included hyperterminal
configuration
//! file SCI_96.ht.
//! To load this configuration in hyperterminal
//!
-# Open hyperterminal
//!
-# Go to file->open
//!
-# Browse to the location of the project and
//!
select the SCI_96.ht file.
//! -# Check the COM port.
//! The configuration file is currently setup for COM1.
//! If this is not correct, disconnect (Call->Disconnect)
//! Open the File-Properties dialogue and select the correct COM
port.
//! -# Connect hyperterminal Call->Call
//! and then start the 2837xS SCI echoback program execution.
//! -# The program will print out a greeting and then ask you to
//! enter a character which it will echo back to hyperterminal.
//!
//! \note If you are unable to open the .ht file, or you are using
//! a different terminal, you can open a COM port with the following
settings
//! - Find correct COM port
//! - Bits per second = 9600
//! - Date Bits = 8
//! - Parity = None
//! - Stop Bits = 1

//! - Hardware Control = None


//!
//! \b Watch \b Variables \n
//! - LoopCount - the number of characters sent
//!
//! \b External \b Connections \n
//! Connect the SCI-A port to a PC via a transceiver and cable.
//! - GPIO28 is SCI_A-RXD (Connect to Pin3, PC-TX, of serial DB9
cable)
//! - GPIO29 is SCI_A-TXD (Connect to Pin2, PC-RX, of serial DB9
cable)
//!
//
//###################################################################
########
// $TI Release: F2837xS Support Library v200 $
// $Release Date: Tue Jun 21 13:52:16 CDT 2016 $
// $Copyright: Copyright (C) 2014-2016 Texas Instruments Incorporated
//
http://www.ti.com/ ALL RIGHTS RESERVED $
//###################################################################
########
//
// Included Files
//
#include "F28x_Project.h"
//
// Globals
//
Uint16 LoopCount;
//
// Function Prototypes
//
void scia_echoback_init(void);
void scia_fifo_init(void);
void scia_xmit(int a);
void scia_msg(char *msg);
//
// Main
//
void main(void)
{
Uint16 ReceivedChar;
char *msg;

//
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the F2837xS_SysCtrl.c file.
//
InitSysCtrl();
//
// Step 2. Initialize GPIO:
// This example function is found in the F2837xS_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
//
InitGpio();
GPIO_SetupPinMux(12, GPIO_MUX_CPU1, 1);
GPIO_SetupPinOptions(12, GPIO_OUTPUT, GPIO_PUSHPULL);
//
// For this example, only init the pins for the SCI-A port.
// GPIO_SetupPinMux() - Sets the GPxMUX1/2 and GPyMUX1/2 register
bits
// GPIO_SetupPinOptions() - Sets the direction and configuration of
the GPIOS
// These functions are found in the F2837xS_Gpio.c file.
//
GPIO_SetupPinMux(42, GPIO_MUX_CPU1, 15);
GPIO_SetupPinOptions(42, GPIO_INPUT, GPIO_PUSHPULL);
GPIO_SetupPinMux(43, GPIO_MUX_CPU1, 15);
GPIO_SetupPinOptions(43, GPIO_OUTPUT, GPIO_ASYNC);
// GPIO_SetupPinMux(13, GPIO_MUX_CPU1, 0);
// GPIO_SetupPinOptions(13, GPIO_INPUT, GPIO_PUSHPULL);
//
// 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 F2837xS_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 F2837xS_DefaultIsr.c.
// This function is found in F2837xS_PieVect.c.
//
InitPieVectTable();
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO12 = 0; // Enable
pullup on GPIO10
GpioDataRegs.GPASET.bit.GPIO12 = 1; // Load
output latch
GpioCtrlRegs.GPAMUX1.bit.GPIO12 = 0; // GPIO10 =
GPIO10
GpioCtrlRegs.GPADIR.bit.GPIO12 = 1;
// GPIO10
= output
EDIS;
//
// Step 4. User specific code:
//
LoopCount = 0;
scia_fifo_init();
scia_echoback_init();

// Initialize the SCI FIFO


// Initialize SCI for echoback

msg = "\r\n\n\nHello World!\0";


scia_msg(msg);
msg = "\r\nYou will enter a character, and the DSP will echo it
back! \n\0";
scia_msg(msg);
for(;;)
{
msg = "\r\nEnter a character:\0";
scia_msg(msg);

//
// Wait for inc character
//
while(SciaRegs.SCIFFRX.bit.RXFFST == 0) { } // wait for empty
state
//
// Get character
//
ReceivedChar = SciaRegs.SCIRXBUF.all;
//
// Echo character back
//
msg = " You sent: ";
scia_msg(msg);
scia_xmit(ReceivedChar);
/* if(ReceivedChar=='a')
{
GpioDataRegs.GPASET.bit.GPIO12 = 1;
}
else
if(ReceivedChar=='b')
{
GpioDataRegs.GPACLEAR.bit.GPIO12 = 0;
}
*/
if(ReceivedChar)
{
GPIO_WritePin(12,1);
}
else
{
GPIO_WritePin(12,0);
}
LoopCount++;
}
}
//

// scia_echoback_init - Test 1,SCIA DLB, 8-bit word, baud rate


0x000F,
//
default, 1 STOP bit, no parity
//
void scia_echoback_init()
{
//
// Note: Clocks were turned on to the SCIA peripheral
// in the InitSysCtrl() function
//
SciaRegs.SCICCR.all = 0x0007;
SciaRegs.SCICTL1.all = 0x0003;
SCICLK,

//
//
//
//

1 stop bit, No loopback


No parity,8 char bits,
async mode, idle-line protocol
enable TX, RX, internal

// Disable RX ERR, SLEEP, TXWAKE


SciaRegs.SCICTL2.all = 0x0003;
SciaRegs.SCICTL2.bit.TXINTENA = 1;
SciaRegs.SCICTL2.bit.RXBKINTENA = 1;
//
// SCIA at 9600 baud
// @LSPCLK = 50 MHz (200 MHz SYSCLK) HBAUD = 0x02 and LBAUD =
0x8B.
// @LSPCLK = 30 MHz (120 MHz SYSCLK) HBAUD = 0x01 and LBAUD =
0x86.
//
SciaRegs.SCIHBAUD.all = 0x0002;
SciaRegs.SCILBAUD.all = 0x008B;
SciaRegs.SCICTL1.all = 0x0023;

// Relinquish SCI from Reset

}
//
// scia_xmit - Transmit a character from the SCI
//
void scia_xmit(int a)
{
while (SciaRegs.SCIFFTX.bit.TXFFST != 0) {}
SciaRegs.SCITXBUF.all =a;
}
//
// scia_msg - Transmit message via SCIA
//
void scia_msg(char * msg)
{

int i;
i = 0;
while(msg[i] != '\0')
{
scia_xmit(msg[i]);
i++;
}
}
//
// scia_fifo_init - Initialize the SCI FIFO
//
void scia_fifo_init()
{
SciaRegs.SCIFFTX.all = 0xE040;
SciaRegs.SCIFFRX.all = 0x2044;
SciaRegs.SCIFFCT.all = 0x0;
}
//
// End of file
//

S-ar putea să vă placă și