CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

SPI communication for PIC18f46

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sani



Joined: 24 Nov 2010
Posts: 1

View user's profile Send private message

SPI communication for PIC18f46
PostPosted: Wed Nov 24, 2010 8:59 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Nov 24, 2010 12:39 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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