基于stm32F1的示波器设计,外加三角波发生器
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "lcd.h"
- #include "usart.h"
- #include "adc.h"
- #include "dac.h"
- #include "exti.h"
- #include "stm32f10x_adc.h"
-
- u16 buf[1000];
- u16 table[50] ={0,25,50,75,100,125,150,175,200,225,250,275,300,325,350,375,400,425,450,475,500,525,550,575,600,625,650,675,700,725,750,775,800,825,850};
- u16 table1[160] ={0,5,10,15,20,30,35,40,45,55,60,65,70,80,85,
- 90,95,105,110,115,120,130,135,140,145,155,
- 160,165,170,180,185,190,195,205,210,215,220,
- 230,235,240,245,250,255,260,265,270,275,280,285,290,295,300,305,
- 310,315,320,325,330,335,340,345,350,355,360,365,370,375,
- 380,385,390,395,400,405,410,415,420,425,430,435,440,445,450,455,460,465,470,475,
- 480,485,490,495,500,505,510,515,520,525,530,535,540,545,550,555,560,565,570,575,
- 580,585,590,595,600,605,610,615,620,625,630,635,640,645,650,655,660,665,670,675,
- 680,685,690,695,700,705,710,715,720,725};
- u16 Ypos1=100,Ypos2=100,Xpos1,Xpos2;
- u16 adcx,dsl;
- float temp1;
- u16 t=75;
- float p;
- u16 h=3;
- u16 d=13;
- u32 tim=1;
- u32 bei=1;
- u16 Yinit=75;
- u8 a;
- u8 len;
- void drawgaid() //画网格
- {
- u16 x,y;
- for(x=0;x<301;x=x+25)
- for(y=0;y<201;y=y+5)
- {
- LCD_Fast_DrawPoint(x,y,BLUE );
- }
- for(y=0;y<201;y=y+25)
- for(x=0;x<301;x=x+5)
- {
- LCD_Fast_DrawPoint(x,y,BLUE );
- }
- }
- void clear_point(u16 hang)//更新显示屏当前列
- {
- float index_clear_lie = 50;
- POINT_COLOR = BLACK ;
- for(index_clear_lie = 50;index_clear_lie <426;index_clear_lie=index_clear_lie+1)
- {
- LCD_DrawPoint(hang,index_clear_lie );
- LCD_DrawPoint(hang+1,index_clear_lie );
- LCD_DrawPoint(hang+2,index_clear_lie );
- LCD_DrawPoint(hang+3,index_clear_lie );
- LCD_DrawPoint(hang+4,index_clear_lie );
- }
- if(hang==table[h])//判断hang是否为25的倍数
- {
- for(index_clear_lie = 50;index_clear_lie <426;index_clear_lie=index_clear_lie+5)
- {
- LCD_Fast_DrawPoint(hang ,index_clear_lie, GREEN );
- }
- h++;
- if(h>29) h=3;
- }
- if(hang ==table1[d])//判断hang是否为5的倍数
- {
- for(index_clear_lie = 50;index_clear_lie <426;index_clear_lie=index_clear_lie+25)//行数
- {
- LCD_Fast_DrawPoint(hang ,index_clear_lie,YELLOW );
- }
- d++;
- if(d>135) d=13;//行长度
- }
-
- POINT_COLOR=RED;
- }
- void DrawOscillogram()//画波形图
- {
- for(t=15;t<146;t++)//存储AD数值
- {
- buf[t] =Get_Adc(ADC_Channel_1);
- if(tim>1)
- delay_us(tim );//改变AD取样间隔
- }
- for(t=15;t<146;t++)
- {
- p=t*5;
- clear_point(p );
- Ypos2=Yinit+buf[t]*100/4096;//转换坐标
- Ypos2=Ypos2*bei;
- if(Ypos2 >450) Ypos2 =450; //超出范围不显示
- LCD_DrawLine (5*t ,2*Ypos1+75 ,5*(t+1),2*Ypos2+75 );
- Ypos1 =Ypos2 ;
- }
- }
- float get_vpp(void) //获取峰峰值
- {
-
- u32 max_data=buf[0];
- u32 min_data=buf[0];
- u32 n=0;
- float Vpp=0;
- for(n = 1;n<201;n++)
- {
- if(buf[n]>max_data)
- {
- max_data = buf[n];
- }
- if(buf[n]<min_data)
- {
- min_data = buf[n];
- }
- }
- Vpp = (float)(max_data-min_data);
- Vpp = Vpp*(3300.0/4095);
- return Vpp;
- }
-
- int main(void)
- {
- u16 adcx;
- float temp;
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
- uart_init(115200); //串口初始化为115200
- LED_Init(); //LED端口初始化
- LCD_Init();
- Adc_Init(); //ADC初始化
- Dac1_Init(); //DAC初始化
-
- KEY_Init ();
- POINT_COLOR=RED;
- LCD_Display_Dir(1);
- //LCD_ShowString(0,210,200,24,24,"Vpp=0000mv");
- //LCD_ShowxNum(284,220,tim ,3,16,0);
- // POINT_COLOR=RED;//设置字体为红色
- //LCD_ShowString(60,50,200,16,16,"WarShip STM32");
- //LCD_ShowString(60,70,200,16,16,"ADC TEST");
- //LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK");
- //LCD_ShowString(60,110,200,16,16,"2015/1/14");
- //显示提示信息
- //POINT_COLOR=BLUE;//设置字体为蓝色
- //LCD_ShowString(60,130,200,16,16,"ADC_CH0_VAL:");
- //LCD_ShowString(60,150,200,16,16,"ADC_CH0_VOL:0.000V");
- while(1)
- {
- adcx=Get_Adc_Average(ADC_Channel_1,10);
- //LCD_ShowxNum(156,130,adcx,4,16,0);//显示ADC的值
- temp=(float)adcx*(3.3/4096);
- adcx=temp;
- //LCD_ShowxNum(156,150,adcx,1,16,0);//显示电压值
- temp-=adcx;
- temp*=1000;
- //LCD_ShowxNum(172,150,temp,3,16,0X80);
- DrawOscillogram();//画波形
- temp1=get_vpp();//峰峰值mv
- LCD_ShowxNum(100,450,temp1,4,24,0); //显示峰峰值mv
- LED0=!LED0;//判断程序是否运行
- }
- }
复制代码
所有资料51hei提供下载:
单通道示波器+DAC1输出三角波.7z
(242.46 KB, 下载次数: 37)
|