You are on page 1of 2

/*

* LS7366R.c
*
* Created: 10/08/2015 22:04:48
* Author: FARAON
*/
#include <avr/io.h>
#include "CommunicationSPI.h"
#include "LS7366R.h"
void LS7366R_RST(unsigned char op_code)
{
unsigned char spi_data;
for (int i = 0; i < ENC_NUM; i++)
{
SPI_PORT |= ( (1 << SS2) | (1 << SS1) | (1 << SS0) );
SPI_PORT &= ~(1 << i);
spi_data = SPI_Transfer(op_code);
SPI_PORT |= ( (1 << SS2) | (1 << SS1) | (1 << SS0) );
}
return;
}
void LS7366R_Transmit(unsigned char op_code, unsigned char c)
{
unsigned char spi_data;
for (int i = 0; i < ENC_NUM; i++)
{
SPI_PORT |= ( (1 << SS2) | (1 << SS1) | (1 << SS0) );
SPI_PORT &= ~(1 << i);
spi_data = SPI_Transfer(op_code);
spi_data = SPI_Transfer(c);
SPI_PORT |= ( (1 << SS2) | (1 << SS1) | (1 << SS0) );
}
return;
}
void LS7366R_SingleByteRead(unsigned char op_code, char c[])
{
unsigned char spi_data;
for (int i = 0; i < ENC_NUM; i++)
{
SPI_PORT |= ( (1 << SS2) | (1 << SS1) | (1 << SS0) );
SPI_PORT &= ~(1 << i);
spi_data = SPI_Transfer(op_code);
c[i] = SPI_Transfer(0xFF);
SPI_PORT |= ( (1 << SS2) | (1 << SS1) | (1 << SS0) );
}
}
void LS7366R_TwoByteReceive(unsigned char op_code, int c[])
{
unsigned char spi_data;

int aux;
for (int i = 0; i < ENC_NUM; i++)
{
aux = 0;
SPI_PORT |= ( (1 << SS2) | (1 << SS1) | (1 << SS0) );
SPI_PORT &= ~(1 << i);
spi_data = SPI_Transfer(op_code);
for (int j = 0; j < DATA_LENGTH; j++)
{
aux = (aux << 8) + SPI_Transfer(0xFF);
}
SPI_PORT |= ( (1 << SS2) | (1 << SS1) | (1 << SS0) );
c[i] = aux;
}
return;
}
void LS7366R_FourByteReceive(unsigned char op_code, long int c[])
{
unsigned char spi_data;
long int aux;
for (int i = 0; i < ENC_NUM; i++)
{
aux = 0;
SPI_PORT |= ( (1 << SS2) | (1 << SS1) | (1 << SS0) );
SPI_PORT &= ~(1 << i);
spi_data = SPI_Transfer(op_code);
for (int j = 0; j < DATA_LENGTH; j++)
{
aux = (aux << 8) + SPI_Transfer(0xFF);
}
SPI_PORT |= ( (1 << SS2) | (1 << SS1) | (1 << SS0) );
c[i] = aux;
}
return;
}
void LS7366R_Init()
{
LS7366R_Transmit(WRITE_MDR0,QUADX4|FREE_RUN|INDX_LOADC|SYNCH_INDX|FILTER
_2);
LS7366R_Transmit(WRITE_MDR1,BYTE_4|EN_CNTR|IDX_FLAG|CMP_FLAG);
return;
}

You might also like