#include "app.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)//¶¨ÒåÓ²¼þADC1µÄÎïÀíµØÖ·
__IO uint32_t ADC_DualConvertedValueTab[32];//ת»»µÄͨµÀADÖµ
/**********************************************************
** º¯ÊýÃû: void All_GPIO_Config(void)
** ¹¦ÄÜÃèÊö: ËùÓеÄGPIO¿ÚÅäÖ÷ÅÔÚÕâÀï
** ÊäÈë²ÎÊý: ÎÞ
** Êä³ö²ÎÊý: ÎÞ
***********************************************************/
void All_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //¶¨ÒåGPIO½á¹¹Ìå
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//ʹÄÜGPIOBʱÖÓ
/*½«PB0ÅäÖÃΪÍÆÍìÊä³ö*/
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0;//PB0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50MHzʱÖÓËÙ¶È
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/**********************************************************
** º¯ÊýÃû: ADC1_DMA_Config
** ¹¦ÄÜÃèÊö: ADC1µÄDMA·½Ê½ÅäÖÃ
** ÊäÈë²ÎÊý: ÎÞ
** Êä³ö²ÎÊý: ÎÞ
***********************************************************/
void ADC1_DMA_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;//¶¨ÒåADC½á¹¹Ìå
DMA_InitTypeDef DMA_InitStructure;//¶¨ÒåDMA½á¹¹Ìå
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//ʹÄÜDMA1ʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1| RCC_APB2Periph_ADC2 | RCC_APB2Periph_GPIOA, ENABLE ); //ʹÄÜADC1¼°GPIOAʱÖÓ
/*×÷ΪADC1µÄ6ͨµÀÄ£ÄâÊäÈëµÄGPIO³õʼ»¯ÅäÖÃ*/
//PA2,3,4,5,6,7ÅäÖÃΪģÄâÊäÈë
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//Ä£ÄâÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*DMA1µÄͨµÀ1ÅäÖÃ*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//´«ÊäµÄÔ´Í·µØÖ·
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_DualConvertedValueTab;//Ä¿±êµØÖ·
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //ÍâÉè×÷Ô´Í·
DMA_InitStructure.DMA_BufferSize = 32;//Êý¾Ý³¤¶È
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//ÍâÉèµØÖ·¼Ä´æÆ÷²»µÝÔö
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//ÄÚ´æµØÖ·µÝÔö
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;//ÍâÉè´«ÊäÒÔ×Ö½ÚΪµ¥Î»
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;//ÄÚ´æÒÔ×Ö½ÚΪµ¥Î»
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//Ñ»·Ä£Ê½
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;//4ÓÅÏȼ¶Ö®Ò»µÄ(¸ßÓÅÏÈ)
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //·ÇÄÚ´æµ½ÄÚ´æ
DMA_Init(DMA1_Channel1, &DMA_InitStructure);//¸ù¾ÝÒÔÉϲÎÊý³õʼ»¯DMA_InitStructure
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);//ÅäÖÃDMA1ͨµÀ1´«ÊäÍê³ÉÖжÏ
DMA_Cmd(DMA1_Channel1, ENABLE);//ʹÄÜDMA1
//ÁÙʱ¼ÓDMAͨµÀ4£¬²»¶Ô¼´¿Éɾ³ý
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1 -> DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_DualConvertedValueTab;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 32;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_Cmd (DMA1_Channel4,ENABLE);
//
RCC_ADCCLKConfig(RCC_PCLK2_Div4);//ʱÖÓΪ4·ÖƵ£¬56/4=14MHZ
/*ÏÂÃæΪADC1µÄÅäÖÃ*/
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;//ADC1¹¤×÷ÔÚË«¹æÔòģʽ
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Êý¾ÝÓÒ¶ÔÆë
ADC_InitStructure.ADC_NbrOfChannel = 1;//ת»»µÄADCͨµÀµÄÊýĿΪ1
ADC_Init(ADC1, &ADC_InitStructure);//Òª°ÑÒÔϲÎÊý³õʼ»¯ADC_InitStructure
/* ÉèÖÃADC1µÄ¹æÔò×éͨµÀ£¬ÉèÖÃËüÃǵÄת»¯Ë³ÐòºÍ²ÉÑùʱ¼ä*/
//ת»»Ê±¼äTconv=²ÉÑùʱ¼ä+12.5¸öÖÜÆÚ
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_1Cycles5);//ADC1ͨµÀ4
/*ʹÄÜADC1µÄDMA´«Ê䷽ʽ*/
ADC_DMACmd(ADC1, ENABLE);
/*ʹÄÜADC1 */
ADC_Cmd(ADC1, ENABLE);
/*ÖØÖÃADC1µÄУ׼¼Ä´æÆ÷ */
ADC_ResetCalibration(ADC1);
/*»ñÈ¡ADCÖØÖÃУ׼¼Ä´æÆ÷µÄ״̬*/
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1); /*¿ªÊ¼Ð£×¼ADC1*/
while(ADC_GetCalibrationStatus(ADC1)); //µÈ´ýУ׼Íê³É
/* ADC2 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult; //
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //
ADC_InitStructure.ADC_NbrOfChannel = 1; //
ADC_Init(ADC2, &ADC_InitStructure); //
/* ADC2 regular channels configuration */
ADC_RegularChannelConfig(ADC2, ADC_Channel_6, 1, ADC_SampleTime_1Cycles5); //
/* Enable ADC2 external trigger conversion */
ADC_ExternalTrigConvCmd(ADC2, ENABLE); //??ADC2?????
/* Enable ADC2 */
ADC_Cmd(ADC2, ENABLE); //
/* Enable ADC2 reset calibration register */
ADC_ResetCalibration(ADC2); //
/* Check the end of ADC2 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC2));
/* Start ADC2 calibration */
ADC_StartCalibration(ADC2);
/* Check the end of ADC2 calibration */
while(ADC_GetCalibrationStatus(ADC2));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//ʹÄÜADC1Èí¼þת»»
}
/**********************************************************
** º¯ÊýÃû: void NVIC_Config(void)
** ¹¦ÄÜÃèÊö: ÖжϷÖ×é¼°ÓÅÏȼ¶ÅäÖÃ
** ÊäÈë²ÎÊý: ÎÞ
** Êä³ö²ÎÊý: ÎÞ
***********************************************************/
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //²ÉÓÃ×é±ð2
/*DMA1µÄͨµÀ1ÖжÏÅäÖÃ*/
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//Õ¼ÏÈʽÓÅÏȼ¶ÉèÖÃΪ0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//¸±ÓÅÏȼ¶ÉèÖÃΪ1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//ÖжÏʹÄÜ
NVIC_Init(&NVIC_InitStructure);//°´Ö¸¶¨²ÎÊý³õʼ»¯ÖжÏ
}
/**********************************************************
** º¯ÊýÃû: u8 All_Init(void)
** ¹¦ÄÜÃèÊö: ϵͳµÄËùÓгõʼ»¯ÅäÖ÷ÅÔÚÕâÀï
** ÊäÈë²ÎÊý: ÎÞ
** Êä³ö²ÎÊý: ³õʼ»¯³É¹¦·µ»Ø1£¬·ñÔò·µ»Ø0
***********************************************************/
u8 All_Init(void)
{
SystemInit(); //ϵͳʱÖÓ³õʼ»¯
// All_GPIO_Config();//GPIO³õʼ»¯ÅäÖÃ
NVIC_Config();//ÖжϷÖ×é¼°ÓÅÏȼ¶ÅäÖÃ
Usart_Configuration();//´®¿Ú³õʼ»¯£¬²¨ÌØÂÊ115200
ADC1_DMA_Config(); //ADC1µÄDMA³õʼ»¯
return 1;
}
#include "stm32f10x.h"
#include <stdio.h> //ÏÂÃæstrlenº¯ÊýÐèÒª´ËÍ·Îļþ
#include "USART.h"
/**********************************************************
** º¯ÊýÃû: Usart_Configuration
** ¹¦ÄÜÃèÊö: ´®¿Ú1ÅäÖÃ, °üÀ¨´®¿ÚʱÖÓ£¬GPIOÅäÖÃ
** ÊäÈë²ÎÊý: ÎÞ
** Êä³ö²ÎÊý: ÎÞ
***********************************************************/
void Usart_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO¿âº¯Êý½á¹¹Ìå
USART_InitTypeDef USART_InitStructure;//USART¿âº¯Êý½á¹¹Ìå
USART_ClockInitTypeDef USART_ClockInitStructure;
//ʹÄÜ´®¿Ú1£¬GPIOA£¬AFIO×ÜÏß
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1,ENABLE);
/* Configure USART1 Tx (PA9) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//PA9ʱÖÓËÙ¶È50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉÏÀÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate =256000; //²¨ÌØÂÊ115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8λÊý¾Ý
USART_InitStructure.USART_StopBits = USART_StopBits_1; //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_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;//¿ÕÏÐʱÖÓΪµÍµçƽ
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;//ʱÖÓµÚ¶þ¸ö±ßÑؽøÐÐÊý¾Ý²¶»ñ
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;//×îºóһλÊý¾ÝµÄʱÖÓÂö³å²»´ÓSCLKÊä³ö
USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_Init(USART1, &USART_InitStructure); //³õʼ»¯½á¹¹Ìå
USART_Cmd(USART1, ENABLE); //ʹÄÜ´®¿Ú1
}
//¼ÓÈëÒÔÏ´úÂë,Ö§³Öprintfº¯Êý,¶ø²»ÐèҪѡÔñuse MicroLIB
/***************************START*********************/
#if 1
#pragma import(__use_no_semihosting)
//±ê×¼¿âÐèÒªµÄÖ§³Öº¯Êý
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
/* FILE is typedef¡¯ d in stdio.h. */
FILE __stdout;
//¶¨Òå_sys_exit()ÒÔ±ÜÃâʹÓðëÖ÷»úģʽ
_sys_exit(int x)
{
x = x;
}
//Öض¨Òåfputcº¯Êý
void USART1_IRQHandler(void)//´®¿Ú1ÖжϷþÎñ³ÌÐò
{
u8 ch;
if(USART_GetFlagStatus(USART1, USART_IT_RXNE) != RESET)//½ÓÊÕÖжÏ
{
ch = USART_ReceiveData(USART1);//¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
}
}
int fputc(int ch, FILE *f)
{
/* ·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ýµ½USART1 */
USART_SendData(USART1, (uint8_t) ch);
/* µÈ´ý·¢ËÍÍê±Ï */
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return (ch);
}
/// Öض¨Ïòc¿âº¯Êýscanfµ½USART1
int fgetc(FILE *f)
{
/* µÈ´ý´®¿Ú1ÊäÈëÊý¾Ý */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(USART1);
}
#endif
|
|