0
\$\begingroup\$

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 
#endif 

#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; 
   while(!((SPSR)&(1<<SPIF))); 
   return SPDR; 
} 

void led_blink(int x) 
{ 
   for (int i=0; i<x ; i++) 
   { 
      PORTD|=(1<<7); 
      _delay_ms(150); 
      PORTD^=(1<<7); 
      _delay_ms(70); 
   } 
} 

int main(void) 
{ 
   spi_init_master(); 
   DDRD=0xff; 
   DDRC=0; 
   PORTC|=(1<<0)|(1<<1); 
   int counter = 0; 
   int flag = 0; 
   unsigned char data; 

   while(1) 
   { 
      unsigned char pressed = ~PINC; 
      if (PINC & 0x01) { 
         flag=1; 
      } 

      if ((pressed & 0x01) && (flag == 1)) { 
         flag=0; 
         counter++; 
         PORTD|=(1<<PD7); 
         _delay_ms(20); 
      } 
      PORTD=0; 

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

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?

\$\endgroup\$
  • 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
0
\$\begingroup\$

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.

\$\endgroup\$

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.