132 lines
3.6 KiB
C
132 lines
3.6 KiB
C
#include "relay.h"
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
//This program is for educational purposes only, without the author's permission, may not be used for any other purpose
|
|
//ALIENTEK STM32F407 Development Board
|
|
//RELAY driver code
|
|
//Hot Atomic @ALIENTEK
|
|
//Forum: www.openedv.com
|
|
//Date created: 2014/6/10
|
|
//Version: V1.0
|
|
//All rights reserved pirated reserved.
|
|
//Copyright(C) Guangzhou wing electronic technology limited company 2014-2024
|
|
//All rights reserved
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Relay IO Initialize (KT mBMS-16S Rev1.0.3 schematic reference)
|
|
void Relay_Init(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
// Enable Port C & Port D AHB1 clock
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
|
|
|
|
// Active-Low: Set high before configuring as OUT to prevent glitch
|
|
GPIO_SetBits(GPIOC, GPIO_Pin_9);
|
|
GPIO_SetBits(GPIOD, GPIO_Pin_13 | GPIO_Pin_15);
|
|
|
|
/* 1. Charge Relay Control (PC9: CHARGE_CONTROL, Active-Low) */
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; // Open-drain output
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
|
|
|
/* 2. Charge Relay Sense Input (PC8: CHARGE_CON_SEN) */
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
|
|
|
/* 3. Discharge Relay Control (PD13: DISCHARGE_CONTROL, Active-Low) */
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; // Open-drain output
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
|
|
|
/* 4. Discharge Relay Sense Input (PD12: DISCHARGE_CON_SEN) */
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
|
|
|
/* 5. Pre-charge Relay Control (PD15: PRECHARGE_CONTROL, Active-Low) */
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; // Open-drain output
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
|
|
|
/* 6. Pre-charge Relay Sense Input (PD14: PRECHARGE_CON_SEN) */
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
|
|
|
CHG_RLY_OFF;
|
|
DSG_RLY_OFF;
|
|
PRE_CHG_RLY_OFF;
|
|
}
|
|
|
|
void Relay_On(u8 id)
|
|
{
|
|
switch (id)
|
|
{
|
|
case 0:
|
|
case '0':
|
|
DSG_RLY_ON;
|
|
break;
|
|
case 1:
|
|
case '1':
|
|
CHG_RLY_ON;
|
|
break;
|
|
case 2:
|
|
case '2':
|
|
PRE_CHG_RLY_ON;
|
|
break;
|
|
}
|
|
}
|
|
|
|
void Relay_Off(u8 id)
|
|
{
|
|
switch (id)
|
|
{
|
|
case 0:
|
|
case '0':
|
|
DSG_RLY_OFF;
|
|
break;
|
|
case 1:
|
|
case '1':
|
|
CHG_RLY_OFF;
|
|
break;
|
|
case 2:
|
|
case '2':
|
|
PRE_CHG_RLY_OFF;
|
|
break;
|
|
}
|
|
}
|
|
|
|
u8 Relay_Status(u8 id)
|
|
{
|
|
if ((id == 0) || (id == '0'))
|
|
{
|
|
// Discharge Relay Status: PD12
|
|
return GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_12);
|
|
}
|
|
else if ((id == 1) || (id == '1'))
|
|
{
|
|
// Charge Relay Status: PC8
|
|
return GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8);
|
|
}
|
|
else
|
|
{
|
|
// Pre-charge Relay Status: PD14
|
|
return GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_14);
|
|
}
|
|
}
|
|
|