Software I2C doesn't work when trying it on PIC12F683 ? help

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
roee
Posts: 51
Joined: 07 Sep 2006 07:04

Software I2C doesn't work when trying it on PIC12F683 ? help

#1 Post by roee » 28 Mar 2009 17:14

Hi,
I am trying to use SW I2C on PIC12F683 but can't ?
The line for configuration the PORTS (SDA and SCL) are:
Soft_I2C_Config(&GPIO,1,3);
May be the Soft_I2C_Config doesn't recognize GPIO, and it only "understand" : Soft_I2C_Config(&PORTA,1,3); or PORTB etc?
Did any one try to use the Soft_I2C_Config on PIC12F683?
Thanks,
Roee. :(

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

Re: Software I2C doesn't work when trying it on PIC12F683 ?

#2 Post by milan » 29 Mar 2009 11:38

Hi,
roee wrote: May be the Soft_I2C_Config doesn't recognize GPIO, and it only "understand" : Soft_I2C_Config(&PORTA,1,3); or PORTB etc?
No it can work with all kind of ports.
When you type GPIO, PORTA ... compiler looks at the MCU definition files,
in your case ...\mikroC PRO for PIC\defs\P12F683.c
You can use all the registers, bits and constants defined in this file.

------------------------------------------------------------------

You have to be very careful with 8-pin MCUs, they have small number of I/O pins and they are multiplexed with other functions.

When you are trying to use some pins as digital I/O :
1. Check ADC input functions and turn off AN input mode
2. Check comparators functions and turn off comparator mode
3. Check everything else that can disable digital I/O function of the pin

Code: Select all

Soft_I2C_Config(&GPIO,1,3); 
See datasheet for PIC12F683 -> TABLE 1-1: PIC12F683 PINOUT DESCRIPTION
1. You used GP1, it has analog input function. Read about ANSEL register to turn this function off. Check the comparators too.

2. You used GP3 pin :shock:
This pin is multiplexed with MCLR function. You must turn it off. This is done by setting configuration words.
Open EditProjcet window in the compiler and set "MCLR Enabled" option to Internal(Disabled).
But this is not enough. Datasheet states : "GPIO input with interrupt-on-change", when you make it digital, it is not I/O pin, it's just Input pin.

My advice to you is to choose some other pins for Soft_I2C (perhaps GP0 and GP1) and read the datasheet carefully.
SmartADAPT2 rules !

roee
Posts: 51
Joined: 07 Sep 2006 07:04

Hi, and thanks, I have changed to GP1 and GP2

#3 Post by roee » 29 Mar 2009 12:20

Here is my code ...
Can you advice how to change it?

This code works fine in another microchip.

Should I define TRISIO?? (or it might block softi2c?).
Thanks,
Roee.

//main

i2c_mess_const (const char *s,int line,int clean); //message to LCD
I2C_mess(char message[],int line,int clean);
I2C_mess_specific_place(char message[],unsigned char pos);
main()
{
char message[] = "Ultrasonic By Roee.";
char message1[] = "Timer1 is:";
char lcd;
lcd=1;
INTCON=0;
TRISIO = 0x21; // bit 5,0 is in
//led blinks
while(1)
{
GPIO = 0x10;
delay_ms(200);
GPIO=0;
delay_ms(200);
GPIO = 0x10;
delay_ms(200);
GPIO=0;
delay_ms(1000);

I2C_mess(message,1,1);

}



return 1;
}




// i2c_message

//#ifdef LCDON
// this procedure will send message to LCd
send_lcd_char_const(const char message[20]);
send_lcd_char(char message[]) ;
light_lcd(char lit);
send_lcd_clear();
lcd_line2();
I2C_mess_const(const char message[],int line,int clean)
{
// INTCON =0;
// take GP1(pin 6) and GP2(pin5) - first SDA second SCL
Soft_I2C_Config(&GPIO,2,1); // init soft I2C pin 5=data and pin 4=clock
if (clean==1) send_lcd_clear();
//clear and home
light_lcd(0); // 0 for light on
if (line==2) lcd_line2();
send_lcd_char_const(message);
return 1;
// INTCON = 255;
}
I2C_mess(char message[],int line,int clean)
{
// INTCON =0;
Soft_I2C_Config(&GPIO,2,1); // init soft I2C
if (clean==1) send_lcd_clear(); //clear and home
light_lcd(0); // 0 for light on
if (line==2) lcd_line2();
send_lcd_char(message);
return 1;
// INTCON =255;
}
I2C_mess_specific_place(char message[],unsigned char pos)
{
// INTCON =0;
Soft_I2C_Config(&GPIO,2,1); // init soft I2C
light_lcd(0); // 0 for light on
pos = pos+128;
Soft_I2C_Start (); //send start bit
Soft_I2C_Write(0x42); //LCD address
Soft_I2C_Write(0x1); //Command
Soft_I2C_Write(pos);
Soft_I2C_Stop(); //send stop bit
send_lcd_char(message);
return 1;
// INTCON=255;

}
send_lcd_char_const(const char message[]) // this one send string to LCD
{
int i=0;
Soft_I2C_Start (); //send start bit
Soft_I2C_Write(0x42); //LCD address
Soft_I2C_Write(2); //write to I2c
//start string
while (message){ // write all string to LCD
Soft_I2C_Write(message[i++]);
}
Soft_I2C_Stop(); //send stop bit
return 1;

}
send_lcd_char(char message[]) // this one send string to LCD this one for variables chars
{
int i=0;
Soft_I2C_Start (); //send start bit
Soft_I2C_Write(0x42); //LCD address
Soft_I2C_Write(2); //write to I2c
//start string
while (message){ // write all string to LCD
Soft_I2C_Write(message[i++]);
}
Soft_I2C_Stop(); //send stop bit
return 1;

}
send_lcd_clear() // clear and home
{
Soft_I2C_Start (); //send start bit
Soft_I2C_Write(0x42); //LCD address
Soft_I2C_Write(1); //write to I2c
Soft_I2C_Write(1); //write to I2c
Soft_I2C_Stop(); //send stop bit
return 1;
}
light_lcd(char lit)
{
Soft_I2C_Start (); //send start bit
Soft_I2C_Write(0x42); //LCD address
Soft_I2C_Write(3); //write to I2c
if (lit==1) // light off
Soft_I2C_Write(1); //write to I2c
else // light on
Soft_I2C_Write(0); //write to I2c
Soft_I2C_Stop(); //send stop bit
return 1;
}
lcd_line2()
{ //command A8 from http://www.doc.ic.ac.uk/~ih/doc/lcd/instruct.html
Soft_I2C_Start (); //send start bit
Soft_I2C_Write(0x42); //LCD address
Soft_I2C_Write(0x1); //Command
Soft_I2C_Write(0xc0);
Soft_I2C_Stop(); //send stop bit
return 1;
}
// #endif



Thanks,
Roee.

womai
Posts: 239
Joined: 16 Apr 2008 07:45

#4 Post by womai » 30 Mar 2009 06:40

Just a general recommendation (and no offense intended - this is meant in a constructive way and applies to many other postings as well):

When you want to maximize the chance that somebody actually looks at your code (or even tries to run and debug it), keep the code short; i.e. strip it down to minimum size that just shows the problem (but still compiles), and remove all other pieces.

Few people will bother with ten pages of code and the question "this does not work - can you fix it for me" (often not even telling WHAT does not work). Many more will do this when the code segment is 10 lines. Especially if you also add extensive description of what the code is intended to do, what does not work, what you have already tried in order to make it working, and so on.

Wolfgang

Clubinho.x
Posts: 9
Joined: 05 Apr 2009 04:22
Location: Sumaré, Brazil

I2C for 16F73

#5 Post by Clubinho.x » 05 Apr 2009 04:28

Please help me.
I don´t have the codes for I2c communication to PIC 16F73.
Any peaple can help me?

Tanks
My email is clubinho.x.x@gmail.com

womai
Posts: 239
Joined: 16 Apr 2008 07:45

#6 Post by womai » 05 Apr 2009 06:14

Not sure where the problem lies (btw, here we go again - if you expect help, please provide SUFFICIENT information about what exactly you want to do, and what does not work. Most peoples mind reading skills are limited.)

The PIC16F73 is supported by MicroC, so just use the library hardware I2C or software I2C routines. Refer to the help file for commands and for sample code.

Clubinho.x
Posts: 9
Joined: 05 Apr 2009 04:22
Location: Sumaré, Brazil

#7 Post by Clubinho.x » 05 Apr 2009 22:57

I don´t found the I2C suport in the MikroC.
My MikroC is:

mikroC, mikroElektronika C compiler
for Microchip PIC microcontrollers
Version: 8.2.0.0

I have the I2C suport only the 18F452.
Do you can send me your version the MikroC?
tanks,

clubinho.x.x@gmail.com

womai
Posts: 239
Joined: 16 Apr 2008 07:45

#8 Post by womai » 06 Apr 2009 00:39

You should be able to use the same library functions. The MikroC distribution contains sample code for a limited number of different devices, but you can use the same for other devices as well (sometimes changes to certain register initialization will be necessary though). Don't forget to select the correct device in the development environment (combobox on the left side). But the library function are usable across devices, so just look up the I2C examples or the software I2C examples for e.g. the PIC16F887 and us that as your starting point . It's really just a few lines of code.

Sending you "my" version on MikroC wouldn't be very useful, since you can just download the installer from the MikroE website; I do not have any "secret" or "special" version of that software.

Wolfgang

Clubinho.x
Posts: 9
Joined: 05 Apr 2009 04:22
Location: Sumaré, Brazil

#9 Post by Clubinho.x » 06 Apr 2009 03:05

Thanks, I install de new Mikroc but the instructions not compile.
The MikroC Pro V1.65.

this is a line error.
I2C1_Init(100000); //Inicializa a I2C com frequência de 100KHz

This is a code error the line
7 312 Internal error 'I2C1_Init not found' i2ctest.c

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

#10 Post by milan » 07 Apr 2009 08:14

Hi,

Have you selected I2C check box in Library Manager.
SmartADAPT2 rules !

Clubinho.x
Posts: 9
Joined: 05 Apr 2009 04:22
Location: Sumaré, Brazil

#11 Post by Clubinho.x » 07 Apr 2009 11:11

Who make this?
I´m a initial programer.

Charlie
Posts: 2744
Joined: 01 Dec 2004 22:29

#12 Post by Charlie » 07 Apr 2009 12:33

Well it has been mentioned,but I don't see where the GPIO register is setup as digital I/O in the code example.
Regards Charlie M.

Post Reply

Return to “mikroC PRO for PIC General”