-
Notifications
You must be signed in to change notification settings - Fork 0
/
period.c
175 lines (148 loc) · 4.27 KB
/
period.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// PeriodEFM8.c: Measure the period of a signal on pin P0.1.
//
// By: Jesus Calvino-Fraga (c) 2008-2018
//
// The next line clears the "C51 command line options:" field when compiling with CrossIDE
// ~C51~
#include <EFM8LB1.h>
#include <stdio.h>
#define SYSCLK 72000000L // SYSCLK frequency in Hz
#define BAUDRATE 115200L // Baud rate of UART in bps
unsigned char overflow_count;
char _c51_external_startup (void)
{
// Disable Watchdog with key sequence
SFRPAGE = 0x00;
WDTCN = 0xDE; //First key
WDTCN = 0xAD; //Second key
VDM0CN |= 0x80;
RSTSRC = 0x02;
#if (SYSCLK == 48000000L)
SFRPAGE = 0x10;
PFE0CN = 0x10; // SYSCLK < 50 MHz.
SFRPAGE = 0x00;
#elif (SYSCLK == 72000000L)
SFRPAGE = 0x10;
PFE0CN = 0x20; // SYSCLK < 75 MHz.
SFRPAGE = 0x00;
#endif
#if (SYSCLK == 12250000L)
CLKSEL = 0x10;
CLKSEL = 0x10;
while ((CLKSEL & 0x80) == 0);
#elif (SYSCLK == 24500000L)
CLKSEL = 0x00;
CLKSEL = 0x00;
while ((CLKSEL & 0x80) == 0);
#elif (SYSCLK == 48000000L)
// Before setting clock to 48 MHz, must transition to 24.5 MHz first
CLKSEL = 0x00;
CLKSEL = 0x00;
while ((CLKSEL & 0x80) == 0);
CLKSEL = 0x07;
CLKSEL = 0x07;
while ((CLKSEL & 0x80) == 0);
#elif (SYSCLK == 72000000L)
// Before setting clock to 72 MHz, must transition to 24.5 MHz first
CLKSEL = 0x00;
CLKSEL = 0x00;
while ((CLKSEL & 0x80) == 0);
CLKSEL = 0x03;
CLKSEL = 0x03;
while ((CLKSEL & 0x80) == 0);
#else
#error SYSCLK must be either 12250000L, 24500000L, 48000000L, or 72000000L
#endif
P0MDOUT |= 0x10; // Enable UART0 TX as push-pull output
XBR0 = 0x01; // Enable UART0 on P0.4(TX) and P0.5(RX)
XBR1 = 0X00;
XBR2 = 0x40; // Enable crossbar and weak pull-ups
#if (((SYSCLK/BAUDRATE)/(2L*12L))>0xFFL)
#error Timer 0 reload value is incorrect because (SYSCLK/BAUDRATE)/(2L*12L) > 0xFF
#endif
// Configure Uart 0
SCON0 = 0x10;
CKCON0 |= 0b_0000_0000 ; // Timer 1 uses the system clock divided by 12.
TH1 = 0x100-((SYSCLK/BAUDRATE)/(2L*12L));
TL1 = TH1; // Init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit auto-reload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI = 1; // Indicate TX0 ready
return 0;
}
// Uses Timer3 to delay <us> micro-seconds.
void Timer3us(unsigned char us)
{
unsigned char i; // usec counter
// The input for Timer 3 is selected as SYSCLK by setting T3ML (bit 6) of CKCON0:
CKCON0|=0b_0100_0000;
TMR3RL = (-(SYSCLK)/1000000L); // Set Timer3 to overflow in 1us.
TMR3 = TMR3RL; // Initialize Timer3 for first overflow
TMR3CN0 = 0x04; // Sart Timer3 and clear overflow flag
for (i = 0; i < us; i++) // Count <us> overflows
{
while (!(TMR3CN0 & 0x80)); // Wait for overflow
TMR3CN0 &= ~(0x80); // Clear overflow indicator
}
TMR3CN0 = 0 ; // Stop Timer3 and clear overflow flag
}
void waitms (unsigned int ms)
{
unsigned int j;
for(j=ms; j!=0; j--)
{
Timer3us(249);
Timer3us(249);
Timer3us(249);
Timer3us(250);
}
}
void TIMER0_Init(void)
{
TMOD&=0b_1111_0000; // Set the bits of Timer/Counter 0 to zero
TMOD|=0b_0000_0001; // Timer/Counter 0 used as a 16-bit timer
TR0=0; // Stop Timer/Counter 0
}
void main (void)
{
float period;
TIMER0_Init();
waitms(500); // Give PuTTY a chance to start.
printf("\x1b[2J"); // Clear screen using ANSI escape sequence.
printf ("EFM8 Period measurement at pin P0.1 using Timer 0.\n"
"File: %s\n"
"Compiled: %s, %s\n\n",
__FILE__, __DATE__, __TIME__);
while (1)
{
// Reset the counter
TL0=0;
TH0=0;
TF0=0;
overflow_count=0;
while(P0_1!=0); // Wait for the signal to be zero
while(P0_1!=1); // Wait for the signal to be one
TR0=1; // Start the timer
while(P0_1!=0) // Wait for the signal to be zero
{
if(TF0==1) // Did the 16-bit timer overflow?
{
TF0=0;
overflow_count++;
}
}
while(P0_1!=1) // Wait for the signal to be one
{
if(TF0==1) // Did the 16-bit timer overflow?
{
TF0=0;
overflow_count++;
}
}
TR0=0; // Stop timer 0, the 24-bit number [overflow_count-TH0-TL0] has the period!
period=(overflow_count*65536.0+TH0*256.0+TL0)*(12.0/SYSCLK);
// Send the period to the serial port
printf( "\rT=%f ms ", period*1000.0);
}
}