Analog Comparator Example - PalouseRobosub/SUBLIBinal GitHub Wiki

Comparator Example

This page contains information describing the provided Comparator example. Code examples can be found within the examples folder of the repository.

#include "sublibinal.h"
#include "sublibinal_config.h"

void comparator_callback();

int main()
{
	ANSELA = 0;
	ANSELB = 0;

	Comparator_Config comparatorConfig = {0};			//Initialize configuration structure to 0
	comparatorConfig.callback = &comparator_callback;	//Link the callback function
	comparatorConfig.channel = COMP_CH_1;				//Utilize comparator 1
	comparatorConfig.edge = rising;						//Search for rising edges
	comparatorConfig.enable = TRUE;						//Start the comparator on initialization
	comparatorConfig.input = Pin_RPB2;					//Utilize Pin RPB2 as our time-varying input
	comparatorConfig.voltage_reference = 0.5; 			//Use a voltage reference of 0.5V

	initialize_Comparator(comparatorConfig);			//Initialize the comparator

	TRISB &= ~(1<<13); 									//Enable output on RPB13
	LATB &= ~(1<<13);									//Initialize output to 0

	enable_Interrupts();								//Global interrupt enable

	while (1);											//Embedded System loop
}

void comparator_callback()
{
	LATBINV |= 1<<13;									//Toggle the status of Pin RPB13
}

In this example, comparator 1 is set up to take input on Pin B2. The internal voltage is then configured to use 0.5V, and the comparator is set up to notice falling edges. That is, the comparator registers when the comparison on RB2 falls below the internal reference.

When an edge is detected that matches the edges specified in the configuration, an interrupt is triggered. Within the interrupt, we specify that the status of Pin B13 will be toggled.

top