Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Topic 6 : SPI Serial Port

U2 U1
22 9 9 22
PC0/SCL RESET RESET PC0/SCL
23 23
PC1/SDA PC1/SDA
24 13 13 24
PC2/TCK XTAL1 XTAL1 PC2/TCK
25 12 12 25
PC3/TMS XTAL2 XTAL2 PC3/TMS
26 26
PC4/TDO PC4/TDO
27 40 40 27
PC5/TDI PA0/ADC0 PA0/ADC0 PC5/TDI
28 39 39 28
PC6/TOSC1 PA1/ADC1 PA1/ADC1 PC6/TOSC1
29 38 38 29
PC7/TOSC2 PA2/ADC2 PA2/ADC2 PC7/TOSC2
37 37
PA3/ADC3 PA3/ADC3
BT0 14 36 36 14 LED0
PD0/RXD PA4/ADC4 PA4/ADC4 PD0/RXD
BT1 15 35 35 15 LED1
PD1/TXD PA5/ADC5 PA5/ADC5 PD1/TXD
BT2 16 34 34 16 LED2
PD2/INT0 PA6/ADC6 PA6/ADC6 PD2/INT0
BT3 17 33 33 17 LED3
PD3/INT1 PA7/ADC7 PA7/ADC7 PD3/INT1
BT4 18 18 LED4
PD4/OC1B PD4/OC1B
BT5 19 1 1 19 LED5
PD5/OC1A PB0/T0/XCK PB0/T0/XCK PD5/OC1A
BT6 20 2 2 20 LED6
PD6/ICP1 PB1/T1 PB1/T1 PD6/ICP1
BT7 21 3 3 21 LED7
PD7/OC2 PB2/AIN0/INT2 PB2/AIN0/INT2 PD7/OC2
4 4
PB3/AIN1/OC0 PB3/AIN1/OC0
5 5
PB4/SS PB4/SS
6 6
PB5/MOSI PB5/MOSI
32 7 7 32
AREF PB6/MISO PB6/MISO AREF
30 8 8 30
AVCC PB7/SCK PB7/SCK AVCC
ATMEGA32 ATMEGA32

D1 D5
BT0 BT4 LED0
R2 LED4
R6
680 680
LED-YELLOW LED-YELLOW

BT1 BT5 D2 D6
LED1
R3 LED5
R7
680 680
LED-YELLOW LED-YELLOW

BT2 BT6
D3 D7
LED1
R4 LED6
R8
680 680
LED-YELLOW LED-YELLOW
BT3 BT7

D4 D8
LED3
R5 LED7
R9
680 680
LED-YELLOW LED-YELLOW

Problem 1 : Design two embedded systems using ATMEGA32 (working at 8Mhz) communicate together by SPI serial interface. One system
work as the Master SPI, One system work as the Slave SPI. The Master SPI Microcontoller has 8 buttons, The Slave SPI Microcontoller has
LEDs. Wrrite the C program to control the two system, When button0 is pressed the Master system will send character ‘0’ to slave system,
When button1 is pressed the Master system will send character ‘1’ to slave system . When the slave system receiving new character from the SPI
interface the system will toggle the relevant LED ( for example when the character received is ‘0’ LED0 will be toggled, when the character
received is ‘1’ LED1 will be toggled).

Master

# define F_CPU 8000000UL // define crystal frequency for delay.h

#include <avr/io.h> // standard AVR header

#include <util/delay.h>

#define SS 4 // Slave Select is Bit No.4

#define MOSI 5 // Master Out Slave In is Bit No.5

#define MISO 6 // Master In Slave Out is Bit No.6

#define SCK 7 // Shift Clock is Bit No.7

void SPI_MasterInit(void){

// Set MOSI, SCK and SS as Output Pins

DDRB |= (1<<MOSI) | (1<<SCK) | (1<<SS) ;

DDRB &= ~(1<<MISO); // Set MISO as an Input Pin


// Enable SPI, Master mode, Shift Clock = CLK /16

SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);

PORTB &= ~(1<<SS); // Enable Slave Select Pin

unsigned char SPI_MasterTransmit(unsigned char cData){

SPDR = cData; // Start transmission

while(!(SPSR & (1<<SPIF))); // Wait for transmission complete

return SPDR; // return the received data

int main(){

DDRD = 0x00; // PORTA is Output Port

DDRC = 0xFF;

DDRA = 0xFF; // Run as SPI Master

PORTD=0xFF; //Pull up resistors

SPI_MasterInit(); // Run as SPI Master

while(1)

if((PIND & 1<<0)==0) //If BT0 is pressed

{ SPI_MasterTransmit(0);

//-----------

if((PIND & 1<<1)==0) //If BT1 is pressed

{ SPI_MasterTransmit(1);

//--------------

if((PIND & 1<<2)==0) //If BT3 is pressed

{ SPI_MasterTransmit(2);
}

if((PIND & 1<<3)==0) //If BT4 is pressed

{ SPI_MasterTransmit(3);

if((PIND & 1<<4)==0) //If BT5 is pressed

{ SPI_MasterTransmit(4);

if((PIND & 1<<5)==0) //If BT6 is pressed

{ SPI_MasterTransmit(5);

if((PIND & 1<<6)==0) //If BT7 is pressed

{ SPI_MasterTransmit(6);

if((PIND & 1<<7)==0) //If BT8 is pressed

{ SPI_MasterTransmit(7);

return 0;

Slave

#define F_CPU 8000000UL // define crystal frequency for delay.h

#include <avr/io.h> // standard AVR header


#include <util/delay.h>

#define SS 4 // Slave Select is Bit No.4

#define MOSI 5 // Master Out Slave In is Bit No.5

#define MISO 6 // Master In Slave Out is Bit No.6

#define SCK 7 // Shift Clock is Bit No.7

void SPI_SlaveInit(void){

DDRB |= (1<<MISO); // Set MISO as an Output Pin

// Set MOSI, SCK and SS as Input Pins

DDRB &= ~(1<<MOSI) & ~(1<<SCK) & ~(1<<SS) ;

// Enable SPI as a Slave Device

SPCR = (1<<SPE);

unsigned char SPI_SlaveReceive(unsigned char cData){

SPDR = cData; // send cData to master

// Wait for reception complete

while(!(SPSR & (1<<SPIF)));

// Return received data

return SPDR;

int main(){

unsigned char ch;

DDRD = 0xFF; // PortA is Output

SPI_SlaveInit(); // configure as SPI Slave

while(1)

// send value of PORTA to Master and

// Show Received data at PORTA

PORTD ^= 1<<SPI_SlaveReceive(PORTA);

return 0;

Problem 2 : Convert the C Program of Master SPI Microcontroller into AVR Assembly code.

Chịu, dài vãi luôn ạ

You might also like