Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[AVR] Sending LSB first Instead of sending MSB

Status
Not open for further replies.

achar.deepak

Member level 1
Joined
Dec 12, 2016
Messages
33
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
415
Hi all,

I am interfacing AS1116 with AVR32 bit controller through SPI, I need to send LSB first in the code. I am not how to do it. Please help regarding this. I have attached the code with this thread.

Thanks
HTML:
/**
 * \file
 *
 * \brief Empty user application template
 *
 */

/**
 * \mainpage User Application template doxygen documentation
 *
 * \par Empty user application template
 *
 * Bare minimum empty user application template
 *
 * \par Content
 *
 * -# Include the ASF header files (through asf.h)
 * -# "Insert system clock initialization code here" comment
 * -# Minimal main function that starts with a call to board_init()
 * -# "Insert application code here" comment
 *
 */

/*
 * Include header files for all drivers that have been imported from
 * Atmel Software Framework (ASF).
 */
#include <asf.h>

void spi_init_module(void);
void test_display(void);
//#define SPI_EXAMPLE             (&AVR32_SPI1)
#define AS1116_SPI1                 (&AVR32_SPI1)


#define AT25160B_SPI_BAUDRATE    8000000


#define AS1116_SPI1_NPCS            0
#define AS1116_SPI1_SCK_PIN         AVR32_SPI1_SCK_0_0_PIN
#define AS1116_SPI1_SCK_FUNCTION    AVR32_SPI1_SCK_0_0_FUNCTION
#define AS1116_SPI1_MISO_PIN        AVR32_SPI1_MISO_0_0_PIN
#define AS1116_SPI1_MISO_FUNCTION   AVR32_SPI1_MISO_0_0_FUNCTION
#define AS1116_SPI1_MOSI_PIN        AVR32_SPI1_MOSI_0_0_PIN
#define AS1116_SPI1_MOSI_FUNCTION   AVR32_SPI1_MOSI_0_0_FUNCTION
#define AS1116_SPI1_NPCS0_PIN       AVR32_SPI1_NPCS_0_0_PIN
#define AS1116_SPI1_NPCS0_FUNCTION  AVR32_SPI1_NPCS_0_0_FUNCTION

#define AS1116_SHUT				0x0C
#define AS1116_DECODE			0x09
#define AS1116_INTENSITY		0x0A
#define AS1116_SCAN				0x0B
#define AS1116_TESTMODE			0x0F
#define AS1116_FUTURE			0x0E

#define SPITESTMODE				0x00
#define SPISHUT					0x01
#define SPIDECODE				0xFF //00
#define SPIINTENSITY			0xFF //09
#define SPISCAN					0x05
#define SPIFUTURE				0x42

#define DIGIT0                  0X01
#define DIGIT1                  0X02
#define DIGIT2                  0X03
#define DIGIT3                  0X04
#define DIGIT4                  0X05
#define DIGIT5                  0X06

struct spi_device spi_device_conf = {
	.id =   AS1116_SPI1_NPCS                    // IOPORT_CREATE_PIN(PORTD, 1)
};

uint8_t data[]={0x02};
uint8_t data1[]={0xFF};


void spi_init_module(void)
{	
	
	
	//gpio of spi configuration
	static const gpio_map_t AS1116_SPI_GPIO_MAP =
	{
		{AS1116_SPI1_SCK_PIN , AS1116_SPI1_SCK_FUNCTION},  // SPI Clock.
		{AS1116_SPI1_MISO_PIN ,AS1116_SPI1_MISO_FUNCTION },  // MISO.
		{AS1116_SPI1_MOSI_PIN , AS1116_SPI1_MOSI_FUNCTION},  // MOSI.
		{AS1116_SPI1_NPCS0_PIN , AS1116_SPI1_NPCS0_FUNCTION}   // Chip Select NPCS.
	};	
	gpio_enable_module(AS1116_SPI_GPIO_MAP,sizeof(AS1116_SPI_GPIO_MAP) / sizeof(AS1116_SPI_GPIO_MAP[0]));
	
	
	//Init SPI module as master
	spi_master_init( AS1116_SPI1  );
	//Setup parameters for the slave device
	spi_master_setup_device(AS1116_SPI1  , &spi_device_conf,SPI_MODE_0 , AT25160B_SPI_BAUDRATE , 0);
	//Allow the module to transfer data
	spi_enable( AS1116_SPI1  );
}




