TA的每日心情 | 开心 2018-3-29 09:22 |
---|
签到天数: 15 天 连续签到: 1 天 [LV.4]偶尔看看III
|
在Gokit2所用的wifi模块驱动,电机驱动,红外检测传感器,温湿度检测器,RGB三色灯驱动,可以移植到项目中去,
1,电机驱动程序及数据手册
#ifndef _HAL_RGB_LED_H
#define _HAL_RGB_LED_H
#include "include.h"
#define R_MAX 255
#define G_MAX 255
#define B_MAX 255
#define SCL_LOW GPIO_ResetBits(GPIOB,GPIO_Pin_8)
#define SCL_HIGH GPIO_SetBits(GPIOB,GPIO_Pin_8)
#define SDA_LOW GPIO_ResetBits(GPIOB,GPIO_Pin_9)
#define SDA_HIGH GPIO_SetBits(GPIOB,GPIO_Pin_9)
void RGB_LED_Init(void);
void LED_R_Control(uint8_t R);
void LED_G_Control(uint8_t G);
void LED_B_Control(uint8_t B);
void LED_RGB_Control(uint8_t R,uint8_t B,uint8_t G);
#endif /*_HAL_RGB_LED_H*/
#include "include.h"
void LED_delay(unsigned int ms)
{
volatile unsigned int i=0;
for(i=0;i<ms;i++);
}
/************ generation clock *********************/
void ClkProduce(void)
{
SCL_LOW; // SCL=0
LED_delay(40);
SCL_HIGH; // SCL=1
LED_delay(40);
}
/********** send 32 zero ********************/
void Send32Zero(void)
{
unsigned char i;
SDA_LOW; // SDA=0
for (i=0; i<32; i++)
ClkProduce();
}
/********* invert the grey value of the first two bits ***************/
uint8_t TakeAntiCode(uint8_t dat)
{
uint8_t tmp = 0;
tmp=((~dat) & 0xC0)>>6;
return tmp;
}
/****** send gray data *********/
void DatSend(uint32_t dx)
{
uint8_t i;
for (i=0; i<32; i++)
{
if ((dx & 0x80000000) != 0)
{
SDA_HIGH; // SDA=1;
}
else
{
SDA_LOW; // SDA=0;
}
dx <<= 1;
ClkProduce();
}
}
/******* data processing ********************/
void DataDealWithAndSend(uint8_t r, uint8_t g, uint8_t b)
{
uint32_t dx = 0;
dx |= (uint32_t)0x03 << 30; // The front of the two bits 1 is flag bits
dx |= (uint32_t)TakeAntiCode(g) << 28;
dx |= (uint32_t)TakeAntiCode(b) << 26;
dx |= (uint32_t)TakeAntiCode(r) << 24;
dx |= (uint32_t)g << 16;
dx |= (uint32_t)b << 8;
dx |= r;
DatSend(dx);
}
void RGB_LED_Init(void)
{
RCC->APB2ENR|=1<<3; //使能PORTB时钟
GPIOB->CRH&=0X0FFFFF00;//PA13,15设置成输出
GPIOB->CRH|=0X80800033;
GPIOB->ODR|=1<<8; //PB8上拉,PA0默认下拉
GPIOB->ODR|=1<<9; //PB9上拉
Send32Zero();
DataDealWithAndSend(0,0,0); // display red
DataDealWithAndSend(0,0,0);
}
void LED_RGB_Control(uint8_t R, uint8_t G, uint8_t B)
{
Send32Zero();
DataDealWithAndSend(R, G, B); // display red
DataDealWithAndSend(R, G, B); // display red
// printf("LED_R = %d LED_G = %d LED_B = %d",R,G,B);
}
|
|