902| 8
|
简易示波器STM32 |
[/free][hide=3,money]
#include "button.h" void button_init() { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE, ENABLE);//ʹÄÜPB\E¶Ë¿ÚʱÖÓ GPIO_InitStructure.GPIO_Pin = k_up; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //ÏÂÀÊäÈë GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_ResetBits(GPIOA,GPIO_Pin_0); GPIO_InitStructure.GPIO_Pin = k_left|k_down|k_right; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉÏÀÊäÈë GPIO_Init(GPIOE, &GPIO_InitStructure); } u8 keyscan(u8 mode) //mode=0 µ¥´Î°´£¬mode=1Á¬Ðø°´ { static u8 key_up=1; if(mode) { key_up=1; } if(key_up&&(K_UP==1||K_LEFT==0||K_DOWN==0||K_RIGHT==0)) { delay_ms(10); key_up=0; if(K_UP==1) { return K_UP_PRESS; } else if(K_LEFT==0) { return K_LEFT_PRESS; } else if(K_DOWN==0) { return K_DOWN_PRESS; } else if(K_RIGHT==0) { return K_RIGHT_PRESS; } } else if(K_UP==0&&K_LEFT==1&&K_DOWN==1&&K_RIGHT==1) { key_up=1; } return 0; } #ifndef _button_H #define _button_H #include "system.h" #include "SysTick.h" #define k_up GPIO_Pin_0 //PA0 #define k_left GPIO_Pin_2 //PE2 #define k_down GPIO_Pin_3 //PE3 #define k_right GPIO_Pin_4 //PE4 #define K_UP GPIO_ReadInputDataBit(GPIOA,k_up) #define K_LEFT GPIO_ReadInputDataBit(GPIOE,k_left) #define K_DOWN GPIO_ReadInputDataBit(GPIOE,k_down) #define K_RIGHT GPIO_ReadInputDataBit(GPIOE,k_right) #define K_UP_PRESS 1 #define K_LEFT_PRESS 2 #define K_DOWN_PRESS 3 #define K_RIGHT_PRESS 4 void button_init(void); u8 keyscan(u8 mode); #endif #include "tim.h" u32 count = 0; u32 frequency = 0; void time2_init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitTypeStruct; TIM_TimeBaseInitTypeStruct.TIM_Prescaler = 7199; //tim2 72000000 10us TIM_TimeBaseInitTypeStruct.TIM_CounterMode = TIM_CounterMode_Up;//Ôö¼ÆÊý TIM_TimeBaseInitTypeStruct.TIM_Period = 9999+1; //10000 100ms TIM_TimeBaseInitTypeStruct.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitTypeStruct); TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //TIM2³õʼ»¯ TIM_Cmd(TIM2,DISABLE); } void time3_init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitTypeStruct; //ÅäÖà TIMx ÍⲿʱÖÓģʽ 2 TIM_ETRClockMode2Config(TIM3,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0); TIM_TimeBaseInitTypeStruct.TIM_Prescaler = 0; TIM_TimeBaseInitTypeStruct.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitTypeStruct.TIM_Period = 65535; TIM_TimeBaseInitTypeStruct.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitTypeStruct); TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); TIM_Cmd(TIM3,DISABLE); //ʧÄÜ } void time1_init(void) { //ÓÃÀ´´¥·¢adת»»£¬TIM1´¥·¢ADC TIM_TimeBaseInitTypeDef TIM_TimeBaseInitTypeStruct; TIM_OCInitTypeDef TIM_OCInitTypeStruct; TIM_TimeBaseInitTypeStruct.TIM_Prescaler = 71; //72·ÖƵ TIM_TimeBaseInitTypeStruct.TIM_CounterMode = TIM_CounterMode_Up; //Ôö¼ÆÊý TIM_TimeBaseInitTypeStruct.TIM_Period = 1; //×Ô¶¯ÖØ×°Öµ£¬1us´¥·¢Ò»´Î TIM_TimeBaseInitTypeStruct.TIM_ClockDivision = TIM_CKD_DIV1;//ʱÖÓ²»·ÖƵ TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitTypeStruct); //TIM1³õʼ»¯ TIM_Cmd(TIM1,DISABLE); //TIM1²»Ê¹ÄÜ TIM_OCInitTypeStruct.TIM_OCMode = TIM_OCMode_PWM1; //Õâ¸öÒ²ÊDZØÐëÅäÖóÉpwmģʽ TIM_OCInitTypeStruct.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitTypeStruct.TIM_Pulse = 1; TIM_OCInitTypeStruct.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM1, &TIM_OCInitTypeStruct); TIM_CtrlPWMOutputs(TIM1, ENABLE); //Õâ¸öÊDZØÐëÓÐµÄ } void time_init(void) { time2_init(); time3_init(); time1_init(); } void time_enable(void) { TIM_Cmd(TIM2,ENABLE); TIM_Cmd(TIM3,ENABLE); } #ifndef _tim_H #define _tim_H #include "system.h" #include "SysTick.h" extern u32 count; extern u32 frequency; void time_init(void); void time_enable(void); void time1_init(void); #endif #include "adc.h" #include "math.h" #include "tim.h" #include "stm32f10x_it.h" #define ADC1_DR_Address ((u32)0x4001244C) u16 a[640]; u16 index = 0; u16 index1 = 0; void DMA1_Init(void) { DMA_InitTypeDef DMA_InitTypeStruct; DMA_DeInit(DMA1_Channel1); //ADC1µÄDMAͨµÀ DMA_InitTypeStruct.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitTypeStruct.DMA_MemoryBaseAddr = (u32)a; DMA_InitTypeStruct.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitTypeStruct.DMA_BufferSize = 640; DMA_InitTypeStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitTypeStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitTypeStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitTypeStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitTypeStruct.DMA_Mode = DMA_Mode_Normal; DMA_InitTypeStruct.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitTypeStruct.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1,&DMA_InitTypeStruct); //DMA³õʼ»¯£¬ADC½á¹û¼Ä´æÆ÷´æÈëSRAMÖÐ DMA_Cmd(DMA1_Channel1,ENABLE); //DMAʹÄÜ } void AD_Init(void) { ADC_InitTypeDef ADC_InitTypeStruct; ADC_DeInit(ADC1); //¸´Î»ADC1 ADC_InitTypeStruct.ADC_Mode = ADC_Mode_Independent;//ADC¹¤×÷ÔÚ¶ÀÁ¢Ä£Ê½ ADC_InitTypeStruct.ADC_ScanConvMode = DISABLE; //µ¥Í¨µÀģʽ ADC_InitTypeStruct.ADC_ContinuousConvMode = DISABLE; //µ¥´Îģʽ ADC_InitTypeStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;//Ñ¡Ôñ¶¨Ê±Æ÷1À´´¥·¢ ADC_InitTypeStruct.ADC_DataAlign = ADC_DataAlign_Right; //Êý¾ÝÓÒ¶ÔÆë ADC_InitTypeStruct.ADC_NbrOfChannel = 1; //ͨµÀÊýĿΪ1 ADC_Init(ADC1, &ADC_InitTypeStruct); //ADC³õʼ»¯ ADC_ExternalTrigConvCmd(ADC1,ENABLE); //ADCÍⲿ´¥·¢Ê¹ÄÜ ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_1Cycles5);//ÅäÖÃADC1£¬Í¨µÀ1µÈ } void ADC1_Init(void) //adc³õʼ»¯ { DMA1_Init(); AD_Init(); ADC_DMACmd(ADC1, ENABLE); //ADC DMAʹÄÜ ADC_Cmd(ADC1, ENABLE); //ADCʹÄÜ ADC_ResetCalibration(ADC1); //ADCУ׼¸´Î» while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); //ADC¿ªÊ¼Ð£×¼ while(ADC_GetCalibrationStatus(ADC1)); } void ADC_Get_Value(void) //µÃµ½Êý¾Ý£¬ { float gao_pin_period = 0; DMA1_Init(); TIM_SetCounter(TIM1,0); //Çå¿Õ¼Ä´æÆ÷ if(num_shao_miao>7) { TIM_PrescalerConfig(TIM1,71,TIM_PSCReloadMode_Immediate); TIM_SetCompare1(TIM1, (shao_miao_shu_du/25)-1); TIM_SetAutoreload(TIM1, (shao_miao_shu_du/25)-1); //É趨ɨÃèËÙ¶È } else { TIM_PrescalerConfig(TIM1,0,TIM_PSCReloadMode_Immediate); gao_pin_period = 288000000.0/frequency +gao_pin_palus-1; TIM_SetCompare1(TIM1, gao_pin_period); TIM_SetAutoreload(TIM1,gao_pin_period); } TIM_Cmd(TIM1,ENABLE); while(!DMA_GetFlagStatus(DMA1_FLAG_TC1)); DMA_ClearFlag(DMA1_FLAG_TC1); if(DMA_GetFlagStatus(DMA1_FLAG_TE1)) { DMA_ClearFlag(DMA1_FLAG_TE1); ADC_Get_Value(); } TIM_Cmd(TIM1,DISABLE); } u16 ADC_Get_Vpp(void) { u32 max_data=a[0]; u32 min_data=a[0]; u32 n=0; float pp=0; for(n = 1;n<320;n++) { if(a[n]>max_data) { max_data = a[n]; } if(a[n]<min_data) { min_data = a[n]; index = n; } } pp = (float)(max_data-min_data); pp = pp*(3300.0/4095); return pp; } #ifndef _adc_H #define _adc_H #include "system.h" #include "SysTick.h" extern u16 a[640]; extern u16 index; extern u16 index1; void ADC1_Init(void); void ADC_Get_Value(void); u16 ADC_Get_Vpp(void); #endif #include "system.h" #include "SysTick.h" #include "usart.h" #include "led.h" #include "button.h" #include "tftlcd.h" #include "gui.h" #include "tim.h" #include "stm32f10x_it.h" #include "adc.h" u16 j = 0; float temp; float temp1; void clear_point(u16 hang) { u8 index_clear_lie = 0; FRONT_COLOR = BLUE; for(index_clear_lie = 0;index_clear_lie <201;index_clear_lie++) { lcd_huadian(hang,index_clear_lie,FRONT_COLOR); } FRONT_COLOR=RED; } void nvic_init(void) { NVIC_InitTypeDef NVIC_InitTypeStruct; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitTypeStruct.NVIC_IRQChannel = EXTI0_IRQn; NVIC_InitTypeStruct.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitTypeStruct.NVIC_IRQChannelSubPriority = 0; NVIC_InitTypeStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitTypeStruct); NVIC_InitTypeStruct.NVIC_IRQChannel = EXTI3_IRQn; NVIC_InitTypeStruct.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitTypeStruct.NVIC_IRQChannelSubPriority = 1; NVIC_InitTypeStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitTypeStruct); NVIC_InitTypeStruct.NVIC_IRQChannel = EXTI4_IRQn; NVIC_InitTypeStruct.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitTypeStruct.NVIC_IRQChannelSubPriority = 2; NVIC_InitTypeStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitTypeStruct); NVIC_InitTypeStruct.NVIC_IRQChannel = TIM2_IRQn; //ÅäÖÃÖжÏÓÅÏȼ¶ NVIC_InitTypeStruct.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitTypeStruct.NVIC_IRQChannelSubPriority = 3; NVIC_InitTypeStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitTypeStruct); NVIC_InitTypeStruct.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitTypeStruct.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitTypeStruct.NVIC_IRQChannelSubPriority = 3; NVIC_InitTypeStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitTypeStruct); } void rcc_init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE|RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_ADCCLKConfig(RCC_PCLK2_Div6); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE); } void gpio_init(void) { GPIO_InitTypeDef GPIO_InitTypeStruct; GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_2; GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_50MHz; //ÍⲿʱÖӵģ¬ÓÃÀ´²âƵÂʵģ¬ GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_InitTypeStruct); GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_2; GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_50MHz; //adcÊäÈëÒý½Å GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitTypeStruct); GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4; GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_50MHz; //ÍⲿÖжϵÄioÅäÖà GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOE, &GPIO_InitTypeStruct); GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_0; GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_50MHz; //ÍⲿÖжϵÄioÅäÖà GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_IPD; GPIO_Init(GPIOA, &GPIO_InitTypeStruct); GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_8; //¶¨Ê±Æ÷1´¥·¢adת»»µÄÊä³öµÄÄǸö¿Ú GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitTypeStruct); GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_3; GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_50MHz; // adc3 GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitTypeStruct); GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_4; GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_50MHz; // adc4 GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitTypeStruct); GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_5; GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_50MHz; // adc5 GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitTypeStruct); GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_6; GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_50MHz; // adc6 GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitTypeStruct); GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_7; GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_50MHz; // adc7 GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitTypeStruct); } int main(void) { u8 vpp_buf[6]; SysTick_Init(72); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÖжÏÓÅÏȼ¶·Ö×é ·Ö2×é USART1_Init(9600); //³õʼ»¯´®¿Ú²¨ÌØÂÊΪ115200 rcc_init(); //ÍâÉèʱÖÓÅäÖà led_init(); TFTLCD_Init(); LCD_Clear(BLUE); nvic_init(); // ÖжÏÓÅÏȼ¶ÅäÖà gpio_init(); //ÍâÉèio¿ÚÅäÖà set_io0(); key_init(); ADC1_Init(); //adcÅäÖà set_background(); //³õʼ»¯±³¾° time_init(); //¶¨Ê±Æ÷ÅäÖ㬲âƵÂÊÓõĶþ¸ö¶¨Ê±Æ÷ time_enable(); //ͬ²½¿ªÊ¼¼ÆÊý ADC_Get_Value(); vpp = ADC_Get_Vpp(); while(1) { for(j=index;j<index+250;j++) { temp = a[j] * 3300 / 4096 * 25 /vcc_div; temp1 = a[j + 1] * 3300 / 4096 * 25 / vcc_div; clear_point(j-index); if(temp>200) { temp=200; } if(temp<0) { temp=0; } if(temp1>200) { temp1=200; } if(temp1<0) { temp1=0; } lcd_huadian(j-index,temp,FRONT_COLOR); lcd_huaxian(j-index,temp,j-index+1,temp1,FRONT_COLOR); hua_wang(); } vpp_buf[0]=vpp/10000+0x30; vpp_buf[1]=vpp%10000/1000+0x30; vpp_buf[2]=vpp%10000%1000/100+0x30; vpp_buf[3]=vpp%10000%1000%100/10+0x30; vpp_buf[4]=vpp%10000%1000%100%10+0x30; vpp_buf[5]='\0'; GUI_Show12ASCII(164,224,vpp_buf,FRONT_COLOR,WHITE); ADC_Get_Value(); vpp = ADC_Get_Vpp(); } } [/hide][free]
购买主题
本主题需向作者支付 3 枚金币 才能浏览
《《《 点击这里展开全文 》》》 | |
发表于 2020-12-1 09:59:25
|
显示全部楼层
| |
发表于 2020-12-1 10:01:08
|
显示全部楼层
| |
发表于 2020-12-1 11:17:10
|
显示全部楼层
| |
| |
发表于 2020-12-1 17:48:21
|
显示全部楼层
| |
发表于 2020-12-1 17:50:08
|
显示全部楼层
| |
发表于 2020-12-2 08:24:23
|
显示全部楼层
| |
发表于 2023-3-7 21:39:01
|
显示全部楼层
| |
q
|
|