AMTS/Mieke/SW/MT/ne555.c

49 lines
1,012 B
C
Raw Normal View History

2018-03-18 22:43:59 +00:00
#include "ne555.h"
volatile uint32_t *NE555STick, NE555STickCur;
uint8_t old_state;
void init_ne555(volatile uint32_t *SysTickCnt)
{
2018-04-03 15:18:28 +00:00
// Enable GPIOb clock
2018-03-18 22:43:59 +00:00
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
2018-04-03 15:18:28 +00:00
// Create gpio struct and fill it with default values
2018-03-18 22:43:59 +00:00
GPIO_InitTypeDef gpio;
GPIO_StructInit(&gpio);
2018-04-03 15:18:28 +00:00
// Set PB0 to input floating
2018-03-18 22:43:59 +00:00
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpio.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOB, &gpio);
NE555STick = SysTickCnt;
return;
}
void run_ne555(float *freq)
{
uint8_t state;
*freq = 0.0f;
NE555STickCur = *NE555STick;
2018-04-03 15:18:28 +00:00
// Run for one second
2018-03-18 22:43:59 +00:00
while((*NE555STick - NE555STickCur) <= 1000)
{
state = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0);
2018-04-03 15:18:28 +00:00
// If the state changes low -> high or high -> low increment freq and save state
2018-03-18 22:43:59 +00:00
if (state != old_state)
{
(*freq)++;
old_state = state;
}
}
2018-04-03 15:18:28 +00:00
// Divide freq by two to get the frequency
2018-03-18 22:43:59 +00:00
*freq /= 2;
return;
}
void deinit_ne555(void)
{
return;
}