标题:
使用0.96OLED与STC单片机的音乐频谱程序
[打印本页]
作者:
红石渣工作室
时间:
2019-6-23 13:42
标题:
使用0.96OLED与STC单片机的音乐频谱程序
自己写的一个项目,基于FFT算法实现的音乐频谱,利用OLED实现显示,需要在32M晶振运行
单片机源程序如下:
#include"OLED.H"
sbit sb0=P3^6;//定义按钮位置
sbit sb1=P3^5;
idata uchar fft_shuzu[2][32];
uint s_time;
struct compx //定义复数结构体
{
float real;
float imag;
};
xdata struct compx s[ 64 ]; //FFT数据缓存放在XDATA空间
struct compx EE(struct compx,struct compx); //复数乘法函数的声明
void FFT(struct compx xin[],int N); //FFT函数的声明
struct compx EE(struct compx a1,struct compx b2) //复数乘法
{
struct compx b3;
b3.real=a1.real*b2.real-a1.imag*b2.imag;
b3.imag=a1.real*b2.imag+a1.imag*b2.real;
return(b3);
}
/*FFT函数*/
void FFT(struct compx xin[],int N)
{
int f,m,nv2,nm1,i,k,j=1,l;
struct compx v,w,t;
nv2=N/2;
f=N;
for(m=1;(f=f/2)!=1;m++){;}
nm1=N-1;
for(i=0;i<nm1;i++) //倒序操作
{
if(i<j)
{
t=xin[j];
xin[j]=xin[i];
xin[i]=t;
}
k=nv2; //k为倒序中相应位置的权值
while(k<j)
{
j=j-k;
k=k/2;
}
j=j+k;
}
{
int le,lei,ip;
float pi;
for(l=1;l<=m;l++)
{
le=pow(2,l); //乘方
lei=le/2;
pi=3.14159265;
v.real=1.0;
v.imag=0.0;
w.real=cos(pi/lei); //旋转因子
w.imag=-sin(pi/lei);
for(j=1;j<=lei;j++) //控制蝶形运算的级数
{
for(i=j-1;i<N;i=i+le) //控制每级蝶形运算的次数
{
ip=i+lei;
t=EE(xin[ ip ],v);
xin[ ip ].real=xin[ i ].real-t.real; //蝶形计算
xin[ ip ].imag=xin[ i ].imag-t.imag;
xin[ i ].real=xin[ i ].real+t.real;
xin[ i ].imag=xin[ i ].imag+t.imag;
}
v=EE(v,w);
}
}
}
}
void showbar0()//延时跳帽函数
{
unsigned char i,j,x,p,high,tigh,temp,itemp;
xdata unsigned char dis_rdata[16];
for(i=0;i<16;i++) //读取FFT转换数据
{
float t0=0;
t0=sqrt(pow((s[i ].real+s[i+1].real),2)+pow((s[i ].imag+s[i+1].imag),2))/2;
dis_rdata[i]=(unsigned char)t0;
}
// OLED_Display_On();
for(i=0;i<16;i++)//16个条形柱控制
{
itemp=high=0;
itemp=high=dis_rdata[i];//对处理变量赋值
// high=high/1;
for(x=0;x<8;x++)//清空当前条形柱空间
{
OLED_Set_Pos(i*8,7-x);
temp=0x00;
for(j=0;j<8;j++)
{
OLED_WR_Byte(temp,1);
}
}
if(high>64)high=64;//溢出值限制
if(fft_shuzu[0][i]<=high)//检测当前大小
{
fft_shuzu[0][i]=high;//大于赋值当前频谱帽为此值
fft_shuzu[1][i]=10;//重新设置当前延时
}
fft_shuzu[1][i]--;//当前延时自减
if(fft_shuzu[1][i]==0)//如果当前延时为0
{
fft_shuzu[0][i]=high;//强度等于当前大小
}
p=high/8; //扫描图形生成
tigh=high%8;
for(x=0;x<p;x++)//填满单元区域
{
OLED_Set_Pos(i*8,7-x);
temp=0xff;
for(j=0;j<7;j++)
{
OLED_WR_Byte(temp,1);
}
}
p=fft_shuzu[0][i]/8;//生成延时帽所在位置函数
tigh=fft_shuzu[0][i]%8;
if((itemp/8)==p)//如果与所在位置重合
{
OLED_Set_Pos(i*8,7-p);//到达所在位置
temp=0x80>>tigh;//生成延时帽
temp=temp|~0xff>>(high%8);//合并当前位置
for(j=0;j<7;j++)//发送屏幕
{
OLED_WR_Byte(temp,1);
}
}
else//如果不重合
{
OLED_Set_Pos(i*8,7-p);//到达生成位置
temp=0x80>>tigh;//生成延时帽
for(j=0;j<7;j++)//发送屏幕
{
OLED_WR_Byte(temp,1);
}
OLED_Set_Pos(i*8,7-(high/8));//到达非满数单元
temp=~0xff>>(itemp%8);//生成条形柱
for(j=0;j<7;j++)//发送
{
OLED_WR_Byte(temp,1);
}
}
}
}
/*----以下生成函数原理差不多----*/
void showbar1()//条形柱函数
{
unsigned char i,j,x,p,high,tigh,temp;
xdata unsigned char dis_rdata[16];
for(i=0;i<16;i++) //读取FFT转换数据
{
float t0=0;
t0=sqrt(pow((s[i ].real+s[i+1].real),2)+pow((s[i ].imag+s[i+1].imag),2))/2;
dis_rdata[i]=(unsigned char)t0;
}
// OLED_Display_On();
for(i=0;i<16;i++)
{
high=0;
high=dis_rdata[i];
// high=high/1;
for(x=0;x<8;x++)
{
OLED_Set_Pos(i*8,7-x);
temp=0x00;
for(j=0;j<8;j++)
{
OLED_WR_Byte(temp,1);
}
}
if(high>64)high=64;
if(high==0)high=1;
p=high/8;
tigh=high%8;
for(x=0;x<p;x++)
{
OLED_Set_Pos(i*8,7-x);
temp=0xff;
for(j=0;j<7;j++)
{
OLED_WR_Byte(temp,1);
}
}
OLED_Set_Pos(i*8,7-(high/8));
temp=~0xff>>(high%8);
for(j=0;j<7;j++)
{
OLED_WR_Byte(temp,1);
}
}
}
void showbar2()//等号指示函数
{
unsigned char i,j,x,p,high,tigh,temp,itemp;
xdata unsigned char dis_rdata[16];
for(i=0;i<16;i++) //读取FFT转换数据
{
float t0=0;
t0=sqrt(pow((s[i ].real+s[i+1].real),2)+pow((s[i ].imag+s[i+1].imag),2))/2;
dis_rdata[i]=(unsigned char)t0;
}
// OLED_Display_On();
for(i=0;i<16;i++)
{
itemp=high=0;
itemp=high=dis_rdata[i];
// high=high/1;
for(x=0;x<8;x++)
{
OLED_Set_Pos(i*8,7-x);
temp=0x00;
for(j=0;j<8;j++)
{
OLED_WR_Byte(temp,1);
}
}
if(high>64)high=64;
p=high/8;
tigh=high%8;
if(p==0)
{
p=1;
temp=0x80;
}
else temp=0x88;
for(x=0;x<p;x++)
{
OLED_Set_Pos(i*8,7-x);
for(j=0;j<7;j++)
{
OLED_WR_Byte(temp,1);
}
}
}
}
void showbar3()//延时跳帽函数取反
{
unsigned char i,j,x,p,high,tigh,temp,itemp;
xdata unsigned char dis_rdata[16];
for(i=0;i<16;i++) //读取FFT转换数据
{
float t0=0;
t0=sqrt(pow((s[i ].real+s[i+1].real),2)+pow((s[i ].imag+s[i+1].imag),2))/2;
dis_rdata[i]=(unsigned char)t0;
}
// OLED_Display_On();
for(i=0;i<16;i++)
{
itemp=high=0;
itemp=high=dis_rdata[i];
// high=high/1;
for(x=0;x<8;x++)
{
OLED_Set_Pos(i*8,7-x);
temp=0xFF;
for(j=0;j<8;j++)
{
OLED_WR_Byte(temp,1);
}
}
if(high>64)high=64;
if(fft_shuzu[0][i]<=high)
{
fft_shuzu[0][i]=high;
fft_shuzu[1][i]=10;
}
fft_shuzu[1][i]--;
if(fft_shuzu[1][i]==0)
{
fft_shuzu[0][i]=high;
}
p=high/8;
tigh=high%8;
for(x=0;x<p;x++)
{
OLED_Set_Pos(i*8,7-x);
temp=0x00;
for(j=0;j<7;j++)
{
OLED_WR_Byte(temp,1);
}
}
p=fft_shuzu[0][i]/8;
tigh=fft_shuzu[0][i]%8;
if((itemp/8)==p)
{
OLED_Set_Pos(i*8,7-p);
temp=~0x80>>tigh;
temp=temp|0xff>>(high%8);
for(j=0;j<7;j++)
{
OLED_WR_Byte(temp,1);
}
}
else
{
OLED_Set_Pos(i*8,7-p);
temp=~0x80>>tigh;
for(j=0;j<7;j++)
{
OLED_WR_Byte(temp,1);
}
OLED_Set_Pos(i*8,7-(high/8));
temp=0xff>>(itemp%8);
for(j=0;j<7;j++)
{
OLED_WR_Byte(temp,1);
}
}
}
}
void showbar4()//条形柱取反函数
{
unsigned char i,j,x,p,high,tigh,temp;
xdata unsigned char dis_rdata[16];
for(i=0;i<16;i++) //读取FFT转换数据
{
float t0=0;
t0=sqrt(pow((s[i ].real+s[i+1].real),2)+pow((s[i ].imag+s[i+1].imag),2))/2;
dis_rdata[i]=(unsigned char)t0;
}
// OLED_Display_On();
for(i=0;i<16;i++)
{
high=0;
high=dis_rdata[i];
// high=high/1;
for(x=0;x<8;x++)
{
OLED_Set_Pos(i*8,7-x);
temp=0xff;
for(j=0;j<8;j++)
{
OLED_WR_Byte(temp,1);
}
}
if(high>64)high=64;
if(high==0)high=1;
p=high/8;
tigh=high%8;
for(x=0;x<p;x++)
{
OLED_Set_Pos(i*8,7-x);
temp=0x00;
for(j=0;j<7;j++)
{
OLED_WR_Byte(temp,1);
}
}
OLED_Set_Pos(i*8,7-(high/8));
temp=0xff>>(high%8);
for(j=0;j<7;j++)
{
OLED_WR_Byte(temp,1);
}
}
}
void showbar5()//等号指示函数
{
unsigned char i,j,x,p,high,tigh,temp,itemp;
xdata unsigned char dis_rdata[16];
for(i=0;i<16;i++) //读取FFT转换数据
{
float t0=0;
t0=sqrt(pow((s[i ].real+s[i+1].real),2)+pow((s[i ].imag+s[i+1].imag),2))/2;
dis_rdata[i]=(unsigned char)t0;
}
// OLED_Display_On();
for(i=0;i<16;i++)
{
itemp=high=0;
itemp=high=dis_rdata[i];
// high=high/1;
for(x=0;x<8;x++)
{
OLED_Set_Pos(i*8,7-x);
temp=0xff;
for(j=0;j<8;j++)
{
OLED_WR_Byte(temp,1);
}
}
if(high>64)high=64;
p=high/8;
tigh=high%8;
if(p==0)
{
p=1;
temp=~0x80;
}
else temp=~0x88;
for(x=0;x<p;x++)
{
OLED_Set_Pos(i*8,7-x);
for(j=0;j<7;j++)
{
OLED_WR_Byte(temp,1);
}
}
}
}
/*主函数*/
void main()
{
int N=64,i,k; //变量初始化,64点FFT运算
float offset;
s_time=0;
TMOD=0X01; //定时器开到模式1
TH0=(65535-10000)/256; //设置延时时间
TL0=(65535-10000)%256;
TR0=1; //启动定时器0
ET0=1; //开启定时器0中断
EA=1; //开启中断
oled_init(); //OLED
OLED_Clear();
P1ASF=0x01; //P10口做AD 使用
P1M0 = 0x01; //0000,0001用于A/D转换的P1.x口,先设为开漏
P1M1 = 0x01; //0000,0001 P1.0先设为开漏。断开内部上拉电阻
ADC_CONTR=0xC8; //40.96K采样率
while(!(ADC_CONTR&0x10));
offset=((float)ADC_RES*4+(float)(ADC_RESL%0x04)); //AD结果高8位左移2位,低2位不变,然后相加
while(1)
{ if(P3==(P3&0xFE))IAP_CONTR=0x60;
for(i=0;i<N;i++) //采集音频信号
{
ADC_CONTR=0xC8; //40.96K采样率
while(!(ADC_CONTR&0x10));
s[i].real=((float)ADC_RES*4+(float)(ADC_RESL%0x04)-offset);//((((int)ADC_DATA-128)/2))*4;
s[i].imag=0;
}
FFT(s,N); //调用FFT函数进行变换
EA=0;
if(sb0==0) //模式调节按钮控制
{
delay(100);
if(sb0==0)
{
while(!sb0);
delay(100);
k+=1;
if(k==6)k=0;
}
}
if(sb1==0) //延时调节按钮控制
{
delay(100);
if(sb1==0)
{
while(!sb1);
delay(100);
s_time+=100;
if(s_time==1100)s_time=0;
}
}
switch(k) //显示频谱
{
case 0:showbar0();break; //延时跳帽函数
case 1:showbar1();break; //条形柱函数
case 2:showbar2();break; //等号条函数
case 3:showbar3();break; //延时跳帽函数取反
case 4:showbar4();break; //条形柱函数取反
case 5:showbar5();break; //等号条函数取反
}
EA=1;
delay(s_time);//显示延时函数
}
}
void yp_l()interrupt 1
{
TH0=(65535-10000)/256; //设置延时时间
TL0=(65535-10000)%256;
}
复制代码
所有程序51hei提供下载:
OLED FFT.rar
(72.2 KB, 下载次数: 152)
2019-6-23 13:41 上传
点击文件名下载附件
源码
下载积分: 黑币 -5
作者:
TOMMY_liu
时间:
2019-10-30 22:14
有原理图吗
作者:
qjy822
时间:
2021-9-20 14:31
感谢分享,正在学习这方面的知识!
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1