This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Regarding about

How can i go about calculating the timer values if i want to use the codes below to make the LED blink at 1Hz? How do i do calculation if i want it to blink at different Hz?

//-----------------------------------------------------------------------------
// F34x_Blinky.c
//-----------------------------------------------------------------------------
// Copyright 2007 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program flashes the P2.2 green LED on the C8051F34x target board
// five times a second using the interrupt handler for Timer2.
//
//
// How To Test:
//
// 1) Download code to a 'F34x target board
// 2) Run the code and if the P2.2 LED blinks, the code works
//
//
// FID: 34X000001
// Target: C8051F34x
// Tool chain: Keil C51 7.50 / Keil EVAL C51
// Command Line: None
//
// Release 1.1
// -27 SEP 2006 (GP)
// -Fixed #define for SYSCLK from 24.5/8 Mhz to 12/8 Mhz.
//
// Release 1.0
// -Initial Revision (GP)
// -01 DEC 2005
//

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <c8051f340.h> // SFR declarations
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F34x
//-----------------------------------------------------------------------------

sfr16 TMR2RL = 0xca; // Timer2 reload value(stand for standard film resistor 16 bits)
sfr16 TMR2 = 0xcc; // Timer2 counter

//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------

#define SYSCLK 12000000 / 8 // SYSCLK frequency in Hz

#define BLINK_RATE 10 // Timer2 Interrupts per second

sbit LED1 = P2^2; // LED='1' means ON

//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------

void OSCILLATOR_Init (void);
void PORT_Init (void);
void Timer2_Init (int counts);
void Timer2_ISR (void);

//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{ PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer // enable)

OSCILLATOR_Init (); // Initialize system clock PORT_Init (); // Initialize crossbar and GPIO

Timer2_Init (SYSCLK / 12 / BLINK_RATE); // Init Timer2 to generate // interrupts at a 10Hz rate.

EA = 1; // Enable global interrupts

while (1) {} // Spin forever
}

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function initializes the system clock to (12 Mhz / 8)
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{ OSCICN = 0x80; // Configure internal oscillator for // its lowest frequency
}

//----------------------------------------------------------------------7-------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and GPIO ports.
//
// P2.2 digital push-pull LED1
//-----------------------------------------------------------------------------
void PORT_Init (void)
{ XBR0 = 0x00; // No digital peripherals selected XBR1 = 0x40; // Enable crossbar and weak pull-ups P2MDOUT |= 0x04; // Enable LED as a push-pull output
}

//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) int counts - calculated Timer overflow rate
// range is postive range of integer: 0 to 32767
//
// Configure Timer2 to 16-bit auto-reload and generate an interrupt at
// interval specified by <counts> using SYSCLK/48 as its time base.
//
//-----------------------------------------------------------------------------
void Timer2_Init (int counts)
{ TMR2CN = 0x00; // Stop Timer2; Clear TF2; // Use SYSCLK/12 as timebase CKCON &= ~0x60; // Timer2 clocked based on T2XCLK;

TMR2RL = -counts; // Init reload values TMR2 = 0xffff; // Set to reload immediately ET2 = 1; // Enable Timer2 interrupts TR2 = 1; // Start Timer2
}

//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Timer2_ISR
//-----------------------------------------------------------------------------
//
// This routine changes the state of the LED whenever Timer2 overflows.
//
//-----------------------------------------------------------------------------
void Timer2_ISR (void) interrupt 5
{ TF2H = 0; // Clear Timer2 interrupt flag LED1 = ~LED1; // Change state of LED
}

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

0