void test_display(void)
{
	spi_selectChip(AS1116_SPI1,0);
    spi_write_packet(AS1116_SPI1,DIGIT0,1);
	spi_write_packet(AS1116_SPI1,0x01,1);// send command
	spi_unselectChip(AS1116_SPI1,1);
	
	spi_selectChip(AS1116_SPI1,0);
    spi_write_packet(AS1116_SPI1,DIGIT1,1);
	spi_write_packet(AS1116_SPI1,0x02,1);// send command
	spi_unselectChip(AS1116_SPI1,1);
	
	spi_selectChip(AS1116_SPI1,0);
    spi_write_packet(AS1116_SPI1,DIGIT2,1);
	spi_write_packet(AS1116_SPI1,0X03,1);// send command
    spi_unselectChip(AS1116_SPI1,1);
	
	spi_selectChip(AS1116_SPI1,0);
    spi_write_packet(AS1116_SPI1,DIGIT3,1);
	spi_write_packet(AS1116_SPI1,0X04,1);// send command
	spi_unselectChip(AS1116_SPI1,1);
	
	spi_selectChip(AS1116_SPI1,0);
    spi_write_packet(AS1116_SPI1,DIGIT4,1);
	spi_write_packet(AS1116_SPI1,0X05,1);// send command
	spi_unselectChip(AS1116_SPI1,1);
	
	spi_selectChip(AS1116_SPI1,0);
    spi_write_packet(AS1116_SPI1,DIGIT5,1);
	spi_write_packet(AS1116_SPI1,0X06,1);// send command
    spi_unselectChip(AS1116_SPI1,1);
}


int main (void)
{
	// Insert system clock initialization code here (sysclk_init()).
	board_init();
	
	//Buffer to send data to SPI slave
	uint8_t txdata[1]={0x0C};	//Buffer to receive data from SPI slave
	uint8_t rxdata[1]={0x01};
	
	spi_init_module();
	
	
	
	
	while(1)
	{


/*// Select the slave device with chip select
spi_select_device(AS1116_SPI1 ,&spi_device_conf);
// Send the data to slave
spi_write_packet(AS1116_SPI1 , txdata, 1);
// Read data from slave
spi_read_packet(AS1116_SPI1, rxdata,1);
// De select the slave
spi_deselect_device(AS1116_SPI1 ,&spi_device_conf);*/

spi_selectChip(AS1116_SPI1,0);

spi_write_packet(AS1116_SPI1,AS1116_SHUT,1);// send command
spi_write_packet(AS1116_SPI1,SPISHUT,1);

spi_unselectChip(AS1116_SPI1,1);

spi_selectChip(AS1116_SPI1,0);

spi_write_packet(AS1116_SPI1,AS1116_INTENSITY,1);// send command
spi_write_packet(AS1116_SPI1,SPIINTENSITY,1);

spi_unselectChip(AS1116_SPI1,1);

spi_selectChip(AS1116_SPI1,0);

spi_write_packet(AS1116_SPI1,AS1116_SCAN,1);// send command
spi_write_packet(AS1116_SPI1,SPISCAN,1 );

spi_unselectChip(AS1116_SPI1,1);

spi_selectChip(AS1116_SPI1,0);

spi_write_packet(AS1116_SPI1,AS1116_DECODE,1);// send command
spi_write_packet(AS1116_SPI1,SPIDECODE,1);

spi_unselectChip(AS1116_SPI1,1);

spi_selectChip(AS1116_SPI1,0);

spi_write_packet(AS1116_SPI1,AS1116_TESTMODE,1);// send command
spi_write_packet(AS1116_SPI1,SPITESTMODE,1);

spi_unselectChip(AS1116_SPI1,1);

spi_selectChip(AS1116_SPI1,0);

spi_write_packet(AS1116_SPI1,AS1116_FUTURE,1);// send command
spi_write_packet(AS1116_SPI1,SPIFUTURE,1);

spi_unselectChip(AS1116_SPI1,1);

test_display();		
	
	}
	
	// Insert application code here, after the board has been initialized.
}
 

This is the code which I made.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#define SPI_En at PORTD.0
#define SPI_Data at PORTD.1
#define SPI_CLK at PORTD.2
 
void SPI_Send_Lsb_First(unsigned char byte) {
 
    unsigned char mask = 0x01;
    unsigned char i = 0;
    
    SPI_En = 0;
 
    for(i = 0; i < 8; i++) {
        if(byte & mask) {
            SPI_Data = 1;               
        }
        else {
            SPI_Data = 0;
        }
 
        SPI_CLK = =1;
        Delay_us(200);
        SPI_CLK = 0;
        Delay_us(200);  
 
        msak <<= 1;
    }
 
    SPI_En = 1;
}

 
There is a typo in Okada's code but it looks like you are using a library function to send the data through a hardware SPI module anyway. Sorry, I'm not up to speed on AVR32 hardware! If that is the case, you only option is to reverse the bits in the data before sending it.

Brian.
 
Suppose Here i m sending data of 0x0C in msb first out way ..how to send that data (0x0C) in LSB first out
 

Hi,

Did you read Brian's post?

Reorder the byte: 0x0C --> 0x30
You could do this with a table, bit copies, shifting...

Klaus
 


Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top