基于英飞凌TC275通过ADC采集电位器值进行PWM控制LED闪烁频率
通过TC275三核MCU中的CPU0,通过内部ADC模块采集外部电位器电压,然后通过PWM模块调整LED闪烁频率
标签
嵌入式系统
Funpack活动
ADC
PWM
TC275
林伟521
更新2022-10-13
哈尔滨理工大学
801

1,板卡介绍

 (1)TC275CPU介绍

三核微控制器(TC1.6P)(TC1.6E)

每核主频均200Mhz

片内达4MB的flash存储

384KB的DFLASH 带有ECC错误校验

多外设接口(4路ASCLIN,4路QSPI,HSSL,4路CAN,ETHERNET,VADC,DSADC)

硬件安全模块。

(2)板载外围模块:

 搭载了基于AURIX™ TriCore™ 单片三核微控制器TC275

  • 板载Micro USB接口的miniWiggler调试器
  • 两个Infineon Shield2Go扩展接口
  • 兼容MikroBUS 和Arduino扩展连接
  • 带有Infineon新一代CAN 收发器TLE9251VSJ ,可用于汽车和工业应用的HS CAN网络开发
  • 已焊接可调旋转电位计,用于评估模拟电压的采集
  • 一个用户输入按键
  • 预留三个LED可作为工作指示灯

2.任务实现原理

我选择的题目是设计一个呼吸灯,通过旋转板卡上的电位计,改变呼吸灯闪烁速率,同时将ADC采集的数据通过串口,发送到PC上显示。

我们需要用到TC275三核cpu中的cpu0,其他两个cpu没有用到。用到内部的ADC模块,用来采集外部电位器的实时变化电压值(0-3.3V),经过ADC模块转换,取值范围为0-4095,我们使得LED闪烁,需要用到GTM模块内的ATOM,用于PWM控制,我们通过ADC获得到的值用来改变PWM的频率,从而达到LED闪烁的快慢效果。

3,代码示例说明

int core0_main(void)
{
    IfxCpu_enableInterrupts();

    /* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!
     * Enable the watchdogs and service them periodically if it is required
     */
    IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
    IfxScuWdt_disableSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());

    /* Wait for CPU sync event */
    IfxCpu_emitEvent(&g_cpuSyncEvent);
    IfxCpu_waitEvent(&g_cpuSyncEvent, 1);

    initShellInterface();                 /* Initialize the module                  */
   // IfxCpu_enableInterrupts();
    /* Initialize VADC */
    vadcBackgroundScanInit();

    /* Start the background scan */
    vadcBackgroundScanRun();

    /* Initialize a time variable */
    Ifx_TickTime ticksFor10ms = IfxStm_getTicksFromMilliseconds(BSP_DEFAULT_TIMER, WAIT_TIME);

    /* Call the initialization function */
    initGtmATomPwm();
    int adc_val = 0;
    while(1)
    {
        /* Update the LEDs depending on the measured value */
        adc_val = indicateConversionValue();
        runShellInterface(adc_val);
        if(adc_val < 20){
            fadeLED(adc_val/4 + 20);      /* Change the intensity of the LED  */
        }else{
            fadeLED(adc_val/4);
        }
        waitTime(ticksFor10ms);

    }
    return (1);
}

我们使用的是cpu0核心,所有外设功能都在cpu0实现,以上是基于英飞凌官方例程,我们将uart函数初始化,ADC模块初始化,PWM模块初始化,进行移植过来,其中串口初始化为

void initShellInterface(void)
{
    /* Initialize the hardware peripherals */
    //initLEDs();
    initSerialInterface();

    /* Initialize the Standard Interface */
    IfxAsclin_Asc_stdIfDPipeInit(&g_ascStandardInterface, &g_asclin);

    /* Initialize the Console */
    Ifx_Console_init(&g_ascStandardInterface);

    /* Print info to the console */
    printInfo(&g_ascStandardInterface);
    Ifx_Console_print(ENDLINE "Enter '" COMMAND_HELP "' to see the available commands" ENDLINE);

    /* Initialize the shell */
    Ifx_Shell_Config shellConf;
    Ifx_Shell_initConfig(&shellConf);                       /* Initialize the structure with default values         */

    shellConf.standardIo = &g_ascStandardInterface;         /* Set a pointer to the standard interface              */
    shellConf.commandList[0] = &g_shellCommands[0];         /* Set the supported command list                       */

    Ifx_Shell_init(&g_shellInterface, &shellConf);          /* Initialize the Shell with the given configuration    */
}

我们输出打印函数如下:

void runShellInterface(int a)
{
    /* Process the received data */
    IfxStdIf_DPipe_print(&g_ascStandardInterface, "adc=%d. \r\n",a);

}

ADC模块的初始化

    /* Initialize VADC */
    vadcBackgroundScanInit();
/* The VADC module and group are initialized */
void vadcBackgroundScanInit(void)
{
    /* VADC module configuration */
    /* Create VADC configuration */
    IfxVadc_Adc_Config adcConfig;

    /* Initialize the VADC configuration with default values */
    IfxVadc_Adc_initModuleConfig(&adcConfig, &MODULE_VADC);

    /* Initialize the VADC module using the VADC configuration */
    IfxVadc_Adc_initModule(&g_vadcBackgroundScan.vadc, &adcConfig);

    /* VADC group configuration */
    /* Create group configuration */
    IfxVadc_Adc_GroupConfig adcGroupConfig;

    /* Initialize the group configuration with default values */
    IfxVadc_Adc_initGroupConfig(&adcGroupConfig, &g_vadcBackgroundScan.vadc);

    /* Define which ADC group is going to be used */
    adcGroupConfig.groupId = VADC_GROUP;
    adcGroupConfig.master = VADC_GROUP;

    /* Enable background scan source */
    adcGroupConfig.arbiter.requestSlotBackgroundScanEnabled = TRUE;

    /* Enable background auto scan mode */
    adcGroupConfig.backgroundScanRequest.autoBackgroundScanEnabled = TRUE;

    /* Enable the gate in "always" mode (no edge detection) */
    adcGroupConfig.backgroundScanRequest.triggerConfig.gatingMode = IfxVadc_GatingMode_always;

    /* Initialize the group using the group configuration */
    IfxVadc_Adc_initGroup(&g_vadcBackgroundScan.adcGroup, &adcGroupConfig);
}

