Arduino Pure C-Programming Part-4 delaymicroseconds
#include<avr/io.h>
#define SET_BIT(X,Y) X|=1<<Y
#define CLR_BIT(X,Y) X&=~(1<<Y)
#define IS_BIT_SET(X,Y) X&(1<<Y)
#define TOGGLE_BIT(X,Y) X^=(1<<Y)
void delaymicroseconds(unsigned long us)
{
unsigned long temp = us;
SET_BIT(TCCR0B,CS01); //Starting the timer with prescaler clock/8 which is 0.125us
while(temp != 0) {
TCNT0 = 247;
while(!IS_BIT_SET(TIFR0,TOV0));
CLR_BIT(TIFR0,TOV0);
temp--;
}
}
int main()
{
SET_BIT(DDRB,PB5);
TOGGLE_BIT(PORTB,PB5);
TOGGLE_BIT(PORTB,PB5);
while(1) {
delaymicroseconds(5000000L);
TOGGLE_BIT(PORTB,PB5);
}
return 0;
}
#define SET_BIT(X,Y) X|=1<<Y
#define CLR_BIT(X,Y) X&=~(1<<Y)
#define IS_BIT_SET(X,Y) X&(1<<Y)
#define TOGGLE_BIT(X,Y) X^=(1<<Y)
void delaymicroseconds(unsigned long us)
{
unsigned long temp = us;
SET_BIT(TCCR0B,CS01); //Starting the timer with prescaler clock/8 which is 0.125us
while(temp != 0) {
TCNT0 = 247;
while(!IS_BIT_SET(TIFR0,TOV0));
CLR_BIT(TIFR0,TOV0);
temp--;
}
}
int main()
{
SET_BIT(DDRB,PB5);
TOGGLE_BIT(PORTB,PB5);
TOGGLE_BIT(PORTB,PB5);
while(1) {
delaymicroseconds(5000000L);
TOGGLE_BIT(PORTB,PB5);
}
return 0;
}
Comments
Post a Comment