1,风扇开发概述
L9110风扇模块介绍
L9110芯片特性:
- 控制和驱动电机
- 两通道推挽式功率放大专用集成电路期件
- 电压范围:2.5v-12v
- 每通道具有800mA连续电流输出能力
- 输出具有正转,反转,高阻和刹车 四种状态
引脚定义:
控制电路:
引出端波形:
接线:
2,风扇开发接线
风扇GND-------开发板GND
VCC-------5V
IA---------PA15
IB---------PA0
3,风扇产品创建
4,风扇代码编写
创建工程:aos create project fenshan -b mk3080 -t linkkit_demo -d temp
工程目录下link_example_solo.c中加入以下代码:
#include "aos/hal/gpio.h"
#define GPIO_CLOCKW 1 //PA15
#define GPIO_ACLOKW 6 //PA0
gpio_dev_t clockWiseIO;
gpio_dev_t antiClockWiseIO;
static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
{
int res = 0;
cJSON *root ,*prop;
root = cJSON_Parse(request);
if(root == NULL){
return res;
}
prop = cJSON_GetObjectItem(root,"powerstate");
if(prop!=NULL && cJSON_IsNumber(prop)){
if(prop->valueint == 1){
fanPowerStatus = 1;
hal_gpio_output_high(&clockWiseIO);
hal_gpio_output_low(&antiClockWiseIO);
printf("power on\n");
}else if(prop->valueint == 0){
fanPowerStatus = 0;
hal_gpio_output_low(&clockWiseIO);
hal_gpio_output_low(&antiClockWiseIO);
printf("power off\n");
}
}
prop = cJSON_GetObjectItem(root,"direction");
if(prop!=NULL && cJSON_IsNumber(prop)){
if(fanPowerStatus == 1){
if(prop->valueint == 1){
hal_gpio_output_low(&clockWiseIO);
hal_gpio_output_high(&antiClockWiseIO);
printf("direction 1\n");
}else if(prop->valueint == 0){
hal_gpio_output_high(&clockWiseIO);
hal_gpio_output_low(&antiClockWiseIO);
printf("direction 0\n");
}
}else{
hal_gpio_output_high(&clockWiseIO);
hal_gpio_output_high(&antiClockWiseIO);
}
}
//linkkit_main()函数前定义驱动风扇转动的GPIO
void initGPIO()
{
clockWiseIO.port = GPIO_CLOCKW;
antiClockWiseIO.port = GPIO_ACLOKW;
clockWiseIO.config = OUTPUT_PUSH_PULL;
antiClockWiseIO.config = OUTPUT_PUSH_PULL;
hal_gpio_init(&clockWiseIO);
hal_gpio_init(&antiClockWiseIO);
sys_jtag_off();
hal_gpio_output_high(&clockWiseIO);
hal_gpio_output_high(&antiClockWiseIO);
}
aos make menuconfig 进行配置(cJSON library勾选上),aos make编译,烧录,手机app/天猫精灵语音配网,putty端查看打印信息,控制风扇的正转反转,停止灯属性。
5,风扇IO温度控制演示
link_example_solo.c中添加:
#include "aos/hal/gpio.h"
#define GPIO_CLOCKW 1 //PA15
#define GPIO_ACLOKW 6 //PA0
#define G18b20IO 0 //PA14
gpio_dev_t GPIO_18b20;
float temp;
gpio_dev_t clockWiseIO;
gpio_dev_t antiClockWiseIO;
#define CLKWISE 1 //正转
#define ANT_CLKWISE 2 //反转
int fanPowerStatus = 0;
int fanDirection = 0;
void initForInput()
{
GPIO_18b20.config=INPUT_PULL_UP;
if(GPIO_18b20io.priv){
free(GPIO_18b20io.priv);
GPIO_18b20io.priv = NULL;
}
hal_gpio_init(&GPIO_18b20);
}
void initForOutput()
{
GPIO_18b20.config = OUTPUT_PUSH_PULL;
hal_gpio_init(&GPIO_18b20);
}
//初始化
void init_18b20()
{
int inputValue=0;
hal_gpio_output_high(&GPIO_18b20);
DelayUs(100);
hal_gpio_output_low(&GPIO_18b20);
DelayUs(750);
hal_gpio_output_high(&GPIO_18B20);
initForInput();
DelayUs(50);
hal_gpio_input_get(&GPIO_18b20,inputValue);
if(inputValue){
printf("18b20 init failed\n");
return -1;
}
initForOutput();
hal_gpio_output_high(&GPIO_18b20);
DelayUs(15);
retrun ;
}
//写时序
void write_18b20_byte(unsigned char data)
{
int i;
for(i=0;i<8;i++){
hal_gpio_output_low(&GPIO_18B20);
if(data&0x01){ //write 1 to 18b20
DelayUs(10);
hal_gpio_output_high(&GPIO_18b20);
DelayUs(45);
}else{
DelayUs(60);
hal_gpio_output_high(&GPIO_18b20);
}
DelayUs(10);
data>>=1;
}
}
//读时序
unsigned char read_18b20_byte()
{
int i;
int inputValue;
int reByte=0;
for(i=0;i<8;i++){
hal_gpio_output_low(&GPIO_18b20);
initForInput();
DelayUs(5);
hal_gpio_input_get(&GPIO_18b20,&inputValue);
reByte = reByte>>1;
if(inputValue){
reByte |= 0x80;
}
initForOutput();
hal_gpio_output_high(&GPIO_18b20);
DelayUs(50);
}
retrun reByte;
}
//从18b20温度传感器读温度值到暂存寄存器中
unsigned short readTempFrom18b20()
{
int ret;
unsigned char tempH;
unsigned char tempL;
unsigned short tmep;
ret = init_18b20();
if(ret<0){
return -1;
}
write_18b20_byte(0xCC);
write_18b20_byte(0x44);
DelayUs(2000);
write_18b20_byte(0xCC);
write_18b20_byte(0xbe);
DelayUs(500);
tempL= read_18b20_byte();
tempH = read_18b20_byte();
temp = tempH*256 + tempL;
printf("read th=%d,tl=%d,temp=%d\n",tempH,tempL,temp);
return temp;
}
void user_post_property(void)
{
static int cnt = 0;
int res = 0;
static int tempLowsetFlag = 0;
char property_payload[30] = {0};
// HAL_Snprintf(property_payload, sizeof(property_payload), "{\"Counter\": %d}", cnt++);
temp = readTempFrom18b20();
temp = temp/16;
printf("read temp=%f\n",temp);
sprintf(property_payload,"{\"temperature\":%f}",temp);
if(temp>30){
if(fanPowerStatus == 1){
if(fanDirection = CLKWISE){
hal_gpio_output_high(&clockWiseIO);
hal_gpio_output_low(&antiClockWiseIO);
}else if(fanDirection == ANT_CLKWISE){
hal_gpio_output_low(&clockWiseIO);
hal_gpio_output_high(&antiClockWiseIO);
}
}
tempLowsetFlag = 0;
}else{
if(tempLowsetFlag == 0){
hal_gpio_output_high(&clockWiseIO);
hal_gpio_output_high(&antiClockWiseIO);
tempLowsetFlag = 1;
}
}
}
res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_POST_PROPERTY,
(unsigned char *)property_payload, strlen(property_payload));
EXAMPLE_TRACE("Post Property Message ID: %d", res);
}
static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
{
int res = 0;
cJSON *root ,*prop;
root = cJSON_Parse(request);
if(root == NULL){
return res;
}
prop = cJSON_GetObjectItem(root,"powerstate");
if(prop!=NULL && cJSON_IsNumber(prop)){
if(prop->valueint == 1){
fanPowerStatus = 1;
hal_gpio_output_high(&clockWiseIO);
hal_gpio_output_low(&antiClockWiseIO);
printf("power on\n");
}else if(prop->valueint == 0){
fanPowerStatus = 0;
hal_gpio_output_low(&clockWiseIO);
hal_gpio_output_low(&antiClockWiseIO);
printf("power off\n");
}
}
prop = cJSON_GetObjectItem(root,"direction");
if(prop!=NULL && cJSON_IsNumber(prop)){
if(fanPowerStatus == 1){
if(prop->valueint == 1){
hal_gpio_output_low(&clockWiseIO);
hal_gpio_output_high(&antiClockWiseIO);
printf("direction 1\n");
fanDirection = CLKWISE;
}else if(prop->valueint == 0){
hal_gpio_output_high(&clockWiseIO);
hal_gpio_output_low(&antiClockWiseIO);
fanDirection = ANT_CLKWISE;
printf("direction 0\n");
}
}else{
hal_gpio_output_high(&clockWiseIO);
hal_gpio_output_high(&antiClockWiseIO);
}
}
//linkkit_main()函数前定义驱动风扇转动的GPIO
void initGPIO()
{
clockWiseIO.port = GPIO_CLOCKW;
antiClockWiseIO.port = GPIO_ACLOKW;
clockWiseIO.config = OUTPUT_PUSH_PULL;
antiClockWiseIO.config = OUTPUT_PUSH_PULL;
hal_gpio_init(&clockWiseIO);
hal_gpio_init(&antiClockWiseIO);
sys_jtag_off();
hal_gpio_output_high(&clockWiseIO);
hal_gpio_output_high(&antiClockWiseIO);
}
app_entry.c中添加:
initGPIO(); //初始化风扇的gpio
initGPIO18b20(); //初始化18b20的gpio
aos_loop_run();