STM32产生频率可调正弦波、锯齿波、三角波&LCD界面控制
此程序能输出两路通道,三路波形,利用STM32的DAC,TIM,LCD,和DMA控制的发射出正弦波,锯齿波和三角波,每种波形频率可调,独立输出,独立显示频率,使能。最高频率达12.5KHz。三种波形都是有一个简易小UI界面,电阻式触摸屏,能够完全手动控制通道的选择,波形的选择频率的选择以及各种波形的使能。
/******************************************************************************************** 使用手册:
0.初学即兴所敲,不喜勿喷。
1.通道1和通道2只能分开输出。
2.只有选中响应的波形选择框,才能设置频率或者使能。
3.使能端只能在通道1或者通道2打开的情况下才能被使按下操作有效。 4.每次使能一个波形,其他波形使能将自动关闭。 5.当通道1和通道2都关闭,使能将自动无效。 6.通道1和通道2切换,必须重新使能。 7.开发平台为STM32战舰版。
USER:Zhang Changhao
*********************************************************************************************/
#include \#include \#include \#include \#include \
#include \ #include \#include \#include \
#include \
u8 t,Mode1,Mode2,Mode1_Flag,Mode2_Flag;
u8 sinewave_flag,sawtooth_flag,triangle_flag,
sinewave_mode,sawtooth_mode,triangle_mode;
u8 sinewave_en,sawtooth_en,triangle_en;
u8 sinewave_fre,sawtooth_fre,triangle_fre;
u8 output_wave; u16 output_fre; #define MODE1 1 #define MODE2 2 #define sinewave 1 #define sawtooth 2 #define triangle 3
void wave_show(u8 wavemode); void en_show(u8 wavemode); void Mode_Show(u8 mode); void wave_fre(u8 wavemode); void judge_tongdao(); void judge_wave(); void judge_en(); void judge_fre(); void show_init();
void fengbi_tongdao();
int main(void) {
delay_init(); NVIC_Configuration(); uart_init(9600); LED_Init(); LCD_Init(); KEY_Init(); tp_dev.init(); show_init(); while(1) { t=TP_Scan(0); if(t) { t=0;
//延时函数初始化
//设置NVIC中断分组2:2位抢占优先级,2位响应优先级 //串口初始化为9600 //LED端口初始化
// // // printf(\ judge_tongdao(); judge_wave(); judge_en(); judge_fre(); if(sinewave_en==1||sawtooth_en==1||triangle_en==1) { if(Mode1_Flag!=DISABLE) { if(sinewave_en) { output_wave=sinewave; output_fre=sinewave_fre*100; } else if(sawtooth_en) { output_wave=sawtooth; output_fre=sawtooth_fre*100; } else if(triangle_en) { output_wave=triangle; output_fre=triangle_fre*100; } printf(\
Wave_Init((output_wave-1),output_fre,ENABLE,(output_wave-1),output_fre,DISABLE); printf(\ Set_WaveFre(0,output_fre); } else if(Mode2_Flag!=DISABLE) { if(sinewave_en) { output_wave=sinewave; output_fre=sinewave_fre*100; } else if(sawtooth_en) { output_wave=sawtooth; output_fre=sawtooth_fre*100; }
else if(triangle_en) { output_wave=triangle; output_fre=triangle_fre*100; } Wave_Init((output_wave-1),output_fre,DISABLE,(output_wave-1),output_fre,ENABLE); printf(\ } } else { Wave_Init(0,1,DISABLE,0,1,DISABLE); } } //if(t) LED1=!LED1; delay_ms(100); } }
void show_init() {
//清屏
LCD_Clear(CYAN);
POINT_COLOR=BROWN;//设置字体为红色 LCD_Fill(0,0,2,320,BROWN); LCD_Fill(0,0,240,2,BROWN) ; LCD_Fill(0,318,240,320,BROWN); LCD_Fill(238,0,240,320,BROWN); LCD_Fill(7,7,9,313,BROWN); LCD_Fill(7,7,233,9,BROWN) ; LCD_Fill(7,311,233,313,BROWN); LCD_Fill(231,7,233,313,BROWN); LCD_DrawLine(0,0,7,7);
LCD_DrawLine(233,313,240,320); LCD_DrawLine(233,7,240,0); LCD_DrawLine(7,313,0,320);
//通道1 2的初始化
POINT_COLOR=RED;//设置字体为红色 LCD_Fill(20,20,110,65,MAGENTA);
LCD_ShowString(38,35,80,16,16,\LCD_Fill(130,20,220,65,MAGENTA);
LCD_ShowString(148,35,80,16,16,\//正弦初始化
LCD_Fill(50,80,145,110,LIGHTGREEN) ;
LCD_ShowString(67,88,64,16,16,\LCD_Fill (155,80,200,110,RED) ; POINT_COLOR=GREEN; Draw_Circle(55,125,13); LCD_DrawLine(42,125,68,125); LCD_DrawLine(55,112,55,138); POINT_COLOR=BROWN; Draw_Circle(168,125,13);
LCD_DrawLine(155,125,181,125) ;
LCD_ShowString(72,120,80,16,16,\//锯齿初始化
POINT_COLOR=RED;
LCD_Fill(50,150,145,180,DARKBLUE);
LCD_ShowString(67,158,64,16,16,\LCD_Fill (155,150,200,180,RED) ; POINT_COLOR= GREEN; Draw_Circle(55,195,13);
LCD_DrawLine(42,195,68,195); LCD_DrawLine(55,182,55,208); POINT_COLOR=BROWN; Draw_Circle(168,195,13);
LCD_DrawLine(155,195,181,195);
LCD_ShowString(72,190,80,16,16,\//三角初始化
POINT_COLOR=RED;
LCD_Fill(50,220,145,250,YELLOW) ;
LCD_ShowString(63,228,64,16,16,\LCD_Fill(155,220,200,250,RED) ; POINT_COLOR= GREEN; Draw_Circle(55,265,13);
LCD_DrawLine(42,265,68,265); LCD_DrawLine(55,252,55,278); POINT_COLOR= BROWN;