Define an internal function called startTimer in original Blink application. Sta
ID: 3664926 • Letter: D
Question
Define an internal function called startTimer in original Blink application. Start timers in this new defined internal function instead of starting in Boot event.
//////////////////////////BlinkC.nc\\\\\\\\\\\\\\\\\\\
/**
* Implementation for Blink application. Toggle the red LED when a
* Timer fires.
**/
#include "Timer.h"
module BlinkC @safe()
{
uses interface Timer as Timer0;
uses interface Timer as Timer1;
uses interface Timer as Timer2;
uses interface Leds;
uses interface Boot;
}
implementation
{
event void Boot.booted()
{
call Timer0.startPeriodic( 250 );
call Timer1.startPeriodic( 500 );
call Timer2.startPeriodic( 1000 );
}
event void Timer0.fired()
{
dbg("BlinkC", "Timer 0 fired @ %s. ", sim_time_string());
call Leds.led0Toggle();
}
event void Timer1.fired()
{
dbg("BlinkC", "Timer 1 fired @ %s ", sim_time_string());
call Leds.led1Toggle();
}
event void Timer2.fired()
{
dbg("BlinkC", "Timer 2 fired @ %s. ", sim_time_string());
call Leds.led2Toggle();
}
}
//////////////////////////////////////BlinkAppC.nc\\\\\\\\\\\\\\\\\
configuration BlinkAppC
{
}
implementation
{
components MainC, BlinkC, LedsC;
components new TimerMilliC() as Timer0;
components new TimerMilliC() as Timer1;
components new TimerMilliC() as Timer2;
BlinkC -> MainC.Boot;
BlinkC.Timer0 -> Timer0;
BlinkC.Timer1 -> Timer1;
BlinkC.Timer2 -> Timer2;
BlinkC.Leds -> LedsC;
}
Explanation / Answer
/*
** File: main.c
** Date: 2015-MAR-19
** Target: PIC18F2520
** MPLAB: 8.92
** Compiler: C18 v3.47
*/
#include <p18cxxx.h>
void T0Delay(void);
/* Set up the configuration bits */
#pragma config OSC = HS, STVREN = ON, PWRT = OFF, BOREN = OFF
#pragma config WDT = OFF, PBADEN = OFF, CCP2MX = PORTBE
#pragma config MCLRE = ON, LVP = OFF, FCMEN = OFF, IESO = OFF
#pragma config BORV = 0, WDTPS = 32768, LPT1OSC = ON
#pragma config CP0 = OFF ,CP1 = OFF ,CP2 = OFF ,CP3 = OFF
#pragma config CPB = OFF ,CPD = OFF
#pragma config WRT0 = OFF ,WRT1 = OFF ,WRT2 = OFF ,WRT3 = OFF
#pragma config WRTB = OFF ,WRTD = OFF, WRTC = OFF
#pragma config EBTR0 = OFF,EBTR1 = OFF,EBTR2 = OFF,EBTR3 = OFF
#pragma config EBTRB = OFF
#define FOSC (20000000L)
#define FCYC (FOSC/4L) /* this is the frequency that the CPU will run at */
/*----------------------------------------------*/
/* Initialize this PIC */
/*----------------------------------------------*/
void PIC_Init( void )
{
INTCON = 1; /* Disable all interrupts */
INTCON3 = 1;
PIE1 = 0;
PIE2 = 0;
/* Keep us happy by turning off the comparators */
/* and let their pins be digital I/O. */
CMCON = 0x07; /* Turn off comparators */
/* Keep simulator happy by initializing the ADC */
/* Keep us happy by turning the ADC off. */
ADCON2 = 0x02; /* Select internal RC as ADC clock source */
ADCON1 = 0x0F; /* set all ADC inputs for digital I/O */
ADCON0 = 0x00; /* Turn off ADC */
/* Initialize the interrupt system */
RCONbits.IPEN = 0; /* Select legacy interrupt model. */
INTCONbits.PEIE = 0; /* Disable peripheral interrupt sources */
INTCONbits.GIE = 1; /* Disable interrupts */
}
void main ( void )
{
PIC_Init();
TRISC = 0x00; /* make all of PORTA outputs */
//for(;;) /* loop forever */
while(1)
{
PORTCbits.RC1=~PORTCbits.RC1;
T0Delay();
T0Delay();
T0Delay();
T0Delay();
T0Delay();
T0Delay();
T0Delay();
T0Delay();
T0Delay();
T0Delay();
T0Delay();
T0Delay();
T0Delay();
T0Delay();
T0Delay();
T0Delay();
T0Delay();
}
}
void T0Delay()
{
T0CON=0x01;//timer 0,16bit mode,1:4 prescalar
TMR0H=0x85; //load TH0
TMR0L=0xEE; //LOAD TL0
T0CONbits.TMR0ON=1; //START TIMER
while(INTCONbits.TMR0IF==0);// WAIT FOR TIMER0 TF0 TO ROLL OVER
T0CONbits.TMR0ON=0;//TURN OFF TIMER
INTCONbits.TMR0IF==0;// CLEAR TF0
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.