//Author:"泪与汗的缠绵"
//Data:2011-11月
//功能:DA驱动程序,函数配置
#include "main.h"
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
u8 Rx_Buffer[128*2];
vu32 FLASH_ID = 0;
#define M25P64_FLASH_ID 0x202017
float AD_value;
//u16 colors[]={
// 0xf800,0x07e0,0x001f,0xffe0,0x0000,0x07ff,0xf81f,0xffff ,0x7bef
//};
static vu32 TimingDelay = 0;
static vu32 LedShowStatus = 0;
static volatile ErrorStatus HSEStartUpStatus = SUCCESS;
void ADC_configuration();
int main(void)
{
RCC_Init();
ADC_configuration();
LCD_Display_Color(0xffe0);
LCD_SetBackColor(0xffe0);
LCD_SetTextColor(0x07e0);
while (1)
{
AD_value=ADC_GetConversionValue(ADC1);
AD_value=AD_value/4095.0*3.3;
AD_value=AD_value*1000.0;
Display_variate(60,16,AD_value);
}
}
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_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_GPIOB |RCC_APB2Periph_GPIOC
| RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM4, ENABLE);
SysTick_Config();
STM3210B_LCD_Init();
SPI_FLASH_Init();
FLASH_ID = SPI_FLASH_ReadID();
if(FLASH_ID == M25P64_FLASH_ID)
{
GPIO_WriteBit(GPIOC, GPIO_Pin_6, Bit_SET);
}
else
{
GPIO_WriteBit(GPIOC, GPIO_Pin_7, Bit_SET);
}
}
void SysTick_Config(void)
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
SysTick_SetReload(720000);
SysTick_ITConfig(ENABLE);
}
void Delay(u32 nCount)
{
TimingDelay = nCount;
SysTick_CounterCmd(SysTick_Counter_Enable);
while(TimingDelay != 0)
{
}
SysTick_CounterCmd(SysTick_Counter_Disable);
SysTick_CounterCmd(SysTick_Counter_Clear);
}
void Decrement_TimingDelay(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
////////////AD的配置函数/////////////////////////////
void ADC_configuration()
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_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_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel=1; //指定用于转换的通道数
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, 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);
}