初始化后,我们进行使能该模块

/* The input channels to be used are setup and the VADC is set into run mode */
void vadcBackgroundScanRun(void)
{
    /* Initialize the channel configuration of application handle g_vadcBackgroundScan with default values */
    IfxVadc_Adc_initChannelConfig(&g_vadcBackgroundScan.adcChannelConfig, &g_vadcBackgroundScan.adcGroup);

    g_vadcBackgroundScan.adcChannelConfig.channelId = (IfxVadc_ChannelId)CHANNEL_ID;
    g_vadcBackgroundScan.adcChannelConfig.resultRegister = (IfxVadc_ChannelResult)CHANNEL_RESULT_REGISTER;
    g_vadcBackgroundScan.adcChannelConfig.backgroundChannel = TRUE;

    /* Initialize the channel of application handle g_VadcBackgroundScan using the channel configuration */
    IfxVadc_Adc_initChannel(&g_vadcBackgroundScan.adcChannel, &g_vadcBackgroundScan.adcChannelConfig);

    /* Enable background scan for the channel */
    IfxVadc_Adc_setBackgroundScan(&g_vadcBackgroundScan.vadc,
                                  &g_vadcBackgroundScan.adcGroup,
                                  (1 << (IfxVadc_ChannelId)CHANNEL_ID),
                                  (1 << (IfxVadc_ChannelId)CHANNEL_ID));

    /* Start background scan conversion */
    IfxVadc_Adc_startBackgroundScan(&g_vadcBackgroundScan.vadc);
}

PWM模块

/* This function initializes the ATOM */
void initGtmATomPwm(void)
{
    IfxGtm_enable(&MODULE_GTM); /* Enable GTM */

    IfxGtm_Cmu_setClkFrequency(&MODULE_GTM, IfxGtm_Cmu_Clk_0, CLK_FREQ);        /* Set the CMU clock 0 frequency    */
    IfxGtm_Cmu_enableClocks(&MODULE_GTM, IFXGTM_CMU_CLKEN_CLK0);                /* Enable the CMU clock 0           */

    IfxGtm_Atom_Pwm_initConfig(&g_atomConfig, &MODULE_GTM);                     /* Initialize default parameters    */

    g_atomConfig.atom = LED.atom;                                       /* Select the ATOM depending on the LED     */
    g_atomConfig.atomChannel = LED.channel;                             /* Select the channel depending on the LED  */
    g_atomConfig.period = PWM_PERIOD;                                   /* Set timer period                         */
    g_atomConfig.pin.outputPin = &LED;                                  /* Set LED as output                        */
    g_atomConfig.synchronousUpdateEnabled = TRUE;                       /* Enable synchronous update                */

    IfxGtm_Atom_Pwm_init(&g_atomDriver, &g_atomConfig);                 /* Initialize the PWM                       */
    IfxGtm_Atom_Pwm_start(&g_atomDriver, TRUE);                         /* Start the PWM                            */
}

我们所改变的就是  ATOM的PWM的周期PWM_PERIOD

/* This function is creating the fade effect for the LED */
void fadeLED(int per)
{
    if(g_fadeValue >= per)
    {
        g_fadeDir = -1; /* Set the direction of the fade */
    }
    else if(g_fadeValue <= 0)
    {
        g_fadeDir = 1;  /* Set the direction of the fade */
    }
    g_fadeValue += g_fadeDir * FADE_STEP; /* Calculation of the new duty cycle */
    setDutyCycle(g_fadeValue,per); /* Call the function which is setting the duty cycle of the PWM */
}

/* This function sets the duty cycle of the PWM */
void setDutyCycle(uint32 dutyCycle,int per)
{
    g_atomConfig.period = per;
    g_atomConfig.dutyCycle = dutyCycle;                 /* Set duty cycle        */
    IfxGtm_Atom_Pwm_init(&g_atomDriver, &g_atomConfig); /* Re-initialize the PWM */
}

上面的就是PWM核心代码,传入的per值改变period,即可改变LED闪烁的快慢。

4,成果展示

我们可以看到下面ADC模块采到的值,通过uart发送到PC串口显示图:

Fo_bDMXKJT-QhE6iY0IxhxQjnFER

FufL1K8T9LkMOiI3TUu6vLNf8TI9

5,心得体会

们虽然完成了本次课题的目标,但是只用到了该芯片一点点的功能,在众多外设中,也只有用到了最基本的外设,这个在实际工作中,不会用到这么简单的东西,所以,我们要在以后的学习生活中,继续挖掘该芯片内部的重要模块,这样才能在工作中游刃有余,不会力不足信。

 

附件下载
GTM_ATOM_PWM_1_KIT_TC275_LK.rar
团队介绍
目前一人身兼硬件画板,软件工程师
团队成员
林伟521
评论
0 / 100
查看更多
目录
硬禾服务号
关注最新动态
0512-67862536
info@eetree.cn
江苏省苏州市苏州工业园区新平街388号腾飞创新园A2幢815室
苏州硬禾信息科技有限公司
Copyright © 2023 苏州硬禾信息科技有限公司 All Rights Reserved 苏ICP备19040198号