kmdineshece
Member level 1
SPI Communication!///74HC595 with ATmega88Pa, my code is working perfectly in Proteus(software simulation), but it's not working in hardware!....i don't know where i did the mistake, pls guide me!...
Code:
#include <avr/io.h>
#define F_CPU 4000000UL
#include <util/delay.h>
#include <avr/interrupt.h>
#include<string.h>
#define F_CPU 8000000UL
#define SPI_PORT PORTB
#define SPI_DDR DDRB
#define SPI_CS PINB2
#define SPI_SCK PINB5
//static uint8_t digits[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
volatile unsigned char t=0;
volatile uint8_t c=0,q,r,s,n,z=0,e,f,g,x=0,a[5];
void SPI_Write(uint8_t dataout)
{
// Start transmission (MOSI)
SPDR = dataout;
SPI_PORT |= (1<<SPI_SCK);
_delay_us(10); // Hold pulse for 1 micro seconds
SPI_PORT &= ~(1<<SPI_SCK);
// Latch the Output using rising pulse to the RCK Pin
// Wait for transmission complete
while(!(SPSR & (1<<SPIF)));
// Latch the Output using rising pulse to the RCK Pin
SPI_PORT |= (1<<SPI_CS);
_delay_us(10); // Hold pulse for 1 micro seconds
// Disable Latch
SPI_PORT &= ~(1<<SPI_CS);
// Latch the Output using rising pulse to the RCK Pin
// Latch the Output using rising pulse to the RCK Pin
}
void setupSPI(void)
{
// Set MOSI and SCK as output, others as input
SPI_DDR|= (1<<PINB3)|(1<<PINB5)|(1<<PINB2);
// Latch Disable (RCK Low)
SPI_PORT &= ~(1<<SPI_CS);
// Enable SPI, Master, set clock rate fck/2 (maximum)
SPCR|= (1<<SPIE)|(1<<SPE)|(1<<MSTR);
SPSR|= (1<<SPI2X);
}
int main()
{
unsigned char u8TempData;
DDRC=0xff;
DDRD|=(1<<PIND4)|(1<<PIND5)|(1<<PIND6)|(1<<PIND7);
setupSPI();
sei();
PORTD|=(1<<PIND4);
PORTD|=(1<<PIND5);
while(1)
{
SPI_Write(((2*100)+(5*10)+(5*1)));
_delay_ms(1000);
SPI_Write(((0*100)+(0*10)+(0*1)));
_delay_ms(1000);
}
//return 0;
}