#pragma config FOSC = INTOSC // ->INTOSC oscillator; I/O function on CLKIN pin
#pragma config WDTE = ON // Watchdog Timer Enable->WDT enabled
#pragma config PWRTE = OFF // Power-up Timer Enable->PWRT disabled
#pragma config MCLRE = ON // MCLR Pin Function Select->MCLR/VPP pin function is MCLR
#pragma config CP = OFF // Flash Program Memory Code Protection->Program memory code protection is disabled
#pragma config BOREN = ON // Brown-out Reset Enable->Brown-out Reset enabled
#pragma config CLKOUTEN = OFF // Clock Out Enable->CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection->Write protection off
#pragma config PLLEN = OFF // PLL Enable->4x PLL disabled
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable->Stack Overflow or Underflow will cause a Reset
#pragma config BORV = LO // Brown-out Reset Voltage Selection->Brown-out Reset Voltage (Vbor), low trip point selected.
#pragma config LPBOREN = OFF // Low Power Brown-out Reset enable bit->LPBOR is disabled
#pragma config LVP = ON // Low-Voltage Programming Enable->Low-voltage programming enabled
#include <xc.h>
//1. big board vishy mos
//2. small board Ao mos
#include "mcc.h"
static uint16_t adc_data=0;
static uint8_t adc_voltage=0;
static uint16_t pwm_data0=0;
static uint16_t power_data=0;
static uint16_t power_voltage=0;
uint16_t timer_100ms_flag=0;
uint8_t timer_10ms_flag=0;
void delay_proces (uint32_t delaytime)
{
uint32_t cnt;
cnt=delaytime;
while(cnt--)
{
CLRWDT();
}
}
void adc_process(void)
{
uint16_t k;
uint8_t i;
k=0;
for (i=0;i<16;i++)
{
k+=ADC1_GetConversion(3);
CLRWDT();
NOP();NOP();NOP();NOP();NOP();NOP();NOP();
}
adc_data = k >> 4;
}
void power_process(void)
{
uint32_t kk;
kk = (uint32_t)adc_data * (uint32_t)33 * (uint32_t)25 * (uint32_t)10 / (uint32_t)1024;
power_voltage = (uint16_t)(kk+80) / 10;
}
void get_pwm_data(void)
{
uint32_t kk;//,kkm;
if(timer_100ms_flag < 50)
{
kk = (uint32_t)((uint32_t)58 * (uint32_t)799);
kk = ((uint32_t)kk / (uint32_t)power_voltage);
//kk=((uint32_t)kk*(uint32_t)20/(uint32_t)10)+((uint32_t)power_voltage/90); //34W
kk = ((uint32_t)kk * (uint32_t)16 / (uint32_t)10) + ((uint32_t)power_voltage / 90); //34W
pwm_data0 = (uint16_t)kk + 2;
if( pwm_data0 > 799 )
{
pwm_data0 = 800; //full power pwm
}
if(power_voltage < 70)
{
pwm_data0 = 0;
kk = (power_voltage & 0x0f);
delay_proces(kk * 16667);
timer_100ms_flag = 0;
}
else if(power_voltage < 120)
{
pwm_data0 = 800; //full power pwm
}
}
else
{
kk = (uint32_t)((uint32_t)131 * (uint32_t)799);
kk = ((uint32_t)kk / (uint32_t)power_voltage) * (uint32_t)23 / (uint32_t)100 * (uint32_t) 2;
if(power_voltage < 50)
{
if(timer_10ms_flag > 6)
{
PWM3_DutyCycleSet(0);
}
}
else
{
timer_10ms_flag = 0;
if( power_voltage < 122)
{
kk = kk + 3 + 2 + 2;
}
else if( power_voltage < 160)
{
kk = kk + 2 + 2;
}
else if( power_voltage < 246)
{
kk = kk + 2;
}
else if(power_voltage > 460 && power_voltage < 730)
{
kk = kk + 4;
}
else if(power_voltage >= 730)
{
pwm_data0=0;
}
pwm_data0=kk+9; //?????????????
if(pwm_data0>799)
{
pwm_data0=800; //full power pwm
}
}
}
}
void main(void)
{
SYSTEM_Initialize();
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
while((power_voltage < 84) || (power_voltage > 730))
{
adc_process(); //gather 16 adc-data,get the average
PWM3_DutyCycleSet(pwm_data0); //get the power_voltage
CLRWDT();
}
timer_100ms_flag = 0;
while(timer_100ms_flag < 2)
{
CLRWDT();
}
//timer_2ms_flag=1;
timer_100ms_flag = 0;
while (1)
{
CLRWDT();
adc_process();
power_process();
get_pwm_data();
PWM3_DutyCycleSet(pwm_data0);
}
} |