stm32課程的一個項目,用protues仿真stm32讀取ADC,做完之后把這些分享下來。
0.png (31.91 KB, 下載次數: 136)
下載附件
2019-5-12 15:50 上傳
單片機源程序如下:
- //#include "main.h"
- #include "stm32f10x.h"
- #include "stm32f10x_it.h"
- #include <stdio.h>
- #define ADC1_DR_Address ((uint32_t)0x40012400+0x4C)
- u8 seg_table[]={
- 0xc0,0xf9,0xa4,0xb0,
- 0x99,0x92,0x82,0xf8,
- 0x80,0x90,0x88,0x83,
- 0x39,0x5e,0x79,0x71}; //共陰極表
- uint16_t ADCConvertedValue;
- uint32_t DMA_ADC_OK;
- void NVIC_Config(void);
- void ADC_Config(void);
- void delay(void);
- void xdelay(void);
- void GPIO_Config(void);
- u16 Get_Adc(u8 ch);
- //u16 Get_Adc_Average(u8 ch,u8 times);
- int main(void)
- {
- float temp=0;
- DMA_ADC_OK = 0;
- NVIC_Config();
- SysTick_Config(72000);
- ADC_Config();
- GPIO_Config();
-
-
-
- while(1)
- {
- int p,q;
-
- // GPIO_ResetBits(GPIOA,GPIO_Pin_8);
- // GPIO_ResetBits(GPIOD,GPIO_Pin_2);
- // delay();
- // GPIO_SetBits(GPIOA,GPIO_Pin_8);
- // GPIO_SetBits(GPIOD,GPIO_Pin_2);
- // delay();
-
- if(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)!= RESET)
- {
- DMA_ADC_OK = 1;
- ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
- }
-
- if(DMA_ADC_OK)
- {
- ADCConvertedValue=ADC_GetConversionValue(ADC1);
- //ADCConvertedValue=Get_Adc(ADC_Channel_1);
-
- temp = (float)ADCConvertedValue* (3.3/4096);
-
-
- if (temp>=1)
- {
- temp=temp+1;
- //GPIO_WriteBit(GPIOA,GPIO_Pin_3,
- // (BitAction)((1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_3))));
-
-
- p=(int)temp;
- temp=(temp-p)*10;
- q=(int)temp;
-
- GPIOB->ODR=0xfa; // 高位
- GPIOC->ODR=seg_table[p];
- xdelay();
-
- GPIOB->ODR=0xfd; // 低位
- GPIOC->ODR=seg_table[q];
- xdelay();
- }
- else {
- temp=temp*10;
- q=(int)temp;
-
- GPIOB->ODR=0xfa; // 高位
- GPIOC->ODR=0xc0;
- xdelay();
-
- GPIOB->ODR=0xfd; // 低位
- GPIOC->ODR=seg_table[q+1];
- xdelay();
-
-
-
-
- }
-
-
-
-
-
- }
- }
- }
- /*NVIC??*/
- void NVIC_Config(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* DMA??NVIC?? */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void ADC_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- ADC_InitTypeDef ADC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);//PC4
-
- RCC_ADCCLKConfig(RCC_PCLK2_Div6);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- ADC_DeInit(ADC1);
-
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
- ADC_InitStructure.ADC_ScanConvMode = DISABLE;
- ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfChannel = 1;
- ADC_Init(ADC1, &ADC_InitStructure);
-
- ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);
-
-
- ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1
- ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- }
- void delay(void)
- {
- int i=0xffa;
- while(i--);
- }
- void xdelay(void)
- {
- int i=0xfff;
- while(i--);
- }
- void GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* GPIOD Periph clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//PC4
-
-
- /* Configure PD2 in output pushpull mode */
- // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- // GPIO_Init(GPIOA, &GPIO_InitStructure);//PD2????,?????
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);//PD2????,?????
-
-
- /* Configure PD2 in output pushpull mode */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);//PD2????,?????
- // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//PC4
- // /* Configure PD2 in output pushpull mode */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);//PD2????,?????
- //
-
-
-
- // /* Configure PA8 in output pushpull mode */
- // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- // GPIO_Init(GPIOC, &GPIO_InitStructure);//PD2????,?????
- }
- void xianshi(float x)
- { GPIO_InitTypeDef GPIO_InitStructure; //定義一個管腳初始化的結構體
- //開啟PA口時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- //配置PA所有的模式
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
-
-
-
- }
- void ADC1_2_IRQHandler(void)
- {
- if(ADC_GetITStatus(ADC1, ADC_IT_EOC)!= RESET)
- {
- DMA_ADC_OK=1;
- ADC_ClearITPendingBit(ADC1, ADC_IT_EOC);
- }
- }
復制代碼
所有資料51hei提供下載:
keil.zip
(325.06 KB, 下載次數: 358)
2019-5-12 13:42 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
proteus.zip
(188.01 KB, 下載次數: 356)
2019-5-12 13:42 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|