Вт сен 07, 2021 19:06:03
Вт сен 07, 2021 19:16:27
if ((adc_raw <= 207)&&(adc_raw > 183))
{
if (countMode == 1)
{
PORTB = 0b00000111;
timer1 = 0;
timer2 = 0;
}
if (countMode == 0)
{
PORTB = 0b00000101;
timer1 = 0;
timer2 = 0;
}
}
Вт сен 07, 2021 19:20:52
Вт сен 07, 2021 19:27:25
Вт сен 07, 2021 19:29:36
Вт сен 07, 2021 20:58:01
Вт сен 07, 2021 21:47:04
Вт сен 07, 2021 21:54:23
Ср сен 08, 2021 06:19:02
Ср сен 08, 2021 09:21:52
Ср сен 08, 2021 18:36:47
Чт сен 09, 2021 08:37:19
Пн сен 13, 2021 17:39:42
#ifndef MAIN_H_
#define MAIN_H_
#define F_CPU 1200000UL
#include <util/delay.h>
#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <stdbool.h> // bool in C99
#define nop __asm__ __volatile__ ("nop");
typedef uint8_t u8;
typedef uint16_t u16;
int main(void);
static void setup(void);
static void wakeup(void);;
static u8 bat_getvoltage(void);
#endif /* MAIN_H_ */
#include <util/delay.h>
#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <stdbool.h> // bool in C99
int count = 0;
volatile uint8_t timer1 = 0;
volatile uint8_t timer2 = 0;
volatile uint8_t timerADC = 0;
volatile uint8_t countMode = 0; //????? ?????????? ??????
volatile uint8_t reqChangeMode = 0; //????, ???????????? ????? ????????? ???????? ?????
u8 adc_raw = 0;
volatile uint8_t timerADC2 = 0;
ISR(TIM0_COMPA_vect)
{
knopka_check();
bat_check();
}
ISR(TIM0_OVF_vect){}
u8 bat_getvoltage_knopka(void) {
_delay_us(50);
ADCSRA |= (1 << ADSC);
while (ADCSRA & (1 << ADSC));
return ADCH;
}
void bat_check(void)
{
u8 adc_raw = bat_getvoltage();
if ((adc_raw <= 207)&&(adc_raw > 183))
{
if (countMode == 1)
{
PORTB = 0b00000111;
} else if (countMode == 0)
{
PORTB = 0b00000101;
}
} else if ((adc_raw <= 183)&&(adc_raw > 130))
{
if (countMode == 1)
{
timer1++;
PORTB = 0b00001011;
if(timer1 == 100)
{
PORTB = 0b00001000;
timer1 = 0;
}
} else if (countMode == 0)
{
timer1++;
PORTB = 0b00001001;
if(timer1 == 100)
{
PORTB = 0b00001000;
timer1 = 0;
}
}
} else if (adc_raw <= 130)
{
if (countMode == 1)
{
timer2++;
PORTB = 0b00001111;
if (timer2 >= 3)
{
PORTB = 0b00001100;
}
if (timer2 >= 103)
{
PORTB = 0b00001111;
timer2 = 0;
}
} else if (countMode == 0)
{
timer2++;
PORTB = 0b00001101;
if (timer2 >= 3)
{
PORTB = 0b00001100;
}
if (timer2 >= 103)
{
PORTB = 0b00001101;
timer2 = 0;
}
}
} else if (adc_raw > 210)
{
if (countMode == 1)
{
PORTB = 0b00001111;
} else if (countMode == 0)
{
PORTB = 0b00001101;
}
} else if (adc_raw < 30)
{
DDRB = 0b00000011;
PORTB = 0b00000000;
}
}
void knopka_check(void) {
u8 adc_knopki = bat_getvoltage_knopka();
if (adc_knopki < 90)
{
if (count < 250)
count++;
} else {
if (count > 0)
count--;
if ((count >= 4)&&(count < 50))
{
countMode = (countMode + 1) & 0x1;
reqChangeMode = 1;
count = 0;
}
}
if (reqChangeMode) { //???? ????????? ???????? ?????
reqChangeMode = 0; //???????? ?????????
switch (countMode) { //?????????? ????? ??????
case 0:
{
PORTB = 0b00001101;
}
break;
case 1:
{
PORTB = 0b00001111;
}
break;
}
}
}
int main(void) {
setup();
sei ();
while(1) {
}
}
void setup(void){
ADMUX =
(1 << ADLAR) | (1 << REFS0) | (1 << MUX1) | (0 << MUX0);
ADCSRA |= (1 << ADEN)|(1 << ADPS2)|(1 << ADPS1)|(1 << ADATE); // Предделитель на 64 (частота АЦП 125kHz)
ADCSRB = (1 << ADTS1)|(1 << ADTS0);
// Выключаем аналоговый компаратор
ACSR=(1<<ACD) | (0<<ACBG) | (0<<ACO) | (1<<ACI) | (0<<ACIE) | (0<<ACIS1) | (0<<ACIS0);
TCCR0B = (0 << CS02) | (1 << CS01) | (1 << CS00 ); // тактовый генератор CLK/64
TCCR0A = (0 << WGM02) | (1 << WGM01) | (0 << WGM00); // Режим CTC
OCR0A = 69;
TCNT0 = 0;
TIMSK0|=(1<<OCIE0A)|(0<<TOIE0);
DDRB = 0b00001111;
PORTB = 0b00001101;
wakeup();
}
void wakeup(void) {
ADCSRA |= (1 << ADSC);
}
u8 bat_getvoltage(void) {
_delay_us(50);
ADCSRA |= (1 << ADSC);
while ((ADCSRA & (1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
return ADCH;
}
Пн сен 13, 2021 19:22:04
else if ((adc_raw <= 183) && (adc_raw > 130)) {
if (countMode == 1) {
timer1++;
PORTB = 0b00001011;
if (timer1 == 100) {
PORTB = 0b00001000;
timer1 = 0;
}
}
else if (countMode == 0) {
timer1++;
PORTB = 0b00001001;
if (timer1 == 100) {
PORTB = 0b00001000;
timer1 = 0;
}
}
}
else if ((adc_raw <= 183) && (adc_raw > 130)) {
if (++timer1 == 100) {
timer1 = 0;
PORTB = 0b1000;
}
else
PORTB = (countMode) ? 0b1011 : 0b1001;
}
Пн сен 13, 2021 19:53:00
Пн сен 13, 2021 19:55:31
Пн сен 13, 2021 19:58:32
Пн сен 13, 2021 20:06:22
Пн сен 13, 2021 20:09:26
Пн сен 13, 2021 20:18:02