View previous topic :: View next topic |
Author |
Message |
jacqueskleynhans
Joined: 10 Apr 2008 Posts: 109 Location: Cape Town, South Africa
|
Serial question >.< |
Posted: Tue Nov 30, 2010 10:05 am |
|
|
Hey Guys...
I want to use a pic18f46k20 in a project. I need a spi for a sd card and i2c both master and slave. This pic has 2 serial modules.
MY question : I am using the hardware i2c as is but I have a 8bit data bus going to portd where the spi is located. Can I implement the i2c slave and spi in software.
Background:
Pic need to acquire a i2c instruction fro a external board (need a i2c slave). Pic then sends onboard components i2c commands (using hardware i2c pic). Data from onboard hardware is send to portd and is saved on a spi sd card (need spi). External board has its own pullups.
Regards _________________ "THE ONLY EASY DAY WAS YESTERDAY" |
|
|
Geps
Joined: 05 Jul 2010 Posts: 129
|
|
Posted: Tue Nov 30, 2010 10:12 am |
|
|
Yup code below (based originally on code by Ttelmah)
Code: | void send_spi_byte(int8 ClockPin, int8 DataOutPin,int8 val_to_send) {
int8 count;
for (count=0;count<8;count++) {
output_bit(DataOutPin, shift_left(&val_to_send,1,0));
output_high(ClockPin);
output_low(ClockPin);
}
}
int8 get_spi_byte(int8 ClockPin, int8 DataInPin) {
int8 count;
int8 val;
for (count=0;count<8;count++) {
output_high(ClockPin);
shift_left(&val,1,input(DataInPin));
output_low(ClockPin);
}
return(val);
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Tue Nov 30, 2010 10:34 am |
|
|
Beware:
Pin definitions need to be an int16, for a PIC18. int8 definition, will only work for a PIC12/16.
Best Wishes |
|
|
jacqueskleynhans
Joined: 10 Apr 2008 Posts: 109 Location: Cape Town, South Africa
|
|
Posted: Tue Nov 30, 2010 10:52 am |
|
|
Cool thx !! _________________ "THE ONLY EASY DAY WAS YESTERDAY" |
|
|
|