?? main.c
字號(hào):
/*----------------------------------------------------------------------------
本例子通過串口發(fā)送AD0的值,AD0連接一個(gè)NTC溫度電阻
用手觸摸NTC溫度電阻,輸出數(shù)字將變化
使用我提供的串口調(diào)試工具,選擇【串口超級終端】波特率設(shè)置115200
*---------------------------------------------------------------------------*/
/* Includes ------------------------------------------------------------------*/
#define SYS_GLOBALS
#include "include.h"
#define ADC1_DR_Address ((u32)0x4001244C)
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void USART1_InitConfig(uint32 BaudRate);
/* Private functions ---------------------------------------------------------*/
/*----------------------------------------------------------------------------
從串口發(fā)送一個(gè)字節(jié)數(shù)據(jù)
Write character to Serial Port.
*----------------------------------------------------------------------------*/
int SendChar (int ch) {
//等待發(fā)送結(jié)束
while (!(USART1->SR & USART_FLAG_TXE));
//將數(shù)據(jù)放入發(fā)送寄存器
USART1->DR = (ch & 0x1FF);
return (ch);
}
/*----------------------------------------------------------------------------
從串口讀取一個(gè)字節(jié)數(shù)據(jù),直到讀到數(shù)據(jù)才返回
Read character to Serial Port.
*----------------------------------------------------------------------------*/
int GetKey (void) {
//等待接收結(jié)束
while (!(USART1->SR & USART_FLAG_RXNE));
//從接受寄存器讀取數(shù)據(jù)并返回
return ((int)(USART1->DR & 0x1FF));
}
unsigned short int ADC_ConvertedValue;
/*------------------------------------------------------------------------------
Initialises the Analog/Digital converter
PA1 (ADC Channel1) is used as analog input
use DMA Channel1 for ADC1 (see DMA request mapping)
*------------------------------------------------------------------------------*/
void adc_Init (void) {
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//使能GPIO時(shí)鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Configure PC.04 (ADC Channel14) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //GPIO設(shè)置為模擬輸入
GPIO_Init(GPIOA, &GPIO_InitStructure);
// enable periperal clock for DMA
//使能DMA時(shí)鐘
// RCC->AHBENR |= (1<<0);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
// DMA1_Channel1->CMAR = (u32)&ADC_ConvertedValue;// set channel1 memory address
// DMA1_Channel1->CPAR = (u32)&(ADC1->DR); // set channel1 peripheral address
// DMA1_Channel1->CNDTR = 1; // transmit 1 word
// DMA1_Channel1->CCR = 0x00002520; // configure DMA channel
// DMA1_Channel1->CCR |= (1 << 0); // DMA Channel 1 enable
/* DMA1 channel1 configuration 配置DMA通道----------------------------------------------*/
DMA_DeInit(DMA1_Channel1);
//設(shè)置通道1外設(shè)地址
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
//設(shè)置DMA內(nèi)存地址,ADC轉(zhuǎn)換結(jié)果直接放入該地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//DMA傳送1個(gè)字
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE); //使能DMA通道
// RCC->APB2ENR |= (1<<9); // enable periperal clock for ADC1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
// ADC1->SQR1 = 0x00000000; // only one conversion
// ADC1->SMPR2 = 0x00000028; // set sample time channel1 (55,5 cycles)
// ADC1->SQR3 = 0x00000001; // set channel1 as 1st conversion
// ADC1->CR1 = 0x00000100; // use independant mode, SCAN mode
// ADC1->CR2 = 0x000E0103; // use data align right,continuous conversion
// EXTSEL = SWSTART
// enable ADC, DMA mode, no external Trigger
// ADC1->CR2 |= 0x00500000; // start SW conversion
/* ADC1 configuration 配置ADC------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
//使用獨(dú)立模式,掃描模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
//無需外接觸發(fā)器
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
//使用數(shù)據(jù)右對齊
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
// 只有1個(gè)轉(zhuǎn)換通道
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel-0 configuration */
//通道1采樣周期55.5個(gè)時(shí)鐘周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
/* Enable ADC1 DMA */
//使能ADC的DMA
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
//使能ADC1
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
//開始轉(zhuǎn)換
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
void Delay(unsigned long time)
{unsigned long i,j;
for(j=0; j<time; j++)
{
for(i=0;i<12000;i++);
}
}
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
int AD_value;
/* System Clocks Configuration */
RCC_Configuration();//配置系統(tǒng)時(shí)鐘
GPIO_Configuration();//配置GPIO
/* NVIC configuration */
NVIC_Configuration();//配置中斷
USART1_InitConfig(115200);
adc_Init();
printf ("ADC Example\r\n\r\n");
while (1)
{ // Loop forever
//Delay(1000);
//為了方便觀察軟件仿真好觀察采用下面的延時(shí)
Delay(50);
AD_value = ADC_ConvertedValue;
printf("AD value = 0x%04X\r\n", AD_value);
} // end while
}
void USART1_InitConfig(uint32 BaudRate)
{USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//使能串口的RCC時(shí)鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
//串口使用的GPIO口配置
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置串口
USART_InitStructure.USART_BaudRate = BaudRate;
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;
/* Configure USART1 */
USART_Init(USART1, &USART_InitStructure);//配置串口1
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);//使能串口1
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);//使能外部時(shí)鐘
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();//等待外部時(shí)鐘就緒
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer 啟用預(yù)取緩沖器 */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state FLASH設(shè)置2個(gè)等待周期*/
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK 設(shè)置系統(tǒng)設(shè)置*/
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK PCLK2時(shí)鐘=主時(shí)鐘*/
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 PCLK1時(shí)鐘為主時(shí)鐘1/2*/
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz 設(shè)置時(shí)鐘為72M*/
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL 使能PLL*/
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready 等待PLL工作穩(wěn)定*/
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source 選擇PLL做為系統(tǒng)時(shí)鐘源*/
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source 準(zhǔn)備就緒,開始干活*/
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO_LED clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能GPIO時(shí)鐘
//LED_init-------------------------------------------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure); //設(shè)置GPIO為輸出
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
//#ifdef VECT_TAB_RAM
#if defined (VECT_TAB_RAM)
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); //設(shè)置中斷向量在RAM中
//#elif defined(VECT_TAB_FLASH_IAP)
// NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x2000);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //設(shè)置中斷向量在FLASH中
#endif
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -