This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
et:software:homelab:library:timer [2010/02/11 14:00] – mikk.leini | et:software:homelab:library:timer [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 32: | Line 32: | ||
* // | * // | ||
* // | * // | ||
+ | < | ||
* **// | * **// | ||
* **// | * **// | ||
Line 186: | Line 186: | ||
timer0_overflow_interrupt_enable(true); | timer0_overflow_interrupt_enable(true); | ||
- | // Globaalne | + | // Globaalne |
sei(); | sei(); | ||
} | } | ||
</ | </ | ||
- | |||
- | ===== Lähtekood ===== | ||
- | |||
- | Järgnevalt on lühidalt toodud teegi taimer 0 funktsioonid. Sarnased on ka teiste taimerite funktsioonid mida pole siinkohal kirja pandud liiga pika programmikoodi tõttu. Pikemalt saab teegi lähtekoodiga tutvuda selle Kodulabori veebilehelt alla tõmmates. | ||
- | |||
- | <code c> | ||
- | #include < | ||
- | #include " | ||
- | |||
- | typedef enum | ||
- | { | ||
- | TIMER0_NO_PRESCALE | ||
- | TIMER0_PRESCALE_8 | ||
- | TIMER0_PRESCALE_32 | ||
- | TIMER0_PRESCALE_64 | ||
- | TIMER0_PRESCALE_128 | ||
- | TIMER0_PRESCALE_256 | ||
- | TIMER0_PRESCALE_1024 | ||
- | } | ||
- | timer0_prescale; | ||
- | |||
- | inline void timer0_init_normal(timer0_prescale prescale) | ||
- | { | ||
- | TCCR0 = prescale & 0x07; | ||
- | } | ||
- | |||
- | inline void timer0_stop() | ||
- | { | ||
- | TCCR0 = 0x00; | ||
- | } | ||
- | |||
- | inline unsigned char timer0_get_value(void) | ||
- | { | ||
- | return TCNT0; | ||
- | } | ||
- | |||
- | inline void timer0_set_value(unsigned char value) | ||
- | { | ||
- | TCNT0 = value; | ||
- | } | ||
- | |||
- | inline void timer0_overflow_interrupt_enable(bool enable) | ||
- | { | ||
- | bit_set_to(TIMSK, | ||
- | } | ||
- | |||
- | inline bool timer0_overflow_flag_is_set(void) | ||
- | { | ||
- | return (bit_is_set(TIFR, | ||
- | } | ||
- | |||
- | inline void timer0_overflow_flag_clear(void) | ||
- | { | ||
- | bit_set(TIFR, | ||
- | } | ||
- | </ | ||
- | /* | ||
- | typedef enum | ||
- | { | ||
- | TIMER1_NO_PRESCALE | ||
- | TIMER1_PRESCALE_8 | ||
- | TIMER1_PRESCALE_64 | ||
- | TIMER1_PRESCALE_256 | ||
- | TIMER1_PRESCALE_1024 | ||
- | TIMER1_PRESCALE_T1_FALLING = 0x06, | ||
- | TIMER1_PRESCALE_T1_RISING | ||
- | } | ||
- | timer1_prescale; | ||
- | |||
- | typedef enum | ||
- | { | ||
- | TIMER1_CTC_TOP_OCRA = 0x04, | ||
- | TIMER1_CTC_TOP_ICR | ||
- | } | ||
- | timer1_ctc_top; | ||
- | |||
- | typedef enum | ||
- | { | ||
- | TIMER1_FAST_PWM_TOP_256 | ||
- | TIMER1_FAST_PWM_TOP_512 | ||
- | TIMER1_FAST_PWM_TOP_1024 = 0x03, | ||
- | TIMER1_FAST_PWM_TOP_ICR | ||
- | TIMER1_PAST_PWM_TOP_OCRA = 0x0F, | ||
- | } | ||
- | timer1_fast_pwm_top; | ||
- | |||
- | typedef enum | ||
- | { | ||
- | TIMER1_FAST_PWM_OUTPUT_DISABLE | ||
- | TIMER1_FAST_PWM_OUTPUT_TOGGLE_ON_MATCH = 0x01, | ||
- | TIMER1_FAST_PWM_OUTPUT_CLEAR_ON_MATCH | ||
- | TIMER1_FAST_PWM_OUTPUT_SET_ON_MATCH | ||
- | } \ | ||
- | timer1_fast_pwm_output_mode; | ||
- | |||
- | inline void timer1_init_normal(timer1_prescale prescale) | ||
- | { TCCR1A = 0x00; TCCR1B = (prescale & 0x07); TCCR1C = 0x00; } | ||
- | |||
- | inline void timer1_init_ctc(timer1_prescale prescale, timer1_ctc_top top) | ||
- | { | ||
- | TCCR1A = (top & 0x03); | ||
- | TCCR1B = ((top & 0x0C) << 1) | (prescale & 0x07); | ||
- | TCCR1C = 0x00; | ||
- | } | ||
- | |||
- | inline void timer1_init_fast_pwm( | ||
- | timer1_prescale prescale, | ||
- | timer1_fast_pwm_top top, | ||
- | timer1_fast_pwm_output_mode output_a, | ||
- | timer1_fast_pwm_output_mode output_b, | ||
- | timer1_fast_pwm_output_mode output_c) | ||
- | { | ||
- | TCCR1A = | ||
- | ((output_a & 0x03) << 6) | | ||
- | ((output_b & 0x03) << 4) | | ||
- | ((output_c & 0x03) << 2) | | ||
- | (top & 0x03); \ | ||
- | TCCR1B = ((top & 0x0C) << 1) | (prescale & 0x07); | ||
- | TCCR1C = 0x00; | ||
- | } | ||
- | |||
- | inline void timer1_stop() | ||
- | { TCCR1A = 0x00; TCCR1B = 0x00; TCCR1C = 0x00; } | ||
- | |||
- | inline unsigned short timer1_get_value(void) { return TCNT1; } | ||
- | inline void timer1_set_value(unsigned short value) { TCNT1 = value; } | ||
- | |||
- | inline unsigned short timer1_get_compare_match_unitA_value(void) { return OCR1A; } | ||
- | inline unsigned short timer1_get_compare_match_unitB_value(void) { return OCR1B; } | ||
- | inline unsigned short timer1_get_compare_match_unitC_value(void) { return OCR1C; } | ||
- | |||
- | inline void timer1_set_compare_match_unitA_value(unsigned short value) { OCR1A = value; } | ||
- | inline void timer1_set_compare_match_unitB_value(unsigned short value) { OCR1B = value; } | ||
- | inline void timer1_set_compare_match_unitC_value(unsigned short value) { OCR1C = value; } | ||
- | |||
- | inline unsigned short timer1_get_input_capture_value(void) { return ICR1; } | ||
- | |||
- | inline void timer1_set_input_capture_value(unsigned short value) { ICR1 = value; } | ||
- | |||
- | inline void timer1_overflow_interrupt_enable(bool enable) { bit_set_to(TIMSK, | ||
- | |||
- | inline void timer1_compare_match_unitA_interrupt_enable(bool enable) { bit_set_to(TIMSK, | ||
- | inline void timer1_compare_match_unitB_interrupt_enable(bool enable) { bit_set_to(TIMSK, | ||
- | inline void timer1_compare_match_unitC_interrupt_enable(bool enable) { bit_set_to(TIMSK, | ||
- | |||
- | inline void timer1_input_capture_interrupt_enable(bool enable) { bit_set_to(TIMSK, | ||
- | |||
- | inline bool timer1_overflow_flag_is_set(void) { return (bit_is_set(TIFR, | ||
- | |||
- | inline void timer1_overflow_flag_clear(void) { bit_set(TIFR, | ||
- | |||
- | inline bool timer1_input_capture_flag_is_set(void) { return (bit_is_set(TIFR, | ||
- | |||
- | inline void timer1_input_capture_flag_clear(void) { bit_set(TIFR, | ||
- | */ | ||