📄 main.c
字号:
#include <LPC23xx.H>
#include <stdio.h>
#include <math.h>
#include "Hardware.h"
#include "lcd.h"
#include "GPIO.h"
#include "adc.h"
#include "time_rtc.h"
#include "key.h"
#include "main.h"
#include "iap.h"
#define PI ((double)3.1415926)
#define N 2048
#define r 11
#define FS 20480 //(0xDB=>72M,4)
#define Record_Length 60
__align(4) unsigned char Param[512]={0};
float fftm[N];
float fft_ideal[N];
unsigned char lcd_data[1024];
unsigned short pos[11];
float coef[12] = {0.98, 1.0, 1.156, 1.496, 2.25, 1.72, 1.21, 1.027, 1.0, 1.0, 1.0, 1.05};
unsigned char Cap11_ctn=0;
unsigned char status_mode=0;
unsigned short status_R=1;
union P_Float
{
float p_val;
unsigned char p_char[4];
} F_Char;
/*
Power_F.f_val = 0.111;
Power_F.c_val;
*/
unsigned char Flash()
{
unsigned long status=0;
unsigned long temp=0;
status=VICIntEnable; //存储被使能的IRQ中断
VICIntEnClr=0xffffffff;
SelSector(11 ,11 ); // 选择扇区
EraseSector(11 ,11); // 擦除扇区
SelSector(11 ,11 ); // 选择扇区
RamToFlash(DestAddr11 , (unsigned long)Param, 512); // 写数据到FLASH
temp=Compare(DestAddr11 , (unsigned long)Param, 512);
VICIntEnable=status;
return temp;
}
float Time_Power()
{
unsigned short i=0;
double power_val=0;
for(i=0;i<N;i++)
{
fft_ideal[i]=(x_Input[i]-0x800)/4096.0*10;
power_val+=(fft_ideal[i]*fft_ideal[i]);
}
power_val=power_val/N;
power_val=power_val/status_R;
return power_val;
}
void Fft()
{
float Real,Ideal=0;
unsigned int p=0;
unsigned short i,j,k=0;
unsigned short b=0;
for(i=0;i<N;i++)
{
fftm[i]=(x_Input[i]-0x800)/4096.0*10;
fft_ideal[i]=0;
}
//蝶形运算
for(k=0;k<r;k++)
{
for(j=0;j<(1<<k);j++)
{
b=(1<<(r-k));
for(i=0;i<(b>>1);i++)
{
p=j*b;
Real=fftm[i+p]-fftm[i+p+(b>>1)];
Ideal=fft_ideal[i+p]-fft_ideal[i+p+(b>>1)];
fftm[i+p]=fftm[i+p]+fftm[i+p+(b>>1)];
fft_ideal[i+p]=fft_ideal[i+p]+fft_ideal[i+p+(b>>1)];
fftm[i+p+(b>>1)]=(Real*cos(-(i*(1<<k))*PI*2/N))-(Ideal*sin(-(i*(1<<k))*PI*2/N));
fft_ideal[i+p+(b>>1)]=(Ideal*cos(-(i*(1<<k))*PI*2/N))+(Real*sin(-(i*(1<<k))*PI*2/N));
}
}
}
for(j=0;j<N;j++)
{
p=0;
for(i=0;i<r;i++)
{
if(j&(1<<i))
{
p+=(1<<(r-i-1));
}
}
if(j<p)
{
Real=fftm[j];
fftm[j]=fftm[p];
fftm[p]=Real;
Ideal=fft_ideal[j];
fft_ideal[j]=fft_ideal[p];
fft_ideal[p]=Ideal;
}
fftm[j]=sqrt((fftm[j]*fftm[j])+(fft_ideal[j]*fft_ideal[j]));
if(j==0)
{
fftm[0]=fftm[0]/1.414;
}
}
}
void Sort(int length)
{
unsigned short i=0;
unsigned char j=0,k=0;
fftm[1024]=0;
for(i=0;i<11;i++)
{
pos[i]=1024;
}
for(i=1;i<length-1;i++)
{
if(fftm[i]>fftm[i-1]&&fftm[i]>fftm[i+1])
{
for(j=0;j<11;j++)
{
if(fftm[i]>fftm[pos[j]])
{
k=11;
while(--k!=j)
{
pos[k]=pos[k-1];
}
pos[k]=i;
break;
}
}
}
}
for(j=0;j<11;j++)
{
if(fftm[0]/2>fftm[pos[j]])
{
k=11;
while(--k!=j)
{
pos[k]=pos[k-1];
}
pos[k]=0;
break;
}
}
for(j=0;j<11;j++)
{
if(fftm[length-1]>fftm[pos[j]])
{
k=11;
while(--k!=j)
{
pos[k]=pos[k-1];
}
pos[k]=length-1;
break;
}
}
}
int main()
{
config_IO();
initLCDM();
Gpio_Interrupt_Init();
init_Time0();
init_Timer1();
RTCInit();
Logo();
Menu();
RTCStart();
while(1)
{
key_Scan();
switch(key_Status)
{
case KEY_RIGHT:
curse_right();
key_Status=KEY_WAIT;
break;
case KEY_DOWN:
curse_down();
key_Status=KEY_WAIT;
break;
case KEY_ENTER:
key_Status=KEY_WAIT;
Sure();
break;
case KEY_HOLD:
break;
case KEY_WAIT:
break;
default:
break;
}
}
}
void curse_right(void)
{
curse_x+=2;
if(curse_x>=24) curse_x=13;
CmdWrite(0x00,0xC5);
CmdWrite(0x60,curse_x);
CmdWrite(0x70,curse_y);
}
void curse_down(void)
{
curse_y+=32;
curse_x=13;
CmdWrite(0x00,0xC5);
if(curse_y>=128) curse_y=31;
CmdWrite(0x60,curse_x);
CmdWrite(0x70,curse_y);
}
void Sure(void)
{
CmdWrite(0x00,0xC5);
switch (curse_y)
{
case 31:
if(curse_x>=13&&curse_x<=15){
Print_Check(15,24);
Clr_part(19,24,8,8);
Clr_part(23,24,8,8);
status_R=50;
}
if(curse_x>=16&&curse_x<=19){
Clr_part(15,24,8,8);
Print_Check(19,24);
Clr_part(23,24,8,8);
status_R=10000;
}
if(curse_x>=20&&curse_x<23){
Clr_part(15,24,8,8);
Clr_part(19,24,8,8);
Print_Check(23,24);
status_R=1;
}
break;
case 63:
if(curse_x>=13&&curse_x<=17){
Print_Check(17,56);
Clr_part(23,56,8,8);
status_mode=0;
}
if(curse_x>=18&&curse_x<=23){
Clr_part(17,56,8,8);
Print_Check(23,56);
status_mode=1;
}
break;
case 95:
Clr_Screen();
delayms(100);
if(status_mode==0) display_text();
else if(status_mode==1) display_graph();
break;
case 127:
Clr_Screen();
delayms(100);
if(status_mode==0) display_text_1();
else if(status_mode==1) display_graph_1();
break;
}
CmdWrite(0x00,0xC5);
CmdWrite(0x60,curse_x);
CmdWrite(0x70,curse_y);
}
void display_text (void)
{
float time_power=0.0;
double fft_power=0;
double fft_freq = 0, cali_val = 1.0;
unsigned char p_val;
int i_val;
char combuf[10];
unsigned char i=0;
Print_Str(0,16,"Pt=");
Print_Str(0,32,"F1=");
Print_Str(0,48,"P1=");
Print_Str(0,64,"F2=");
Print_Str(0,80,"P2=");
Print_Str(0,96,"F3=");
Print_Str(0,112,"P3=");
Print_Str(12,0,"F4=");
Print_Str(12,16,"P4=");
Print_Str(12,32,"F5=");
Print_Str(12,48,"P5=");
Print_Str(12,64,"F6=");
Print_Str(12,80,"P6=");
Print_Str(12,96,"F7=");
Print_Str(12,112,"P7=");
while(1)
{
Start_Time0();
ADC0IntDone=0;
while(!ADC0IntDone);
ADC0IntDone=0;
Stop_Time0();
if(sec_ctn>=5)
{
sec_ctn=0;
time_power=Time_Power();
Fft();
Sort(N/2);
sprintf(combuf,"%07f",time_power*1000);
combuf[7]='\0';
Print_Str(3,16,(unsigned char *)combuf);
for(i=0;i<7;i++)
{
fft_freq=pos[i]/2048.0*FS;
sprintf(combuf,"%07f",fft_freq);
combuf[7]='\0';
if(i<3)
Print_Str(3,(i+1)*32,(unsigned char *)combuf);
else
Print_Str(15,(i-3)*32,(unsigned char *)combuf);
fft_power=2*((fftm[pos[i]]/N)*(fftm[pos[i]]/N))/status_R;
i_val = (int)fft_freq;
if (i_val>=0 && i_val<10000)
{
p_val = i_val/1000;
cali_val = (coef[p_val+1]-coef[p_val])*(fft_freq/1000.0 - p_val) + coef[p_val];
fft_power *= cali_val;
}
sprintf(combuf,"%07f",fft_power*1000);
combuf[7]='\0';
if(i<3)
Print_Str(3,(i+1)*32+16,(unsigned char *)combuf);
else
Print_Str(15,(i-3)*32+16,(unsigned char *)combuf);
}
}
current_time = RTCGetTime();
sprintf(combuf,"%02d:%02d:%02d",current_time.RTC_Hour,current_time.RTC_Min, current_time.RTC_Sec);
Print_Str(0,0,(unsigned char *)combuf);
delayms(100);
Start_Time0();
key_Scan();
if(key_Status==KEY_ENTER)
{
key_Status=KEY_WAIT;
Clr_Screen();
Dnop(10);
Menu();
break;
}
if(key_Status==KEY_HOLD)
{
CmdWrite(0x10,0x4e);
Dnop(10);
Print_Str(0,0,"Hold On!");
key_Status=KEY_WAIT;
Stop_Time0();
while (1)
{
key_Scan();
if (key_Status==KEY_HOLD)
break;
}
key_Status=KEY_WAIT;
CmdWrite(0x10,0x6e);
Start_Time0();
}
if(key_Status==KEY_RECALL)
{
Stop_Time0();
key_Status=KEY_WAIT;
Wait_Recall();
Clr_Screen();
delayms(50);
Print_Str(0,16,"Pt=");
Print_Str(0,32,"F1=");
Print_Str(0,48,"P1=");
Print_Str(0,64,"F2=");
Print_Str(0,80,"P2=");
Print_Str(0,96,"F3=");
Print_Str(0,112,"P3=");
Print_Str(12,0,"F4=");
Print_Str(12,16,"P4=");
Print_Str(12,32,"F5=");
Print_Str(12,48,"P5=");
Print_Str(12,64,"F6=");
Print_Str(12,80,"P6=");
Print_Str(12,96,"F7=");
Print_Str(12,112,"P7=");
Start_Time0();
}
}
}
void display_text_1 (void)
{
float time_power=0.0;
double fft_power=0;
char combuf[11];
unsigned char i=0;
unsigned char count=0;
float h_power=0;
unsigned char time_flag=1;
double fft_freq = 0, cali_val = 1.0;
unsigned char p_val;
int i_val;
Print_Str(0,16,"Pt=");
Print_Str(0,32,"F1=");
Print_Str(0,48,"P1=");
Print_Str(0,64,"F2=");
Print_Str(0,80,"P2=");
Print_Str(0,96,"F3=");
Print_Str(0,112,"P3=");
Print_Str(12,0, "HD:");
Print_Str(12,16,"T=");
Print_Str(12,32,"F4=");
Print_Str(12,48,"P4=");
Print_Str(12,64,"F5=");
Print_Str(12,80,"P5=");
Print_Str(12,96,"F6=");
Print_Str(12,112,"P6=");
Start_Time0();
while(1)
{
Start_Time0();
ADC0IntDone=0;
while(!ADC0IntDone);
ADC0IntDone=0;
Stop_Time0();
time_power=Time_Power();
Fft();
combuf[0]='P';
combuf[1]='t';
combuf[2]='=';
sprintf(combuf+3,"%07f",time_power*1000);
combuf[10]='\0';
CmdWrite(0x10,0x4e);
Dnop(10);
Print_Str(0,16,(unsigned char *)combuf);
Print_Str(10,16," ");
//计算周期
Start_Time1();
for (Cap11_ctn=0; Cap11_ctn<4; Cap11_ctn++)
{
while (Cap_Done != 2)
{
if(time_1S)
{
time_1S=0;
Cap11_ctn=5;
time_flag=0; //非周期信号
break;
}
}
Cap_Done=0;
pos[Cap11_ctn] = Cap_Count;
if (Cap11_ctn==3)
{
if (abs(pos[1]-pos[2])<50 && abs(pos[1]-pos[3])<50)
{
fft_freq = pos[1]/3000.0;
time_flag=1;
}
else
{
time_flag=0; //非周期信号
}
}
}
Stop_Time1();
combuf[0]='T';
combuf[1]='=';
if(time_flag)
{
sprintf(combuf+2,"%06f",fft_freq);
combuf[8]='m';
combuf[9]='s';
combuf[10]='\0';
Print_Str(12,16,(unsigned char *)combuf);
}
else
{
Print_Str(12,16,"T=??? ");
}
CmdWrite(0x10,0x6e);
Sort(N/2);
for(i=0;i<6;i++)
{
fft_freq=pos[i]/2048.0*FS;
sprintf(combuf,"%07f",fft_freq);
combuf[7]='\0';
if(i<3)
Print_Str(3,(i+1)*32,(unsigned char *)combuf);
else
Print_Str(15,(i-2)*32,(unsigned char *)combuf);
fft_power=2*((fftm[pos[i]]/N)*(fftm[pos[i]]/N))/status_R;
if(i==0)
{
h_power=0;
for(count=2;count<11;count++)
{
if((count*fft_freq)<=10000)
{
h_power+=2*((fftm[pos[0]*count]/N)*(fftm[pos[0]*count]/N))/status_R;
}
}
h_power=sqrt(h_power/time_power);
CmdWrite(0x10,0x4e);
Dnop(10);
combuf[0]='H';
combuf[1]='D';
combuf[2]='=';
if(pos[0]>=500)
{
Print_Str(12,0,"HD:-------");
}
else
{
sprintf(combuf+3,"%07f",h_power);
combuf[10]='\0';
Print_Str(12,0,(unsigned char *)combuf);
}
CmdWrite(0x10,0x6e);
Dnop(10);
}
i_val = (int)fft_freq;
if (i_val>=0 && i_val<10000)
{
p_val = i_val/1000;
cali_val = (coef[p_val+1]-coef[p_val])*(fft_freq/1000.0 - p_val) + coef[p_val];
fft_power *= cali_val;
}
sprintf(combuf,"%07f",fft_power*1000);
combuf[7]='\0';
if(i<3)
{
Print_Str(3,(i+1)*32+16,(unsigned char *)combuf);
}
else
Print_Str(15,(i-2)*32+16,(unsigned char *)combuf);
}
current_time = RTCGetTime();
sprintf(combuf,"%02d:%02d:%02d",current_time.RTC_Hour,current_time.RTC_Min, current_time.RTC_Sec);
CmdWrite(0x10,0x4e);
Dnop(10);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -