#include "p30f4011.h"
#include "oled.h"
//配置位
_FOSC(CSW_FSCM_OFF&XT_PLL4) ;
_FWDT(WDT_OFF) ;
_FBORPOR(PBOR_OFF&MCLR_EN) ;
_FGS(CODE_PROT_OFF);
#define U8 unsigned char
#define Uint16 unsigned int
#define L32 long int
/////////////////////////////////////PWM
#define FCY 8000000
#define FPWM 10000
#define MainCurr 340 //4.5A---2.38V
#define MainVol 688 //8V
#define Ten 408
//(102)
//////////////////////////////////
#define VOLT1 ADCBUF0 //AN4
#define CURR1 ADCBUF1 //AN0
#define CURR2 ADCBUF2 //AN1
#define CURR3 ADCBUF3 //AN2
struct
{
Uint16 ADC:1;
Uint16 SUM:1;
Uint16 cal:1;
Uint16 Key:1;
Uint16 OverCurr:1;
}Flags;
Uint16 count;
L32 AVE,AVEC1,AVEC2,AVEC3;
Uint16 rate;
L32 OverCount;
Uint16 ADC[32],ADCC1[32],ADCC2[32],ADCC3[32];
float frate=400;
void Delay_1ms(unsigned int t)//t = 1000大概1s
{
unsigned int i,j;
for(i = 0;i < t;i ++)
for(j = 0;j < 2000;j ++);
}
void delay(int us)
{
unsigned int i,j;
for(j=us;j>0;j--)
for(i=0;i<8;i++);
}
//振荡器配置
void System_Colck(void)
{
//产生Fosc = 7.3728MHz 117.9648MHz 30MIPS
while (OSCCONbits.COSC!= 0b011)
while (OSCCONbits.LOCK!= 1) {};//PLL 处于锁定状态
}
void OLED_IO_Init(void)
{
TRISBbits.TRISB4 = 0;
TRISBbits.TRISB5 = 0;
TRISBbits.TRISB6 = 0;
TRISBbits.TRISB7 = 0;
TRISBbits.TRISB8 = 0;
}
int main()
{
OLED_IO_Init();
OLED_Init(); //初始化OLED
OLED_Clear();
rate=400;
count=0;
InitPWM();
ADC_Init();
KEY_Init();
Timer1_Init();
// INIT_CURR();
//disn(4,0,55);disn(5,0,69);disn(6,0,76);disn(7,0,67);disn(8,0,79);disn(9,0,77);disn(10,0,69);//Welcome //display one math word 31.25uS
// disn(1,1,36);disn(2,1,51);disn(3,1,48);disn(4,1,16);disn(5,1,45);disn(6,1,35);disn(7,1,53);
System_Colck();
while(1)
{
disn(6,0,rate%1000/100);disn(7,0,rate%100/10);disn(8,0,rate%10);
// i = AVEC1;
/*disn(1,0,14);disn(2,0,1);disn(5,0,i%10000/1000);disn(6,0,i%1000/100);disn(7,0,i%100/10);disn(8,0,i%10);
i = AVEC2;
disn(1,1,14);disn(2,1,2); disn(5,1,i%10000/1000);disn(6,1,i%1000/100);disn(7,1,i%100/10);disn(8,1,i%10);
i = AVEC3;
disn(1,2,14);disn(2,2,3); disn(5,2,i%10000/1000);disn(6,2,i%1000/100);disn(7,2,i%100/10);disn(8,2,i%10);
*/
/*
i = AVE;
disn(1,3,11);disn(2,3,3); disn(4,3,i/10000);disn(5,3,i%10000/1000);disn(6,3,i%1000/100);disn(7,3,i%100/10);disn(8,3,i%10);*/
// disn(4,2,j/10000);disn(5,2,j%10000/1000);disn(6,2,j%1000/100);disn(7,2,j%100/10);disn(8,2,j%10);*/
// j=PDC1;
GET_KEY();
OVER_CURR();
if(Flags.SUM)
{
Flags.SUM =0;
U8 i;
L32 SUM=0,SUMC1=0,SUMC2=0,SUMC3=0;
for(i=0;i<16;i++)
{
SUM+=ADC[i];
SUMC1+=ADCC1[i];
SUMC2+=ADCC2[i];
SUMC3+=ADCC3[i];
}
AVE=(SUM>>4);
AVEC1=(SUMC1>>4);
AVEC2=(SUMC2>>4);
AVEC3=(SUMC3>>4);
Flags.cal=1;
}
else
{
Flags.cal=0;
}
}
}
void PWM_adjust(Uint16 A_VE,Uint16 A_VEC1,Uint16 A_VEC2)
{
if(A_VEC1>MainCurr)
{
if(A_VEC1>(MainCurr+1))
{
PDC1--;
}
else if (A_VEC1<(MainCurr-1))
{
PDC1++;
}
}
else
{
if(A_VE>(MainVol+1))
{
PDC1--;
}
else if (A_VE<(MainVol-1))
{
PDC1++;
}
}
if((rate*A_VEC2)>(Ten*A_VEC1+100))
{
PDC2--;
}
if((rate*A_VEC2)<(Ten*A_VEC1-100))
{
PDC2++;
}
if(PDC1<100)
{
PDC1=100;
}
else if(PDC1>1200)
{
PDC1=1200;
}
if(PDC2<100)
{
PDC2=100;
}
else if(PDC2>1200)
{
PDC2=1200;
}
}
void KEY_Init(void)
{
// TRISBbits.TRISB8 = 1;
TRISEbits.TRISE8 = 1;
// ADPCFGbits.PCFG8=1;
TRISDbits.TRISD3 = 1;
// PORTDbits.RD3 = 0;
}
void ADC_Init(void)
{
TRISBbits.TRISB0 = 1;//??
TRISBbits.TRISB1 = 1;//??
TRISBbits.TRISB2 = 1;//??
TRISBbits.TRISB3 = 1;//??
ADCON1 = 0x0000;//???????
ADCON1bits.SSRC = 7;//????????????????????
ADCON1bits.ASAM = 1;//???? ?????????
ADCON2 = 0x020C;//??????AVDD,AVSS;????/?????????
ADCON3 = 0x1f1f; //?????????????????????31Tad?Tad=32Tcy
ADCHS = 0x0003;////??0????Vref-,??0????AN3
ADPCFGbits.PCFG3 = 0;//AN2?????????????,A/D??????
ADPCFGbits.PCFG2 = 0;//AN2?????????????,A/D??????
ADPCFGbits.PCFG1 = 0;//AN1?????????????,A/D??????
ADPCFGbits.PCFG0 = 0;//AN1?????????????,A/D??????
ADCSSL = 0x0000;//????????
IPC2bits.ADIP = 7; //??AD????????7
IEC0bits.ADIE = 1; //??AD??????
ADCON1bits.ADON = 1; //??ADC
}
void INIT_CURR(void)
{
TRISDbits.TRISD3 = 0;
PORTDbits.RD3 = 0;
}
void OVER_CURR(void)
{
if(AVEC3>487)//2.5V
{
Flags.OverCurr = 1;
OverCount=0;
}
else
{
OverCount++;
if(OverCount>10000)
{
Flags.OverCurr = 0;
}
}
}
void GET_KEY(void)
{
if(PORTDbits.RD3==0)
{
frate=frate-Ten*0.103;
rate=frate;
while(PORTDbits.RD3==0);
}
if(PORTEbits.RE8==0)
{
frate=frate+Ten*0.103;
rate=frate;
while(PORTEbits.RE8==0);
}
}
void Timer1_Init(void)//??????
{
T1CONbits.TON = 0;//?????
T1CON = 0x0000;//???? ???1:1 ?????? FOSC/4
T1CONbits.TSYNC = 1;//????
IEC0bits.T1IE = 1;//??Timer1????
INTCON1 |= 0X8000;//?????? NSTDIS?1
IPC0bits.T1IP = 0x07;//?????7
T1CONbits.TON = 1;//?????
PR1=800;//80khz --800
}//(FCY*4.0)/HZ*200
void __attribute__((__interrupt__,no_auto_psv))_ADCInterrupt(void)//ADC?
{
IFS0bits.ADIF=0; //?AD???????
ADC[count]=VOLT1;
ADCC1[count]=CURR1;
ADCC2[count]=CURR2;
ADCC3[count]=CURR3;
count++;
if(count==16)
{
count=0;
Flags.SUM=1;
}
}
void __attribute__((__interrupt__, auto_psv)) _T1Interrupt(void)//???key??rate?pi??
{
// PORTDbits.RD3 = 1;
IFS0bits.T1IF = 0;
if(Flags.cal)
{
Flags.cal=0;
if(Flags.OverCurr)
{
PDC1=0;
PDC2=0;
}
else
{
PWM_adjust(AVE,AVEC1,AVEC2);
}
}
// PORTDbits.RD3 = 0;
}
void InitPWM() //PWM????????
{
LATE = 0X0000 ;
TRISE = 0X0000 ; //??RE0-RE5??????PWM1-PWM3
PTPER = FCY / FPWM - 1; //20KHzPWM??
PWMCON1 = 0x0033; //PWM I/O ???????????
//??1H ?1L ???I/0?
SEVTCMP = PTPER ; //368*4 PWM ?????? 101110000
PWMCON2 = 0x0F00; //????????????1?16
OVDCON = 0XFF00; //bit 15-8 POVD4H-POVD1L? PWM ?????1 = PWMxx I/O ??????? PWM ?????
PDC1 = 800 ; //PWM1 ??? 0.136(PDC1/PTPER/2)
PDC2 = 800 ; //PWM2 ??? 0.136
PTCON = 0x8000 ; //??PWM????1?1????1:1???????
//PTCON? PWM ??????? bit 7-4 PTOPS<3:0>? PWM ???????????