Skip to content

Commit

Permalink
SPI working
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-accarini committed Apr 20, 2015
1 parent 94ed360 commit d407747
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
2 changes: 0 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ int main(void)

// while(1)
// {
_delay_ms(1);
_Enable_SPI();
_SPI_Send_Data(0x0000,0x33);
_SPI_Send_Data(0x0000,0x33);
(*ADC_DataAddress) = _SPI_Receive_Data(0x0000);
//Input command
//getLine(str);
Expand Down
50 changes: 37 additions & 13 deletions mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,27 @@ void _SPI_Transmit(unsigned char Data)

void _SPI_Send_Data(unsigned int Address, unsigned char Data)
{
unsigned char temp;

//Chip Select Down
_delay_us(10);
PORTB &= ~(1 << PB4);
_delay_us(10);
/* Start transmission */
SPDR = SPI_WriteEnable;
/* Wait for transmission complete */
while (!(SPSR & (1<<SPIF)))
;
//Chip Select up
_delay_us(10);
PORTB |= (1 << PB4);
_delay_us(10);
//Delay
_delay_us(10);
//Chip Select Down
_delay_us(10);
PORTB &= ~(1 << PB4);
_delay_us(10);
//WriteCommand
/* Start transmission */
SPDR = SPI_Write;
Expand All @@ -136,23 +144,38 @@ void _SPI_Send_Data(unsigned int Address, unsigned char Data)
while (!(SPSR & (1<<SPIF)))
;
//Chip Select up
_delay_us(10);
PORTB |= (1 << PB4);
_delay_us(100);
_delay_us(10);


//Wait for the write to be completed
do
{
//Chip Select Down
_delay_us(10);
PORTB &= ~(1 << PB4);
_delay_us(10);
/* Start transmission */
SPDR = SPI_ReadStatusReg;
/* Wait for transmission complete */
while (!(SPSR & (1<<SPIF)))
;
//Generate Clock
SPDR = 0x00;
/* Wait for transmission complete */
while (!(SPSR & (1<<SPIF)))
;
temp = SPDR;
//Chip Select up
_delay_us(10);
PORTB |= (1 << PB4);
_delay_us(10);
} while ((temp & 0x01) == 0x01);
}

unsigned char _SPI_Receive_Data(unsigned int Address)
{
//Chip Select Down
PORTB &= ~(1 << PB4);
//Reset Write Enable Command
SPDR = SPI_ResetWrEnable;
/* Wait for transmission complete */
while (!(SPSR & (1<<SPIF)))
;
//Chip Select Up
PORTB |= (1 << PB4);
//Delay
_delay_us(10);
//Chip Select Down
PORTB &= ~(1 << PB4);
//ReadCommand
Expand All @@ -173,7 +196,7 @@ unsigned char _SPI_Receive_Data(unsigned int Address)
while (!(SPSR & (1<<SPIF)))
;
//Data
SPDR = 0xFF;
SPDR = 0x00;
/* Wait for transmission complete */
while (!(SPSR & (1<<SPIF)))
;
Expand All @@ -183,3 +206,4 @@ unsigned char _SPI_Receive_Data(unsigned int Address)
return SPDR;
}


0 comments on commit d407747

Please sign in to comment.