|
|
View previous topic :: View next topic |
Author |
Message |
Peter-NMB
Joined: 17 Sep 2003 Posts: 11 Location: Glasgow, Scotland
|
PIC18F2585 Soft I2C Pin selection Problem |
Posted: Tue Feb 14, 2006 9:01 am |
|
|
Hi Hope someone can help,
Using a PIC18F2585 with CCS Version 3.242 Complier,
(Pullups are fitted to Pins being used)
During the implementation of a software version of I2C,
(Using the #USE I2C ...)
I have found that if I select for use Pin B0 and Pin B1 as the SDA or SCL lines, I am unable to Master from the I2C bus and communicate with another device
however, If I choose other Pins B3 to B7, C0 toC7 etc the software I2C functions OK, as long as I do not choose B0 and B1 for the clock or data lines
Why is there a problem in using these two pins for the software I2C functionality?
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 14, 2006 2:10 pm |
|
|
Those pins are configured as analog pins upon power-on reset.
CCS puts in start-up code to configure them as digital i/o.
I checked the .LST file for PCM vs. 3.242, and it looks correct.
So I'm not sure what's happening. Can you post a very short
program that demonstrates the problem. I emphasize, SHORT.
Make it be a complete program though, with all #include, #fuses,
#use statements posted. I should be able to copy and paste
it into MPLAB and have it compile OK. Make it SHORT. |
|
|
Peter-NMB
Joined: 17 Sep 2003 Posts: 11 Location: Glasgow, Scotland
|
|
Posted: Tue Feb 14, 2006 3:53 pm |
|
|
Hi thanks for having a wee look for me,
The program is basically communicating with a battery.
thanks, hopefully it is something stupid that I am doing
I have tried to keep the program short, you should be able to paste it in one go.
Code: |
#include <18F2585.h>
#device ADC=10
#include <stdlib.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP
#define ON 1
#define OFF 0
// #define SM_DATA PIN_B2 these pins OK
// #define SM_CLK PIN_B3 these pins OK
#define SM_DATA PIN_B0 // these pins NOT OK
#define SM_CLK PIN_B1 // these pins NOT OK
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use I2C(MASTER,SDA=SM_DATA,SCL=SM_CLK,SLOW,SMBUS)
int16 Select_SMBUS(int8 Slave_address,int8 Command_code);
void intI2C();
void Select_Control();
char rxd_byte;
int16 DAC_V=0;
int16 DAC_I=0;
int8 Status=OFF;
//*******************************************
//serial comms interrupt
#INT_RDA
serial_isr()
{
rxd_byte=toupper(getch());
Select_Control();
}
//*******************************************
main()
{
int16 ADC_Val,From_Battery[5];
signed int16 From_Battery_Curr;
setup_oscillator( OSC_4MHZ);
SET_TRIS_A(0xFF); // configure PortA A2 digital out, A4 digital in
SETUP_ADC_PORTS(ALL_ANALOG|VSS_VREF);
setup_adc(ADC_CLOCK_INTERNAL);
output_low(PIN_B5);
output_high(PIN_B4);
enable_interrupts(int_rda);
enable_interrupts(GLOBAL);
Status=OFF;
do
{
if(Status!=OFF)
{
output_low(PIN_B5);
output_low(PIN_B4);
}
else
{
output_high(PIN_B5);
output_high(PIN_B4);
}
delay_ms(1000);
intI2C();
From_Battery[2]=Select_SMBUS(0x0B,0x09);
From_Battery_Curr=Select_SMBUS(0x0B,0x0A);
From_Battery[3]=Select_SMBUS(0x0B,0x0E);
From_Battery[1]=Select_SMBUS(0x0B,0x14);
From_Battery[0]=Select_SMBUS(0x0B,0x15);
printf("Chrg Volt :%lu mV\n\r",From_Battery[0]);
printf("Chrg Curr :%lu mA\n\r",From_Battery[1]);
printf("*****Battery Readings*****\n\r");
printf("Batt Volt : %lu mV\n\r",From_Battery[2]);
printf("Batt Curr : %ld mA\n\r",From_Battery_Curr);
printf("State Chrg : %lu percent\n\r",From_Battery[3]);
}
while(1);
}
//*******************************************
void Select_Control()
{
switch (rxd_byte)
{
case '1': //off
DAC_V=0;
DAC_I=0;
Status=OFF;
rxd_byte=0;
break;
case '2': // ON
Status=ON;
rxd_byte=0;
break;
default:
DAC_V=0;
DAC_I=0;
Status=OFF;
rxd_byte=0;
break;
}
}
//*******************************************
void intI2C()
{
output_float(SM_DATA);
output_float(SM_CLK);
}
//*******************************************
int16 Select_SMBUS(int8 Slave_address,int8 Command_code)
{
int8 hi,lo;
I2C_start();
I2C_Write(Slave_address<<1);
I2C_Write(Command_code);
I2C_start();
I2C_Write((Slave_address<<1)+1);
lo =I2C_Read(1); // with ack
hi= I2C_Read(0); // with nack
I2C_Stop();
return make16(hi,lo);
} |
Last edited by Peter-NMB on Tue Feb 14, 2006 4:25 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 14, 2006 4:05 pm |
|
|
The code doesn't compile. It gets an error on the "do {" statement.
It's expecting a "}while();".
Maybe you just left out a brace, but I want to be sure about it
Can you just edit your previous post and fix the problem ?
Also, test the edited code on your hardware to be sure that it
still shows the problem. Make a short post to let me know
when you've done this. |
|
|
Peter-NMB
Joined: 17 Sep 2003 Posts: 11 Location: Glasgow, Scotland
|
|
Posted: Tue Feb 14, 2006 4:29 pm |
|
|
Hi PCM Programmer,
I have edited the code, sorry about that, you were correct I had omitted a closing brace, it's now been placed in, within the edited code
I am currently unable to test the code within the hardware at this moment as I am at home. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 14, 2006 10:59 pm |
|
|
This line is causing the problem:
Quote: | SETUP_ADC_PORTS(ALL_ANALOG|VSS_VREF); |
The 18F2585 has analog pins on Port B, not just Port A.
You need to change that line so it excludes those pins on Port B
that you want to use for digital i/o.
Look in the 18F2585.H file, near the end, at this section:
"Constants used in SETUP_ADC_PORTS()"
Next to each constant, CCS has placed a list of the pins that
will be configured as analog if you use the constant.
Pick the appropriate one and use it instead of "ALL_ANALOG". |
|
|
Peter-NMB
Joined: 17 Sep 2003 Posts: 11 Location: Glasgow, Scotland
|
|
Posted: Wed Feb 15, 2006 3:09 am |
|
|
Hi PCM Programmer,
thanks for that, I should have noticed it
I had been using the pin out for the PIC18F258
RB0/INT0 and RB1/INT1
where as you have correctly stated, the PIC18F2585 has the additional functionality of analog pins on PortB
RB0/INT0/AN10 and RB1/INT1/AN8
thanks for the help, its good to have a fresh set of eyes |
|
|
|
|
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
|