经过数据处理,串口传的数据始终是3.3。找了半天不知道问题在哪。本人新手,求大神指点~
程序如下:
#include "usart.h"
#include "adc.h"
#include "delay.h"
//vu16 ADC_ConvertedValue[16];
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÏμí3ÖD¶ÏóÅÏ輶·Ö×é2
delay_init(168);
Adc_Init();
DMA_init();
uart_init(9600); //2¨ìØÂê9600
while(1)
{
if(DMA_GetFlagStatus(DMA2_Stream0,DMA_FLAG_TCIF0)!=RESET)
{
Read_ADC_Result(voltage(0));
Read_ADC_Result(voltage(1));
Read_ADC_Result(voltage(2));
Read_ADC_Result(voltage(3));
Read_ADC_Result(voltage(4));
Read_ADC_Result(voltage(5));
Read_ADC_Result(voltage(6));
Read_ADC_Result(voltage(7));
Read_ADC_Result(voltage(8));
Read_ADC_Result(voltage(9));
Read_ADC_Result(voltage(10));
Read_ADC_Result(voltage(11));
Read_ADC_Result(voltage(12));
Read_ADC_Result(voltage(13));
Read_ADC_Result(voltage(14));
Read_ADC_Result(voltage(15));
/*Read_ADC_Result(ADC_ConvertedValue[0]);
Read_ADC_Result(ADC_ConvertedValue[1]);
Read_ADC_Result(ADC_ConvertedValue[2]);
Read_ADC_Result(ADC_ConvertedValue[3]);
Read_ADC_Result(ADC_ConvertedValue[4]);
Read_ADC_Result(ADC_ConvertedValue[5]);
Read_ADC_Result(ADC_ConvertedValue[6]);
Read_ADC_Result(ADC_ConvertedValue[7]);
Read_ADC_Result(ADC_ConvertedValue[8]);
Read_ADC_Result(ADC_ConvertedValue[9]);
Read_ADC_Result(ADC_ConvertedValue[10]);
Read_ADC_Result(ADC_ConvertedValue[11]);
Read_ADC_Result(ADC_ConvertedValue[12]);
Read_ADC_Result(ADC_ConvertedValue[13]);
Read_ADC_Result(ADC_ConvertedValue[14]);
Read_ADC_Result(ADC_ConvertedValue[15]);*/
DMA_ClearFlag(DMA2_Stream0,DMA_FLAG_TCIF0);
}
delay_ms(1000);
}
}
#include "adc.h"
//float AD_value_1,AD_value_2,AD_value_3,AD_value_4,AD_value_5,AD_value_6,AD_value_7,AD_value_8,AD_value_9,AD_value_10,AD_value_11,AD_value_12,AD_value_13,AD_value_14,AD_value_15,AD_value_16;
vu32 ADC_ConvertedValue[160];
void Adc_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC, ENABLE);//ê1ÄüGPIOAê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 16;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_2,3,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_3,4,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_4,5,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_5,6,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_6,7,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_7,8,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_8,9,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_9,10,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_10,11,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_11,12,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_12,13,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_13,14,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_14,15,ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_15,16,ADC_SampleTime_480Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_SoftwareStartConv(ADC1);
}
void DMA_init(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);
DMA_DeInit(DMA2_Stream0);
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t)ADC1_DR_Address;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC_ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 160;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
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_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0,&DMA_InitStructure);
DMA_Cmd(DMA2_Stream0,ENABLE);
}
unsigned long voltage(u8 count)
{
u8 i = 0;
unsigned long sum = 0;
for (i = 0;i < 10;i++)
{
sum += ADC_ConvertedValue[(i*16)+count];
}
sum /= 10;
return sum;
}
void Read_ADC_Result(unsigned long value)
{
unsigned char a,b,c,d;
value = (unsigned int)(((unsigned long)value)*3300/4095);
a = value/1000;
b =(value-a*1000)/100;
c =(value-a*1000-b*100)/10;
d =(value-a*1000-b*100-c*10);
UART3_send_Char(a+48);
UART3_send_Char(0x2e);//"."
UART3_send_Char(b+48);
UART3_send_Char(c+48);
UART3_send_Char(d+48);
UART3_send_Char(0x20);
}
#include "usart.h"
void uart_init(u32 baud){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_UART4);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_UART4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = baud;
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;
USART_Init(UART4, &USART_InitStructure);
USART_Cmd(UART4, ENABLE);
}
void UART3_send_Char(u8 ch)
{
USART_SendData(UART4,ch);
while(USART_GetFlagStatus(UART4,USART_FLAG_TXE)== RESET);
}
求指点!
一周热门 更多>