全部源码下载:
三自由度(ADC信号) 三开关量信号采集V1.2.zip
(2.78 MB, 下载次数: 41)
主程序:
- #include <stdio.h>
- #include "pbdata.h"
- u8 flag=1;
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void NVIC_Configuration(void);
- void USART_Configuration(void);
- u8 ah,al;
- u16 ad,i;
- u32 tempu32 = 0;
- #define N 256 //
- #define M 4 //
- __IO u16 ADC_ConvertedValue[N][M];
- vu16 After_filter[M];
- /*
- int fputc(int ch,FILE *f)
- {
- USART_SendData(USART1,(u32)ch);
- //USART_SendData(USART1,(unsigned int)ch);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- return ch;
- }
- */
- void ADC_ON(void)
- {
- //íaéèé豸μÄ»ùμØÖ·¶¨òå
- #define ADC1_DR_Address ((u32)0x4001244C)
- DMA_InitTypeDef DMA_InitStructure;
- ADC_InitTypeDef ADC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
- //êäèë¿úÅäÖÃ
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- //DMAí¨μàÅäÖÃ
- DMA_DeInit(DMA1_Channel1);
- DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
- DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_ConvertedValue;
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
- DMA_InitStructure.DMA_BufferSize = N*M;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
- DMA_Init(DMA1_Channel1, &DMA_InitStructure);
- DMA_Cmd(DMA1_Channel1, ENABLE);
-
- //ADCÄ£ê½éèÖÃ
- // ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
- ADC_InitStructure.ADC_ScanConvMode = ENABLE;
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfChannel = M;
- ADC_Init(ADC1, &ADC_InitStructure);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1, ADC_SampleTime_239Cycles5);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 2, ADC_SampleTime_239Cycles5);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 3, ADC_SampleTime_239Cycles5);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 4, ADC_SampleTime_239Cycles5);
- ADC_DMACmd(ADC1, ENABLE);
- /*-----------¿aÆôADC1D£Ñé----------------*/
- ADC_Cmd(ADC1, ENABLE);
- ADC_ResetCalibration(ADC1);
- while(ADC_GetResetCalibrationStatus(ADC1));
- ADC_StartCalibration(ADC1);
- while(ADC_GetCalibrationStatus(ADC1));
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- }
- void filter(void) //¾ùÖμÂË2¨
- {
- u16 count;
- for(i=0;i<M;i++)
- {
- for ( count=0;count<N;count++)
- {
- tempu32 += ADC_ConvertedValue[count][i];
- }
- After_filter[i]=(tempu32/N);
- tempu32=0;
- }
- }
- u16 ADCGet1(u8 channel)
- {
- filter();
- ad = After_filter[0];
- ah = ad >> 8;
- al = ad;
- if(!channel)
- return (ah);
- else
- return (al);
- }
- u16 ADCGet2(u8 channel)
- {
- filter();
- ad = After_filter[1];
- ah = ad >> 8;
- al = ad;
- if(!channel)
- return (ah);
- else
- return (al);
- }
- u16 ADCGet3(u8 channel)
- {
- filter();
- ad = After_filter[2];
- ah = ad >> 8;
- al = ad;
- if(!channel)
- return (ah);
- else
- return (al);
- }
- //上下位机通讯协议:FF FF 00 01 01 01 0F FF 0F FF 0F FF 00 EE EE
- // FF FF 00 00 00 00 00 00 00 00 00 00 00 EE EE
- void UsartDate(void)
- {
- USART_SendData(USART1,0xFF);// Code Head
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- USART_SendData(USART1,0xFF);// Code Head
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
-
- USART_SendData(USART1,0x00);//spare
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- if((GPIOA->IDR&GPIO_Pin_0)==0)
- {
- USART_SendData(USART1,0x01);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- }
- else
- {
- USART_SendData(USART1,0x00);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- }
- if((GPIOA->IDR&GPIO_Pin_1)==0)
- {
- USART_SendData(USART1,0x01);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- }
- else
- {
- USART_SendData(USART1,0x00);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- }
-
- if((GPIOB->IDR&GPIO_Pin_9)==0)
- {
- USART_SendData(USART1,0x01);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- }
- else
- {
- USART_SendData(USART1,0x01);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- }
-
- USART_SendData(USART1,0x00);//spare Ô¤áô
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
-
- USART_SendData(USART1,ADCGet1(0));
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- USART_SendData(USART1,ADCGet1(1));
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- USART_SendData(USART1,ADCGet2(0));
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- USART_SendData(USART1,ADCGet2(1));
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
-
- USART_SendData(USART1,ADCGet3(0));
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- USART_SendData(USART1,ADCGet3(1));
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
-
- USART_SendData(USART1,0xEE);// Code end
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- USART_SendData(USART1,0xEE);// Code end
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
- }
- void RCC_Configuration(void)
- {
- SystemInit();//72m
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
- }
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- }
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void USART_Configuration(void)
- {
- USART_InitTypeDef USART_InitStructure;
- USART_InitStructure.USART_BaudRate=115200;
- USART_InitStructure.USART_WordLength=USART_WordLength_8b;
- USART_InitStructure.USART_StopBits=USART_StopBits_1;
- USART_InitStructure.USART_Parity=USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
- USART_Init(USART1,&USART_InitStructure);
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
- USART_Cmd(USART1,ENABLE);
- USART_ClearFlag(USART1,USART_FLAG_TC);
- }
- /*
- void USART1_Putc(unsigned char c)
- {
- while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
- USART_SendData(USART1, c);
- while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
- }
- */
- /*
- void USART_SendData(USART_TypeDef* USARTx, u16 Data)//
- {
- // Check the parameters
- assert_param(IS_USART_ALL_PERIPH(USARTx));
- assert_param(IS_USART_DATA(Data));
- // Transmit Data
- USARTx->DR = (Data & (u16)0x01FF);
- }
- */
- int main(void)
- {
-
- RCC_Configuration();
- GPIO_Configuration();
- USART_Configuration();
- NVIC_Configuration();
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- ADC_ON();
- while(1)
- {
- UsartDate();
- delay_ms(200);
- }
- }
复制代码
|