-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmillis.c
More file actions
59 lines (52 loc) · 1.08 KB
/
millis.c
File metadata and controls
59 lines (52 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Millis().c
*
* Created: 4/3/2019 12:51:31 AM
* Author : Eng. Hossam
*/
/*
* Timer.c
*
* Created: 3/31/2019 8:59:41 PM
* Author : Eng. Hossam
*/
//255-(16MHZ/Prescaler/Disierd Freq)= 255-(16MHZ/1024/1)=15625 255-((16MHz/1024/100)*10^6)=99 100hz->10ms
//Freq=1/Time = 1 HZ
#include <avr/io.h>
#include <avr/interrupt.h>
volatile unsigned long millis_value;
unsigned long time1=0;
unsigned long millis();
int main(void)
{
DDRB |= (1<<DDB5);
OCR0A = 249;
TCCR0A |= (1<<WGM01) ;
TCCR0B |= (1<<CS00) | (1<<CS01);// Prescaler = 64
TIMSK0 |= (1<<OCIE0A);
sei();
time1=millis();
/* Replace with your application code */
while (1)
{
if((unsigned long)millis()-time1>1000)
{
PORTB ^= (1<<PORTB5);
time1=millis();
}
}
}
ISR(TIMER0_COMPA_vect)
{
millis_value++;
}
unsigned long millis()
{
unsigned long m;
// Disable interrupts while we read millis_value or we might get an
// inconsistent value (e.g. in the middle of a write to millis_value)
cli();
m = millis_value;
sei();
return m;
}