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] SPI code to transmit the received data

Status
Not open for further replies.

Ekansh_Kaushal

Newbie level 3
Joined
Jul 4, 2017
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
34
hi guys i am new to programming and wanted some help with my project as i am trying to receive the data from 74hc165 parallel to serial shift register and want to send the recieved data to 74hc164 can any one help with tx rx part i would be forever greatful.
 

Neither of those devices are SPI.

They can be used to some degree as serial-parallel converters though but we need to know which processor you want code for and in what language it should be.

Brian.
 

controller i am using is atmega16 and trying to write a code in embedded c language and another problem i am facing is that the mcu is not generating the serial clock sck i have declaired the pin as outout as well. kindly help.
 

Hi,

Show your code and your circuit.

Klaus
 

my code is
Code:
#include<avr/io.h>
#include<util/delay.h>
#include<stdlib.h>

#define rs PC0
#define rw PC1
#define en PC2

#define pl PA0

functions for lcd data
functions for lcd cmds
function for string

unsigned int spi_tranceiver (uint8_t data)
{
    data=0;    
    SPDR = data;                       // Load buffer data into variable
    while(!(SPSR & (1<<SPIF)));        // Wait until transmission complete
    return(data);                      // Return received data to 164
}
void main()
{
char arr[7];
DDRC=0xff;
DDRA=0x03;
DDRB|=(1<<PB7)|(1<<PB5);  // SCK /MOSI as Output    
SPCR|=(1<<SPE)|(1<<MSTR)|(1<<SPR0); // spi enabled/master mode/freq div by 16
init();
while(1)
{
string("Hit Switch");
while((SPDR&0xff)==0);
spi_tranceiver('0');
//lcd_data(data);
//_delay_ms(500);
lcd_cmd(0x01);
lcd_cmd(0xc0);
itoa(data, arr, 10);
lcd_data(arr);
_delay_ms(500);
}
}
the sck is not working for this code if anyone could help i would be forever greatful
thanks in advance
 
Last edited by a moderator:

I can't help you with your problem but the first line of the 'spi_transceiver' function looks wrong. Why do you set the passed in parameter value to 0!
Also the return statement will simply pass back the value you have sent, NOT the one you have received.
Does that code even compile? In the 'itoa' function call you reference a variable called 'data' but I can't see where that is declared. If it is a global variable that should be set by the 'spi_transceiver' function, then it will he hidden by the parameter declaraction.
Susan
 

thank u for your advice Susan.
can u help me correct hte transrecieve part of my code??
i would be forever greatful thanks in advance.
 

I have not used the AtMega16 and so I really can't guide you much in that area.
However a simple Google search showed the first hit as https://maxembedded.com/2013/11/the-spi-of-the-avr/ which has code examples that you might find useful, as well as an explanation of what is going on. There are probably many other examples (the Google search came up with nearly 24000 hits) and that URL was just picked at random.
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top