Relay_and_Socket_Protection/src/zero_cross_detection/zero_cross_detection.c

51 lines
734 B
C

/*
* Author: Jake G
* Date: 2024
* filename: zero_cross_detection.c
* description:
*/
#include "zero_cross_detection.h"
#define TRIGGERVAL 512
#ifndef UNIT_TESTING
#include "ADC.h"
#else
#include "MockADC/MockADC.h"
#endif
#define ZCD_PIN 0x07
void ZCD_Setup(void) {
ADC_Init(ZCD_PIN);
ADC_SetPin(ZCD_PIN);
ADC_Enable();
}
bool ZCD_IsTriggered() {
uint16_t first, second;
ZCD_Setup();
first = ADC_ReadValue(ZCD_PIN);
ADC_Disable();
ZCD_Setup();
second = ADC_ReadValue(ZCD_PIN);
ADC_Disable();
if (second < TRIGGERVAL) {
return false;
}
if (second < first) {
return false;
}
return true;
}
void ZCD_Poll(void) {
while (true) {
if (ZCD_IsTriggered()) {
break;
}
}
}