Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views8 pages

Exp 9

The document describes an embedded C code example for configuring Timer 2 on the C8051F12x microcontroller in 16-bit capture mode to measure the frequency of an internal ripple pulse. It outlines the necessary hardware connections, initialization routines, and the interrupt service routine for capturing timer values and toggling an LED. The implementation demonstrates the microcontroller's ability to handle timing-critical applications effectively.

Uploaded by

mayks1011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Exp 9

The document describes an embedded C code example for configuring Timer 2 on the C8051F12x microcontroller in 16-bit capture mode to measure the frequency of an internal ripple pulse. It outlines the necessary hardware connections, initialization routines, and the interrupt service routine for capturing timer values and toggling an LED. The implementation demonstrates the microcontroller's ability to handle timing-critical applications effectively.

Uploaded by

mayks1011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

EXPERIMENT 09

AIM111: Write an Embedded C code to capture frequency of internal generated ripple pulse by

configuring timer 2 in 16 bit capture mode.

Code:

// F12x_Timer2_16bitCaptureTimer.c
// Copyright 2005 Silicon Laboratories, Inc.
// http://www.silabs.com
// Program Description:

// This program presents an example of use of the Timer2 of the


C8051F12x's in
// 16-bit capture mode. It uses the 'F12xDK as HW platform.
// In this example code the TRIGGER I/O toggles at a fixed rate. This I/O
pin
// is connected to the capture input associated with the Timer2. When the
// trigger pin goes up the capture begins and stops when it goes down.
// Once the capture is finished an interrupt is generated and the Timer2
ISR
// reads the captured values from RCAP2H/L into global variables and
toggles
// the LED.
//
// Pinout:
P0.0 - T2EX (Timer2 Capture Input)
P1.5 - TRIGGER (Pin used to trigger capture by Timer2)
P1.6 - LED (LED='1' means ON)
P1.7 - OVERFLOW (Pin toggled if Timer2 overflows instead of captures)
all other port pins unused
// Connections:
//
// P0.0(T2EX) <--> P1.5(TRIGGER)
//
// How To Test:
//
// 1) Open the F12x_Timer2_16bitCaptureTimer.c file in the IDE
// 2) If a different trigger pulse size is necessary change the value of
SOFTWARE_DELAY. If this value is too large, Timer2 will overflow before
the capture pulse occurs, causing an unwanted Timer2 interrupt (seen
on the OVERFLOW pin).
//
//
//
// 3) Connect P1.5(TRIGGER) <--> P1.7(T2EX)
// 4) Verify J3 is populated on the 'F12x TB.
// 5) Compile and download the code
// 6) Run the code

// 7) Check the Capture Value in CaptureValue.(LED toggles at every


capture)
//
//
// FID:
// Target:
// Tool chain:
12X000011
C8051F12x
KEIL C51 7.20 / KEIL EVAL C51
// Command Line: None
//
// Release 1.0
//
//
// -Initial Revision (CG) -09 NOV 2005
//
// Includes
//
#include <C8051F120.h>
//
// Global Constants
//
// SFR declarations
#define SOFTWARE_DELAY 30000
// Number of counts for the software
// timer for capture pulse toggling.
// If this value is too large, Timer2
// will overflow before the capture
// pulse occurs, causing an unwanted
// Timer2 interrupt (seen on the
// OVERFLOW pin).
sbit TRIGGER = P1^5;
sbit LED = P1^6;
sbit OVERFLOW = P1^7;
//
// Global Variables
//
// Pin used to trigger capture by Timer2
// LED='1' means ON
// Toggle the OVERFLOW pin if T2
// overflows instead of captures
unsigned int CaptureValue;
// Contains the value of the timer
// captured by the external trigger
sfr16 RCAP2 = 0xCA;
sfr16 TMR2 = 0xCC;
//
// Function Prototypes
//
// Timer2 reload register
// Timer2 register
void Port_Init (void);
void Timer2_Init (void);
void Oscillator_init(void);
//
// main() Routine
//
// Port initialization routine
// Timer2 initialization routine
void main (void)
{
unsigned int captureCounter;
WDTCN = 0xDE;
WDTCN = 0xAD;
Oscillator_init();
Timer2_Init ();
Port_Init();
EA = 1;
// Disable watchdog timer
// Initialize the Timer2
// Init Ports
// Enable global interrupts
while (1)
{
// This loop waits SOFTWARE_DELAY counts before toggling the TRIGGER
// input. The positive edge starts the capture process and the negative
// edge stops the capture and generates the interrupt.
for(captureCounter=0; captureCounter < SOFTWARE_DELAY; captureCounter++);
TRIGGER=~TRIGGER;
}
}
//
// Initialization Subroutines
//
//
// Port_Init
//
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and GPIO ports.
//
//
P0.0 - T2EX (Timer2 Capture Input)
//
all other port pins unused
//
//
//
//
//
//
//
void Port_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE;
P1.5 - TRIGGER (Pin used to trigger capture by Timer2)
P1.6 - LED (LED='1' means ON)
P1.7 - OVERFLOW (Pin toggled if Timer2 overflows instead of captures)
// Save Current SFR page
SFRPAGE = CONFIG_PAGE;
XBR1 = 0x40;
// Set SFR page
// T2EX input enabled in crossbar
XBR2 = 0x40;
P1MDOUT = 0xE0;
// Enable crossbar
// Set P1.6(LED) to push-pull
SFRPAGE = SFRPAGE_SAVE;
}
// Restore SFR page
//
// Timer2_Init
//
//
// Return Value : None
// Parameters : None
//

// This function configures the Timer2 as 16-bit capture,interrupt


enabled.
// It Uses the internal osc. at 24.5MHz with a prescaler of 1:8 and also a
// timer prescaler of 1:12.
//
void Timer2_Init(void)
{
char SFRPAGE_SAVE = SFRPAGE;
// Save Current SFR page
SFRPAGE = TMR2_PAGE;
TMR2CF = 0x18;
TMR2CN = 0x0D;
ET2 = 1;
// Set SFR page
// Timer2 uses SYSCLK/12
// Enable Timer2 capture
// Timer2 interrupt enabled
SFRPAGE = SFRPAGE_SAVE;
}
void Oscillator_init(void)
{
// Restore SFR page
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
OSCICN = 0x80;
SFRPAGE = SFRPAGE_SAVE;
}
//
// Interrupt Service Routines
//
//
// Timer2_ISR
//
//
// Here we process the timer interrupt and toggle the LED
//
//
void Timer2_ISR (void) interrupt 5
{
if(EXF2)
{
EXF2 = 0;
TMR2 = 0;
// Reset Capture Flag
// Reset Timer2 reg. to avoid
// interrupt
CaptureValue = RCAP2;
LED = ~LED;
}
else if(TF2)
{
// Save capture registers
// Toggle the LED
TF2 = 0;
OVERFLOW = ~OVERFLOW;
// Any eventual overflow will be
// shown in the overflow I/O Pin
}
}
//
// End Of File
//
Result:

Conclusion:
This code effectively demonstrates how to configure Timer 2 on the C8051F12x

microcontroller in 16-bit capture mode to measure the frequency of an internal ripple pulse. By

leveraging interrupts and precise timing configurations, it captures the timer value during

trigger events and provides visual feedback through an LED toggle. This implementation

serves as a practical example for utilizing Timer 2 in embedded systems requiring accurate

frequency measurements or pulse capture, showcasing the microcontroller's capability to

handle timing-critical applications.

You might also like