1.任务目的
设计一个呼吸灯,通过旋转板卡上的电位计,改变呼吸灯闪烁速率,同时将ADC采集的数据通过串口/CAN,发送到另一台设备上显示
2.硬件介绍
- 主机处理器:TC275车规级芯片,具有 200MHz 和 DSP 功能的三重三核(三个高性能 32 位超标量三核 V1.6.1 CPU,在整个汽车温度范围内以 200MHz 运行)
- 内置4MB 闪存,带 ECC 保护(每个内核专用紧密耦合的内存区域)、64 KB EEPROM,500 k 周期、472KB RAM,带 ECC 保护
- Arduino 连接器
- Arduino ICSP 连接器
- 稳压器 5V 至 3.3V
- 可选 0 欧姆电阻器(1210 英制中的 R39_opt/R40_opt)
- Arduino 连接器(数字)
- 用于 AURIX™ TM的 20 MHz 晶振和用于 OCDS 的 12 MHz 晶振
- 用于 WIFI/BLE 的 Mikrobus 连接器
- 英飞凌 CAN 收发器 TLE9251VSJ 和 CAN 连接器
- 针连接器 X2
- 电源指示灯 (D5)
- LED D1/D2 用于 ADBUS7/4 和 LED3 用于 ESR0 信号(低电平有效)
- Arduino 针连接器(电源和模拟输入)
- 电位器 (10 kOhm) 和可焊接的 0 Ohm 电阻器(0805 英制中的 R33)
- 微型 USB(推荐 USB3.0)
- 10 针 DAP 连接器
- 复位按钮
- 2 x Shield2GO 连接器,用于 Infineon Maker Shields
- EEPROM 1Kbit
3.主要代码
CPU0主函数
(1).关闭看门狗,等待CPU同步事件,获取1ms时定时时间大小,初始化外设,开启ADC转换。
(2).读取ADC的值(16位),将其转换为延时大小(0-4096转为1-52)
(3).通过串口将采集的ADC值发送出来
(4).根据延时大小调节循环次数(为了保证每次通过串口发送ADC值的时间相同,不会出现反应过慢的问题)
(5).调用fadeLED函数(每调用100次产生一次完整的呼吸灯流程)并延时
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);
init_UART(); /* Initialize the module */
Ifx_TickTime ticksFor1ms = IfxStm_getTicksFromMilliseconds(BSP_DEFAULT_TIMER, 1);
initGtmATomPwm();
/* Initialize VADC */
vadcBackgroundScanInit();
/* Start the background scan */
vadcBackgroundScanRun();
uint8 str[50];
uint8 wait_time = 10;
while(1)
{
uint16 adc_value = read_ADC_Value();
wait_time = adc_value/80 + 1;
sprintf(str,"ADC:%d\r\n",adc_value);
send_UART_string(str);
for(uint8 i=0; i<200/wait_time; i++)
{
fadeLED();
waitTime(ticksFor1ms*wait_time);
}
}
return (1);
}
呼吸灯产生函数(通过PWM控制)
此函数每次调用会自动执行呼吸灯整个流程的一小部分
void fadeLED(void)
{
if(g_fadeValue >= PWM_PERIOD)
{
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); /* 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)
{
g_atomConfig.dutyCycle = dutyCycle; /* Set duty cycle */
IfxGtm_Atom_Pwm_init(&g_atomDriver, &g_atomConfig); /* Re-initialize the PWM */
}
ADC采集函数
此函数为阻塞式读取,当ADC未转换完毕时,会不断循环读取,直到获取到ADC的值。
uint16 read_ADC_Value(void)
{
Ifx_VADC_RES conversionResult;
/* Retrieve the conversion value until valid flag of the result register is true */
do
{
conversionResult = IfxVadc_Adc_getResult(&g_vadcBackgroundScan.adcChannel);
}
while (!conversionResult.B.VF);
return conversionResult.B.RESULT;
}
串口打印函数
先通过strlen获取字符串长度(此函数要求字符串结尾须有‘\0’),之后调用串口打印函数打印。
void send_UART_string(uint8 *str)
{
Ifx_SizeT count = strlen(str); /* Size of the message */
IfxAsclin_Asc_write(&g_asc, str, &count, TIME_INFINITE);
}
4.实际演示
呼吸灯闪烁
调节电位器后,呼吸灯频率改变
通过串口将主控采集的ADC数据通过串口上传PC,并使用串口助手显示,旋转电位器时打印输出的ADC值也发生了变化
5.总结感想
这次活动扩宽了我的视野,了解了英飞凌这个厂商,很高兴能参加硬禾学堂和得捷电子举办的这一期的活动。
总体感觉非常棒,希望类似活动能够一直搞下去。希望硬禾知名度不断扩大,更多方式盈利,给热爱电子的小伙伴带来更多好玩的东东。
微信交流群氛围很好,小伙伴们很热心,有很多大咖,知识面很广,遇到的问题也都是通过在群中沟通解决的。
虽然是一个简单的任务,但装环境,调试代码,解bug。只有参与了,才知其中滋味。
6.意见建议
资料界面可以增加一些板子上手的内容,比如环境搭建,快速入门(引入一些国内的资料,能让小伙伴们更快速的上手)之类的。
最后吐吐槽,这1000字的要求确实苦手啊。。。。。特别是做的题目简单,完全不知道该写些啥。。。。。