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 CCS Technical Support

Help: 18f452 I2c

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



Joined: 11 Jul 2006
Posts: 1

View user's profile Send private message MSN Messenger

Help: 18f452 I2c
PostPosted: Tue Jul 11, 2006 4:54 am     Reply with quote

Laughing Hi, this is my first time!
so, i have a PIC 18f452 and i am trying to use i2c bus,using CCS Compiler

#use i2c(Slave,Fast,sda=PIN_C4,scl=PIN_C3,restart_wdt,force_hw,address=0xa0)
...
#int_ssp
void SSP_isr() {
restart_wdt();
printf(" i2c");
}
...
main(){
...
output_flow(PIN_C3);
output_flow(PIN_C4);

}

i have an ideal start Condition on the bus,and the rigt address (my signal si about 50Hz),but the interupt seems not to work well.
Anyone can help me,please?

Thanks,

mario
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Tue Jul 11, 2006 8:37 am     Reply with quote

1. Post code using the "code" button. It retains the format.
2. Post complete code. You don't have any PIC info
3. Always have a forever while loop at the end of the test to prevent the PIC from going to sleep.((CCS compiles puts this sleep command in for you))
4. There isn't a function output_flow.. I figured you wanted output_low, but this would in turn override the I2C code
5. You never turned on the interupts. To do this enable the int you want to use, provide a ISR and turn on interupts global.

try this
Code:
#include <18F452>
#use delay(clock=40000000)
#fuses h4,nowdt,noprotect,nolvp,put
#use rs232(baud=19200,xmit=PIN_C0,invert,stream=DEBUG,disable_ints) // stderr(same as debug)
#case
#zero_ram
#use i2c(Slave,Fast,sda=PIN_C4,scl=PIN_C3,restart_wdt,force_hw,address=0xa0)
//#use I2C(MASTER,SDA=FRAM_SDA,SCL=FRAM_SCL,FORCE_HW,FAST=1000000,RESTART_WDT)
int1 print_it_flag=FALSE;
void main(void){
 
  printf("\n\rStart\n\r");
  //output_low(PIN_C3);//can't do this, it will override the I2C code
  //output_low(PIN_C4);//can't do this, it will override the I2C code
  //remember a 10k pull-up resistor on the I2C lines sda,scl
  enable_interrupts(INT_SSP);
  enable_interrupts(GLOBAL);
  while(TRUE) //ie while(1)   while(TRUE)   ie forever while loop
  {
    if(print_it_flag){
      print_it_flag=FALSE;
      printf(" i2c");
    }
  }
}



#INT_SSP
void SSP_isr() {
  restart_wdt();
  print_it_flag=TRUE;
}

..edited for note below
...edited for 2nd note below


Last edited by treitmey on Tue Jul 11, 2006 2:37 pm; edited 4 times in total
Ttelmah
Guest







PostPosted: Tue Jul 11, 2006 9:44 am     Reply with quote

Also though, do not pull the C3/C4 pins down. The slave, needs to listen to the pins, and only activate the data pin when ordered to do so. At present, the code is setting the pins as outputs, and pulling the data and clock line low, thereby overriding the I2C peripheral, which can therefore not work.

Best Wishes
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Jul 11, 2006 1:52 pm     Reply with quote

One word of advice, never _ever_ put a printf() statement inside an interrupt handler. Printf() can create a massive amount of code and an interrupt needs to be as short as possible. Set a variable, inside the interrupt, and then evaluate it in main(). If it is set then print something and clear the variable. You'll find this is the better way to do things as you begin to write more extensive and complicated code.

Ronald
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