-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEFM8_ADC.c
533 lines (447 loc) · 13.9 KB
/
EFM8_ADC.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// ADC.c: Shows how to use the 14-bit ADC. This program
// measures the voltage from some pins of the EFM8LB1 using the ADC.
//
// (c) 2008-2023, Jesus Calvino-Fraga
//
#include <stdio.h>
#include <stdlib.h>
#include <EFM8LB1.h>
// ~C51~
#define SYSCLK 72000000L
#define BAUDRATE 115200L
#define SARCLK 18000000L
#define PRESCALAR 12L // to adjust timer speeds
#define LCD_RS P1_7
// #define LCD_RW Px_x // Not used in this code. Connect to GND
#define LCD_E P2_0
#define LCD_D4 P1_3
#define LCD_D5 P1_2
#define LCD_D6 P1_1
#define LCD_D7 P1_0
#define CHARS_PER_LINE 16
#define OVERFLOW_COUNTER_SIZE 2
#define REF_SIGNAL QFP32_MUX_P2_5
#define SPL_SIGNAL QFP32_MUX_P2_4
#define BT1 P3_3
#define BT2 P3_0
#define RED P0_3
#define GREEN P0_2
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; // enable VDD monitor
RSTSRC=0x02|0x04; // Enable reset on missing clock detector and VDD
#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
// Configure Uart 0
#if (((SYSCLK/BAUDRATE)/(2L*PRESCALAR))>0xFFL)
#error Timer 0 reload value is incorrect because (SYSCLK/BAUDRATE)/(2L*12L) > 0xFF
#endif
SCON0 = 0x10;
TH1 = 0x100-((SYSCLK/BAUDRATE)/(2L*PRESCALAR));
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;
}
void InitADC (void)
{
SFRPAGE = 0x00;
ADEN=0; // Disable ADC
ADC0CN1=
(0x2 << 6) | // 0x0: 10-bit, 0x1: 12-bit, 0x2: 14-bit
(0x0 << 3) | // 0x0: No shift. 0x1: Shift right 1 bit. 0x2: Shift right 2 bits. 0x3: Shift right 3 bits.
(0x0 << 0) ; // Accumulate n conversions: 0x0: 1, 0x1:4, 0x2:8, 0x3:16, 0x4:32
ADC0CF0=
((SYSCLK/SARCLK) << 3) | // SAR Clock Divider. Max is 18MHz. Fsarclk = (Fadcclk) / (ADSC + 1)
(0x0 << 2); // 0:SYSCLK ADCCLK = SYSCLK. 1:HFOSC0 ADCCLK = HFOSC0.
ADC0CF1=
(0 << 7) | // 0: Disable low power mode. 1: Enable low power mode.
(0x1E << 0); // Conversion Tracking Time. Tadtk = ADTK / (Fsarclk)
ADC0CN0 =
(0x0 << 7) | // ADEN. 0: Disable ADC0. 1: Enable ADC0.
(0x0 << 6) | // IPOEN. 0: Keep ADC powered on when ADEN is 1. 1: Power down when ADC is idle.
(0x0 << 5) | // ADINT. Set by hardware upon completion of a data conversion. Must be cleared by firmware.
(0x0 << 4) | // ADBUSY. Writing 1 to this bit initiates an ADC conversion when ADCM = 000. This bit should not be polled to indicate when a conversion is complete. Instead, the ADINT bit should be used when polling for conversion completion.
(0x0 << 3) | // ADWINT. Set by hardware when the contents of ADC0H:ADC0L fall within the window specified by ADC0GTH:ADC0GTL and ADC0LTH:ADC0LTL. Can trigger an interrupt. Must be cleared by firmware.
(0x0 << 2) | // ADGN (Gain Control). 0x0: PGA gain=1. 0x1: PGA gain=0.75. 0x2: PGA gain=0.5. 0x3: PGA gain=0.25.
(0x0 << 0) ; // TEMPE. 0: Disable the Temperature Sensor. 1: Enable the Temperature Sensor.
ADC0CF2=
(0x0 << 7) | // GNDSL. 0: reference is the GND pin. 1: reference is the AGND pin.
(0x1 << 5) | // REFSL. 0x0: VREF pin (external or on-chip). 0x1: VDD pin. 0x2: 1.8V. 0x3: internal voltage reference.
(0x1F << 0); // ADPWR. Power Up Delay Time. Tpwrtime = ((4 * (ADPWR + 1)) + 2) / (Fadcclk)
ADC0CN2 =
(0x0 << 7) | // PACEN. 0x0: The ADC accumulator is over-written. 0x1: The ADC accumulator adds to results.
(0x0 << 0) ; // ADCM. 0x0: ADBUSY, 0x1: TIMER0, 0x2: TIMER2, 0x3: TIMER3, 0x4: CNVSTR, 0x5: CEX5, 0x6: TIMER4, 0x7: TIMER5, 0x8: CLU0, 0x9: CLU1, 0xA: CLU2, 0xB: CLU3
ADEN=1; // Enable ADC
//EIE1=
//(0x1 << 0) ; // EADC0. 0: Disable ADC0 Conversion Complete Interrupt. 1: Enable interrupt requests generated by the ADINT flag.
}
// 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;
//CKCON0 = (CKCON0 & 0b_1111_1100) | prescalar_setting;
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;
unsigned char k;
for(j=0; j<ms; j++)
for (k=0; k<4; k++) Timer3us(250);
}
/* can wait more precisely*/
void wait100us (unsigned long time){
unsigned long j;
for(j = 0; j<time; j++) Timer3us(100);
}
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 counter
TR0=0; // Stop Timer/Counter 0
}
#define VDD 3.3035 // The measured value of VDD in volts
void InitPinADC (unsigned char portno, unsigned char pinno)
{
unsigned char mask;
mask=1<<pinno;
SFRPAGE = 0x20;
switch (portno)
{
case 0:
P0MDIN &= (~mask); // Set pin as analog input
P0SKIP |= mask; // Skip Crossbar decoding for this pin
break;
case 1:
P1MDIN &= (~mask); // Set pin as analog input
P1SKIP |= mask; // Skip Crossbar decoding for this pin
break;
case 2:
P2MDIN &= (~mask); // Set pin as analog input
P2SKIP |= mask; // Skip Crossbar decoding for this pin
break;
default:
break;
}
SFRPAGE = 0x00;
}
unsigned int ADC_at_Pin(unsigned char pin)
{
ADC0MX = pin; // Select input from pin
ADINT = 0;
ADBUSY = 1; // Convert voltage at the pin
while (!ADINT); // Wait for conversion to complete
return (ADC0);
}
float Volts_at_Pin(unsigned char pin)
{
return ((ADC_at_Pin(pin)*VDD)/0b_0011_1111_1111_1111); //b/c 14 bit adc
}
//For lcd printing
void LCD_pulse (void)
{
LCD_E=1;
Timer3us(40);
LCD_E=0;
}
void LCD_byte (unsigned char x)
{
// The accumulator in the C8051Fxxx is bit addressable!
ACC=x; //Send high nible
LCD_D7=ACC_7;
LCD_D6=ACC_6;
LCD_D5=ACC_5;
LCD_D4=ACC_4;
LCD_pulse();
Timer3us(40);
ACC=x; //Send low nible
LCD_D7=ACC_3;
LCD_D6=ACC_2;
LCD_D5=ACC_1;
LCD_D4=ACC_0;
LCD_pulse();
}
void WriteData (unsigned char x)
{
LCD_RS=1;
LCD_byte(x);
waitms(2);
}
void WriteCommand (unsigned char x)
{
LCD_RS=0;
LCD_byte(x);
waitms(5);
}
void LCD_4BIT (void)
{
LCD_E=0; // Resting state of LCD's enable is zero
// LCD_RW=0; // We are only writing to the LCD in this program
waitms(20);
// First make sure the LCD is in 8-bit mode and then change to 4-bit mode
WriteCommand(0x33);
WriteCommand(0x33);
WriteCommand(0x32); // Change to 4-bit mode
// Configure the LCD
WriteCommand(0x28);
WriteCommand(0x0c);
WriteCommand(0x01); // Clear screen command (takes some time)
waitms(20); // Wait for clear screen command to finsih.
}
void LCDprint(char * string, unsigned char line, bit clear)
{
int j;
WriteCommand(line==2?0xc0:0x80);
waitms(5);
for(j=0; string[j]!=0; j++) WriteData(string[j]);// Write the message
if(clear) for(; j<CHARS_PER_LINE; j++) WriteData(' '); // Clear the rest of the line
}
unsigned int Get_ADC(void)
{
ADINT = 0;
ADBUSY = 1;
while (!ADINT);
return (ADC0);
}
void main (void)
{
char buffer[20];
//char skip_count;
int bsel = 0;
int led = 1;
float halfPeriod;
float period;
float freq;
float quarterPeriod;
float prev_period;
float vrms_spl;
float vrms_ref;
float phase_diff_deg;
float phase_diff_time;
LCD_4BIT();
TIMER0_Init();
waitms(500); // Give PuTTy a chance to start before sending
printf("\x1b[2J"); // Clear screen using ANSI escape sequence.
printf ("ADC test program\n"
"File: %s\n"
"Compiled: %s, %s\n\n",
__FILE__, __DATE__, __TIME__);
InitPinADC(2, 2); // Configure P2.2 as analog input
InitPinADC(2, 3); // Configure P2.3 as analog input
InitPinADC(2, 4); // Configure P2.4 as analog input
InitPinADC(2, 5); // Configure P2.5 as analog input
InitPinADC(0, 1);
InitADC();
prev_period = 10000; // initilize these to a period value thats impossible to get
while(1)
{
/*** PERIOD MEASUREMENT ***/
ADC0MX=REF_SIGNAL; // <---- PORT FOR REFERENCE SIGNAL
ADINT = 0;
ADBUSY=1;
while (!ADINT); // wait for conversion to complete
while (Get_ADC()!=0); // wait for signal to be 0
while (Get_ADC()==0); // wait for signal to be pos
overflow_count = 0; // reset timer
TL0=0;
TH0=0;
TR0=1; // start timer 0
while (Get_ADC()!=0){
if (TF0==1){
TF0=0;
overflow_count++; // count overflows
}
}
TR0=0; // stop timer 0
halfPeriod=(overflow_count*65536.0 + TH0*256.0 + TL0)*(12.0/SYSCLK);
overflow_count = 0; // reset timer post count for redundancy
TL0=0;
TH0=0;
period = 2.0*halfPeriod; // period & freq calcs
if(period <= 0.0002){ // freq never exceeds 5000 Hz, ignore bad ones
period = prev_period; // noise correction
}else{
prev_period = period;
}
freq = 1.0/period;
quarterPeriod = period/4.0;
/*** FIND Vmax for REFERENCE ***/
ADINT = 0;
ADBUSY=1;
while (!ADINT); // wait for conversion to complete
do{
while (Get_ADC()!=0); // wait for signal to be 0
while (Get_ADC()==0); // wait for signal to be pos
Timer3us(20);
}while(Get_ADC()==0); // (**SUPER NECESARY**) check if adc aint lying, mitigates noise
wait100us(quarterPeriod*1000*10);
vrms_ref = Volts_at_Pin(REF_SIGNAL)*0.707106781187; // grabs vmax 1/4 T later from 0-cross
waitms(50); // some delay very good
/*** FIND Vmax for SAMPLE ***/
ADC0MX=SPL_SIGNAL; // start tracking SPL signal
ADINT = 0;
ADBUSY=1;
while (!ADINT); // wait for conversion to complete
do{
while (Get_ADC()!=0); // wait for signal to be 0
while (Get_ADC()==0); // wait for signal to be pos
Timer3us(20);
}while(Get_ADC()==0); // check if adc aint lying, mitigates noise
wait100us(quarterPeriod*1000*10);
vrms_spl = Volts_at_Pin(SPL_SIGNAL)*0.707106781187; // grabs vrms 1/4 T later from 0-cross
/*** MEASURE PHASE DIFF ***/
//(1) FIND 0-CROSS OF SPL SIGNAL
ADINT = 0;
ADBUSY=1;
while (!ADINT); // wait for adc conversion to complete
do{
while (Get_ADC()!=0); // wait for 0 cross
while (Get_ADC()==0);
Timer3us(20);
}while(Get_ADC()==0);
//(2) START TIMING AS SOON AS SPL 0-CROSS
overflow_count = 0;
TL0=0; // clear timer 0
TH0=0;
TR0=1; // start timer 0
//(3) WAIT TILL POSEDGE 0-CROSS OF REF SIGNAL
ADC0MX=REF_SIGNAL; // start tracking REF signal
ADINT = 0;
ADBUSY=1;
while (!ADINT); // wait for adc conversion to complete
do{
while (Get_ADC()!=0); // wait for REF signal to be 0
while (Get_ADC()==0){ // wait for REF signal to be positive
if (TF0==1){
TF0=0;
overflow_count++; // count overflows
}
}
}while(Get_ADC()==0); // Check if REALLYMREALLYM POSITIVE!!!!!!!! :DDDDDD
//(4) STOP TIMER, CALCULATE DIFFERENCE
TR0=0; // stop timer 0
phase_diff_time = (overflow_count*65536.0 + TH0*256.0 + TL0)*(12.0/SYSCLK);
// account for when the second rising edge gets missed,
// +/- 360 until within the range of [-180, 180]
phase_diff_deg = (phase_diff_time * 360)/period;
TL0=0;
TH0=0;
overflow_count = 0; // stop timer
while(phase_diff_deg < -180){
phase_diff_deg += 360;
}
while(phase_diff_deg > 180){
phase_diff_deg -= 360;
}
if(phase_diff_deg > 0){
phase_diff_deg += 0.5;
}else if(phase_diff_deg < 0){
phase_diff_deg -= 1.5;
}
if((BT1 == 0) && (bsel == 0)){
bsel = 1;
} else if((BT1 == 0) && (bsel == 1)){
bsel = 2;
} else if((BT1 == 0) && (bsel == 2)){
bsel = 0;
}
if((BT2 == 0) && (led == 0)){
led = 1;
}else if((BT2 == 0) && (led == 1)){
led = 0;
}
printf("Period(T): %7.6f ms Freq(f): %7.6f Hz \n",period*1000, freq);
printf("1/4 Period: %7.6f ms\n", quarterPeriod*1000);
printf("Vrms (ref): %4.4f V Vrms (spl): %4.4f V \n",vrms_ref, vrms_spl);
printf("Phase Difference: %7.6f degrees Phase diff time: %7.6f s \n", phase_diff_deg, phase_diff_time);
printf("\033[A");
printf("\033[A");
printf("\033[A");
printf("\033[A");
if(bsel == 0){
sprintf(buffer,"F: %3dHz T: %2dms",(int)freq%1000, (int)(period*1000)%1000);
LCDprint(buffer,1,1);
sprintf(buffer,"Phase dif: %3.2f deg",phase_diff_deg);
LCDprint(buffer,2,1);
}else if(bsel == 1){
sprintf(buffer,"Vrms(s): %2.2f V",vrms_spl);
LCDprint(buffer,1,1);
sprintf(buffer,"Vrms(r): %2.2f V",vrms_ref);
LCDprint(buffer,2,1);
}else if(bsel == 2){
sprintf(buffer,"Connect Guide");
LCDprint(buffer,1,1);
sprintf(buffer,"SAMPLE | REF");
LCDprint(buffer,2,1);
}
// if(led == 1){
if(phase_diff_deg < 0){
RED = 0;
GREEN = 1;
}else{
RED = 1;
GREEN = 0;
}
// }else{
// RED = 0;
// GREEN = 0;
// }
waitms(500);
}
}