ab.vasanthan
Newbie level 5
Hi,
I am using PIC 16F887 and CCS-C compiler(version 4.093) plugin in MPLAB IDE
I have dumped the code in hardware and I2C is working.
now , i do not know at what speed the I2C communication is taking place ?
can u help?
Master code:
slave code:
I am using PIC 16F887 and CCS-C compiler(version 4.093) plugin in MPLAB IDE
I have dumped the code in hardware and I2C is working.
now , i do not know at what speed the I2C communication is taking place ?
can u help?
Master code:
Code:
#include <16f887.h>
#device ADC=10
#use delay(clock=16000000)
#fuses HS, NOWDT, NOPROTECT, NOLVP
#use RS232(baud = 9600, xmit = PIN_C6, rcv = PIN_C7)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
#include <flex_lcd.c>
#define P1B PIN_D5
#define SLAVE1_WRT_ADDR 0x12
#define SLAVE1_READ_ADDR 0x13
int16 temp_adc1;
float temp1;
int16 temp_adc2;
float temp2;
int16 volt_adc1;
float volt1;
int16 volt_adc2;
float volt2;
int16 crnt_adc1;
float crnt1;
int16 crnt_adc2;
float crnt2;
int pwmv_adc1;
int8 data;
void set_deadband(int8 deadband)
{
#byte PWM1CON = 0x9B
if(deadband > 127)
deadband == 127;
PWM1CON = deadband;
}
void main()
{
lcd_init();
setup_adc(ADC_CLOCK_DIV_32);
setup_timer_2(T2_DIV_BY_16, 255, 1);
setup_ccp1(CCP_PWM_HALF_BRIDGE | CCP_PWM_H_H );
output_low(P1B);
set_deadband(127);
while(1)
{
printf ( lcd_putc, "Battery Status\n" );
delay_ms(5); //print to lcd --- clear LCD --- lcd_putc("\f");
printf ("Battery Status\n\r" ); //print to serial com
delay_ms(5);
//PWM
set_adc_channel(4); //read analog input from channel 0
delay_ms(10);
pwmv_adc1 = read_adc();
set_pwm1_duty(pwmv_adc1);
//I2c
i2c_start();
i2c_write(SLAVE1_READ_ADDR);
data = i2c_read(0);
i2c_stop();
lcd_gotoxy(1,2);
printf ( lcd_putc,"%5.1f", (float)data);
lcd_putc(" i2c_temp");
delay_ms(500);
printf("i2c_temp %d \n\r", data);
delay_ms(500);
//temperature measurement
set_adc_channel(0); //read analog input from channel 0
delay_ms(10);
temp_adc1 = read_adc();
temp1 = 5.00*temp_adc1*100.00/1023.00; // = temp_adc*0.48828125
lcd_gotoxy(1,2);
printf ( lcd_putc,"%5.1f", (float)temp1); // printf(lcd_putc, "value = %lu \n",value)
lcd_putc(223); //this number 223 will display the degree sign
lcd_putc('C');
lcd_putc(" b1_temp");
delay_ms(500);
printf ("\n");
printf ("%5.1f", (float)temp1);
putc(223);
putc('C');
puts(" b1_temp");
printf ("\n\r");
delay_ms(5);
set_adc_channel(1); //read analog input from channel 1
delay_ms(10);
temp_adc2 = read_adc();
temp2 = temp_adc2*0.48828125;
lcd_gotoxy(1,2);
printf ( lcd_putc,"%5.1f", (float)temp2);
lcd_putc(223); //this number 223 will display the degree sign
lcd_putc('C');
lcd_putc(" b2_temp");
delay_ms(500);
printf ("%5.1f", (float)temp2);
putc(223);
putc('C');
puts(" b2_temp");
printf ("\n\r");
delay_ms(5);
//voltage measrement
set_adc_channel(2); //read analog input from channel 2
delay_ms(10);
volt_adc1 = read_adc();
volt1 = (volt_adc1*0.0048875855)+9;//(volt*5)/1023
lcd_gotoxy(1,2);
printf ( lcd_putc,"%5.1f", (float)volt1);
lcd_putc('V');
lcd_putc(" b1_volt");
delay_ms(500);
printf ("%5.1f", (float)volt1);
putc('V');
puts(" b1_volt");
printf ("\n\r");
delay_ms(5);
if(volt1<=10.5)
output_high(PIN_B2);
else
output_low(PIN_B2);
set_adc_channel(5); //read analog input from channel 5
delay_ms(10);
volt_adc2 = read_adc();
volt2 = (volt_adc2*0.0048875855)+9;//(volt*5)/1023
lcd_gotoxy(1,2);
printf ( lcd_putc,"%5.1f", (float)volt2);
lcd_putc('V');
lcd_putc(" b2_volt");
delay_ms(500);
printf ("%5.1f", (float)volt2);
putc('V');
puts(" b2_volt");
printf ("\n\r");
delay_ms(5);
if(volt2<=10.5)
output_high(PIN_B3);
else
output_low(PIN_B3);
// current measurement
set_adc_channel(3); //read analog input from channel 3
delay_ms(10);
crnt_adc1 = read_adc();
crnt1 = crnt_adc1*0.0048875855;//(volt*5)/1023
lcd_gotoxy(1,2);
printf ( lcd_putc,"%5.1f", (float)crnt1);
lcd_putc('A');
lcd_putc(" b1_crnt");
delay_ms(500);
printf ("%5.1f", (float)crnt1);
putc('A');
puts(" b1_crnt");
printf ("\n\r");
delay_ms(5);
set_adc_channel(6); //read analog input from channel 6
delay_ms(10);
crnt_adc2 = read_adc();
crnt2 = crnt_adc2*0.0048875855;//(volt*5)/1023
lcd_gotoxy(1,2);
printf ( lcd_putc,"%5.1f", (float)crnt2);
lcd_putc('A');
lcd_putc(" b2_crnt");
delay_ms(500);
printf ("%5.1f", (float)crnt2);
putc('A');
puts(" b2_crnt");
printf ("\n\r");
delay_ms(5);
}
}
slave code:
Code:
#include <16F887.h>
#device adc=10
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(internal=8M)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x12)
int8 adc_result;
#INT_SSP
void ssp_interrupt()
{
int8 incoming, state;
state = i2c_isr_state();
if(state < 0x80) // Master is sending data
{
incoming = i2c_read();
}
if(state >= 0x80) // Master is requesting data from slave
{
i2c_write(adc_result);
}
}
//======================================
void main ()
{
setup_adc_ports( ALL_ANALOG );
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
delay_us(20);
adc_result = read_adc();
adc_result = adc_result*0.48828125;
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(1)
{
adc_result = read_adc();
adc_result = adc_result*0.48828125;
delay_ms(500);
}
}