/*------------------------------------------------------------------------------------*/
/* --- STC MCU International Limited ---------------------------------------*/
/* --- 演示STC 1T 系列单片机??A/D转换功能-----------------------------*/
/* --- Mobile: (86)13922805190 -----------------------------------------------*/
/* --- Fax: 86-755-82944243 ---------------------------------------------------*/
/* --- Tel: 86-755-82948412 ----------------------------------------------------*/
/* --- Web: www.STCMCU.com -----------------------------------------------*/
/* 如果要在程序中使用或在文章中引用该程序, -------------------- ---*/
/* 请在程序中或文章中注明使用了宏晶科技的资料及程序 -----------*/
/*-------------------------------------------------------------------------------------*/
//#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
//#include "graphics.h"
//#include "conio.h"
/*定义复数类型*/
typedef struct
{
double real;
double img;
}complex;
#include "reg51.h"
#include "intrins.h"
#define FOSC 18432000L
#define BAUD 9600
typedef unsigned char BYTE;
typedef unsigned int WORD;
/*Declare SFR associated with the ADC */ //锁定地址
sfr ADC_CONTR = 0xBC; //ADC control register
sfr ADC_RES = 0xBD; //ADC hight 8-bit result register
sfr ADC_RESL = 0xBE; //ADC low 2-bit result register
sfr P1ASF = 0x9D; //P1 secondary function control register
/*Define ADC operation const for ADC_CONTR*/ //设定参数
#define ADC_POWER 0x80 //ADC power control bit
#define ADC_FLAG 0x10 //ADC complete flag
#define ADC_START 0x08 //ADC start control bit
#define ADC_SPEEDLL 0x00 //540 clocks
#define ADC_SPEEDL 0x20 //360 clocks
#define ADC_SPEEDH 0x40 //180 clocks
#define ADC_SPEEDHH 0x60 //90 clocks
#define N 8
#define uint unsigned int
#define uchar unsigned char
#define LcdBus P2
//#define columnBase 0x40/*列基址:列地址是横向的,从0-127,共128列 */
//#define pageBase 0xB8/*页基址:页是纵向的,每个汉字分两页,每页16行8列点。*/
/*锁定管脚*/
sbit CS1 = P3^3;
sbit CS2 = P3^4;
sbit EP = P3^0;
sbit RS = P3^2;
sbit RW = P3^1;
sbit RST = P3^5;
//#include<reg51.h>
double Fn=0,An=0,Qn=0;
BYTE Fn_char[8], An_char[8],Qn_char[8];
/*函数声明*/
void InitUart();
void SendData(BYTE dat);
void delay(WORD n);
//void adc_isr(); interrupt 5 using 1
void InitADC();
void fft(); /*快速傅里叶变换*/
void initW(); /*初始化旋转因子*/
void change(); /*变址*/
void add(complex ,complex ,complex *); /*复数加法*/
void mul(complex ,complex ,complex *); /*复数乘法*/
void sub(complex ,complex ,complex *); /*复数减法*/
void divi(complex ,complex ,complex *); /*复数除法*/
void output();
void yunsuan(void);
void delay_50ms(WORD n);
void delay_50us(uint i);
complex x[N]; /*输入序列,旋转因子*/
complex *W;
int FS= 1852; //采样频率,跟采样速度有关
double PI;
BYTE ch = 0; //ADC channel NO.
int adc_count=0; //adc转换次数统计
void mainadc()
{
InitUart(); //Init UART, use to show ADC result
InitADC(); //Init ADC sfr
IE = 0xa0; //Enable ADC interrupt and Open master interrupt switch
//adc_isr(); //Start A/D conversion
}
/*----------------------------ADC interrupt service routine----------------------------*/
void adc_isr() interrupt 5 using 1 //使用5中断,第一工作区
{
double AD_val;
ADC_CONTR &= !ADC_FLAG; //Clear ADC interrupt flag
AD_val= ((ADC_RES*4+ADC_RESL)*5)/1024;
x[adc_count++].real=AD_val;
x[adc_count].img=0;
//SendData(ch); //Show Channel NO.
//SendData(ADC_RES); //Get ADC high 8-bit result and Send to UART
//if you want show 10-bit result, uncomment next line
//SendData(ADC_LOW2); //Show ADC low 2-bit result
//if (++ch > 7) ch = 0; //switch to next channel
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ch;
if (adc_count>=N)
return;
}
/*----------------------------Initial ADC sfr----------------------------*/
void InitADC( )
{
P1ASF = 0x01; //Set all P1 as analog input port
ADC_RES = 0; //Clear previous result
ADC_RESL= 0;
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ch;
delay_50us(2); //ADC power-on delay and Start A/D conversion
}
/*----------------------------Initial UART----------------------------*/
/*void InitUart()
{
SCON = 0x5a; //8 bit data ,no parity bit
TMOD = 0x20; //T1 as 8-bit auto reload
TH1 = TL1 = -(FOSC/12/32/BAUD); //Set Uart baudrate
TR1 = 1; //T1 start running
}*/
/*----------------------------Send one byte data to PCInput: dat (UART data)Output:-----------------------------*/
/*void SendData(BYTE dat)
{
while (!TI); //Wait for the previous data is sent
TI = 0; //Clear TI flag
SBUF = dat; //Send current data
}*/
/*----------------------------Software delay function----------------------------*/
void delay_50ms(WORD n)
{
WORD x;
while (n--)
{
x = 6245;
while (x--);
}
}
/********************************************************************
正弦函数快速傅立叶变换C程序
函数简介: 输入正弦函数的频率FC,然后由采样定理进行采样,采样频率为FS,采样后进行M点快速傅立叶变换,最后输出结果并画图。
使用说明:FS>=2FC,M为2的整数次幂。
********************************************************************/
int mainfft()
{
//int i;
PI=atan(1)*4;
/*int gd=DETECT,gm;
system("cls");
printf("输入正弦频率FC:\n");
scanf("%d",&FC);
printf("输入采样频率FS(FS>=FC):\n");
scanf("%d",&FS);
printf("输入采样点数M(2的整数次幂):\n");
scanf("%d",&M);
for(i=0;i<M;i++)
{
x[i].real=sin(2*PI*FC*i/FS);
x[i].img=0;
}
*/
initW();
fft();
yunsuan();
/*output();
for(i=0;i<N;i++)
{
x[i].real=sqrt(x[i].real*x[i].real+x[i].img*x[i].img);
}*/
/*initgraph(&gd,&gm,"");
cleardevice();
setcolor(GREEN);
line(300,400,300,50);
line(300,50,295,55);
line(300,50,305,55);
line(40,400,600,400);
line(600,400,595,395);
line(600,400,595,405);
setcolor(WHITE);
for(i=0;i<M;i++)
line(i*5+300-M*5/2,400,i*5+300-M*5/2,400-(int)x[i].real*5);
getch();
closegraph();*/
return 0;
}
void yunsuan(void)
{
int i=0,max=0;
double fn=0,an=0,qn=0,temp;
for(i=0;i<=N;i++)
{
temp=sqrt(x[i].real*x[i].real+x[i].img*x[i].img);
if(temp>=an)
{
an=temp;
max=i;
}
}
An=an/(N/2);
Fn=(max-1)*FS/N;
Qn=180*atan(x[max].img/x[max].real)/PI;
sprintf(An_char,"%8.2f",An);
sprintf(Fn_char,"%8.2f",Fn);
sprintf(Qn_char,"%8.2f",Qn);
}
/*快速傅里叶变换*/
void fft()
{
int i=0,j=0,k=0,l=0;
complex up,down,product;
change();
for(i=0;i< log(N)/log(2) ;i++)
{
l=1<<i;
for(j=0;j<N;j+= 2*l )
{
for(k=0;k<l;k++)
{
mul(x[j+k+l],W[N*k/2/l],&product);
add(x[j+k],product,&up);
sub(x[j+k],product,&down);
x[j+k]=up;
x[j+k+l]=down;
}
}
}
}
/*初始化旋转因子*/
void initW()
{
int i;
W=(complex *)malloc(sizeof(complex) * N);
for(i=0;i<N;i++)
{
ppy.rar_12864 频谱_51 fft_FFT频谱显示_fft_fft 51
版权申诉
5星 · 超过95%的资源 59 浏览量
2022-09-19
21:17:48
上传
评论
收藏 4KB RAR 举报
朱moyimi
- 粉丝: 64
- 资源: 1万+
最新资源
- 中医临床诊疗术语-证候同义词典
- NovAtelConvert-Setup
- MySql主备数据库配置
- BGP路由基本配置(可以互通)
- 《软件方法2024版》公开内容202405更新-epub版
- 适用于tensorflow-2.11.0 CUDA版本11.2的cuDNN8.1版本
- 5Y study学习平台2016计算机基础-综合测试(8)_哔哩哔哩_bilibili_2580252704.mp4
- (大赛作品)STM32实现的F072RB NUCLEO智能家居控制.zip
- STM32实现的数字示波器源码+数字信号处理教程、配套实例.zip
- 【cookie续续】【cookie续续】【cookie续续】
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