RA6E2的DAC和ADC皆是12bit,通常见到的DAC基本上都是10bit,这个对于要求高的项目是一个不错的选择,除了精度以外还有稳定性,那么本次测试就是围绕着DAC输出进行测试。本测试主要参考了:最新的ek_ra6e2开发板的DAC项目,
首先是建立项目dac_FSP_Project
项目模板选择FBP_RA6E2,项目类型选择keil。
关键的是DAC的设置,这里有个坑需要注意,前一阶段就因为这个坑无法实现DAC输出。
设置如上,注意画红线部分,这个就是我说的坑,一定要打开这个项目,不然将无法输出。
项目的加入uart模块作为输出,引脚P410和P411
这是全部代码,主要是打开DAC,和设置输出电压。
/***********************************************************************************************************************
* Copyright [2020-2023] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved.
*
* This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products
* of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are
* sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use
* of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property
* right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas
* reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION
* IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT
* PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR
* DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM
* EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION
* (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING,
* WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS,
* OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS.
**********************************************************************************************************************/
#include "hal_data.h"
#include <stdio.h>
/* Macro definition */
#define CARRIAGE_ASCII (13u) /* Carriage return */
#define ZERO_ASCII (48u) /* ASCII value of zero */
#define NINE_ASCII (57u) /* ASCII value for nine */
#define DATA_LENGTH (4u) /* Expected Input Data length */
#define UART_ERROR_EVENTS (UART_EVENT_BREAK_DETECT | UART_EVENT_ERR_OVERFLOW | UART_EVENT_ERR_FRAMING | \
UART_EVENT_ERR_PARITY) /* UART Error event bits mapped in registers */
#define RESET_VALUE (0x00)
void R_BSP_WarmStart(bsp_warm_start_event_t event);
extern bsp_leds_t g_bsp_leds;
/* Flag for user callback */
static volatile uint8_t g_uart_event = RESET_VALUE;
static uint8_t g_temp_buffer[DATA_LENGTH] = {RESET_VALUE};
/* Counter to update g_temp_buffer index */
static volatile uint8_t g_counter_var = RESET_VALUE;
/* Flag to check whether data is received or not */
static volatile uint8_t g_data_received_flag = false;
/*******************************************************************************************************************//**
* [url=home.php?mod=space&uid=159083]@brief[/url] Blinky example application
*
* Blinks all leds at a rate of 1 second using the software delay function provided by the BSP.
*
**********************************************************************************************************************/
void hal_entry (void)
{
fsp_err_t err = FSP_SUCCESS;
uint32_t local_timeout = (DATA_LENGTH * UINT16_MAX);
unsigned char send_buff[32]="RA Keil DAC\r";
uint16_t output = 0;
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
/* Define the units to be used with the software delay function */
const bsp_delay_units_t bsp_delay_units = BSP_DELAY_UNITS_MILLISECONDS;
/* Set the blink frequency (must be <= bsp_delay_units */
const uint32_t freq_in_hz = 2;
/* Calculate the delay in terms of bsp_delay_units */
const uint32_t delay = bsp_delay_units / freq_in_hz;
/* LED type structure */
bsp_leds_t leds = g_bsp_leds;
err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg);
if(FSP_SUCCESS != err) __BKPT();
size_t buff_len = strlen((char *)send_buff);
err = R_SCI_UART_Write(&g_uart0_ctrl, send_buff, buff_len);
if(FSP_SUCCESS != err) __BKPT();
while ((UART_EVENT_TX_COMPLETE != g_uart_event) && (--local_timeout))
{
/* Check if any error event occurred */
if (UART_ERROR_EVENTS == g_uart_event)
{
break;
}
}
err = R_DAC_Open (&g_dac0_ctrl, &g_dac0_cfg);
output =(uint16_t)(1.5/3.3*4096);
err = R_DAC_Write (&g_dac0_ctrl, output);
err = R_DAC_Start (&g_dac0_ctrl);
/* If this board has no LEDs then trap here */
if (0 == leds.led_count)
{
while (1)
{
; // There are no LEDs on this board
}
}
/* Holds level to set for pins */
bsp_io_level_t pin_level = BSP_IO_LEVEL_LOW;
memset(send_buff,0,32);
sprintf((char *)send_buff, "a0=%f\r",1.5);
buff_len = strlen((char *)send_buff);
err = R_SCI_UART_Write(&g_uart0_ctrl, send_buff, buff_len);
if(FSP_SUCCESS != err) __BKPT();
while ((UART_EVENT_TX_COMPLETE != g_uart_event) && (--local_timeout))
{
/* Check if any error event occurred */
if (UART_ERROR_EVENTS == g_uart_event)
{
break;
}
}
while (1)
{
/* Enable access to the PFS registers. If using r_ioport module then register protection is automatically
* handled. This code uses BSP IO functions to show how it is used.
*/
R_BSP_PinAccessEnable();
/* Update all board LEDs */
for (uint32_t i = 0; i < leds.led_count; i++)
{
/* Get pin to toggle */
uint32_t pin = leds.p_leds[i];
/* Write to this pin */
R_BSP_PinWrite((bsp_io_port_pin_t) pin, pin_level);
}
/* Protect PFS registers */
R_BSP_PinAccessDisable();
/* Toggle level for next write */
if (BSP_IO_LEVEL_LOW == pin_level)
{
pin_level = BSP_IO_LEVEL_HIGH;
}
else
{
pin_level = BSP_IO_LEVEL_LOW;
}
/* Delay */
R_BSP_SoftwareDelay(delay, bsp_delay_units);
}
}
/*******************************************************************************************************************//**
* This function is called at various points during the startup process. This implementation uses the event that is
* called right before main() to set up the pins.
*
* @param[in] event Where at in the start up process the code is currently at
**********************************************************************************************************************/
void R_BSP_WarmStart (bsp_warm_start_event_t event)
{
if (BSP_WARM_START_RESET == event)
{
#if BSP_FEATURE_FLASH_LP_VERSION != 0
/* Enable reading from data flash. */
R_FACI_LP->DFLCTL = 1U;
/* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
* C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
#endif
}
if (BSP_WARM_START_POST_C == event)
{
/* C runtime environment and system clocks are setup. */
/* Configure pins. */
R_IOPORT_Open(&g_ioport_ctrl, g_ioport.p_cfg);
}
}
/*****************************************************************************************************************
* @brief UART user callback
* @param[in] p_args
* @retval None
****************************************************************************************************************/
void user_uart_callback(uart_callback_args_t *p_args)
{
/* Logged the event in global variable */
g_uart_event = (uint8_t)p_args->event;
/* Reset g_temp_buffer index if it exceeds than buffer size */
if(DATA_LENGTH == g_counter_var)
{
g_counter_var = RESET_VALUE;
}
if(UART_EVENT_RX_CHAR == p_args->event)
{
switch (p_args->data)
{
/* If Enter is pressed by user, set flag to process the data */
case CARRIAGE_ASCII:
{
g_counter_var = RESET_VALUE;
g_data_received_flag = true;
break;
}
/* Read all data provided by user until enter button is pressed */
default:
{
g_temp_buffer[g_counter_var++] = (uint8_t ) p_args->data;
break;
}
}
}
}
DAC输出设置为1.5V电压。万用表输出为1.5104V,这个电压输出比较稳定,没有出现较大的波动。
|