在线时间350 小时
UID1575830
注册时间2015-1-20
NXP金币0
TA的每日心情 | 郁闷 2018-2-7 18:18 |
---|
签到天数: 5 天 连续签到: 1 天 [LV.2]偶尔看看I
金牌会员
 
- 积分
- 1628
- 最后登录
- 2021-7-13
|
由于时间和水平的问题不能做太复杂的任务。简单完成使用Python的serial模块接收由LPC54114采集的ADC结果的任务。数据采集完成后可以使用python语言的功能强大的其他模块进行相应的数据计算和处理和图形化显示。程序比较简单,给打算使用python做简单数据采集的网友做参考吧。
下面是Python的程序运行在PC上:
import serial
ser = serial.Serial()
ser.baudrate = 115200
ser.port = 'COM4'
ser.bytesize = 8
ser.parity = 'N'
ser.open()
i = 0
command ="i"
datalist=[]
while (i<1000):
s = ser.readline()
if(len(s)==6):
s = s[0:4]
# value = s[0]*256*256*256+s[1]*256*256+s[2]*256+s[3]
value = s[2]*256+s[3]
datalist.append(value/4095*3.3)
print(i,value/4095*3.3)
if(i%100 == 0):
if(command == "i"):
command=input("-->")
ser.flushInput()
if(command =="b"):
i=100000
i = i+1
ser.close()
下面是单片机上的主程序,主要完成ADC采集和串口发送:
#include "fsl_device_registers.h"
#include "fsl_debug_console.h"
#include "board.h"
#include "fsl_adc.h"
#include "fsl_clock.h"
#include "fsl_power.h"
#include "pin_mux.h"
#include <stdbool.h>
#include <math.h>
/*******************************************************************************
* Definitions
******************************************************************************/
#define DEMO_ADC_BASE ADC0
//#define DEMO_ADC_SAMPLE_CHANNEL_NUMBER 0U
//#define DEMO_ADC_SAMPLE_CHANNEL_NUMBER 3U
#define DEMO_ADC_SAMPLE_CHANNEL_NUMBER 6U
void delay(void) {
volatile uint32_t i;
for (i = 0; i < 100; i++) {
__asm("NOP");
}
}
/*******************************************************************************
* Prototypes
******************************************************************************/
static void ADC_Configuration(void);
/*******************************************************************************
* Code
******************************************************************************/
static void ADC_ClockPower_Configuration(void)
{
/* SYSCON power. */
POWER_DisablePD(kPDRUNCFG_PD_ADC0); /* Power on the ADC converter. */
POWER_DisablePD(kPDRUNCFG_PD_VD7_ENA); /* Power on the analog power supply. */
POWER_DisablePD(kPDRUNCFG_PD_VREFP_SW); /* Power on the reference voltage source. */
POWER_DisablePD(kPDRUNCFG_PD_TEMPS); /* Power on the temperature sensor. */
/* Enable the clock. */
CLOCK_AttachClk(kFRO12M_to_MAIN_CLK);
/* CLOCK_AttachClk(kMAIN_CLK_to_ADC_CLK); */
/* Sync clock source is not used. Using sync clock source and would be divided by 2.
* The divider would be set when configuring the converter.
*/
CLOCK_EnableClock(kCLOCK_Adc0); /* SYSCON->AHBCLKCTRL[0] |= SYSCON_AHBCLKCTRL_ADC0_MASK; */
}
void long_char(uint32_t l,unsigned char *s)
{
*s = l>>24;
*(s+1) = l>>16;
*(s+2) = l>>8;
*(s+3) = l;
}
/*!
* @brief Main function
*/
unsigned char adc_data[4];
int main(void)
{
adc_result_info_t adcResultInfoStruct;
/* Initialize board hardware. */
/* attach 12 MHz clock to FLEXCOMM0 (debug console) */
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
BOARD_InitPins();
BOARD_BootClockFROHF48M();
BOARD_InitDebugConsole();
/* Enable the power and clock for ADC. */
ADC_ClockPower_Configuration();
// PRINTF("ADC basic example.\r\n");
/* Calibration after power up. */
if (ADC_DoSelfCalibration(DEMO_ADC_BASE))
{
// PRINTF("ADC_DoSelfCalibration() Done.\r\n");
}
else
{
PRINTF("ADC_DoSelfCalibration() Failed.\r\n");
}
/* Configure the converter and work mode. */
ADC_Configuration();
// PRINTF("Configuration Done.\r\n");
while (1)
{
/* Get the input from terminal and trigger the converter by software. */
// GETCHAR();
ADC_DoSoftwareTriggerConvSeqA(DEMO_ADC_BASE);
// mulitM(m1,m2);
// delay();
/* Wait for the converter to be done. */
while (!ADC_GetChannelConversionResult(DEMO_ADC_BASE, DEMO_ADC_SAMPLE_CHANNEL_NUMBER, &adcResultInfoStruct))
{
}
// PRINTF("adcResultInfoStruct.result = %ld\n", adcResultInfoStruct.result);
long_char(adcResultInfoStruct.result,adc_data);
uint8_t j=0;
while(j<4)
PRINTF("%c",adc_data[j++]);
// PRINTF("adcResultInfoStruct.channelNumber = %d\r\n", adcResultInfoStruct.channelNumber);
// PRINTF("adcResultInfoStruct.overrunFlag = %d\r\n", adcResultInfoStruct.overrunFlag ? 1U : 0U);
PRINTF("\r\n");
}
}
static void ADC_Configuration(void)
{
adc_config_t adcConfigStruct;
adc_conv_seq_config_t adcConvSeqConfigStruct;
/* Configure the converter. */
adcConfigStruct.clockMode = kADC_ClockSynchronousMode; /* Using sync clock source. */
adcConfigStruct.clockDividerNumber = 1; /* The divider for sync clock is 2. */
adcConfigStruct.resolution = kADC_Resolution12bit;
adcConfigStruct.enableBypassCalibration = false;
adcConfigStruct.sampleTimeNumber = 0U;
ADC_Init(DEMO_ADC_BASE, &adcConfigStruct);
/* Use the temperature sensor input to channel 0. */
ADC_EnableTemperatureSensor(DEMO_ADC_BASE, true);
/* Enable channel 0's conversion in Sequence A. */
// adcConvSeqConfigStruct.channelMask = (1U << 0); /* Includes channel 0. */
// adcConvSeqConfigStruct.channelMask = (1U << 3);
adcConvSeqConfigStruct.channelMask = (1U << 6);
adcConvSeqConfigStruct.triggerMask = 0U;
adcConvSeqConfigStruct.triggerPolarity = kADC_TriggerPolarityNegativeEdge;
adcConvSeqConfigStruct.enableSingleStep = false;
adcConvSeqConfigStruct.enableSyncBypass = false;
adcConvSeqConfigStruct.interruptMode = kADC_InterruptForEachSequence;
ADC_SetConvSeqAConfig(DEMO_ADC_BASE, &adcConvSeqConfigStruct);
ADC_EnableConvSeqA(DEMO_ADC_BASE, true); /* Enable the conversion sequence A. */
}
下面是程序运行的结果,电压值已经被采集到Python里了。
|
-
|