Chào cả nhà,
Em đang lập trình con ARM STM32F103x8 dùng ADC để đọc tín hiệu khoảng 25kHz. Em có đính kèm hình tín hiệu đo bằng oscilloscope. Em lập trình và check thử giá trị đọc được gửi lên máy tính thì kết quả không như mong muốn. Các anh chị xem thử chỉ cho em với. Em cám ơn nhiều ạ!
https://www.dropbox.com/s/brj55cr4rv...61854.jpg?dl=0
Em đang lập trình con ARM STM32F103x8 dùng ADC để đọc tín hiệu khoảng 25kHz. Em có đính kèm hình tín hiệu đo bằng oscilloscope. Em lập trình và check thử giá trị đọc được gửi lên máy tính thì kết quả không như mong muốn. Các anh chị xem thử chỉ cho em với. Em cám ơn nhiều ạ!
https://www.dropbox.com/s/brj55cr4rv...61854.jpg?dl=0
Code:
#include "application.h"
#define ADC1_DR_Address ((uint32_t) 0x4001244c)
uint16_t ADC_ConvertedValue;
int myArray[1024];
int lastTime = 0;
void setup()
{
//Spark.disconnect();
//WiFi.off();
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
Serial.begin(9600);
//enable ADC1 clock
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) &ADC_ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
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_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
//ADC1 configuration
//select independent conversion mode (single)
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
//We will convert single channel only
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
//We will convert one time
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
//Select no external triggering
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
//Right 12-bit data alignment in ADC data register
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
//Single channel conversion
ADC_InitStructure.ADC_NbrOfChannel = 1;
//Load structure values to control and status registers
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_1Cycles5);
ADC_DMACmd(ADC1, ENABLE);
//Enable ADC1
ADC_Cmd(ADC1, ENABLE);
//Enable ADC1 reset calibration register
ADC_ResetCalibration(ADC1);
//Check the end of ADC1 reset calibration register
while (ADC_GetResetCalibrationStatus(ADC1));
//Start ADC1 calibration
ADC_StartCalibration(ADC1);
//Check the end of ADC1 calibration
while (ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (!Serial.available());
}
void loop()
{
for (int i=0; i<1024; i++)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (!DMA_GetFlagStatus(DMA1_FLAG_TC1));
DMA_ClearFlag(DMA1_FLAG_TC1);
myArray[i]=ADC_ConvertedValue;
}
// long start = micros();
if(millis() - lastTime > 1024)
{
for (int i=0; i<1024; i++)
{
Serial.println(myArray[i]);
}
lastTime = millis();
}
// long end = micros();
//Serial.println(ADC_ConvertedValue);
//Serial.print(" micros ");
//Serial.println(end - start);
//delay(1000);
}

Comment