# Zilog comparator programming for battery and supercap

Status
Not open for further replies.

#### kct

##### Newbie level 1
Good day to everyone.

Currently I'm in the process of using a Zilog MCU (Z8F042A) to design a voltage comparator for comparing a voltage between a mobile phone battery with a maximum voltage of 4.2V, and a supercapacitor(s) rated at 5V. At any instant the supercapacitor's voltage is equivalent to that of the battery, the system will send a voltage to the gate of a P channel MOSFET to switch the MOSFET off. The moment the supercapacitor's voltage falls below that of the battery, the MOSFET is to be switched on again.

The problem now is, I am not so sure about programming the Zilog itself (programming has never been my strong point), not to mention the issue of whether I need to use the ADC or not. It would be great if anyone can help me out.

Code:
#include<eZ8.h>
#include <stdio.h>
#include <sio.h>
#include <adc.h>

unsigned int VINSUP; // Voltage from Supercapacitor
unsigned int VINREF; // Voltage from Battery

void main(void)
{
ADCCTL0 = 0xAE; // Conversion Enabled
// Reference Buffer Disabled
// Continuous Mode

ADCCTL1 = 0x01; //Single Ended Buffered Input with Unity Gain

PCDD = 0xF7; // PC3 as output and others as input

PCOUT = 0xFF; // Turn off all ports

while(1)
{
if ((PCIN & 0x01) == 0) // To check if PC0 is low or not
{
PCOUT = 0xF7;
}
else
{
PCOUT = 0xFF;
}
}

Status
Not open for further replies.