|
|
View previous topic :: View next topic |
Author |
Message |
kazanova64
Joined: 10 Apr 2010 Posts: 8
|
adc2543 pic18f4520 SPI communication |
Posted: Sun Apr 11, 2010 6:36 am |
|
|
Hi everybody, I am new to embedded system design and need help. Using 18F4520, I want to read adc output and put it on the lcd but I could not do it. I want to use ADC2543 with 8 bits CS enable mode.
Code: |
#include "C:\Documents and Settings\ozora\Belgelerim\Workspace\PIC\ccs-pcw\Yeni Klasör\main.h"
#use delay(clock=16000000)
#use spi(DI=PIN_B1, DO=PIN_B0, CLK=PIN_B2, ENABLE=PIN_B4, BITS=8 )
#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)
int8 value=0;
char mystring[15];
int8 i;
#define io_clk PIN_B2
#define ad_adress_in PIN_B0
#define ad_data_out PIN_B1
#define ad_cs PIN_B3
int8 ADC_get(void)
{
output_low(ad_cs);
delay_ms(10);
value=spi_read(0x04);
delay_ms(10);
output_high(ad_cs);
delay_ms(100);
return(value);
}
void ADC_init(void)
{
output_low(io_clk);
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_L_TO_H|SPI_CLK_DIV_64);
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!!
}
}
|
I execute the process with Proteus. SPI outputs can not be generated from PIC. Can you help me? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 11, 2010 4:49 pm |
|
|
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
|
|
Posted: Mon Apr 12, 2010 12:06 pm |
|
|
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
|
|
Posted: Mon Apr 12, 2010 12:16 pm |
|
|
Is it now working ? You didn't say. |
|
|
kazanova64
Joined: 10 Apr 2010 Posts: 8
|
|
Posted: Tue Apr 13, 2010 12:53 am |
|
|
Yes, it is working. I simulated in proteus and the PIC can communicate with adc - lcd and gives the adc result to the lcd. |
|
|
|
|
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
|