Regulátor pro remosku s STM8
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
remostat/User/main.c

101 lines
1.6 KiB

8 years ago
#include "stm8s.h"
#include <stdio.h>
#include "bootstrap.h"
2 years ago
#define LEVEL_TOP 1024
#define LEVEL_MAX 1000
8 years ago
2 years ago
volatile uint16_t level = 0;
#define BYPASSBTN_PIN (1 << 7)
8 years ago
/**
* Set PWM level
*/
void PWM_Write(void)
{
uint16_t tmp = level;
2 years ago
//if (tmp > LEVEL_MAX) tmp = LEVEL_MAX;
if (tmp >= LEVEL_MAX) tmp = LEVEL_TOP;
8 years ago
TIM1_SetCompare4(tmp);
}
void PWM_Cmd(FunctionalState fs)
{
TIM1_CtrlPWMOutputs(fs);
}
2 years ago
void BYPASSBTN_Setup()
{
GPIO_Init(GPIOC,GPIO_PIN_7,GPIO_MODE_IN_PU_NO_IT);
//GPIOC->DDR &= ~BYPASSBTN_PIN;
//GPIOC->CR1 |= BYPASSBTN_PIN;
}
8 years ago
/**
* Set up the PWM generation
*/
void PWM_Setup()
{
// open drain, fast
GPIOC->DDR |= GPIO_PIN_4; // out
//GPIOC->CR1 &= ~GPIO_PIN_4; // open drain
GPIOC->CR2 |= GPIO_PIN_4; // fast
2 years ago
TIM1_TimeBaseInit(1 << 14, TIM1_COUNTERMODE_UP, LEVEL_TOP, 0);
8 years ago
TIM1_OC4Init(TIM1_OCMODE_PWM1,
TIM1_OUTPUTSTATE_ENABLE,
level,
TIM1_OCPOLARITY_HIGH,
TIM1_OCIDLESTATE_SET);
TIM1_Cmd(ENABLE);
}
/**
* Set up the analog input
*/
void AIN_Setup()
{
ADC1_ConversionConfig(ADC1_CONVERSIONMODE_CONTINUOUS,
ADC1_CHANNEL_3,
ADC1_ALIGN_RIGHT);
ADC1_Cmd(ENABLE);
ADC1_StartConversion();
}
void main(void)
{
uint16_t cnt = 0;
uint16_t conv;
SimpleInit();
PWM_Setup();
AIN_Setup();
2 years ago
BYPASSBTN_Setup();
8 years ago
// Go
PWM_Cmd(ENABLE);
while (1) {
if (cnt++ == 65535) {
cnt = 0;
LED_Toggle();
}
// adjust level
if (ADC1_GetFlagStatus(ADC1_FLAG_EOC)) {
conv = ADC1_GetConversionValue();
2 years ago
if (0 == (GPIOC->IDR & BYPASSBTN_PIN)) {
conv = LEVEL_TOP;
}
8 years ago
level = conv;
2 years ago
8 years ago
PWM_Write();
}
}
}