跑马灯已经在前一个程序做好,接下来ADC+uart程序走起。
硬件原理图 + FSP代码配置
配置ADC
硬件原理图说明
ADC引脚: P000
生成代码说明
双击 configuration.xml 打开配置界面->Stacks->New Thread
New Statck->Analog->ADC(r_adc)来配置ADC模块
UART4
硬件原理图说明
从图中可以看出USB转串口使用的UART4,对应的是P512,P511。同时需要将J23的跳线帽接上。
生成代码说明
代码管理器->configuration.xml->Pins->Peripherals->Connectivity:SCI->SCI4
New Statck->Connectivity->UART(r_sci_uart)来配置UART模块
使用 printf 函数时,需要使用到堆,默认情况下堆的大小为 0,因此我们需要修改堆的大小。
修改ADC Thread源码
#include "adc_thread.h"
#include <stdio.h>
void UART4_Init(void);
void adc_Init(void);
double adc_read(void);
void UART4_Init(void)
{
fsp_err_t err = FSP_SUCCESS;
err = R_SCI_UART_Open(&g_uart4_ctrl, &g_uart4_cfg);
assert(FSP_SUCCESS == err);
}
volatile bool uart_send_complete_flag = false;
void uart4_callback(uart_callback_args_t * p_args)
{
switch (p_args->event)
{
case UART_EVENT_RX_CHAR:
{
break;
}
case UART_EVENT_TX_COMPLETE:
{
uart_send_complete_flag = true;
break;
}
default:
break;
}
}
#if defined __GNUC__ && !defined __clang__
int _write(int fd, char *pBuffer, int size);
int _write(int fd, char *pBuffer, int size)
{
(void)fd;
R_SCI_UART_Write(&g_uart4_ctrl, (uint8_t *)pBuffer, (uint32_t)size);
while(uart_send_complete_flag == false);
uart_send_complete_flag = false;
return size;
}
#else
int fputc(int ch, FILE *f)
{
(void)f;
R_SCI_UART_Write(&g_uart4_ctrl, (uint8_t *)&ch, 1);
while(uart_send_complete_flag == false);
uart_send_complete_flag = false;
return ch;
}
#endif
void adc_Init(void)
{
fsp_err_t err;
err = R_ADC_Open(&g_adc0_ctrl, &g_adc0_cfg);
err = R_ADC_ScanCfg(&g_adc0_ctrl, &g_adc0_channel_cfg);
assert(FSP_SUCCESS == err);
}
volatile bool scan_complete_flag = false;
void adc_callback(adc_callback_args_t * p_args)
{
FSP_PARAMETER_NOT_USED(p_args);
xSemaphoreGiveFromISR(g_adc_semaphore, NULL);
}
double adc_read(void)
{
uint16_t adc_data;
double a0;
(void)R_ADC_ScanStart(&g_adc0_ctrl);
xSemaphoreTake(g_adc_semaphore, portMAX_DELAY);
R_ADC_Read(&g_adc0_ctrl, ADC_CHANNEL_0, &adc_data);
a0 = (double)(adc_data * 3.3 / 4095);
return a0;
}
void adc_thread_entry(void *pvParameters)
{
FSP_PARAMETER_NOT_USED (pvParameters);
UART4_Init();
adc_Init();
while (1)
{
vTaskDelay (1000);
printf("adc_value: %.3f\r\n", adc_read());
}
}
FreeRTOS+ADC+UART
|