|
|
View previous topic :: View next topic |
Author |
Message |
arrow
Joined: 17 May 2005 Posts: 213
|
AVR128 translation to PIC C? |
Posted: Tue Jul 25, 2006 8:07 am |
|
|
Hi
I have the following C code for an AVR128:
Code: |
SPDR = dataIn;
while((SPSR & 0x80)==0);
return(SPDR);
|
and have translated it to PIC C as follows:
Code: |
DataReturned = spi_read(dataIn);
while(!(DataReturned & 0X80)){
DataReturned = spi_read();
}
return(DataReturned);
|
Could some one please tell me if this is correct?
(I have never done SPI before).
Thank you in advance.
a. |
|
|
Ttelmah Guest
|
|
Posted: Tue Jul 25, 2006 10:19 am |
|
|
Simple answer. No.
There are two seperate values in the original code. 'SPDR', which is presumably the data I/O register, and 'SPSR', which is probably the 'status' register for the transfer. You are trying to use just one value in your 'converted' form. The original, is actually putting the value in 'dataIn', into the SPI transfer register, then waiting for the byte to be transferred, and then reading back the returned byte. SPDR, and SPSR, are hardware registers on the chip.
You can generate basically exactly the same code, if you define the hardware registers and bits (will depend on the PIC, which you don't say), but in fact you don't have to do this. All you need in CCS, to perfrom the same operation, is:
return(spi_read(dataIn));
The spi_read operation, automatically performs the 'wait', to clock the byte out, and won't return till this happens, and will then return the byte clocked back.
Best Wishes |
|
|
arrow
Joined: 17 May 2005 Posts: 213
|
|
Posted: Fri Jul 28, 2006 5:03 am |
|
|
Hi Ttelmah
Thank you!
Regards
a. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|