#include "stm32f10x.h"
#define run0 GPIO_ResetBits(GPIOA,GPIO_Pin_15);//动行指示 打开
#define spwm0 GPIO_ResetBits(GPIOA,GPIO_Pin_4);//脉冲调节
#define pwmon0 GPIO_ResetBits(GPIOB,GPIO_Pin_5);//脉冲输出打开
#define km0 GPIO_ResetBits(GPIOB,GPIO_Pin_7);//主接触器吸合
#define run1 GPIO_SetBits(GPIOA,GPIO_Pin_15);
#define spwm1 GPIO_SetBits(GPIOA,GPIO_Pin_4);
#define pwmon1 GPIO_SetBits(GPIOB,GPIO_Pin_5);
#define km1 GPIO_SetBits(GPIOB,GPIO_Pin_7);
unsigned char data_GLBIT;//过流标志
unsigned char data_GYBIT;//过压标志
unsigned char data_CWBIT;//超温标志
unsigned char data_TZBIT;//停止标志
unsigned int data_tim_km;//主接触器上处时上电时间定时
unsigned int data_Tickcnt1; //TICK10次计数
unsigned int data_Tickcnt10; //TICK10次计数加一
unsigned int data_DLset; //电流设定值
unsigned int data_DYset; //电压设定值
unsigned int data_DLadc;//电流采样值
unsigned int data_DYadc;//电压采样值
void GPIO_Configuration(void) //IO初始化
{
GPIO_InitTypeDef GPIO_InitPAIN;
GPIO_InitTypeDef GPIO_InitPBIN;
GPIO_InitTypeDef GPIO_InitPAOUT;
GPIO_InitTypeDef GPIO_InitPBOUT;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);//开启GPIOA|GPIOB时钟
//RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitPAIN.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3; //选(pin1 pll-3)/(Pin2 pll-1)/(Pin3 GL)/锁相环频率锁相过流输入
GPIO_InitPAIN.GPIO_Mode=GPIO_Mode_IPU; //上拉输入模式
GPIO_InitPAIN.GPIO_Speed=GPIO_Speed_50MHz; //输出频率50MHz
GPIO_Init(GPIOA,&GPIO_InitPAIN); //PA接口初始化
GPIO_InitPBIN.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_6; //选中(Pin3 QD)/(PB4 TZ)/(Pin6 CW)启动停止超温输入
GPIO_InitPBIN.GPIO_Mode=GPIO_Mode_IPU; //上拉输入模式
GPIO_InitPBIN.GPIO_Speed=GPIO_Speed_50MHz; //输入模式可以不写
GPIO_Init(GPIOB,&GPIO_InitPBIN); //PB接品初始化
GPIO_InitPAOUT.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_15; //(pin-4pwm-set)/(PIN-15run)脉冲调频,动行指示输出
GPIO_InitPAOUT.GPIO_Mode=GPIO_Mode_Out_PP; //开漏输 出模式
GPIO_InitPAOUT.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitPAOUT);
GPIO_InitPBOUT.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_7; //(PIN_5pwm-on0/(PIN_7out-km)脉冲输出打开 主接触器吸合
GPIO_InitPBOUT.GPIO_Mode=GPIO_Mode_Out_PP; //开漏输出模式
GPIO_InitPBOUT.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitPBOUT);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
}
//------------------------------------------------
void input(void)
{
//判断启动
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_3)==0)
{//判断接触器吸合后才能工作PB7=KM
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)==0)
{
spwm0;
pwmon0;
}
}
//判断停止
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_4)==0)
{
if(data_GLBIT==1)
{
data_GLBIT=0;
km0;
}
else
{
spwm1;
pwmon1;
}
}
//判断超温输入信号
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_6)==0)
{
spwm1;
pwmon1;
km1;
data_CWBIT=1;
}
}
//+++++++++++++++++++Tick定时器+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void Delay (u16 m)
{
SysTick->LOAD=m*9000;
SysTick->CTRL|=0X03;
}
//+++++++++++++++++比较ADC转换结果电压和电流值是否超限++++++++++++++++++++++++++++++++++++++++++++
void CMPadc(void)
{
if(data_DLadc>data_DLset)
{
}
if(data_DYadc>data_DYset)
{
}
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//* Set the Vector Table base location at 0x08004000
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置 中断优先级
//* Enable the EXTI1 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; //设置 中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //指定抢占式优先级别
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //次优先级别为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //中断通道使能
NVIC_Init(&NVIC_InitStructure); //初始化中断
}
void EXIT3_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource3); //将EXTI线1连接到端口GPIOA的第在3脚
EXTI_InitStructure.EXTI_Line = EXTI_Line3; //如果配置的是1号针脚那么必须是EXTI——LINE3
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //初始化中断
}
void km_config(void)
{//判断是否过流过流后不再吸合接触器
if(data_GLBIT==0)
{
if(data_Tickcnt10>data_tim_km)
{
km0;
}
}
}
/* USART初始化 */
//void USART1_Init(void)
//{
// GPIO_InitTypeDef GPIO_InitStructure;
// USART_InitTypeDef USART_InitStructure;
//
// RCC_AHBPeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA的时钟
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//使能USART的时钟
// /* USART1的端口配置 *///
// GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);//配置PA9成第二功能引脚 TX
// GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);//配置PA10成第二功能引脚 RX
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
// GPIO_Init(GPIOA, &GPIO_InitStructure);
//
// /* USART1的基本配置 */
// USART_InitStructure.USART_BaudRate = 115200; //波特率
// USART_InitStructure.USART_WordLength = USART_WordLength_8b;
// USART_InitStructure.USART_StopBits = USART_StopBits_1;
// USART_InitStructure.USART_Parity = USART_Parity_No;
// USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
// USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
// USART_Init(USART1, &USART_InitStructure);
// USART_Cmd(USART1, ENABLE); //使能USART1
//
//
//}
//void USART_ENABLE_REV(void)
//{ NVIC_InitTypeDef NVIC_InitStruct;
// NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
// NVIC_InitStruct.NVIC_IRQChannelPriority = 0x02;
// NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStruct);
// USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
//}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++主程序+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*******************************************************************************
* 函数名 : ADC1_Init_Config
* 描述 : ADC1初始化配置
* 输入 : 无
* 输出 : 无
* 返回 : 无
* 说明 : 只是初始化了ADC1但未启动ADC1转换
*******************************************************************************/
void ADC1_Init_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE ); //使能ADC1和GPIOA外设时钟
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //设置ADC时钟分频因子为6(72M/6=12M),ADC最大工作频率为14M
/*设置PA0模拟通道输入引脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6; //选择要初始化的GPIOA的PA5|PA6引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //工作模式为模拟输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置引脚输出最大速率为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //调用库函数中的GPIO初始化函数,初始化GPIOB中的PB5,PB6,PB7,PB8引脚
/*初始化配置ADC1*/
ADC_DeInit(ADC1); //复位ADC1,即将ADC1的所有寄存器设置为缺省值
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE );
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC1工作在独立模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //扫描模式设置,多通道下使用使能,单通道下使用失能
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //模数转换工作在单次转换模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //由软件来触发转换启动,也可以设置成外设启动
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 1; //顺序进行规则转换的ADC通道的数目
ADC_Init(ADC1, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器
// ADC_DMACmd(ADC1,DISABLE);//关闭DMA请求
ADC_Cmd(ADC1, ENABLE); //使能ADC1,但还没启动ADC1
ADC_ResetCalibration(ADC1); //使能ADC1复位校准寄存器
while(ADC_GetResetCalibrationStatus(ADC1)); //等待复位完成
ADC_StartCalibration(ADC1); //开启AD校准
while(ADC_GetCalibrationStatus(ADC1)); //等待校准完成
//ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能指定的ADC1的软件转换启动功能
}
u16 ADC1_Get_AdcValue(u8 ch)
{
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 ); //设置ADC1的转换通道ch,一个序列,采样时间为1.5周期
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //软件启动ADC1开始转换
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC )==RESET); //等待AD转换结束
return ADC_GetConversionValue(ADC1); //返回最近一次ADC1规则组转换的AD值
}
int main(void)
{
GPIO_Configuration();//GPIO初始化
run1;//GPIOA p15
spwm1;//GPIOA p4
pwmon1;//GPIOB p5
km1;//GPIOB p7
Delay(100);
NVIC_Configuration();
EXIT3_Configuration();
//初始化数据
data_tim_km=0;
data_Tickcnt1=0;
data_Tickcnt10=0;
data_GLBIT=data_GYBIT=data_CWBIT=data_TZBIT=0;
data_DYset=data_DLset=200;
data_tim_km=data_Tickcnt10+5;
while(1)
{
data_DLadc = ADC1_Get_AdcValue(5); //读取并返回ADC对应通道的AD转换值
data_DYadc = ADC1_Get_AdcValue(6);
input();//启停超温过流检测
CMPadc();
km_config();
}
}