微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 10路AD驱动程序

10路AD驱动程序

时间:11-26 来源:互联网 点击:
//Author:"泪与汗的缠绵"

//Data:2011-11月

//功能:10路AD驱动程序,TFT函数的配置

#include "stm32f10x_lib.h"
#include"stdio.h"
#include "main.h"
u8 Rx_Buffer[128*2];
vu32 FLASH_ID = 0;
#defineM25P64_FLASH_ID0x202017
//u16 colors[]={
//0xf800,0x07e0,0x001f,0xffe0,0x0000,0x07ff,0xf81f,0xffff ,0x7bef
//};
RCC_Init();
ADC_configuration();
DMA_Configuration();
LCD_Display_Color(0xffe0);
LCD_SetBackColor(0xffe0);
LCD_SetTextColor(0x07e0);
while (1)
{
for(cnt=0;cnt<10;cnt++)
{
AD_value=ADC_ConvertedValue[cnt];//返回最近一次 ADCx 规则组的转换结果
AD_value=AD_value/4095.0*3.3;
readAD[cnt]=AD_value*1000.0;
Display_variate(cnt*15,80,cnt);
}
for(cnt=0;cnt<10;cnt++)
Display_variate(cnt*15,2,readAD[cnt]);
}
}
void RCC_Init(void)
{
RCC_DeInit();


RCC_HSEConfig(RCC_HSE_ON);


HSEStartUpStatus = RCC_WaitForHSEStartUp();

if(HSEStartUpStatus==SUCCESS)
{

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);


FLASH_SetLatency(FLASH_Latency_2);


RCC_HCLKConfig(RCC_SYSCLK_Div1);


RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_ADCCLKConfig(RCC_PCLK2_Div4);


RCC_PCLK1Config(RCC_HCLK_Div2);


RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);


RCC_PLLCmd(ENABLE);


while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

while(RCC_GetSYSCLKSource() != 0x08)
{
}
}

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
STM3210B_LCD_Init();
SPI_FLASH_Init();
}
void Delay(u32 t)// Delay Time about 1 ms;
{
u16 i;
do
{
for(i=0; i<5000; i++);
}
while(t--);
}
voidADC_configuration()
{
ADC_InitTypeDefADC_InitStructure;
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_NbrOfChannel=10;// 规定了顺序进行规则转换的 ADC 通道的数目。这个数目的取值范围是 1 到16。
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0,2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1,3, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2,4, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_3,5, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_4,6, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_5,7, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_6,8, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_7,9, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 10, ADC_SampleTime_55Cycles5);

ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));//等待ADC的校准寄存器被设置完毕
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
}
void DMA_Configuration(void)
{
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)ADC1_DR_Address;//定义DMA外设基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;// 定义DMA内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//外设作为数据传输的来源(外设传到内存) DMA_DIR_PeripheralDST——内存传到外设
DMA_InitStructure.DMA_BufferSize =10;//连续转化10个AD通道值
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设寄存器地址不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//内存寄存器地址递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //外设数据宽度32bit
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//内存数据宽度32bit
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(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top