本帖最后由 zhoushangshi 于 2024-6-24 10:25 编辑
#申请原创# 【英飞凌CYW20829测评】-04-RGBLED灯点亮红橙黄绿青蓝紫白
这一帖展示用PWM方式点亮RGBLED灯,依次点亮红色、橙色、黄色、绿色、青色、蓝色、紫色、白色.
建立一个工程,点击File-New-ModusToolbox Application
索引20829,出来列表,选择开发板CYW920829M2EVK-02
选择Peripherals-HAL PWM Square Wave,工程名字改为RGB
创建完后,MAIN.C文件上可以看到:
必要的初始化:
串口初始化和打印字符串:
/* Initialize the retarget-io to use the debug UART port */
result = cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX,
CY_RETARGET_IO_BAUDRATE);
handle_error(result);
/* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
printf("\x1b[2J\x1b[;H");
printf("****************** "
"HAL: PWM square wave "
"****************** \r\n\n");
用户灯1设置成PWM模式,2HZ 50%占空比:
result = cyhal_pwm_init(&pwm_led_control, CYBSP_USER_LED, NULL);
设置PWM频率、占空比:
result = cyhal_pwm_set_duty_cycle(&pwm_led_control, PWM_DUTY_CYCLE,
PWM_FREQUENCY);
开始输出PWM:
result = cyhal_pwm_start(&pwm_led_control);
点击烧写:
烧写完看到用户灯1闪亮:
加上自己的想法,一上电用户灯1闪3秒:char CYCLE_VAULE=99;
char updown_flag=1;
char timenums=0;
for (;;)
{
/* Set the PWM output frequency and duty cycle */
result = cyhal_pwm_set_duty_cycle(&pwm_led_control, CYCLE_VAULE,
100000);
check_status("API cyhal_pwm_set_duty_cycle failed with error code", result);
/* Start the PWM */
result = cyhal_pwm_start(&pwm_led_control);
check_status("API cyhal_pwm_start failed with error code", result);
if(updown_flag==0)
{
CYCLE_VAULE++;
if(CYCLE_VAULE>=100)
{
updown_flag=1;
}
}
else
{
CYCLE_VAULE--;
if(CYCLE_VAULE==0)
{
updown_flag=0;
timenums++;
if(timenums>=3)
break;
}
}
cyhal_system_delay_ms(20);
/* Put the CPU into sleep mode to save power */
// cyhal_syspm_sleep();
}
cyhal_pwm_free(&pwm_led_control);
用户灯1效果:
RGB三种IO的PWM初始化:
/* Initialize the PWM */
result = cyhal_pwm_init(&pwm_led_rgbred_control, CYBSP_LED_RGB_RED, NULL);
check_status("API cyhal_pwm_init rgbred failed with error code", result);
/* Initialize the PWM */
result = cyhal_pwm_init(&pwm_led_rgbgreen_control, CYBSP_LED_RGB_GREEN, NULL);
check_status("API cyhal_pwm_init rgbgreen failed with error code", result);
/* Initialize the PWM */
result = cyhal_pwm_init(&pwm_led_rgbblue_control, CYBSP_LED_RGB_BLUE, NULL);
check_status("API cyhal_pwm_init rgbblue failed with error code", result);
依次点亮八种颜色,红色、橙色、黄色、绿色、青色、蓝色、紫色、白色,每种颜色点亮1秒:
for(;;)
{
//红色
RGB_Light(PWM_FREQUENCY_RGBRED_red,PWM_DUTY_CYCLE_RGBRED_red,\
PWM_FREQUENCY_RGBGREEN_red,PWM_DUTY_CYCLE_RGBGREEN_red,\
PWM_FREQUENCY_RGBBLUE_red,PWM_DUTY_CYCLE_RGBBLUE_red
);
cyhal_system_delay_ms(1000);
//橙色
RGB_Light(PWM_FREQUENCY_RGBRED_Orange,PWM_DUTY_CYCLE_RGBRED_Orange,\
PWM_FREQUENCY_RGBGREEN_Orange,PWM_DUTY_CYCLE_RGBGREEN_Orange,\
PWM_FREQUENCY_RGBBLUE_Orange,PWM_DUTY_CYCLE_RGBBLUE_Orange
);
cyhal_system_delay_ms(1000);
//黄色
RGB_Light(PWM_FREQUENCY_RGBRED_Yellow,PWM_DUTY_CYCLE_RGBRED_Yellow,\
PWM_FREQUENCY_RGBGREEN_Yellow,PWM_DUTY_CYCLE_RGBGREEN_Yellow,\
PWM_FREQUENCY_RGBBLUE_Yellow,PWM_DUTY_CYCLE_RGBBLUE_Yellow
);
cyhal_system_delay_ms(1000);
//绿色
RGB_Light(PWM_FREQUENCY_RGBRED_Green,PWM_DUTY_CYCLE_RGBRED_Green,\
PWM_FREQUENCY_RGBGREEN_Green,PWM_DUTY_CYCLE_RGBGREEN_Green,\
PWM_FREQUENCY_RGBBLUE_Green,PWM_DUTY_CYCLE_RGBBLUE_Green
);
cyhal_system_delay_ms(1000);
//青色
RGB_Light(PWM_FREQUENCY_RGBRED_Cyan,PWM_DUTY_CYCLE_RGBRED_Cyan,\
PWM_FREQUENCY_RGBGREEN_Cyan,PWM_DUTY_CYCLE_RGBGREEN_Cyan,\
PWM_FREQUENCY_RGBBLUE_Cyan,PWM_DUTY_CYCLE_RGBBLUE_Cyan
);
cyhal_system_delay_ms(1000);
//蓝色
RGB_Light(PWM_FREQUENCY_RGBRED_Blue,PWM_DUTY_CYCLE_RGBRED_Blue,\
PWM_FREQUENCY_RGBGREEN_Blue,PWM_DUTY_CYCLE_RGBGREEN_Blue,\
PWM_FREQUENCY_RGBBLUE_Blue,PWM_DUTY_CYCLE_RGBBLUE_Blue
);
cyhal_system_delay_ms(1000);
//紫色
RGB_Light(PWM_FREQUENCY_RGBRED_Purple,PWM_DUTY_CYCLE_RGBRED_Purple,\
PWM_FREQUENCY_RGBGREEN_Purple,PWM_DUTY_CYCLE_RGBGREEN_Purple,\
PWM_FREQUENCY_RGBBLUE_Purple,PWM_DUTY_CYCLE_RGBBLUE_Purple
);
cyhal_system_delay_ms(1000);
//白色
RGB_Light(PWM_FREQUENCY_RGBRED_white,PWM_DUTY_CYCLE_RGBRED_White,\
PWM_FREQUENCY_RGBGREEN_white,PWM_DUTY_CYCLE_RGBGREEN_White,\
PWM_FREQUENCY_RGBBLUE_white,PWM_DUTY_CYCLE_RGBBLUE_white
);
cyhal_system_delay_ms(1000);
}
RGB效果:
|