基于STM32的波形发生器.docx
《基于STM32的波形发生器.docx》由会员分享,可在线阅读,更多相关《基于STM32的波形发生器.docx(16页珍藏版)》请在冰豆网上搜索。
基于STM32的波形发生器
#include"stm32f10x.h"
#include"pcf8591.h"
voidRCC_Config(void);
voidGPIO_Config(void);
externuint8_tpcf8591_read(uint8_tcmd);
voidDelay_d(__IOuint32_tt);
externboolpcf8591_write(uint8_tcmd,uint8_t*data,uint32_tlength);
voidTimeDecrement(void);
voidDelay_us(__IOuint32_tt);
externuint32_tcounter;
static__IOuint32_tTimingDelay;
voidkey_delay(__IOuint32_tt);
uint8_tkey_scan(void);
uint8_tkey_fscan(void);
uint8_tans=0;
floatvol=0;
uint8_tSine12bit[256]={0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,0x98,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,0xb0,0xb3,
0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,
0xed,0xef,0xf0,0xf2,0xf3,0xf4,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfc,0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xfe,0xfe,0xfd,0xfc,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf3,0xf2,0xf0,0xef,0xed,0xec,0xea,0xe8,
0xe6,0xe4,0xe3,0xe1,0xde,0xdc,0xda,0xd8,0xd6,0xd3,0xd1,0xce,0xcc,0xc9,0xc7,0xc4,0xc1,0xbf,0xbc,0xb9,0xb6,0xb4,0xb1,0xae,
0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x99,0x96,0x92,0x8f,0x8c,0x89,0x86,0x83,0x80,0x7d,0x79,0x76,0x73,0x70,0x6d,0x6a,0x67,0x64,
0x61,0x5e,0x5b,0x58,0x55,0x52,0x4f,0x4c,0x49,0x46,0x43,0x41,0x3e,0x3b,0x39,0x36,0x33,0x31,0x2e,0x2c,0x2a,0x27,0x25,0x23,
0x21,0x1f,0x1d,0x1b,0x19,0x17,0x15,0x14,0x12,0x10,0xf,0xd,0xc,0xb,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x3,0x2,0x1,0x1,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x1,0x1,0x2,0x3,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0xc,0xd,0xe,0x10,0x12,0x13,0x15,0x17,
0x18,0x1a,0x1c,0x1e,0x20,0x23,0x25,0x27,0x29,0x2c,0x2e,0x30,0x33,0x35,0x38,0x3b,0x3d,0x40,0x43,0x46,0x48,0x4b,0x4e,0x51,
0x54,0x57,0x5a,0x5d,0x60,0x63,0x66,0x69,0x6c,0x6f,0x73,0x76,0x79,0x7c};
uint8_tsq12bit[32]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50};//方波
uint8_ttrangle[32]={0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,
30,28,26,24,22,20,18,16,14,12,10,8,6,4,2};//三角波
uint8_txiebo[32]={0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,
34,36,38,40,42,44,46,48,50,52,54,56,58,60,62};//谐波
uint8_tkey,keyf;
uint8_ti,j;
intmain(void)
{
RCC_Config();
GPIO_Config();
SysTick_Config(72);//1us
while
(1)
{key=key_scan();
keyf=key_fscan();
switch(key%4)
{
case0:
pcf8591_write(0x40,Sine12bit,256);
break;
case1:
pcf8591_write(0x40,sq12bit,32);
break;
case2:
pcf8591_write(0x40,trangle,32);
break;
case3:
pcf8591_write(0x40,xiebo,32);
break;
default:
break;
}
switch(keyf%5)
{
case0:
if(key%4==0)
{
pcf8591_write(0x40,Sine12bit,256);
}
elseif(key%4==1)
{
pcf8591_write(0x40,sq12bit,32);
}
elseif(key%4==2)
{
pcf8591_write(0x40,trangle,32);
}
elseif(key%4==3)
{
pcf8591_write(0x40,xiebo,32);
}
break;
case1:
if(key%4==0)
{
pcf8591_write(0x40,Sine12bit,256);
}
elseif(key%4==1)
{
pcf8591_write(0x40,sq12bit,32);
}
elseif(key%4==2)
{
pcf8591_write(0x40,trangle,32);
}
elseif(key%4==3)
{
pcf8591_write(0x40,xiebo,32);
}
break;
case2:
if(key%4==0)
{
pcf8591_write(0x40,Sine12bit,256);
}
elseif(key%4==1)
{
pcf8591_write(0x40,sq12bit,32);
}
elseif(key%4==2)
{
pcf8591_write(0x40,trangle,32);
}
elseif(key%4==3)
{
pcf8591_write(0x40,xiebo,32);
}
break;
case3:
if(key%4==0)
{
pcf8591_write(0x40,Sine12bit,256);
}
elseif(key%4==1)
{
pcf8591_write(0x40,sq12bit,32);
}
elseif(key%4==2)
{
pcf8591_write(0x40,trangle,32);
}
elseif(key%4==3)
{
pcf8591_write(0x40,xiebo,32);
}
break;
case4:
if(key%4==0)
{
pcf8591_write(0x40,Sine12bit,256);
}
elseif(key%4==1)
{
pcf8591_write(0x40,sq12bit,32);
}
elseif(key%4==2)
{
pcf8591_write(0x40,trangle,32);
}
elseif(key%4==3)
{
pcf8591_write(0x40,xiebo,32);
}
break;
default:
break;
}
}
}
voidDelay_us(__IOuint32_tt)
{
TimingDelay=t;
while(TimingDelay!
=0);
}
voidTimeDecrement(void)
{
if(TimingDelay!
=0)
{
TimingDelay--;
}
}
voidRCC_Config(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
}
voidGPIO_Config(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;//SDASCL
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_2|GPIO_Pin_3;//按键
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//LED
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
}
voidDelay_d(__IOuint32_tt)
{
uint32_tT=1000;
while(T--);
while(t--);
}
voidkey_delay(__IOuint32_tt)
{
uint32_tT=90000;
while(T--);
while(t--);
}
uint8_tkey_scan(void)
{
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==0)//K1波形切换
{
key_delay(90000);//18ms
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==0)
{
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==0);
i++;
if(i>20)
{
i=0;
}
}
}
returni;
}
uint8_tkey_fscan(void)
{
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2)==0)
{//k2频率减小
key_delay(90000);
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2)==0)
{
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2)==0);
j++;
if(j>10)
{
j=10;
}
}
}
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_3)==0)
{//k3频率增大
key_delay(90000);
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_3)==0)
{
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_3)==0);
j--;
if(j==0)
{
j=0;
}
}
}
returnj;
}
#include"stm32f10x.h"
#include"pcf8591.h"
voidDelay(void);
externvoidDelay_us(__IOuint32_tt);
externuint8_tj;
#defineSDA_HGPIOC->BSRR=GPIO_Pin_0
#defineSDA_LGPIOC->BRR=GPIO_Pin_0
#defineSCL_HGPIOC->BSRR=GPIO_Pin_1
#defineSCL_LGPIOC->BRR=GPIO_Pin_1
#defineSDA_ReadGPIOC->IDR&GPIO_Pin_0
boolIIC_Start(void)
{
SDA_H;
SCL_H;
Delay();
if(!
SDA_Read)
returnfalse;
SDA_L;
Delay();
if(SDA_Read)
returnfalse;
SCL_L;
returntrue;
}
voidIIC_Stop(void)
{
SDA_L;
SCL_L;
SCL_H;
Delay();
SDA_H;
}
boolIIC_WriteByte(uint8_tbyte)
{
uint8_ti=8;
uint8_tmask=0x80;
while(i--)
{
SCL_L;
Delay();
if(mask&byte)
SDA_H;
else
SDA_L;
mask>>=1;
Delay();
SCL_H;
Delay();
}
SCL_L;
SDA_H;
Delay();
SCL_H;
Delay();
if(SDA_Read)
{
SCL_L;
returnfalse;
}
SCL_L;
returntrue;
}
uint8_tIIC_ReadByte_ACK(void)
{
uint8_tData=0;
uint8_ti=8;
SDA_H;
while(i--)
{
Data<<=1;
SCL_L;
Delay();
SCL_H;
Delay();
if(SDA_Read)
Data|=0x01;
}
SCL_L;
Delay();
SDA_L;
Delay();
SCL_H;
Delay();
SCL_L;
returnData;
}
uint8_tIIC_ReadByte_NACK(void)
{
uint8_ti=8;
uint8_tData=0;
SDA_H;
while(i--)
{
Data<<=1;
SCL_L;
Delay();
SCL_H;
Delay();
if(SDA_Read)
Data|=0x01;
}
SCL_L;
Delay();
SDA_H;
Delay();
SCL_H;
Delay();
SCL_L;
returnData;
}
uint8_tpcf8591_read(uint8_tcmd)
{
uint8_tdata=0;
if(IIC_Start()){IIC_Stop();returnfalse;}
if(IIC_WriteByte(0x90)){IIC_Stop();returnfalse;}
if(IIC_WriteByte(cmd)){IIC_Stop();returnfalse;}
if(IIC_Start()){IIC_Stop();returnfalse;}
if(IIC_WriteByte(0x91)){IIC_Stop();returnfalse;}
data=IIC_ReadByte_NACK();
IIC_Stop();
returndata;
}
boolpcf8591_write(uint8_tcmd,uint8_t*data,uint32_tlength)
{
if(IIC_Start()){IIC_Stop();returnfalse;}
if(IIC_WriteByte(0x90)){IIC_Stop();returnfalse;}
if(IIC_WriteByte(cmd)){IIC_Stop();returnfalse;}
while(length--)
{
if(IIC_WriteByte(*data)){IIC_Stop();returnfalse;}
if(j==0)
{
Delay_us
(1);
}
elseif(j==1)
{
Delay_us(20);
}
elseif(j==2)
{
Delay_us(30);
}
elseif(j==3)
{
Delay_us(40);
}
elseif(j==4)
{
Delay_us(50);
}
elseif(j==5)
{
Delay_us(60);
}
elseif(j==6)
{
Delay_us(70);
}
elseif(j==7)
{
Delay_us(80);
}
elseif(j==8)
{
Delay_us(90);
}
elseif(j==9)
{
Delay_us(100);
}
elseif(j==10)
{
Delay_us(110);
}
data++;
}
IIC_Stop();
returntrue;
}
voidDelay(void)
{
uint8_ttime=10;
while(time)
{
time--;
}
}
#include"stm32f10x.h"
externvoidTimeDecrement(void);
voidSysTick_Handler(void)
{
TimeDecrement();
}