CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

How to get data I2C without Interrupt.

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
harizal



Joined: 13 Apr 2018
Posts: 3
Location: Malaysia

View user's profile Send private message

How to get data I2C without Interrupt.
PostPosted: Wed May 02, 2018 7:31 am     Reply with quote

How can i get data from other pic use I2C without interrupt.?? Because when i put delay it jammed. and can't receive data.

This is my code:-

Master:

i2c_start(); // Start condition
i2c_write(0xa0); // Device address
i2c_write(binary); // Write Command 0000000000000
i2c_stop(); // Stop condition


Slave:-

#include <16f877A.h>
#device adc=10
#use delay (clock=20000000)
#fuses HS,NOWDT,NOPROTECT,NODEBUG,BROWNOUT,NOLVP,NOCPD
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use I2C(SLAVE, sda=PIN_C4, scl=PIN_C3, address=0xa0, SLOW,
FORCE_HW)
#bit ADFM_BIT = 0x9F.7
#INT_SSP
void ssp_interupt ()
{
int32 incoming;
incoming = i2c_read();
printf("\n\rRead byte 1: %Lu \n\r", incoming);
incoming = i2c_read();
printf("Read byte 2: %Lu \n\r\n\r", incoming);
}


void main()
{
delay_ms(1000);
printf("Running");

enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
while (true)
{

}
}


How can i use that reading the I2C on Void Main???
Ttelmah



Joined: 11 Mar 2010
Posts: 13536

View user's profile Send private message

PostPosted: Wed May 02, 2018 7:55 am     Reply with quote

Realistically for the slave the interrupt is far the better way to go.

The SSP interrupt means _one_ byte has been transferred. Just one. Trying to read a second will result in the slave sitting waiting, and also if the transfer was a write, then one has to be sent rather than read.

You are assuming there is always a byte to read. There are four fundamentally different state in the slave:
1) Address has been received with R/W low. Needs to be read, but is not data.
2) A data byte has been received. Read it.
3) Address has been received with R/W high. Needs to be read, and the slave then needs to load the byte to send back.
4) Master has read a byte and is waiting for more. You need to load the byte to send.

Now you can perfectly well put the reads into a 'main' without having to use the interrupt, but you still need to check the status of the transactions. You need to use the I2C_ISR_STATE command to know what the master has signaled. You can't just assume you can read bytes, and that they will be data.

In your interrupt example there are major problems because of delays. Your printf's in the interrupt will hang the I2C. The slave _must_ be ready for each event as it happens. It has a tiny ability to stretch the clock to delay for the read, but you are delaying after this has happened.

Use the interrupt.
Look at the example SSP code, and simply organise the memory buffer to talk to the same piece of memory as your int32.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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