ANS HAFEEZ
Advanced Member level 4
- Joined
- Jul 25, 2013
- Messages
- 101
- Helped
- 5
- Reputation
- 10
- Reaction score
- 5
- Trophy points
- 1,298
- Location
- LAHORE,PAKISTAN
- Activity points
- 1,911
/*
* SPI_8_MASTER.c
*
* Created: 1/8/2015 3:36:36 PM
* Author: ANCC
*/
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#define MOSI PINB3
#define SCK PINB5
#define MISO PINB4
void SPI_MasterInit(void)
{
/* Set MOSI and SCK output, all others input */
DDRB = (1<<MOSI)|(1<<SCK);
DDRB &= ~(1<<MISO);
/* Enable SPI, Master, set clock rate fck/16 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1);
SPSR=0x00;
}
void SPI_MasterTransmit(char cData)
{
/* Start transmission */
SPDR = cData;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));
}
int main(void)
{
SPI_MasterInit();
while(1)
{
SPI_MasterTransmit('C');
}
}
/*
* SPI_32.c
*
* Created: 1/8/2015 3:26:11 PM
* Author: ANCC
*/
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#define MISO PINB6
#define MOSI PINB5
#define SCK PINB7
void SPI_SlaveInit(void)
{
/* Set MISO output, all others input */
DDRB |= (1<<MISO);
DDRB &=~(1<<MOSI)|(1<<SCK);
/* Enable SPI */
SPCR = (1<<SPE)|(1<<SPR1);
SPSR=0x00;
}
char SPI_SlaveReceive(void)
{
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)))
;
/* Return data register */
return SPDR;
}
int main(void)
{
SPI_SlaveInit();
int data=0;
DDRD=0xFF;
while(1)
{
data=SPI_SlaveReceive();
PORTD=data;
}
}
but now simulation Works properly
you mean simulation works with lcd display but not at real hardware?
just a thought why spi direct UART is easy to implement.and you can check the spi logic with logic analyzer.
#define MISO PB6
#define MOSI PB5
#define SCK PB7
void SPI_SLAVE_INIT(void)
{
DDRB |= (1<<MISO);//|(1<<PINB3);
SPCR = (1<<SPE)|(1<<SPIE);//|(1<<CPHA);//|(1<<SPR1)|(1<<SPR0);
}
char SPI_RECEIVE(void)
{
while(!(SPSR & (1<<SPIF)));
return SPDR;
}
/*
* CENTRAL_MEGA32_CODE.c
*
* Created: 10/22/2014 7:41:55
* Author: ANCC
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <compat/twi.h>
#include <math.h>
#include <avr/interrupt.h>
#include "LCD.h"
#include "MPU_6050.h"
#include "UART.h"
#include "MOTOR.h"
#include "SPI.h"
void DISABLE_JTAG()
{
MCUCSR |=1<<JTD;
MCUCSR |=1<<JTD;
}
void START_SPI()
{
int CHK=0;
CMD(LINE1_1);
display("STARTING SPI FOR");
CMD(LINE2_1);
display("OBSTACLES DATA");
_delay_ms(1000);
SPI_SLAVE_INIT();
CHK=SPI_RECEIVE();
LCD_CLS();
if(CHK==1 || CHK==2 || CHK==3 || CHK==4 || CHK==5 || CHK==6 || CHK==7) display("SPI OK! (Y)");
else display("ERROR IN SPI :(");
_delay_ms(1000);
LCD_CLS();
_delay_ms(1);
}
int main(void)
{
DDRD=0x00; // SAVE H-Bridge
DISABLE_JTAG();
START_SPI();
while(1)
{
SPI_RECEIVE();
LCD_DisplayNumber(SPDR);
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?