Added Timer.h and Timer.c. Completed Task to use timers to blink LED1 with a 1Hz frequency
parent
578a2fd693
commit
2bd2215ee9
@ -1,62 +1,80 @@
|
||||
#include <avr/io.h>
|
||||
|
||||
void Led_init(void){
|
||||
void
|
||||
Led_init (void){
|
||||
DDRB = 0xFF; // Set all bits in the B Data-Direction Register to output
|
||||
PORTB = 0x00; // Set all bits in the PORT B Register to low to turn all LEDs off
|
||||
}
|
||||
|
||||
void Led1_On(void){
|
||||
void
|
||||
Led1_On (void){
|
||||
PORTB |= (1<<7); // Set the bit for LED_1 to hight to turn it on
|
||||
}
|
||||
void Led1_Off(void){
|
||||
void
|
||||
Led1_Off (void){
|
||||
PORTB &= ~(1<<7); // Set the bit for LED_1 to low to turn it off
|
||||
}
|
||||
|
||||
void Led2_On(void){
|
||||
void
|
||||
Led2_On (void){
|
||||
PORTB |= (1<<6);
|
||||
}
|
||||
void Led2_Off(void){
|
||||
void
|
||||
Led2_Off (void){
|
||||
PORTB &= ~(1<<6);
|
||||
}
|
||||
|
||||
void Led3_On(void){
|
||||
void
|
||||
Led3_On (void){
|
||||
PORTB |= (1<<5);
|
||||
}
|
||||
void Led3_Off(void){
|
||||
void
|
||||
Led3_Off (void){
|
||||
PORTB &= ~(1<<5);
|
||||
}
|
||||
|
||||
void Led4_On(void){
|
||||
void
|
||||
Led4_On (void){
|
||||
PORTB |= (1<<4);
|
||||
}
|
||||
void Led4_Off(void){
|
||||
void
|
||||
|
||||
Led4_Off (void){
|
||||
PORTB &= ~(1<<4);
|
||||
}
|
||||
|
||||
void Led5_On(void){
|
||||
void
|
||||
Led5_On (void){
|
||||
PORTB |= (1<<3);
|
||||
}
|
||||
void Led5_Off(void){
|
||||
void
|
||||
Led5_Off (void){
|
||||
PORTB &= ~(1<<3);
|
||||
}
|
||||
|
||||
void Led6_On(void){
|
||||
void
|
||||
Led6_On (void){
|
||||
PORTB |= (1<<2);
|
||||
}
|
||||
void Led6_Off(void){
|
||||
void
|
||||
Led6_Off (void){
|
||||
PORTB &= ~(1<<2);
|
||||
}
|
||||
|
||||
void Led7_On(void){
|
||||
void
|
||||
Led7_On (void){
|
||||
PORTB |= (1<<1);
|
||||
}
|
||||
void Led7_Off(void){
|
||||
void
|
||||
Led7_Off (void){
|
||||
PORTB &= ~(1<<1);
|
||||
}
|
||||
|
||||
void Led8_On(void){
|
||||
void
|
||||
Led8_On (void){
|
||||
PORTB |= (1<<0);
|
||||
}
|
||||
void Led8_Off(void){
|
||||
void
|
||||
Led8_Off (void){
|
||||
PORTB &= ~(1<<0);
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Timer.c
|
||||
*
|
||||
* Created: 28/10/2021 17:51:29
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
volatile uint16_t cntr = 0;
|
||||
|
||||
void
|
||||
Timer_init (void){
|
||||
TCCR0B |= (1<<1); /* Set the clock select bit to pre-scaler 2
|
||||
1MHz / 8 (pre-scaler 2) ==> 125KHz */
|
||||
|
||||
TCCR0A |= (1<<1); /* Set WGM01 to high to enable CTC mode */
|
||||
TIMSK0 |= (1<<1); /* Set OCIE0A to high to rise an interrupt when the counter matches OCR0A */
|
||||
|
||||
OCR0A = 125; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 1ms
|
||||
1MHz / 8 (pre-scaler 2) / 125 ==> 1KHz (1ms) */
|
||||
}
|
||||
|
||||
|
||||
uint16_t
|
||||
Timer_getTick(void){
|
||||
return cntr;
|
||||
}
|
||||
|
||||
|
||||
ISR(TIMER0_COMPA_vect){
|
||||
cntr++;
|
||||
}
|
||||
|
||||
/*
|
||||
ISR(TIMER0_OVF_vect){
|
||||
cntr++;
|
||||
}
|
||||
//*/
|
@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Timer.h
|
||||
*
|
||||
* Created: 28/10/2021 17:49:26
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TIMER_H_
|
||||
#define TIMER_H_
|
||||
#include <avr/io.h>
|
||||
|
||||
void Timer_init(void);
|
||||
uint16_t Timer_getTick(void);
|
||||
|
||||
#endif /* TIMER_H_ */
|
Loading…
Reference in New Issue