main.c
来自「stm32初级例程」· C语言 代码 · 共 361 行
C
361 行
/*----------------------------------------------------------------------------
本例子通过串口发送AD0的值,AD0连接一个NTC温度电阻
用手触摸NTC温度电阻,输出数字将变化
使用我提供的串口调试工具,选择【串口超级终端】波特率设置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 ---------------------------------------------------------*/
/*----------------------------------------------------------------------------
从串口发送一个字节数据
Write character to Serial Port.
*----------------------------------------------------------------------------*/
int SendChar (int ch) {
//等待发送结束
while (!(USART1->SR & USART_FLAG_TXE));
//将数据放入发送寄存器
USART1->DR = (ch & 0x1FF);
return (ch);
}
/*----------------------------------------------------------------------------
从串口读取一个字节数据,直到读到数据才返回
Read character to Serial Port.
*----------------------------------------------------------------------------*/
int GetKey (void) {
//等待接收结束
while (!(USART1->SR & USART_FLAG_RXNE));
//从接受寄存器读取数据并返回
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时钟
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设置为模拟输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
// enable periperal clock for DMA
//使能DMA时钟
// 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);
//设置通道1外设地址
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
//设置DMA内存地址,ADC转换结果直接放入该地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//DMA传送1个字
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;
//使用独立模式,扫描模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
//无需外接触发器
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
//使用数据右对齐
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
// 只有1个转换通道
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel-0 configuration */
//通道1采样周期55.5个时钟周期
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 */
//开始转换
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();//配置系统时钟
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);
//为了方便观察软件仿真好观察采用下面的延时
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时钟
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);//使能外部时钟
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();//等待外部时钟就绪
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer 启用预取缓冲器 */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state FLASH设置2个等待周期*/
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK 设置系统设置*/
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK PCLK2时钟=主时钟*/
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 PCLK1时钟为主时钟1/2*/
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz 设置时钟为72M*/
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL 使能PLL*/
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready 等待PLL工作稳定*/
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source 选择PLL做为系统时钟源*/
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source 准备就绪,开始干活*/
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时钟
//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); //设置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); //设置中断向量在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); //设置中断向量在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****/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?