|
|
View previous topic :: View next topic |
Author |
Message |
sani
Joined: 24 Nov 2010 Posts: 1
|
SPI communication for PIC18f46 |
Posted: Wed Nov 24, 2010 8:59 am |
|
|
Hi all,
I'm currently working on two PIC18f46k20 and to get two of them connected using SPI master/slave method. I'm using the MPLab compiler and built with the example code from the website but it doesn't seem to work. There is something to do the the '#fuses' definition. Can anyone tell me how to get it sorted. Appreciate it alot, thanks.
Here is the code for the master.
Code: |
#include <18f4520.h>
#fuses EC,NOLVP,NOWDT,NOPROTECT
#device ADC=8
#use delay(clock=40000000)
//These define the different SPI modes in terms of constants the compiler knows about
//NOTE: our PICs only seemed to work in modes 1 and 3, though they are supposed to work with any modes
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
#define SS_PIN PIN_D0 //this can be any output pin on the master
void main()
{
int val;
//Set up the ADC to read from A0
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
//The next statement sets up the SPI hardware, with this PIC as a master using mode 1
//SPI_CLK_DIV_64 sets the speed of the SPI clock pulses--this is the slowest speed
setup_spi(SPI_MASTER | SPI_MODE_1 | SPI_CLK_DIV_64);
while(true) {
val = read_adc(); //read the value to be sent
output_low(SS_PIN); //pull the slave select line low to select the slave
delay_us(10); //give the slave time to notice this (may be unnecessary)
spi_write(val); //send the value
delay_us(10); //(may be unnecessary)
output_high(SS_PIN); //deselect the slave.
delay_ms(10);
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 24, 2010 12:39 pm |
|
|
Quote: | #include <18f4520.h>
#fuses INTRC_IO,NOLVP,NOWDT,NOPROTECT
#device ADC=8
#use delay(clock=4000000) |
The changes shown in bold above will allow the PIC to run.
The oscillator is changed to Internal, and the clock is changed to 4 MHz.
Make both those changes, re-compile, and then you can start to test
the program. |
|
|
|
|
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
|