ADC配置
ADC配置逻辑
开启时钟→GPIO设置→UART设置→DMA设置→ADC设置→
#define ADC1_DR_Address ((u32)0x4001244C)
vu16 ADC_ConvertedValue;
主函数
Void main(void)
{
开启时钟
RCC_Configuation()
{
使能DMA时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA,ENABLE);
使能ADC1,GPIOC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Peirph_ADC1|RCC_APB2Periph_GPIO,ENABL
E)
打开GPIOD和AFIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RC C_APB2Periph_AFIO,ENABLE)
使能串口时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
}
GPIO配置
GPIO_Configuation()
{
GPIO_InitTypeDef GPIO_InitStructure;
使能GPIO端口映射USART2
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
引脚设置推拉输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
同步异步收发器配置
USART_Configuation()
{
定义串口初始化结构体
USART_InitTypeDef USART_InitStructure;
设置波特率9600
USART_https://www.360docs.net/doc/9117695684.html,ART_BaudRate=115200;
设置数据长度
USART_https://www.360docs.net/doc/9117695684.html,ART_WordLength = USART_WordLength_8b;
设置停止位
USART_https://www.360docs.net/doc/9117695684.html,ART_StopBits = USART_StopBits_1;
设置校验位
USART_https://www.360docs.net/doc/9117695684.html,ART_Parity = USART_Parity_No ;
设置RTSCTS硬件流控制
USART_https://www.360docs.net/doc/9117695684.html,ART_HardwareFlowControl=USART_HardwareFlowControl_None 使能发送接收
USART_https://www.360docs.net/doc/9117695684.html,ART_Mode = USART_Mode_Rx | USART_Mode_Tx;
设置串口时钟
USART_https://www.360docs.net/doc/9117695684.html,ART_Clock = USART_Clock_Disable;
时钟有效位
USART_https://www.360docs.net/doc/9117695684.html,ART_CPOL = USART_CPOL_Low;
时钟沿捕捉位
USART_https://www.360docs.net/doc/9117695684.html,ART_CPHA = USART_CPHA_2Edge;
最后数据位定义
USART_https://www.360docs.net/doc/9117695684.html,ART_LastBit = USART_LastBit_Disable;
初始化串口x
USART_Init(USARTx, &USART_InitStructure);
使能串口x
USART_Cmd(USART2, ENABLE);
}
DMA配置
DMA_Configuation()
{
定义结构体
DMA_InitTypeDef DMAInitStructure;
初始化DMA通道x
DMA_DeInit(DMA_Channelx)
定义DMA通道外设基地址
DMA_InitStructure.DMA_PeripheralBaseAddr=ADC1_DR_Address;
定义DMA通道存储器地址
DMA_InitStructure.DMA_MemoryBaseAddr=(u16)&ADC_ConvertedValue;
指定源地址
DMA_InitSurcurure.DMA_DIR=DMA_DIR_PeripheralSRC;
定义DMA缓冲区大小
DMA _InitStructure.DMA A_BufferSize=x;
当前外设寄存器地址不变
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
当前存储器地址不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
定义存储器数据宽度16位
DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;
DMA通道操作模式位环形缓冲模式
DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;
DMA通道优先级高
DMA_InitStructure.DMA_Priority =DMA_Priority_High;
禁止DMA通道存储器到存储器传输
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
初始化DMA通道
DMA_Init(DMA_Channelx, &DMA_InitStructure);
使能DMA通道x
DMA_Cmd(DMA_Channel1, ENABLE);
}
ADC设置
ADC_Configuation(void)
{
定义ADC初始化结构体变量
ADC_InitTypeDef ADC_InitStructure;
ADC1和2工作模式
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
使能扫描
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC转换模式
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(ADC1, &ADC_InitStructure);
选择信道,音序器等级,采样时间
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_28Cycles5); 使能ADC1模块DMA
ADC_DMACmd(ADC1, ENABLE);
使能ADC1
ADC_Cmd(ADC1, ENABLE);
重置ADC1校准寄存器
ADC_ResetCalibration(ADC1);
等待ADC1校准重置完成
while(ADC_GetResetCalibrationStatus(ADC1));
开始ADC1校准
ADC_StartCalibration(ADC1);
等待ADC1校准完成
while(ADC_GetCalibrationStatus(ADC1));
使能ADC1软件开始转换
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
while(1)
{
Delay(0x8FFFF);
printf("ADC=%XV olt=%dmv\r\n",ADC_ConvertedValue,ADC_ConvertedValue*3300/4096; }
}
向串口输出字符
Put_String(u8 *p)
{
while(*p)
{
USART_SendData(USART2, *p++);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}
}