Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Generating PWM with TIMER2 ATMega328P

Status
Not open for further replies.

Cantafford

Member
Hello,

I have connected 2 ATMEGA328P's with SPI communication one as master one as slave. On the master MCU I have also attached a keypad. I want to read a key from that keypad and send it via SPI to the slave. I will use a test-led which will light up when the value of the key pressed is 0. I have tested the interfacing of the keypad. It works fine.

Should be simple however I cannot seem to be able to send the value of the key pressed. I have managed to send data which I write in code but when I press a key the data does not reach the slave device for some reason.

Here is the code for the master:
Code:
/*
 * SPI.c
 *
 * Created: 2/18/2017 10:09:10 AM
 *  Author: Paul
 */

#define F_CPU 8000000

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





void SPIMasterInit(void);
void SPIMasterSend(int key);
int GetKeyPressed();



int main(void)
{
 //SPIMasterInit();
 int key;

 while (1)
 {
  key=GetKeyPressed(); // key gets read properly

  SPIMasterSend(key); // send the read value of the key!!!
 }
}



//SPI init
void SPIMasterInit(void)
{
 //set MOSI, SCK and SS as output
 DDRB |= (1<<PB3)|(1<<PB5)|(1<<PB2);
 //set SS to high
 PORTB |= (1<<PB2);
 //enable master SPI at clock rate Fck/16
 SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}

//master send function
void SPIMasterSend(int key)
{
 //select slave( SS must be pulled to ground before sending data )
 PORTB &= ~(1<<PB2);
 //send data(=1)
 SPDR=key; // this is the data sent!!!!
 //wait for transmition complete
 while (!(SPSR &(1<<SPIF)));
 //SS to high
 PORTB |= (1<<PB2);
}

int GetKeyPressed()
{
      int r,c;

      PORTD|= 0X0F;

      for(c=0;c<3;c++)
      {
         DDRD&=~(0X7F);

         DDRD|=(0X40>>c);
         for(r=0;r<4;r++)
         {
            if(!(PIND & (0X08>>r)))
            {
               return (r*3+c);
            }
         }
      }

      return 0XFF;//Indicate No key pressed
}

And here is the code for the slave device:

Code:
/*
 * slave.c
 *
 * Created: 2/18/2017 10:48:09 AM
 *  Author: Paul
 */

//SPI slave

#define F_CPU 8000000

#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "lcd.h"
#include "myutils.h"



void SPISlaveInit(void);
void InitLCD();

ISR(SPI_STC_vect)
{
 uint8_t recievedData;
 recievedData = SPDR;
 if(recievedData == 0) PORTB |= (1<<0);
}



int main(void)
{
 
 int key;
 DDRB |= (1<<0); // check led set as output

 InitLCD();
 sei();

 while (1)
 {
 
 
 }
}


void SPISlaveInit(void)
{
 //set MISO as output
 DDRB |= (1<<PB4);
 //enable SPI and enable SPI interrupt
 SPCR = (1<<SPE)|(1<<SPIE);
}


void InitLCD()
{
    LCDInit(LS_NONE|LS_NONE);
    LCDClear();
    LCDWriteString("Test text ");
    _delay_ms(150);
    LCDClear();
}



Here is how I connected everything. Based on my code whenever I press keypad(1) the LED should light up. However the LED is off at all times.
9ixv6e.jpg


Please help me correct this. Thank you.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top