I am trying to do the following small project:

Master MCU have 2 buttons - PINC0 and PINC1

PINC0 will count the clicks

PINC1 will blink the LED n times (n=counter) and send n to the SLAVE MCU

The slave MCU will get the n and blink the LED on his side.

I am using ATmega8

See MASTER code:

#ifndef F_CPU 
#define F_CPU 1000000UL 

#include <avr/io.h> 
#include <util/delay.h> 
#include <avr/interrupt.h> 

#define ACK 0x7E 

void spi_init_master (void) 
   DDRB = (1<<5)|(1<<3); 
   SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0); 

unsigned char spi_tranceiver (unsigned char data) 
   SPDR = data; 
   return SPDR; 

void led_blink(int x) 
   for (int i=0; i<x ; i++) 

int main(void) 
   int counter = 0; 
   int flag = 0; 
   unsigned char data; 

      unsigned char pressed = ~PINC; 
      if (PINC & 0x01) { 

      if ((pressed & 0x01) && (flag == 1)) { 

      if (pressed & 0x02) { 
         data = spi_tranceiver(counter); 

What I am trying to do here is count PINC0 clicks (counter increase each click), and flashing LED "counter" times on PINC1 button click.

Also - on PINC1 button click I want to communicate with SLAVE SPI MCU, which also blink his LED "counter" times.

The hardware connections is:

  1. Vcc, GND to external source
  2. LED positive led to PD7, negative to resistor connected to GND
  3. Buttons, PC0, PC1 connected to one leg of each button, and the other leg connected to GND.

The issues are:

  1. The button is pull up active low. the button connected by 1 leg to IO pin and other leg to GND. It works, but half of the time the LED blinks on the release as well.
  2. The SPI does not work - did I miss something?

Why are those 2 issues occuring?

  • 2
    \$\begingroup\$ Contact bounce. An easy check is to connect a 100nF capacitor from the input to ground. If that reduces the problem, then consider rewriting your program to cope with it. There are several questions on both electronics.SE and arduino.SE addressing this issue. arduino.stackexchange.com/questions/408/… (I know you are not on Arduino, but the concept of debouncing is identical). \$\endgroup\$ – jippie Mar 2 '14 at 9:39
  • \$\begingroup\$ thanks, i will read it. also - what about the SPI ? can you notice any error on my code? \$\endgroup\$ – gabi Mar 2 '14 at 10:34

i used software de-bounce.
thank you all.
i increase counter inside the click if statement, only if it more than 100 i doing my things.. otherwise if probably the debounce.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.