kavya_aithal
Newbie level 4
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.
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.
Code:
#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);
}