|
|
View previous topic :: View next topic |
Author |
Message |
Greg_Smith
Joined: 26 Jul 2004 Posts: 1
|
I2C functions (CCS PIC-C) |
Posted: Mon Jul 26, 2004 3:18 pm |
|
|
I am quite new to PIC and I2C, so please exlain each step! (Sorry)
I am using two PIC 16F8776, one as a Master and one a slave, I have the Master and the Slave working.
However, I cannot work the slave with an Interrupt function but would like to....any help would be very mcuh appreciated....?
This is the slave code attempt for the interrupt but I have become confused as to getting it working? PLEASE HELP
#include <16f876.h>
#fuses HS, NOWDT, NOPROTECT, NOLVP
#use delay(clock=20000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xa0)
#INT_SSP
void ssp_interupt (void)
{
if (i2c_poll()) {
//if (i2c_read()==0xa0) {
set_pwm1_duty(50);
set_pwm2_duty(200);
output_high(pin_b0);
output_low(pin_b1);
output_high(pin_b2);
output_low(pin_b3);
//}
} else
i2c_start();
i2c_write(0xa0);
i2c_stop();
}
void main() {
// slave program
port_b_pullups(TRUE);
set_tris_b(0xff);
setup_timer_2(T2_div_by_16,255,16); // Pulse period = 1/[(PR2+1)*4*(1/clock)*(timer_2_prescaler_value)]
setup_ccp1(ccp_pwm); // Configure CCP1 as a PWM
setup_ccp2(ccp_pwm);
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
while(1) {
delay_ms(50);
};
} |
|
|
valemike Guest
|
|
Posted: Mon Jul 26, 2004 6:34 pm |
|
|
I implemented Microchip's App note AN735, as many others have done as well.
I used CCS's libraries for the master, but implemented my own interrupt-based one (you have to use interrupt-based i2c for the slave).
Here is the code that I have running...
http://www.ccsinfo.com/forum/viewtopic.php?t=19555&postdays=0&postorder=asc&highlight=valemike&start=15
The slave does report NAKs, which I think is due to the master implementation, but i've never found NAKs to be a problem. Maybe i'm doing something wrong, but hey, it gets the data through |
|
|
Thomas Blake
Joined: 18 Jan 2004 Posts: 22 Location: Burbank CA
|
NAKs |
Posted: Mon Jul 26, 2004 7:36 pm |
|
|
For the record, a lot of i2c slave devices use the NAK as an indication that a read operation is complete. So while this is OK for 2 PICs it may be teeth-gnashing if you ever work with a chip like (say) the Cirrus CS8416 and many others. |
|
|
valemike Guest
|
Re: NAKs |
Posted: Tue Jul 27, 2004 7:35 am |
|
|
Thomas Blake wrote: | For the record, a lot of i2c slave devices use the NAK as an indication that a read operation is complete. So while this is OK for 2 PICs it may be teeth-gnashing if you ever work with a chip like (say) the Cirrus CS8416 and many others. |
Oh, so it is normal to get NAKs then? That's good. I thought i was supposed to get something else, like maybe an ACK (I haven't read the i2c spec completely yet, since my "RTFM" activities usually are just limited to glancing at pictures. ) |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Tue Jul 27, 2004 9:03 am |
|
|
The Master will, usually, send a NACK at the last transmitted byte indicating, to the Slave, that it is finished doing it's thing. Then, if the Master is going to free up the I2C bus, it will send a Stop indicating to any other Master, on the bus, that they are free to do their thing too. I've never used CCS's build in functions in the slave code, just used it's interrupt and had the ISR look at the data being sent and then do according to what it received. Clear as mud?
Try searching this forum on this. That's what I had to do to get my Slave to work properly.
Ronald |
|
|
Guest
|
|
Posted: Tue Jul 27, 2004 12:33 pm |
|
|
Cheers for the replies, I have the slave and master working completly, but have scrapped the inerrupt for the moment!
I had a few timing issues that adding 5-10 cycles within the program seemed to work, but its never good too add lines in, not knowing whats going on!
When I have finished the code I will post it on here, maybe it could be useful to someone.
I am controlling 6 PIC 16F8776, ten PWM outputs and 10 ADC inputs |
|
|
|
|
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
|