View previous topic :: View next topic |
Author |
Message |
jayanthd
Joined: 06 Dec 2012 Posts: 47 Location: Banned - pirate
|
|
Posted: Sat Mar 23, 2013 10:41 am |
|
|
@Ttelmah
Quote: | You are not reading the byte on state 0x80 in the slave. |
I think this code is reading the byte on state 0x80 in the slave
if(state >= 0x80) // Master is requesting data from slave
{
i2c_write(data2send[0]);
}
If you say that it is writing to i2c if state is 0x80 then what should I read with i2c_read() function? Just give the solution. |
|
|
jayanthd
Joined: 06 Dec 2012 Posts: 47 Location: Banned - pirate
|
|
Posted: Sat Mar 23, 2013 10:44 am |
|
|
@Gabriel
Quote: | have you tried reading the .H file for your PIC? |
Yes. That's the file I read.
Quote: | I believe the problem lays between the screen and the back of your chair. |
What does that mean?
All it the header file says is OR desired values.
I said setup_adc_ports(AN0_AN1_AN4_AN5_VREF_VREF); doesn't work for 887
and the other one works. ")" was a typo it was "0".
Last edited by jayanthd on Sat Mar 23, 2013 10:54 am; edited 1 time in total |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Sat Mar 23, 2013 10:46 am |
|
|
I edited my last post... added some info... re-read it. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Sat Mar 23, 2013 10:49 am |
|
|
jayanthd wrote: | @Gabriel
I have read the manual and it is useless like to setup adc ports it says
setup_adc_ports(AN0_AN1_AN4_AN5_VREF_VREF); and it doesn't work and I found another way that is
setup_adc_ports(sAN0|sAN1|sAN4|sAN5|VREF_VREF);
Maybe the other form works for some PICs but it didn't work for PIC16F887 |
I would definitely check the CCSinfo website for the most recent manual. It doesn't appear to have the example you mentioned and has the correct syntax for the function call. Check the examples for each of the adc functions as they will often times have other functions in them. Also note that the parameters for most built in functions are chip specific, so while the manual is good for the function prototype, you'll need to verify with the .h file for your chip what the actual parameters are.
Having the most recent version of the manual will hopefully help reduce issues for ya. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Sat Mar 23, 2013 10:50 am |
|
|
jayanthd wrote: | @Ttelmah
Quote: | You are not reading the byte on state 0x80 in the slave. |
I think this code is reading the byte on state 0x80 in the slave
if(state >= 0x80) // Master is requesting data from slave
{
i2c_write(data2send[0]);
}
If you say that it is writing to i2c if state is 0x80 then what should I read with i2c_read() function? Just give the solution. |
What he means is 0x80 is a special case where it both must be read (to get the address byte) and also must perform a write. From the code we saw, you are only performing the write, but not the read, which can actually affect how the hardware works on some chips. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Mar 23, 2013 10:52 am |
|
|
jayanthd wrote: | temtronic wrote: | why?
Cause he's a sloppy programmer...cut and pasting...bits and pieces from other chunks of code,probably from other C compilers..
CCS C is very clean and easy to use AND they supply working examples of 99.999% of the things everyone uses PICs for!
hth
jay |
I am not cutting and pasting codes written for other c compilers. I have that much and more knowledge of using the codes.
I am just trying to find some working CCS C i2c code. | Things are starting to get 'touchy' here. Before it really escalates, I understand it that Jay was responding to this question:The 'he' Jay was referring to is the same 'he' as you (jayanthd) mentions. It was not a personal attack for you copy/pasting code.
Please take a long breath and calm down.
We all here want to help you and we do want to respect you but we do expect to get respect in return.
You have 10 years experience, which is a lot. But you are discussing here with at least 3 people who have over 30 years experience each.
So if one of us gives you a probing question there is a reason for that. Better not ignore it 'because you know better' but get your study books out and look it up again.
"%3.4f" is technically correct syntax but is a command the compiler can never perform as requested and as such makes no sense. I looked up your C book and they didn't teach you so. (page 84)
It will work, but the first parameter is useless.
Last edited by ckielstra on Sat Mar 23, 2013 10:58 am; edited 1 time in total |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Sat Mar 23, 2013 10:56 am |
|
|
so the mistake you posted which i commented on, magically disappeared from your post...
anyways.. the .h file tells you what are the allowed parameters for your pic and how to use them.. read that with the ccs manual and you should be fine...
CCS also provides an ample source of examples and in this forum there are thousands of examples of other users...
you seem to be tackling many issues at the same time.. focus on small programs and then join them...
I'm out..good luck.
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
jayanthd
Joined: 06 Dec 2012 Posts: 47 Location: Banned - pirate
|
|
Posted: Sat Mar 23, 2013 10:59 am |
|
|
@ckielstra
I will read that book again...
Ok. Thank you everyone. Can somebody tell what is the solution to my two problems .i.e.,
1. Slave's lcd not printing the value of temp when i2c is used.
2. how to do a i2c read when status is 0x80? i.e., read i2c in the slave and do what? |
|
|
jayanthd
Joined: 06 Dec 2012 Posts: 47 Location: Banned - pirate
|
|
Posted: Sat Mar 23, 2013 11:05 am |
|
|
Gabriel wrote: | so the mistake you posted which i commented on, magically disappeared from your post...
anyways.. the .h file tells you what are the allowed parameters for your pic and how to use them.. read that with the ccs manual and you should be fine...
CCS also provides a ample source of examples and in this forum there are thousands of examples of other users...
you seem to be tackling many issues at the same time.. focus on small programs and then join them...
I'm out..good luck.
G |
Yes. I said the ) was a typo. I didn't do that typo in the code. when I typed AN0, I pressed the shift key while entering 0. So it became )
Ok. I changed the code to
Code: |
if(state >= 0x80) // Master is requesting data from slave
{
i2c_read();
i2c_write(data2send[0]);
}
|
but still it doesn't work. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
Typos |
Posted: Sat Mar 23, 2013 11:45 am |
|
|
Quote: | Yes. I said the ) was a typo. I didn't do that typo in the code. when I typed AN0, I pressed the shift key while entering 0. So it became )
| In general the compiler complains about silly typos,
When I find posted code won't compile, I KNOW it can't be the version tested.
The poster is wasting my time and theirs.
In my book the typos you describe should NEVER appear in posted code.
When I post code it's cut and pasted in an endeavour to make it EXACTLY as tried and tested it.
(Unless I make it clear it's untested code.)
Mike |
|
|
jayanthd
Joined: 06 Dec 2012 Posts: 47 Location: Banned - pirate
|
Re: Typos |
Posted: Sat Mar 23, 2013 12:26 pm |
|
|
Finally what is the solution to my problem? |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat Mar 23, 2013 12:35 pm |
|
|
I've just noticed you're persisting in doing this again Quote: | but still it doesn't work |
As an experienced programmer you SHOULD be asking my probing questions of yourself.
What IS it doing?
What's it NOT doing?
What does work?
What doesn't work?
What kind of diagnostic test will tell me the answers?
Where can I get more information from?
Has someone else had the same problem?
Can I learn something from the sample code?
I'm out. Best of luck.
Bye.
Mike
EDIT It's Saturday night here. Time for a few beers. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Sat Mar 23, 2013 1:28 pm |
|
|
jayanthd wrote: |
Ok. I changed the code to
Code: |
if(state >= 0x80) // Master is requesting data from slave
{
i2c_read();
i2c_write(data2send[0]);
}
|
but still it doesn't work. |
The issue with doing it this way is that now you are reading when the state is 0x81-0xFF, which you should be only writing. In short:
State is 0x00-0x7F ==> only read
State is 0x80 ==> read and write
State is 0x81-0xFF --> only write
There is an example of how to do this if you look up i2c_isr_state() in the manual (it has a typo: i@c_read instead of i2c_read, but you can fix that). You would just need to modify the typo and where the data is written from and saved to. |
|
|
jayanthd
Joined: 06 Dec 2012 Posts: 47 Location: Banned - pirate
|
|
Posted: Sat Mar 23, 2013 3:26 pm |
|
|
@jeremiah
This is my latest code. Lcd is working now but data received by master is wrong and after receiving data 2 times it is not receiving data again.
Master Code
Code: |
#include <16F887.h>
#device adc=10
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000) //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3) //FORCE_HW, stream=I2CM
#define SLAVE1_WRT_ADDR 0x12
#define SLAVE1_READ_ADDR 0x13
#define SLAVE2_WRT_ADDR 0x22
#define SLAVE2_READ_ADDR 0x23
float fdata1 = 0, fdata2 = 0;
union float2bytes {
float fval;
int8 bytes[4];
} fvalue;
union float2bytes1 {
float fval1;
int8 bytes1[4];
} fvalue1;
#define LCD_ENABLE_PIN PIN_D2
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#include <lcd.c>
void main()
{
set_tris_d(0x00);
output_d(0x00);
delay_ms(250);
lcd_init();
delay_ms(250);
lcd_putc("\fMaster...\n");
while(TRUE)
{
i2c_start();
i2c_write(SLAVE1_READ_ADDR);
fvalue.bytes[0] = i2c_read(0);
i2c_start();
fvalue.bytes[1] = i2c_read(0);
i2c_start();
fvalue.bytes[2] = i2c_read(0);
i2c_start();
fvalue.bytes[3] = i2c_read(0);
i2c_stop();
delay_ms(100);
i2c_start();
i2c_write(SLAVE2_READ_ADDR);
fvalue1.bytes1[0] = i2c_read(0);
i2c_start();
fvalue1.bytes1[1] = i2c_read(0);
i2c_start();
fvalue1.bytes1[2] = i2c_read(0);
i2c_start();
fvalue1.bytes1[3] = i2c_read(0);
i2c_stop();
if((fvalue.fval != fdata1) || (fvalue1.fval1 != fdata2)) {
printf(lcd_putc, "\a%5.2f %5.2f", fvalue.fval, fvalue1.fval1);
printf("%5.2f %5.2f\r\n", fvalue.fval, fvalue1.fval1);
fdata1 = fvalue.fval;
fdata2 = fvalue1.fval1;
}
delay_ms(500);
}
}
|
Slave 1 Code
Code: |
#include <16F887.h>
#device adc=10
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000) //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x12) //FORCE_HW, stream=I2CS
#define LCD_ENABLE_PIN PIN_D2
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#include <lcd.c>
float temp = 0, old_val = 0;
int8 ctr = 0;
union float2bytes {
float fval;
int8 bytes[4];
} fvalue;
#INT_SSP
void ssp_interrupt()
{
int8 incoming, state;
state = i2c_isr_state();
if(state < 0x80) // Master is sending data
{
incoming = i2c_read();
}
if((state == 0) || (state == 0x80)) // Master is requesting data from slave
{
i2c_read();
}
if(state > 0x80) // Master is requesting data from slave
{
i2c_write(fvalue.bytes[ctr]);
i2c_start();
i2c_write(fvalue.bytes[ctr++]);
i2c_start();
i2c_write(fvalue.bytes[ctr++]);
i2c_start();
i2c_write(fvalue.bytes[ctr++]);
i2c_stop();
ctr = 0;
}
}
void main()
{
int16 adc_val = 0;
set_tris_a(0xFF);
set_tris_d(0x00);
output_a(0x00);
output_d(0x00);
setup_adc_ports(sAN0|VREF_VREF);
setup_adc(ADC_CLOCK_DIV_2);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
delay_ms(250);
lcd_init();
delay_ms(250);
lcd_putc("\fSlave 1...\n");
set_adc_channel(0);
delay_us(20);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(TRUE)
{
adc_val = read_adc();
delay_ms(20);
temp = (float)(adc_val * 0.196078431372549);
fvalue.fval = temp;
if(old_val != temp){
printf(lcd_putc, "\a%5.2f", temp);
printf("Temperature is: %5.2f", temp);
printf(" Degree Centigrade\r\n");
printf("%5.2f\r\n", fvalue.fval);
printf("%lu\r\n", adc_val);
}
old_val = temp;
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Sat Mar 23, 2013 3:38 pm |
|
|
Have you just tried the examples you have been pointed to?.
Have you ever actually written a program that works?.
You _claim_ to have used other languages, but every language _requires_ you to follow it's syntax precisely. You are not even trying.
1) You are now trying to read twice in state zero.
2) You are now trying to issue start conditions in the slave - the _master_ is the only chip that can issue starts.
3) Same applies for stops
4) You are now trying to write four bytes in one ISR in the slave - I've already shown how to write multiple bytes.
5) When you actually solve the above, stop and think for a moment. What happens if the slave changes the value half way through an I2C transaction?. |
|
|
|