#include #define F_CPU 1000000UL // 1 MHz #include // convert from loop cycles to msecs #define _delay_ms(x) _delay_loop_2((x)*(F_CPU/4000)) /* void delay_ms(unsigned int t) _delay_loop_2() can only take a 16-bit argument. This routine repeatedly calls _delay_loop_2() in order to get the desired busy wait length. Note that there is some overhead in the loop that is not counted in the delay (so the delay is actually just a little longer) This code is also specific to the 1 MHz clock */ void delay_ms(unsigned int t) { unsigned int nloops = (unsigned short) (t >> 8); unsigned int i; for(i = 0; i < nloops; ++i) { _delay_ms(256); }; i = t & 0xFF; if(i > 0) { _delay_ms(i); }; }; int main (void) { /* Set PORTB (LEDs) to all outputs */ DDRB = 0xFF; while(1) { PORTB = PORTB ^ 0x1; // Toggle bit 0 delay_ms(500); PORTB = PORTB ^ 0x2; // Toggel bit 1 delay_ms(250); PORTB = PORTB ^ 0x2; // Toggel bit 1 delay_ms(250); }; return (0); }