Вт окт 28, 2014 16:12:10
Вт окт 28, 2014 17:37:09
Вт окт 28, 2014 20:59:43
Вт окт 28, 2014 21:12:54
Вт окт 28, 2014 21:23:51
Вт окт 28, 2014 21:31:56
Вт окт 28, 2014 23:45:53
Ср окт 29, 2014 10:02:25
/*****************************************************
Chip type : ATtiny2313
AVR Core Clock frequency: 8,000000 MHz
Memory model : Tiny
External RAM size : 0
Data Stack size : 32
*****************************************************/
#include <tiny2313.h>
#include <delay.h>
// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
// Input/Output Ports initialization
// Port A initialization
// Func2=In Func1=In Func0=In
// State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=Out Func3=Out Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=0 State3=0 State2=T State1=T State0=T
PORTB=0x03;
DDRB=0x18;
// Port D initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x00;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 8000,000 kHz
// Mode: Ph. & fr. cor. PWM top=ICR1
// OC1A output: Non-Inv.
// OC1B output: Non-Inv.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0xA0;
TCCR1B=0x11;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=0x07; //настраиваем скважность А 30% [1..ICR1-1]
OCR1AL=0xD0;
ICR1H=0x1A; //настраиваем частоту регистром захвата 600Hz
ICR1L=0x0B;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// Interrupt on any change on pins PCINT0-7: Off
GIMSK=0x00;
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x80;
// Universal Serial Interface initialization
// Mode: Disabled
// Clock source: Register & Counter=no clk.
// USI Counter Overflow Interrupt: Off
USICR=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
if(PINB.0 == 0) //если кнопка "больше" нажата
{
if (OCR1A < 13334)
{ // коэффициент заполнения ШИМ изменяется от 0 до ICR1
OCR1A = OCR1A+100; // записываем переменную в регистр сравнения
delay_ms(30); // задержка 30ms
}
}
if (PINB.1==0)
{
if(ICR1<13334)
{
//TCNT1=0x00;
ICR1=ICR1+100;
delay_ms(30);
}
}
}
}
Ср окт 29, 2014 19:14:44
Пт окт 31, 2014 18:39:40
Вт июл 25, 2023 22:15:17
Ср июл 26, 2023 09:48:00
Ср июл 26, 2023 19:10:13
Ср июл 26, 2023 19:20:20
Ср июл 26, 2023 19:39:52
Сб июл 29, 2023 10:11:39
/* ATmega328 AtmelStudio -std=c++14 */
#define F_CPU 8000000UL
#include <avr/io.h>
#include "E:\lib\src\AVR\GPIO.h"
#include "E:\AVR_PWM\Timers328(test)\src\TimerHelpers.h"
using TimHelp1 = TimerHelper16<14, PRESCALE::PRESCALE_8, (PORT::CLEAR_A_ON_COMPARE | PORT::CLEAR_B_ON_COMPARE)>;
TimHelp1 timer16;
GPIO<BOARD::OC1A> OC1A; // PinB1
GPIO<BOARD::OC1B> OC1B; // PinB2
int main(void){
// Порты на выход
OC1A.output();
OC1B.output();
timer16.init<TCCR::TCCR_1A, TCCR::TCCR_1B>();
// 100 = 8000000 / (8 * 10000) Частота ШИМ 100 Герц
ICR1 = 10000;//
OCR1A = 4400;// 9999 верхний предел
OCR1B = 3300;
while (1) { }
return 0;
} // main/
#ifndef TIMERHELPERS_H
#define TIMERHELPERS_H
#include <avr/io.h>
#include <assert.h>
// ATmega640/1280/1281/2560/2561
#if defined(__AVR_ATmega640__) || \
defined(__AVR_ATmega1280__) || \
defined(__AVR_ATmega1281__) || \
defined(__AVR_ATmega2560__) || \
defined(__AVR_ATmega2561__)
// Timer control registers
enum struct TCCR : uint16_t {
// Timer1 Control_Register_A/_B/_C (16:bit)
TCCR_1A = (const uint16_t) &TCCR1A, // (0x80)
TCCR_1B = (const uint16_t) &TCCR1B, // (0x81)
TCCR_1C = (const uint16_t) &TCCR1C, // (0x82)
// Timer3 Control_Register_A/_B/_C (16:bit)
TCCR_3A = (const uint16_t) &TCCR3A, // (0x90)
TCCR_3B = (const uint16_t) &TCCR3B, // (0x91)
TCCR_3C = (const uint16_t) &TCCR3C, // (0x92)
// Timer4 Control_Register_A/_B/_C (16:bit)
TCCR_4A = (const uint16_t) &TCCR4A, // (0xA0)
TCCR_4B = (const uint16_t) &TCCR4B, // (0xA1)
TCCR_4C = (const uint16_t) &TCCR4C, // (0xA2)
// Timer5 Control_Register_A/_B/_C (16:bit)
TCCR_5A = (const uint16_t) &TCCR5A, // (0x120) = 288
TCCR_5B = (const uint16_t) &TCCR5B, // (0x121) = 289
TCCR_5C = (const uint16_t) &TCCR5C // (0x122) = 290
};
// ATmega48A/PA/88A/PA/168A/PA/328/P
#elif defined(__AVR_ATmega48__) ||\
defined(__AVR_ATmega48A__) ||\
defined(__AVR_ATmega48PA__) ||\
defined(__AVR_ATmega48P__) ||\
defined(__AVR_ATmega88__) ||\
defined(__AVR_ATmega88A__) ||\
defined(__AVR_ATmega88PA__) ||\
defined(__AVR_ATmega168__) ||\
defined(__AVR_ATmega168A__) ||\
defined(__AVR_ATmega168PA__)||\
defined(__AVR_ATmega328__) ||\
defined(__AVR_ATmega328P__)
enum struct TCCR : uint16_t {
// Timer1 Control_Register_A/_B/_C (16:bit)
TCCR_1A = (const uint16_t) &TCCR1A, // (0x80)
TCCR_1B = (const uint16_t) &TCCR1B, // (0x81)
TCCR_1C = (const uint16_t) &TCCR1C // (0x82)
};
#else
#error *!*!* AVR MCUs NOT SUPPORTED *!*!*
#endif
enum struct PRESCALE : uint8_t {
NO_CLOCK = 0b00000'000, // (0) - таймер/счетчик остановлен
PRESCALE_1 = 0b00000'001, // (1) - тактовый генератор CLK
PRESCALE_8 = 0b00000'010, // (2) - CLK/8
PRESCALE_64 = 0b00000'011, // (3) - CLK/64
PRESCALE_256 = 0b00000'100, // (4) - CLK/256
PRESCALE_1024 = 0b00000'101, // (5) - CLK/1024
Tn_FALLING = 0b00000'110, // (6) - Внешний источник тактирования на выводе Tn. Часы на падающем фронте.
Tn_RISING = 0b00000'111 // (7) - Внешний источник тактирования на выводе Tn. Часы на переднем фронте.
};
// способы вывода
enum struct PORT : uint8_t {
NO_PORT = 0b00'00'00'00,
TOGGLE_A_ON_COMPARE = 0b01'000000,
CLEAR_A_ON_COMPARE = 0b10'000000,
SET_A_ON_COMPARE = 0b11'000000,
TOGGLE_B_ON_COMPARE = 0b00'01'0000,
CLEAR_B_ON_COMPARE = 0b00'10'0000,
SET_B_ON_COMPARE = 0b00'11'0000,
TOGGLE_C_ON_COMPARE = 0b0000'01'00,
CLEAR_C_ON_COMPARE = 0b0000'10'00,
SET_C_ON_COMPARE = 0b0000'11'00
};
constexpr PORT operator | (const PORT P0, const PORT P1) {
return static_cast<PORT>(
static_cast<uint8_t>(P0) | static_cast<uint8_t>(P1) );
}
constexpr uint8_t operator | (const uint8_t p0, const PORT P1) {
return static_cast<uint8_t>( p0 | static_cast<uint8_t>(P1) );
}
constexpr uint8_t operator | (const uint8_t p0, const PRESCALE P1) {
return static_cast<uint8_t>( p0 | static_cast<uint8_t>(P1) );
}
// положение битов WGMn1, WGMn0, WGMn3, WGMn2 в регистрах TCCRnA, TCCRnB
const uint8_t Modes[16][2] = {
{ 0b0000'0000, 0b0000'0000 }, // 0: Normal, top = 0xFFFF
{ 0b0000'0001, 0b0000'0000 }, // 1: PWM, Phase-correct, 8 bit, top = 0xFF
{ 0b0000'0010, 0b0000'0000 }, // 2: PWM, Phase-correct, 9 bit, top = 0x1FF
{ 0b0000'0011, 0b0000'0000 }, // 3: PWM, Phase-correct, 10 bit, top = 0x3FF
{ 0b0000'0000, 0b0000'1000 }, // 4: CTC, top = OCR1A
{ 0b0000'0001, 0b0000'1000 }, // 5: Fast PWM, 8 bit, top = 0xFF
{ 0b0000'0010, 0b0000'1000 }, // 6: Fast PWM, 9 bit, top = 0x1FF
{ 0b0000'0011, 0b0000'1000 }, // 7: Fast PWM, 10 bit, top = 0x3FF
{ 0b0000'0000, 0b0001'0000 }, // 8: PWM, phase and frequency correct, top = ICR1
{ 0b0000'0001, 0b0001'0000 }, // 9: PWM, phase and frequency correct, top = OCR1A
{ 0b0000'0010, 0b0001'0000 }, // 10: PWM, phase correct, top = ICR1A
{ 0b0000'0011, 0b0001'0000 }, // 11: PWM, phase correct, top = OCR1A
{ 0b0000'0000, 0b0001'1000 }, // 12: CTC, top = ICR1
{ 0b0000'0001, 0b0001'1000 }, // 13: reserved
{ 0b0000'0010, 0b0001'1000 }, // 14: Fast PWM, TOP = ICR1
{ 0b0000'0011, 0b0001'1000 } // 15: Fast PWM, TOP = OCR1A
}; // end of Timers::Modes
// ***********************************************************
template<uint8_t mode, PRESCALE prescale, PORT port>
struct TimerHelper16 {
static_assert( !(mode < 0 || mode > 15), "*!*!* BAD MODE *!*!*" );
template<TCCR tccrA = TCCR::TCCR_1A, TCCR tccrB = TCCR::TCCR_1B>
static inline __attribute__((always_inline)) void init() {
*(volatile uint8_t*)(tccrA) |= Modes[mode][CHANNEL::A] | port;
*(volatile uint8_t*)(tccrB) |= Modes[mode][CHANNEL::B] | prescale;
}
private:
enum CHANNEL : uint8_t { A, B };
};
#endif // TIMERHELPERS_H
Сб июл 29, 2023 13:25:29
Сб июл 29, 2023 19:12:31
Сб июл 29, 2023 19:58:39
Вс июл 30, 2023 10:58:32
это что, один импульс на 3 (ТРИ) оборота коленвала?Alex_ka писал(а):На авто 800 об/ мин. это 800/60 сек/3 = 4,44 Гц.