用了大概一个月的时间编写出来的,希望大家多提建议
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "lcd.h"
- #include "adc.h"
- #include "key.h"
- #include "stm32f10x_it.h"
- #include "stm32f10x.h"
- #include "timer.h"
- u16 j = 0;
- float temp;
- float temp1;
- //ALIENTEK Mini STM32开发板范例代码15
- //ADC实验
- //加入程序指示用外部中断来控制按键
- //加入程序指示灯,看看程序执行到哪一步。
- void nvic_init(void);
- void rcc_init(void);
- void gpio_init(void);
-
- void clear_point(u16 hang)
- {
- u8 index_clear_lie = 0;
- POINT_COLOR = DARKBLUE;
- for(index_clear_lie = 0;index_clear_lie <201;index_clear_lie++)
- {
- lcd_huadian(hang,index_clear_lie);
- }
- POINT_COLOR=RED;
- }
-
-
-
-
- int main(void)
- {
- SystemInit();
- nvic_init();
- rcc_init(); //外设时钟配置
- gpio_init();
- set_io0();
-
- delay_init(); //延时函数初始化
- EXTIX_Init(); //外部中断初始化
-
- uart_init(9600); //串口初始化为9600
- LED_Init(); //初始化与LED连接的硬件接口
- LCD_Init();
- Adc_Init();
- set_background(); //初始化背景
- LED0=!LED0;
- delay_ms(250);
- // LED1=!LED1;
- // delay_ms(1000);
- time_init(); //定时器配置,测频率用的二个定时器
- time_enable(); //同步开始计数
- get_value();
- vpp = get_vpp();
-
-
- //LED1=0;
-
- while(1)
- {
- for(j=bndex;j<bndex+250;j=j+1)
- {
-
- // LED1=!LED1;
- temp = a[j] * 3300 / 4096 * 25 /vcc_div;
- temp1 = a[j + 1] * 3300 / 4096 * 25 / vcc_div;
- clear_point(j-bndex);
- if(temp>200)
- {
- temp=200;
- }
- if(temp<0)
- {
- temp=0;
- }
- if(temp1>200)
- {
- temp1=200;
- }
- if(temp1<0)
- {
- temp1=0;
- }
- lcd_huadian(j-bndex,temp);
- lcd_huaxian(j-bndex,temp,j-bndex+1,temp1);
- hua_wang();
- }
-
- LCD_ShowxNum(144,224,vpp,5,16,0);
- get_value();
- vpp = get_vpp();
- }
-
- }
-
-
-
-
-
- //ADC初始化
- //POINT_COLOR=RED;//设置字体为红色
- // LCD_ShowString(60,50,200,16,16,"Mini 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,"2014/3/9");
- ////显示提示信息
- //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);
- //LED0=!LED0;
- //delay_ms(250);
- //}
-
-
-
-
- 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_1;
- 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_15;
- // GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_50MHz; //外部中断的io配置
- // GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_IPU;
- // GPIO_Init(GPIOA, &GPIO_InitTypeStruct);
- //
- //
- //
- // GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_5 ;
- // GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_50MHz; //外部中断的io配置
- // GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_IPU;
- // GPIO_Init(GPIOC, &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; // S3
- 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; // S4
- 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; // S5
- 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; // S6
- 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; // S7
- GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA, &GPIO_InitTypeStruct);
- }
-
- void nvic_init(void)
- {
- NVIC_InitTypeDef NVIC_InitTypeStruct;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- NVIC_InitTypeStruct.NVIC_IRQChannel = EXTI0_IRQn;
- NVIC_InitTypeStruct.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitTypeStruct.NVIC_IRQChannelSubPriority =2;
- NVIC_InitTypeStruct.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitTypeStruct);
- NVIC_InitTypeStruct.NVIC_IRQChannel = EXTI9_5_IRQn;
- NVIC_InitTypeStruct.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitTypeStruct.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitTypeStruct.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitTypeStruct);
-
-
-
- NVIC_InitTypeStruct.NVIC_IRQChannel = EXTI15_10_IRQn;
- NVIC_InitTypeStruct.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitTypeStruct.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitTypeStruct.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitTypeStruct);
-
-
- NVIC_InitTypeStruct.NVIC_IRQChannel = TIM2_IRQn; //配置中断优先级
- NVIC_InitTypeStruct.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitTypeStruct.NVIC_IRQChannelSubPriority = 2;
- NVIC_InitTypeStruct.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitTypeStruct);
- NVIC_InitTypeStruct.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitTypeStruct.NVIC_IRQChannelPreemptionPriority = 2;
- 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, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
- RCC_ADCCLKConfig(RCC_PCLK2_Div6);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
- }
复制代码
所有资料51hei提供下载:
ADC转osc.rar
(372.88 KB, 下载次数: 28)
|