LPC54000
LPC54000 微控制器也可以使用 C 語言編寫 PLC 程序。以下是一個示例程序,實現了讀取 ADC 輸入並將其輸出到串口:
c
#include <stdio.h>
#include "board.h"
#include "fsl_adc.h"
#include "fsl_usart.h"
#define ADC_CHANNEL 1
#define USART_TX_PIN 12U
#define USART_RX_PIN 13U
static void delay(void) {
volatile uint32_t i = 0;
for (i = 0; i < 1000000; ++i) {
__NOP();
}
}
int main(void) {
usart_config_t usartConfig;
adc_config_t adcConfigStruct;
adc_channel_config_t adcChannelConfigStruct;
BOARD_InitPins();
BOARD_BootClockFRO30M();
BOARD_InitDebugConsole();
// 配置 UART
USART_GetDefaultConfig(&usartConfig);
usartConfig.baudRate_Bps = 115200U;
USART_Init(USART0, &usartConfig, SystemCoreClock);
// 配置 ADC
ADC_GetDefaultConfig(&adcConfigStruct);
ADC_Init(ADC0, &adcConfigStruct);
ADC_EnableHardwareTrigger(ADC0, false);
ADC_DoAutoCalibration(ADC0);
adcChannelConfigStruct.channelNumber = ADC_CHANNEL;
adcChannelConfigStruct.enableInterruptOnConversionCompleted = false;
ADC_SetChannelConfig(ADC0, 0U, &adcChannelConfigStruct);
while (1) {
// 啟動 ADC 轉換
ADC_StartConversion(ADC0, 0U);
// 等待 ADC 轉換完成
while (!(ADC_GetStatusFlags(ADC0) & kADC_ConversionCompletedFlag));
// 讀取 ADC 值並輸出到串口
uint32_t result = ADC_GetChannelConversionValue(ADC0, 0U);
char buf;
snprintf(buf, sizeof(buf), "ADC value: %d\r\n", result);
USART_WriteBlocking(USART0, (uint8_t *)buf, strlen(buf));
// 延時 500ms
delay();
}
}
頁:
[1]