I tried to enable the SPI in ATmega328P, through arduino code.
First I simply try to enable the SPI as master mode. As per the 328P datasheet when we enable the SPI as master then clock will automatically comes from D13 th pin. Is this correct ?.
DDRB = 0b00101000; // Pin B5 and B3 as output; clock and MOSI
SPCR = 0b11010000; // SPIE,SPE, MSTR is set as one.
After uploading this into nano. I hooked the probe Chinese logic analyzer D13 th PIN and use Pulse view software for analyzing purpose. But I din't get any clock signal.
Note : Capturing time is 1M Samples 1Mhz speed selected in Pulse view tool.
My doubt is in port B6 and B7 is external clock. So In this case I used to enable the MOS and SCK 0b00101000 so this higher bit may disable the clock... is this correct ?.
You're not sending any data, so the clock doesn't do anything.
Why are you changing the registers directly? What's wrong with the SPI library that comes with Arduino?
No No First of all I am Arduino lover. But I want to access the register directly. that is why. please guide me what is the next step in SPI. the above two lines is enabling the SPI ?. or any other is need ?.
/* The following code examples show how to initialize the SPI as a Master and how to perform a simple transmission. DDR_SPI in the examples must be replaced by the actual Data Direction Register controlling the SPI pins. DD_MOSI, DD_MISO and DD_SCK must be replaced by the actual data direction bits for these pins. E.g. if MOSI is placed on pin PB5, replace DD_MOSI with DDB5 and DDR_SPI with DDRB. */
/* Set MOSI and SCK output, all others input */
DDR_SPI = (1 << DD_MOSI) | (1 << DD_SCK);
/* Enable SPI, Master, set clock rate fck/16 */
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0);
void SPI_MasterTransmit(char cData)
/* Start transmission */
SPDR = cData;
/* Wait for transmission complete */
while (!(SPSR & (1 << SPIF)))
You are setting SPIE (SPI Interrupt Enable) but you are not declaring an interrupt handler for it.
Are you looking for the data on the right pin? MOSI is Pin 11 on the Arduino UNO.
Arduino Nano(same pin)
Yes I am looking my data out through MOSI pin,
How to set the interrupt handler. Ok, then Without SPIE can I transfer data ?. What is the purpose of the the SPIE.(after finishing data transfer this bit will set ? Correct ?)
When configured as a Master, the SPI interface has no automatic control of the SS line. This must be
handled by user software before communication can start. When this is done, writing a byte to the SPI
Data Register starts the SPI clock generator, and the hardware shifts the eight bits into the Slave. After shifting one byte, the SPI clock generator stops, setting the end of Transmission Flag (SPIF). If the SPI
Interrupt Enable bit (SPIE) in the SPCR Register is set, an interrupt is requested. The Master may
continue to shift the next byte by writing it into SPDR, or signal the end of packet by pulling high the Slave
Select, SS line. The last incoming byte will be kept in the Buffer Register for later use.
When a serial transfer is complete, the SPIF Flag is set. An interrupt is generated if SPIE in SPCR is set
and global interrupts are enabled. If SS is an input and is driven low when the SPI is in Master mode, this
will also set the SPIF Flag. SPIF is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, the SPIF bit is cleared by first reading the SPI Status Register with SPIF set, then accessing the SPI Data Register (SPDR).
You do not do anything. In fact the SPIF flag is read only so you can't do anything.