Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define CPU_CLK_HZ 16000000
- #define LOOPS_PER_MS (((CPU_CLK_HZ/1000)-(1+1+2+2))/(2+2)) // accounting for the overhead of 6 (1+1+2+2) cycles per ms and the 4 (2+2) cycles per inner loop iteration
- static inline void ms_spin(unsigned short ms) {
- if (ms) {
- unsigned short dummy;
- __asm__ __volatile__ (
- "ms_spin_outer_loop_%=: \n"
- " ldi %A[loopcnt], lo8(%[loops]) \n"
- " ldi %B[loopcnt], hi8(%[loops]) \n"
- "ms_spin_inner_loop_%=: \n"
- " sbiw %A[loopcnt], 1 \n"
- " brne ms_spin_inner_loop_%= \n"
- " sbiw %A[ms], 1 \n"
- " brne ms_spin_outer_loop_%= \n"
- : [ms] "+w" (ms),
- [loopcnt] "=&w" (dummy)
- : [loops] "i" (LOOPS_PER_MS)
- : // none.
- );
- }
- }
- // the setup function runs once when you press reset or power the board
- void setup() {
- // initialize digital pin LED_BUILTIN as an output.
- pinMode(LED_BUILTIN, OUTPUT);
- }
- // the loop function runs over and over again forever
- void loop() {
- digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
- ms_spin(500); //delay(1000); // wait for a second
- digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
- ms_spin(500); //delay(1000); // wait for a second
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement