Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- * Created: 2016/08/22 04:02:04 PM
- * Author: Kai
- This code is written in order to set up a manual delay function using the 16 bit counter TCNT1
- and its control registers TCCR1A and TCCR1B which is set accordingly to make the counter run in normal mode
- and count for 4 seconds.
- the bit 0x01 in the TIFR1 register is also used to indicate when the timer has reached overflow
- */
- #include <avr/io.h>
- #define F_CPU 16000000UL
- #include <util/delay.h>
- void to_delay (void);
- int main(void)
- {
- DDRB=0xff; // outputs
- while(1)
- {
- PORTB = 0x20; // sets pin 13 high
- to_delay(); // calls a function which manually delays the code by 4 seconds
- PORTB = 0x00; // sets pin 13 low
- to_delay(); // again, calls the same function which manually delays the code by 4 seconds
- }
- }
- void to_delay (void){
- TCNT1 = 3026; // presets the 16 bit counter to the decimal value of 3026 / 65536
- TCCR1A = 0; // sets the control register to normal mode
- TCCR1B = 5; // sets the prescale factor to 1024. This devides the clock input by 1024. this also starts the count
- while((TIFR1&0x01)==0); // holds this line of code until the counter reaches overflow
- TCNT1 = 0; // clears the counter
- TIFR1 = 0x01; // resets the flag bit in the TIFRn register. this is done by SETTING the TOVn bit
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement