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

adc2543 pic18f4520 SPI communication

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Apr 11, 2010 4:49 pm     Reply with quote

You have some problems in your code.

Quote:

#use spi(DI=PIN_B1, DO=PIN_B0, CLK=PIN_B2, ENABLE=PIN_B4, BITS=8 )

In the code above, you specify a software SPI port on Port B.

Quote:

setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_64);

value=spi_read(0x04);

But then in your program shown above, you use the hardware SPI
functions setup_spi() and spi_read() which are on pins C3, C4, and C5.
Delete the #use spi() statement. You are not using it.


Quote:

setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_64);

Another thing. The TLC2543 data sheet shows that SPI mode 0 is used,
but in the code above, you have specified SPI mode 1. That's wrong.
Look at Figure 13 on page 11 of the data sheet:
http://focus.ti.com/lit/ds/symlink/tlc2543.pdf
It shows that I/O Clock (SCLK) idles at a low level, and samples data
on the rising edge. According to this diagram, that's SPI mode 0:
http://www.totalphase.com/support/kb/10045/#modes
Here are the SPI modes #defines for 16F and 18F PICs:
Code:

#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)
kazanova64



Joined: 10 Apr 2010
Posts: 8

View user's profile Send private message

PostPosted: Mon Apr 12, 2010 12:06 pm     Reply with quote

Thanks for your reply. I had not known that spi pins are in portc.

Code:

#include "C:\Documents and Settings\ozora\Belgelerim\Workspace\PIC\ccs-pcw\Yeni Klasör\main.h"
#use delay(clock=16000000)
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_TYPE 2
#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)

unsigned int8 value=0;
char mystring[15];
int8 i;

#define ad_cs                 PIN_B3

int8 ADC_get(void)

   output_low(ad_cs);
   delay_ms(10);
   value=spi_read(0x04);
   delay_ms(100);
   output_high(ad_cs);
   return(value);
}

void ADC_init(void)
{
   output_high(ad_cs);
   ADC_get();
}   

#include <lcd.c>
void main()

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_MASTER|SPI_MODE_0|SPI_CLK_DIV_4);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   lcd_init();
   adc_init();
   
   while(1) {
      value=ADC_get();
      sprintf(mystring,"<%d>",value);
      lcd_putc("\f");
      for(i=0;i<=14;i++){
         lcd_putc(mystring[i]);
      }
      delay_ms(100);

//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab

   // TODO: USER CODE!!
}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 12, 2010 12:16 pm     Reply with quote

Is it now working ? You didn't say.
kazanova64



Joined: 10 Apr 2010
Posts: 8

View user's profile Send private message

PostPosted: Tue Apr 13, 2010 12:53 am     Reply with quote

Yes, it is working. I simulated in proteus and the PIC can communicate with adc - lcd and gives the adc result to the lcd.
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