Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Module
ReloadSensor.c
Revision
1.0.1
Description
This is a template file for implementing a simple service under the
Gen2 Events and Services Framework.
Notes
History
When Who
--------------------
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
/* include header files for this state machine as well as any machines at the
next lower level in the hierarchy that are sub-machines to this machine
*/
#include <stdio.h>
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_Port.h"
#include "ES_DeferRecall.h"
#include "ES_Timers.h"
#include "termio.h"
#include "BITDEFS.h" // standard bit definitions to make things more readable
//Time define
#define TicksPerMS 40000 //use system clock
#define USperMS 1000
#define OneShotTimeout 100*TicksPerMS //100ms oneshot timeout
//The reload emitter freq is between 500Hz-1000Hz, period is between 1 ms-2ms
#define PeriodMinTicks 1*TicksPerMS
#define PeriodMaxTicks 2*TicksPerMS
//PWM definition
//#define ALL_BITS (0xff<<2)
// 40,000 ticks per mS assumes a 40Mhz clock, we will use SysClk/32 for PWM
#define PWMTicksPerMS 40000/32
//IR Emitter is PE4: M1PWM2 (M1PWM1GENA)
// program generator A to go to 1 at rising comare A, 0 on falling compare A
#define GenA_Normal (PWM_1_GENA_ACTCMPAU_ONE | PWM_1_GENA_ACTCMPAD_ZERO )
// program generator B to go to 1 at rising comare B, 0 on falling compare B
#define GenB_Normal (PWM_1_GENB_ACTCMPBU_ONE | PWM_1_GENB_ACTCMPBD_ZERO )
// for convenience to write data to registers
#define BitsPerNibble 4
//Reload emitter sensor is PC6 WT1CCP0
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this service.They should be functions
relevant to the behavior of this service
*/
uint32_t ThisEdgeTime;
uint32_t SignalPeriod;
//grab ThisEdgeTime from timer register
ThisEdgeTime = HWREG(WTIMER1_BASE+TIMER_O_TAR);
//calculate SignalPeriod using ThisEdgeTime and LastEdgeTime
SignalPeriod = ThisEdgeTime - LastEdgeTime;
//increment EdgeCounter
EdgeCounter ++;
//calculate AvgSignalPeriod by caiculating the average period during the whole
EdgeCCounter time
AvgSignalPeriod = (AvgSignalPeriod * (EdgeCounter - 1) + SignalPeriod) /
EdgeCounter;
/*******************************************************************************
*****************************/
void InitReloadOneShot( void ){
// start by enabling the clock to the timer (Wide Timer 1)
HWREG(SYSCTL_RCGCWTIMER) |= SYSCTL_RCGCWTIMER_R1;
// kill a few cycles to let the clock get going
while((HWREG(SYSCTL_PRWTIMER) & SYSCTL_PRWTIMER_R1) != SYSCTL_PRWTIMER_R1);
// make sure that timer (Timer B) is disabled before configuring
HWREG(WTIMER1_BASE+TIMER_O_CTL) &= ~TIMER_CTL_TBEN; //TBEN = Bit0
// set it up in 32bit wide (individual, not concatenated) mode
// the constant name derives from the 16/32 bit timer, but this is a 32/64
// bit timer so we are setting the 32bit mode
HWREG(WTIMER1_BASE+TIMER_O_CFG) = TIMER_CFG_16_BIT; //bits 0-2 = 0x04
// set up timer B in 1-shot mode so that it disables timer on timeouts
// first mask off the TBMR field (bits 0:1) then set the value for
// 1-shot mode = 0x01
HWREG(WTIMER1_BASE+TIMER_O_TBMR) =
(HWREG(WTIMER1_BASE+TIMER_O_TBMR)& ~TIMER_TBMR_TBMR_M)|
TIMER_TBMR_TBMR_1_SHOT;
// set up timer B DOWN-counting (TBCDIR = 0)
// so that rewrting the OneShotTimeout will restart timerB
HWREG(WTIMER1_BASE+TIMER_O_TBMR) &= ~TIMER_TBMR_TBCDIR;
// set timeout
HWREG(WTIMER1_BASE+TIMER_O_TBILR) = OneShotTimeout;
// enable a local timeout interrupt. TBTOIM = bit 0
HWREG(WTIMER1_BASE+TIMER_O_IMR) |= TIMER_IMR_TBTOIM; // bit0
// enable the Timer B in Wide Timer 1 interrupt in the NVIC
// it is interrupt number 97 so appears in EN3 at bit 1
HWREG(NVIC_EN3) |= BIT1HI;
// make sure interrupts are enabled globally
__enable_irq();
}
/*******************************************************************************
***********************************/
void InitIRPWM(void){
//IR output is PE4 M1PWM2
// start by enabling the clock to the PWM Module (PWM1)
HWREG(SYSCTL_RCGCPWM) |= SYSCTL_RCGCPWM_R1;
// make sure that the PWM module clock has gotten going
while ((HWREG(SYSCTL_PRPWM) & SYSCTL_PRPWM_R1) != SYSCTL_PRPWM_R1)
;
// Set the PWM period. Since we are counting both up & down, we initialize
// the load register to 1/2 the desired total period. We will also program
// the match compare registers to 1/2 the desired high time
HWREG( PWM1_BASE+PWM_O_1_LOAD) = (P_EmitTicks)>>1;
// Set the initial Duty cycle on A to 50% by programming the compare value
// to 1/2 the period to count up (or down). Technically, the value to program
// should be Period/2 - DesiredHighTime/2, but since the desired high time is
1/2
// the period, we can skip the subtract
HWREG( PWM1_BASE+PWM_O_1_CMPA) = HWREG( PWM1_BASE+PWM_O_1_LOAD)>>1; // Left
Motor Duty cycle 50%
// now choose to map PWM to those pins, this is a mux value of 5 that we
// want to use for specifying the function on bits 4
HWREG(GPIO_PORTE_BASE+GPIO_O_PCTL) =
(HWREG(GPIO_PORTE_BASE+GPIO_O_PCTL) & 0xfff0ffff) +
(5<<(4*BitsPerNibble));
// Enable pins 4 Port E for digital I/O
HWREG(GPIO_PORTE_BASE+GPIO_O_DEN) |= BIT4HI;
// set the up/down count mode, enable the PWM generator and make
// both generator updates locally synchronized to zero count
HWREG(PWM1_BASE+ PWM_O_1_CTL) = (PWM_1_CTL_MODE | PWM_1_CTL_ENABLE |
PWM_1_CTL_GENAUPD_LS |
PWM_1_CTL_GENBUPD_LS);
}
/*------------------------------- Footnotes -------------------------------*/
/*------------------------------ End of file ------------------------------*/