打印

【东软载波ESF0654 PDS开发板活动】+串口显示ADC值

[复制链接]
1718|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
dami|  楼主 | 2019-12-18 21:13 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
【东软载波ESF0654 PDS开发板活动】+串口显示ADC值
接着上次的串口通讯程序,把ADC采集加上。由串口显示出来。
1,ADC是PB0    ADC0 14通道:
main.h中加入:
#include "md_adc.h"
#include "md_uart.h"
/* ADC IO pin definition */
#define GPIO_CH_PORT          GPIOB
#define ADC_CH14_PIN          MD_GPIO_PIN_0

#define UART2_TX_PORT        GPIOC
#define UART2_TX_PIN        MD_GPIO_PIN_12
#define UART2_RX_PORT        GPIOD
#define UART2_RX_PIN        MD_GPIO_PIN_2
main.c中加入:
#include <string.h>
#include "main.h"
#include "md_gpio.h"
md_uart_init_t init;
uint8_t tx_buf[64];
uint8_t rx_buf[64];
/** @addtogroup Projects_Examples_MD
  * @{
  */

/** @addtogroup Examples_ADC
  * @{
  */

/**
  * [url=home.php?mod=space&uid=247401]@brief[/url]  Initializate buf of adc normal value.
  */
uint32_t nm_value;
/**
  * @brief  adc_handle_t structure.
  */
adc_init_t h_adc;

/**
  * @brief  Initializate pin of adc module.
  * @retval None
  */
void adc_pin_init(void)
{
        md_gpio_init_t x;

        /* Initialize sck pin */
        x.mode = GPIO_MODE_INPUT;
        x.pupd = GPIO_PUSH_UP;
        x.odrv = GPIO_OUT_DRIVE_NORMAL;
        x.flt  = GPIO_FILTER_DISABLE;
        x.type = GPIO_TYPE_CMOS;
        x.func = GPIO_FUNC_0;
        md_gpio_init(GPIO_CH_PORT, ADC_CH14_PIN, &x);

        return;
}
void uart_pin_init(void)
{
        md_gpio_init_t x;

        /* Initialize tx pin */
        x.mode = GPIO_MODE_OUTPUT;
        x.odos = GPIO_PUSH_PULL;
        x.pupd = GPIO_PUSH_UP;
        x.odrv = GPIO_OUT_DRIVE_NORMAL;
        x.flt  = GPIO_FILTER_DISABLE;
        x.type = GPIO_TYPE_TTL;
        x.func = GPIO_FUNC_5;
        md_gpio_init(UART2_TX_PORT, UART2_TX_PIN, &x);

        /* Initialize rx pin */
        x.mode = GPIO_MODE_INPUT;
        x.odos = GPIO_PUSH_PULL;
        x.pupd = GPIO_PUSH_UP;
        x.odrv = GPIO_OUT_DRIVE_NORMAL;
        x.flt  = GPIO_FILTER_DISABLE;
        x.type = GPIO_TYPE_TTL;
        x.func = GPIO_FUNC_5;
        md_gpio_init(UART2_RX_PORT, UART2_RX_PIN, &x);

        return;
}

/**
  * @brief  Test main function
  * @retval Status.
  */
int main()
{
        /* Initialize 1ms tick */
        md_init_1ms_tick();

        /* clear adc_handle_t structure */
        memset(&h_adc, 0x0, sizeof(h_adc));
       
        /* Initialize adc pin */
        adc_pin_init();

        /* Initialize ADC0 */
        md_adc_struct_init(&h_adc);
        md_adc_init(ADC0, &h_adc);

        /* Initialize normal convert channel */
        md_adc_set_normal_1st_conv_ns1(ADC0, ADC_NC_RANK_14);
        md_adc_set_smpt1_cht(ADC0, ADC_SAMPLETIME_4);
       
        /* clear md_uart_init_t structure */
        memset(&init, 0x0, sizeof(md_uart_init_t));
        /* Initialize pin */
        uart_pin_init();
        /* Initialize tx_buf */
        memset(tx_buf, 0x55, 32);

        /* Initialize UART */
        init.baud        = 115200;
        init.word_length = MD_UART_WORD_LENGTH_8B;
        init.stop_bits   = MD_UART_STOP_BITS_1;
        init.parity      = MD_UART_PARITY_NONE;
        init.fctl        = MD_UART_FLOW_CTL_DISABLE;
        md_uart_init(UART2, &init);

        /* Send a message */
        md_uart_send_data8(UART2, tx_buf[0]);
        /* Receive a message */
        rx_buf[0] = (uint8_t)md_uart_recv_data8(UART2);

       
        while (1) {       
                  uint8_t i;
      /* Enable ADC0 and start normal convert */
            md_adc_converter_enable_adcen(ADC0);
            md_adc_set_normal_channel_conv_start_ichtrg(ADC0);
       
            /* Wait convert finish */
            while (md_adc_get_stat_nche(ADC0) == 0);
       
            /* Read normal convert result */
            nm_value = md_adc_get_normal_channel_val(ADC0);               
                  tx_buf[0] =nm_value>>24;
                  tx_buf[1] =nm_value>>16;
                  tx_buf[2] =nm_value>>8;
                  tx_buf[3] =nm_value;
                  tx_buf[4] =0x0d;
                  tx_buf[5] =0x0a;
                  for(i=0;i<6;i++)
                  {       
                                md_uart_send_data8(UART2, tx_buf[i]);
                                md_delay_1ms(1);
                  }
                        md_delay_1ms(1000);
        }
}
编译运行:

连接串口3 设置波特率115200 N 8 1 打开串口,扭动ADC按钮 观察数据变化:



使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

61

主题

1042

帖子

5

粉丝