Hello,
I am a computer engineering student, an I am working on my senior year project.
I am trying to connect an accelerometer to the HC12 using the SPI
However I am not getting any readings
Can anyone please help me or at least tell me ways to debug it
here is the code>
#include <D12_LCD.h>
#include <spi0util.c>
void openspi0(void)
{
SPI0BR = 0x30; /* set baud rate to 6 MHz */
SPI0CR1 = 0x50;
SPI0CR2 = 0x02;
WOMS = 0x00; /* enable Port S pull-up */
}
void main(void)
{
char m;
int num;
int num2;
init_lcd();
clr_disp();
while (1)
{
//DDRM |= BIT1;/* configure the PM1 pin for output */
DDRM = 0xFF;
PTM &= ~BIT1;
openspi0(); /* configure SPI0 module */
putcspi0(0x0F);
m=getcspi0();
num=m;
//num2=num & 0x0F;
//num= (int ) m;
lcd_print_txt("I am: ",LINE1);
//put_char(m);
lcd_print_val(num);
}
}
header file:
/*******************************************************************************\
******/
/* The following function outputs a character to the SPI0 interface.
*/
/*******************************************************************************\
******/
void putcspi0 (char cx)
{ char temp;
while(!(SPI0SR & SPTEF)); /* wait until write is permissible */
SPI0DR = cx; /* output the byte to the SPI */
while(!(SPI0SR & SPIF)); /* wait until write operation is complete */
temp = SPI0DR; /* clear SPIF flag */
}
/*******************************************************************************\
*****/
/* The following function outputs a string to the SPI0 interface.
*/
/*******************************************************************************\
*****/
void putsspi0(char *ptr)
{
while(*ptr) { /* continue until all characters have been output */
putcspi0(*ptr);
ptr++;
}
}
/*******************************************************************************\
*****/
/* The following function reads a character from the SPI0 interface.
*/
/*******************************************************************************\
*****/
char getcspi0(void)
{
while(!(SPI0SR & SPTEF)); /* wait until write is permissible */
SPI0DR = 0x00; /* trigger 8 SCK pulses to shift in data */
while(!(SPI0SR & SPIF)); /* wait until a byte has been shifted in */
return SPI0DR; /* return the character and clear SPIF flag */
}
/*******************************************************************************\
*****/
/* The following function reads a string of count bytes from the SPI interface.
*/
/*******************************************************************************\
*****/
void getsspi0(char *ptr, char count)
{
while(count) { /* continue while byte count is nonzero */
*ptr++ = getcspi0(); /* get a byte and save it in buffer */
count--;
}
*ptr = 0; /* terminate the string with a NULL */
}
Thank you