#include "C8051F120.h"
#include<stdio.h>
#define SYSCLK 24500000
sbit LED0 = P1^6;
sbit LED1 = P1^7;
sbit Test =P0^3;
sfr16 T2 = 0xCC;
sfr16 RCAP3 = 0xCA;
sfr16 TMR3 = 0xCC;
xdata unsigned int delay2,delay1;
xdata unsigned long int FrequencyCount;
xdata unsigned int Timer2OverFlowCount;
xdata unsigned int Timer0Count;
unsigned int DelayinMsec = 0;
void Timer_Init();
void PORT_Init();
void Oscillator_Init();
void Interrupts_Init();
void Timer_Init();
void Reset_Sources_Init();
void Timer3_Init (int counts);
xdata unsigned int Count=0;
xdata int frequency= 0;
void main()
{
Reset_Sources_Init();
SFRPAGE = CONFIG_PAGE;
PORT_Init ();
Oscillator_Init();
SFRPAGE = TIMER01_PAGE;
Timer_Init();
SFRPAGE = TMR3_PAGE;
Timer3_Init (SYSCLK / 12 / 15000);
Interrupts_Init();
EA =1;
while(1)
{
}
}
void PORT_Init()
{
SFRPAGE = CONFIG_PAGE;
P0MDOUT = 0xFF;
XBR1 = 0x20;
XBR2 = 0xC0;
}
void Oscillator_Init()
{
SFRPAGE = CONFIG_PAGE;
OSCICN = 0x83;
}
void Interrupts_Init()
{
IE = 0x82;
}
void Reset_Sources_Init()
{
WDTCN = 0xDE;
WDTCN = 0xAD;
}
void Timer_Init()
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = TIMER01_PAGE;
TCON = 0x10;
TMOD = 0x01;
TR0 = 1;
SFRPAGE = TMR2_PAGE;
TMR2CN = 0x0F;
SFRPAGE = SFRPAGE_SAVE;
}
void Timer3_Init (int counts)
{
TMR3CN = 0x00;
RCAP3 = -counts;
TMR3 = 0xffff;
EIE2 |= 0x01;
TR3 = 1;
}
void Timer0_ISR (void) interrupt 1
{
Test = ~Test;
TF0 = 0;
TL0 = 0x06;
TH0 = 0xF8;
TR0=1;
Count++;
if(Count > 500)
{
LED0 = ~LED0;
Count = 0;
}
if(Timer0Count++ > 10000)
{
frequency = (Timer2OverFlowCount * 65535 + T2);
Timer2OverFlowCount = 0;
Timer0Count = 0;
T2 = 0;
}
}
void Timer2_ISR() interrupt 5
{
if(TF2)
{
TF2 = 0;
Timer2OverFlowCount++;
}
if(EXF2)
{
EXF2 = 0;
}
}
void Timer3_ISR (void) interrupt 14
{
TF3 = 0;
LED1 = ~LED1;
}