I appreciate your honesty about the software but it really is VERY easy.
1. configure the registers including setting the PWM rate.
2. detect zero crossing
3. delay. This would be 5mS for 50Hz mains.
4. read ADC.
5. Set PWM width.
6. go back to step 2.
Simplest zero crossing is a high value resistor between rectified (but not filtered) AC and one of the PIC pins.
Provided you don't use a silly frequency, the PWM resolution is 10-bit so you from a 5V supply you can produce an analog output in ~4.8mV steps.
That would be easier and probably more accurate than taking ADC readings at 5KHz rate and then sorting the results. Bear in mind you still have to work out when the peak has passed to know when to stop sampling and do the sorting/reporting.
I'm picturing an 8-pin PIC 12F683 which has ADC, PWM and an internal clock.
Brian.
- - - Updated - - -
Just to see how easy it is, I wrote the code below and simulated it. The integrated (10ms) PWM output was on GP2, the rectified AC is on pin GP0 and the ZCD input is on GP4. As an aid to check timing, a positive going pulse is produced on GP1 at peak AC cycle. Written in 'C' for 12F683 is uses in total 265 bytes! It could be much smaller if written in assembler.
Code:
#include "Y:\\projects\\VillaCon\\ACPeakToPWM\\ACPeakToPWM_Auto.h"
#include <Delays.h>
void UserInterrupt()
{
}
void UserInitialise()
{
PeakDetected = 0; // initial state
}
//*******************************************************************************
void UserLoop()
{
}
void ZCDetected()
{
Wait(5);
PeakDetected = 1;
PeakDetected = 0;
ADCON0 |= 1 << GO;
}
void ADCFinished()
{
unsigned int ADCResult;
ADCResult = (unsigned int) ADRESH << 8;
ADCResult += (unsigned int) ADRESL;
SetPWM1Volts(ADCResult);
}
Note that this is not 'stand alone' code, it uses libraries in the compiler and the 'Wait()' time may need some tweaking as it starts from the rising edge of the ZCD input signal, if you feed it with rectified AC you need to take into account the time for the voltage to cross the logic threshold. It will still be representative of the peak but not exactly at the peak.
Should add that the compiler is a UK written and marketed product!
Brian.