Timer2 Capture Led

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#include <C8051F120.

h> #define SYSTEMCLOCK 24500000/8

// SFR declarations

void OSCILLATOR_Init (void); pt (seen on the void UART1_Init(); void Port_Init (void); void Timer2_Init (void); unsigned int CaptureValue; sfr16 RCAP2 = 0xCA; sfr16 TMR2 = 0xCC; unsigned int cap_cnt=0x00; bit action_flag=0; void main (void) { unsigned char i;

// Timer2 interru // OVERFLOW pin). // Port initialization routine // Contains the value of the timer // captured by the external trigger // Timer2 reload register // Timer2 register

float tmr_update_cnt = 1.531; WDTCN = 0xDE; // Disable watchdog timer WDTCN = 0xAD; Timer2_Init (); Port_Init(); EA = 1; while (1) { if(cap_cnt < 1500 && action_flag == 1) { cap_cnt = 0x00; action_flag=0; SFRPAGE = CONFIG_PAGE; P5 = 0xff; } else if(cap_cnt > 1500 && action_flag == 1) { cap_cnt = 0x00; action_flag = 0; SFRPAGE = CONFIG_PAGE; P5 = 0x00; } else { SFRPAGE = CONFIG_PAGE; // Initialize the Timer2 // Enable global interrupts

P5 = 0x00; } } } void Port_Init (void) { char SFRPAGE_SAVE = SFRPAGE; SFRPAGE = CONFIG_PAGE; XBR0 = 0x37; XBR1 = 0x7e; XBR2 = 0x5c; P5MDOUT = 0xff; P5 = 0x00; P4MDOUT = 0x00; P4 = 0xff; P2MDOUT = 0x00; P2 = 0xff; SFRPAGE = SFRPAGE_SAVE; } // // 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; SFRPAGE = TMR2_PAGE; TMR2CF = 0x18; TMR2CN = 0x09; ET2 = 1; TMR2 = 0x0000; TR2=1; SFRPAGE = SFRPAGE_SAVE; } void OSCILLATOR_Init (void) { char SFRPAGE_SAVE = SFRPAGE; SFRPAGE = CONFIG_PAGE; OSCICN = 0x80; SFRPAGE = SFRPAGE_SAVE; // Save Current SFR page // Set SFR page // Timer2 uses SYSCLK/2 // Enable Timer2 capture // Restore SFR page

// Save Current SFR page // Set SFR page

// Timer2 interrupt enabled // Restore SFR page

// Save Current SFR page // Set SFR page // Set internal oscillator to run // at its highest 24.5 MHz frequency // Restore SFRPAGE

void Timer2_ISR (void) interrupt 5 { static unsigned int current_cnt_1,current_cnt_2;//,previous_cnt,temp; if(TF2 == 1) { TF2 = 0; return; } if(EXF2 == 1 && cntr == 0) //&& cap_flag==1) { EXF2 = 0; //previous_cnt = current_cnt; current_cnt_1 = RCAP2; cntr++; } if(EXF2 == 1 && cntr == 1 ) { EXF2 = 0; current_cnt_2 = RCAP2; if(current_cnt_2 > current_cnt_1) cap_cnt = current_cnt_2 - current_cnt_1; else { current_cnt_1=0xFFFF.current_cnt_1; cap_cnt = current_cnt_1 - current_cnt_2; } cntr = 0; action_flag=1; } return; }

You might also like