stm32 rgb多彩led驱动

给大家分享下stm32的rgb多彩led驱动

头文件:

#ifndef _LED_H
#define _LED_H

#ifdef __cplusplus
 extern "C" {
#endif

#define RED     1
#define GREEN   2
#define BLUE    3
#define YELLOW  4
#define PURPLE  5
#define NAVY    6
#define WHITE   7

typedef enum { false= 0, true = !false} bool;	 
	 
void led_init(void);
void led_ctrl(u8 colour, bool state);
	 
#ifdef __cplusplus
}
#endif
#endif
源文件:

#include "led.h"

void led_init(void)
{
		GPIO_InitTypeDef GPIO_InitStructure;
		RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
	
		GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_15;    
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;       
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOA, &GPIO_InitStructure);

//      GPIO_ResetBits(GPIOA, GPIO_Pin_11);
//      GPIO_ResetBits(GPIOA, GPIO_Pin_12);
//      GPIO_ResetBits(GPIOA, GPIO_Pin_15);
		GPIO_SetBits(GPIOA, GPIO_Pin_11);
		GPIO_SetBits(GPIOA, GPIO_Pin_12);
		GPIO_SetBits(GPIOA, GPIO_Pin_15);
}

void led_ctrl(u8 colour, bool state)
{
    switch(colour){
        case RED:
            if(state){
                GPIO_ResetBits(GPIOA, GPIO_Pin_11);
            }
            else {
                GPIO_SetBits(GPIOA, GPIO_Pin_11);
            }
            break;
        case GREEN:
            if(state){
                GPIO_ResetBits(GPIOA, GPIO_Pin_12);
            }
            else{
		        GPIO_SetBits(GPIOA, GPIO_Pin_12);
            }
            break;
        case BLUE:
            if(state){
                GPIO_ResetBits(GPIOA, GPIO_Pin_15);
            }
            else {
                GPIO_SetBits(GPIOA, GPIO_Pin_15);
            }
            break;
        case YELLOW:
            if(state){
                GPIO_ResetBits(GPIOA, GPIO_Pin_11);
                GPIO_ResetBits(GPIOA, GPIO_Pin_12);
            }
            else{
                GPIO_SetBits(GPIOA, GPIO_Pin_11);
                GPIO_SetBits(GPIOA, GPIO_Pin_12);
            }
            break;
        case PURPLE:
            if(state){
                GPIO_ResetBits(GPIOA, GPIO_Pin_11);
                GPIO_ResetBits(GPIOA, GPIO_Pin_15);
                }
            else{
                GPIO_SetBits(GPIOA, GPIO_Pin_11);
                GPIO_SetBits(GPIOA, GPIO_Pin_15);
            }
            break;
        case NAVY:
            if(state){
                GPIO_ResetBits(GPIOA, GPIO_Pin_12);
                GPIO_ResetBits(GPIOA, GPIO_Pin_15);
            }
            else{
                GPIO_SetBits(GPIOA, GPIO_Pin_12);
                GPIO_SetBits(GPIOA, GPIO_Pin_15);
            }
            break;
        case WHITE:
            if(state){
                GPIO_ResetBits(GPIOA, GPIO_Pin_11);
                GPIO_ResetBits(GPIOA, GPIO_Pin_12);
                GPIO_ResetBits(GPIOA, GPIO_Pin_15);
            }
            else{
                GPIO_SetBits(GPIOA, GPIO_Pin_11);
                GPIO_SetBits(GPIOA, GPIO_Pin_12);
                GPIO_SetBits(GPIOA, GPIO_Pin_15);
            }
            break;
        default:
            break;
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值