View previous topic :: View next topic |
Author |
Message |
anjali
Joined: 22 May 2012 Posts: 9
|
code for tlv2543 using i2c protocol |
Posted: Tue May 29, 2012 1:14 am |
|
|
hello,
here i ve a code for tlv2543 using i2c protocol for pic c compiler.
we are not getting any results on serial terminal.pls rectify the errors and let us know how do we correct it.
#include <16F877A.h>
#device adc=16
#BYTE PORTB = 0x06
#BYTE PORTD = 0x08
#BYTE PORTC = 0x07
#BYTE PORTE = 0x09
#BYTE PORTA = 0x05
#BIT RC0 = 0x07.0
#BIT RC1 = 0x07.1
#BIT RC2 = 0x07.2
#BIT RC3 = 0x07.3
#BIT RC4 = 0x07.4
#BIT RC5 = 0x07.5
#BIT ADGO = 0x1F.1
#BIT hsp1 = 0x07.2
#BIT hsp2 = 0x05.1
#BIT tsp1 = 0x05.2
#BIT tsp2 = 0x05.3
#BIT trisc4 = 0x87.4
//#BIT ADGO = 0x1F.2
#BIT trisb0 = 0x86.0
#BIT trisb1 = 0x86.1
#BIT trisb2 = 0x86.2
#BIT trisb3 = 0x86.3
#BIT RA0 = 0x05.0
#BIT RA1 = 0x05.1
#BIT Ra2 = 0x05.2
#BIT RD0 = 0x08.0
#BIT RD1 = 0x08.1
#BIT RD2 = 0x08.2
#BIT RD3 = 0x08.3
#BIT RD4 = 0x08.4
#BIT RD5 = 0x08.5
#BIT RD6 = 0x08.6
#BIT RD7 = 0x08.7
#BIT RB0 = 0x06.0
#BIT RB1 = 0x06.1
#BIT RB2 = 0x06.2
#BIT RB3 = 0x06.3
#BIT RB4 = 0x06.4
#BIT RB5 = 0x06.5
#BIT RB6 = 0x06.6
#BIT RB7 = 0x06.7
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#use delay(clock=12000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#define io_clk RB0 //clock on port B bit 7
#define io_clk_dir TRISB0 //clock on port B bit 7
#define ad_data_in RB1
#define ad_data_in_dir TRISB1
#define ad_data_out RB2 //data on port A bit 1
#define ad_data_out_dir TRISB2 //data on port A bit 1
#define ad_cs RB3
#define ad_cs_dir TRISB3
#define port_out 0
#define port_in 1
#use i2c(master,sda=PIN_C3 ,scl=PIN_C4)
//unsigned long adcv;
void ad_setup();
unsigned int ad_setgetchannel(unsigned char);
unsigned int ad_getchannel(unsigned char);
void init(void);
int x;
BYTE result;
void main()
{
init();
for(;;)
{
ad_setgetchannel(1);
ad_getchannel(1);
for(x=0;x<=15;x++)
{
i2c_start (); // init I2C protocol
i2c_write (0x08); // slave device address
// i2c_write (x); // slave 'internal memory address' [0;15]
//i2c_write(15-x); // data
i2c_stop(); // stop communication with slave
delay_ms(50);
i2c_start (); // init I2C protocol
i2c_write (0x09); // slave device address
result = i2c_read(0); // read slave last received data
i2c_stop (); // stop communication with slave
printf("Result[%d]=%d\r\n",x,result); // debug data
delay_ms(50);
}
delay_ms(1000);
}
}
void ad_setup()
{
ad_data_in_dir = port_out;
ad_data_out_dir = port_in;
io_clk_dir = port_out;
ad_cs_dir = port_out;
}
unsigned int ad_setgetchannel(unsigned char channel)
{
signed char i;
unsigned int data = 0x0;
unsigned int ch_config;
ch_config = channel<<8;
io_clk = 0; //ensure clock is low
ad_cs = 0;
for(i=11; i>=0; i--)
{
ad_data_in = ((ch_config>>i) & 0x01); //bit to send
data <<= 1; //read the next bit
data |= ad_data_out;
io_clk = 1; //ensure clock is high
io_clk = 0; //ensure clock is low
}
ad_cs = 1;
return data;
}
//recommended routine to call, its only a little slower
unsigned int ad_getchannel(unsigned char channel)
{
unsigned int dummy;
// call it twice to get and set the channel
dummy=ad_setgetchannel(channel); //get garbage channel, but set next one
dummy=ad_setgetchannel(channel); //get current channel
return dummy;
}
void init(void)
{
setup_adc_ports(NO_ANALOGS);
//setup_oscillator(OSC_12MHZ);
ad_setup();
setup_psp(PSP_DISABLED);
set_tris_a(0xFF);
set_tris_d(0x00);
set_tris_e(0x0F);
set_tris_c(0x04);
set_tris_b(0x02);
} |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Tue May 29, 2012 1:51 am |
|
|
1) Learn to use the code buttons.
2) Get rid of all the port and bit definitions. These are not needed in CCS.
3) Learn to read a data sheet - hint the tlv2543, does not use I2C communications. It uses SPI. A bit like putting diesel in a petrol car. Won't get you very far....
Best Wishes |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue May 29, 2012 2:25 am |
|
|
Ttelmah wrote: | 3) Learn to read a data sheet - hint the tlv2543, does not use I2C communications. It uses SPI. |
This makes me wonder how he connected a 3.3V A/D converter to the 5V PIC... |
|
|
anjali
Joined: 22 May 2012 Posts: 9
|
|
Posted: Tue May 29, 2012 4:36 am |
|
|
Okay fine... I need to write a code for tlv2543 timing diagram, can you give me the hint how to start with and how to use configuration words in ccs.... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Tue May 29, 2012 5:29 am |
|
|
hint #1. Your PIC type only has a 10 bit adc, so your second line is wrong.I'm suprised the compiler doesn't kick out an error.
hint #2. Post your compiler version.
hint #3. whenver using the use rs232(..) directive ALWAYS add 'errors' to the options.
hint #4 . Always read the datasheets to be sure the peripheral is compatible with the PIC.Be aware that you cannot use chips that have different Vss (unless you add MORE chips).
hint #5. Be sure to get the 'Hello World' and 'blinkin LED' programs up and running BEFORE you try ANY other program. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Tue May 29, 2012 9:30 am |
|
|
anjali wrote: | Okay fine... I need to write a code for tlv2543 timing diagram, can you give me the hint how to start with and how to use configuration words in ccs.... |
First, get a TLC2543. Note the different letter. This chip can be interfaced to your PIC, the TLV can't easily.
Then, connect the extra pin to implement an SPI interface, and just use the SPI commands. Read the manual. 80% of your questions so far are directly answerable in the manual.
For Temtronic, adc=16, is the command to give a result _left justified_ to 16bits from the ADC. Hence the lack of complaint.
Best Wishes |
|
|
anjali
Joined: 22 May 2012 Posts: 9
|
|
Posted: Tue May 29, 2012 10:59 am |
|
|
Thank you both for the reply. Yes I didn't get any error for the 2nd line.
Now the thing is, actually I have to send 10bit data serially and have to get it as 2bytes. I'm seeing output using the serial terminal. Now I need to write a main program for the timing diagram of tlv2543 and have to add it in the code, by removing those i2c lines which i have used. The problem is I am not getting an idea to write the program by looking to timing diagram of tvl2543. So could you please help me how to start the code and what all should I need to include in that. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Tue May 29, 2012 2:44 pm |
|
|
You still have the major problem of using the tlv. This chip is rated for 3.6v max operation. You are trying to use it with a PIC operating at 5v...
Timing wise, look at figure 14. This is the mode you should be using for simplicity, since the PIC hardware sends in 8byte pieces, so 16bit operation is the easiest way to go. This looks to be SPI mode 0.
Sequence is simply drop the CS line, then send send two bytes to the device, using SPI_READ. The first byte needs to be the address/control byte for the next transfer. Then the second byte is just '0'. The two bytes you get back from the reads, are the result of the _last_ conversion. Then raise CS. Setup SPI fo3 3MHz operation (the chip supports 4MHz max, but 3MHz is the best the PIC can manage using a 12MHz master clock, and is the recommended setting anyway).
The first pair of bytes read back, will be garbage, since a conversion won't have been done.
The format of control byte sent, is in table1, and you will have to select the channel you want, 16bit operation, and I'd suggest MSB first (since SPI as standard sends MSb first, and it makes sense to maintain the order on the bytes as well). Unipolar data unless you are doing something with an offset op-amp feeding the input. So:
0b00001100 for the first channel.
Then use make16 to assemble the bytes into a single 16bit word, rotate right four times to get rid of the dummy zeros at the right of the data, and the transfer is complete.
Best Wishes |
|
|
anjali
Joined: 22 May 2012 Posts: 9
|
|
Posted: Tue May 29, 2012 9:19 pm |
|
|
Thank you Ttelmah... :) The constraint here is I should not make use of spi. Thanks for the detail explaination of tlv timing. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Wed May 30, 2012 1:49 am |
|
|
Why should you 'not make use of SPI'. This is what the chip uses?. Remember you don't have to use the SPI hardware, to still use SPI code. CCS offers a software SPI implementation already written for you, if you select pins that are not the hardware pins.
You _have_ to use SPI. This is what the chip wants. You have three routes:
1) Use the hardware.
2) Let CCS generate software for you.
3) Write your own software.
With 1, and 2 available, why would anyone use 3?.
Best Wishes |
|
|
anjali
Joined: 22 May 2012 Posts: 9
|
|
Posted: Wed May 30, 2012 3:49 am |
|
|
yeah we can use spi but in our project the constrain is that we should not make use of spi instead we have to write the main program of tlv as the i2c prtocol procedure.... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Wed May 30, 2012 4:23 am |
|
|
In which case you need to find a different chip.
As I said before, it is like the difference between diesel and petrol. An engine built for one, won't run on the other. The chip you are trying to use uses SPI, not I2C, and cannot be interfaced to an I2C bus, without something like another microprocessor to do the interfacing. You _cannot_ connect to this chip, using I2C.
Best Wishes |
|
|
|