C program to set up and use HC12 SPI in master mode
C program spi_master.c
#include "hc12.h"
main()
{
/****************************************************************
* SPI Setup
*****************************************************************/
DDRS = DDRS | 0xE0; /* SS, SCLK, MOSI outputs */
PORTS = PORTS | 0x80; /* Bring SS high to deselect slave */
SP0CR1 = 0x50; /* 0 1 0 1 0 0 0 0
| | | | | | | |
| | | | | | | \____ MSB first
| | | | | | \______ multiple bytes with SS asserted
| | | | | \________ 0 phase
| | | | \__________ 0 polarity
| | | \____________ Master mode
| | \______________ not open drain
| \________________ Enable SPI
\__________________ No interrupts
*/
SP0CR2 = 0; /* Normal (not bi-directional) mode */
SP0BR = 0x00; /* 4 MHz SPI clock */
/****************************************************************
* End of SPI Setup
****************************************************************/
PORTS = PORTS & ~0x80; /* Bring SS low to select slave */
SP0DR = 'h'; /* Send 'h' */
while ((SP0SR & 0x80) == 0) ; /* Wait for transfer to finish */
SP0DR = 'e'; /* Send 'e' */
while ((SP0SR & 0x80) == 0) ; /* Wait for transfer to finish */
SP0DR = 'l'; /* Send 'l' */
while ((SP0SR & 0x80) == 0) ; /* Wait for transfer to finish */
SP0DR = 'l'; /* Send 'l' */
while ((SP0SR & 0x80) == 0) ; /* Wait for transfer to finish */
SP0DR = 'o'; /* Send 'o' */
while ((SP0SR & 0x80) == 0) ; /* Wait for transfer to finish */
PORTS = PORTS | 0x80; /* Bring SS high to deselect slave */
}