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

i2c slave dies with globals

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



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

i2c slave dies with globals
PostPosted: Thu Feb 23, 2012 3:14 am     Reply with quote

Hi, I have a problem. In the past I used to use this code on 18F4520 and it was working, now I try to run it on 18F4620 and it is crashing upfront when enable_int(global) is on. This is simple testing program which is meant to get data from another chip (18F4620 too) and turn leds on. I have there DS1307 in circuit so I know that i2c works because the other chip can get time and program time into it. Any suggestions why program is crashing after I enable enable_interrupts(GLOBAL); ? If I turn it off then all works but my IRQ is not working :( Thnx for help
Code:

#include <18F4620.h> //libs from compiler
#device ADC=10 //analogue to digital converter init specs
#include <ctype.h> //libs from compiler
#include <stddef.h> //libs from compiler
#include <string.h> //libs from compiler

//=========DEFINE====================
#define RX                 PIN_C7            //    serial data receive pin
#define TX                 PIN_C6            //    serial data transmit pin
#define TX_ON              PIN_C5            //    serial TX on
#define STATUS             PIN_B2            //    status led
#define GOUT1              PIN_D7
#define GOUT2              PIN_D6
#define GOUT3              PIN_D5
#define GOUT4              PIN_D4
#define GOUT5              PIN_D3
#define GOUT6              PIN_D2
#define GOUT7              PIN_D1
#define GOUT8              PIN_D0
#define BUZZ               PIN_A4                                       
#define POWER              PIN_E1                             
#define BATTERY            PIN_E2     
#define MAINS_TEST         PIN_AN3                                     
#define POWER_TEST         PIN_AN4
#define BATTERY_TEST       PIN_AN5
#define INTMP              PIN_AN0 
#define TMP1               PIN_AN1 
#define TMP2               PIN_AN2 
#define SLOT0              PIN_B1
#define SLOT1              PIN_B0               
#define SLOT2              PIN_B3 
#define SLOT3              PIN_B4
#define SLOT4              PIN_B5
#define SLOT5              PIN_B6                                               
#define SLOT6              PIN_B7
#define SLOT7              PIN_C0
#define SLOT8              PIN_C1
#define SLOT9              PIN_C2
#define I2CSCL             PIN_C3                           
#define I2CSDA             PIN_C4
//===================================
                                                       

#FUSES H4
#FUSES MCLR                       
#FUSES BROWNOUT
#FUSES NODEBUG
#FUSES NOPROTECT                               
//#FUSES WDT1024
#FUSES NOWDT
#FUSES PUT                                               
#FUSES NOCPD                                           
#FUSES NOXINST  // Config: ext reset, no code protect, no watchdog, high speed clock         
#use delay (clock=40M, oscillator=10M)                           
                                             
#use rs232(baud=19200,xmit=TX,rcv=RX,parity=n,bits=8,stop=1,RESTART_WDT,ERRORS,TIMEOUT=15)
#use i2c(slave, sda=I2CSDA, scl=I2CSCL, address=0xA0) 
                                                               
#use fixed_io(D_outputs=GOUT1,GOUT2,GOUT3,GOUT4,GOUT5,GOUT6,GOUT7,GOUT8)
#use fixed_io(A_outputs=BUZZ)
#use fixed_io(B_outputs=STATUS)
#use fixed_io(C_outputs=TX_ON,TX)
#use fixed_io(E_outputs=POWER,BATTERY)

int state;                                                                         
int c, buffer[5];
//=================================== 
#INT_SSP NOCLEAR                             

void ssp_interupt () {                                           

   clear_interrupt(int_SSP);
   state = i2c_isr_state();

   if(state < 0x80)  {               //master is sending data

      if (state == 1) {                                 
         c = i2c_read();
         buffer[0] = c;       
      }                     
      if (state == 2) {                 
         c = i2c_read();
         buffer[1] = c;
      }                       
      if (state == 3) {
         c = i2c_read();
         buffer[2] = c;   
      }
      if (state == 4) {
         c = i2c_read();
         buffer[3] = c;
      }
      if (state == 5) {
         c = i2c_read();
         buffer[4] = c;
      }

   }
   if(state == 0x80) {              //master is requesting data
      // i2c_write (buffer[c]);  //send requested data
   }
   
}                           
//==========================

void comms_TX_high() {                   
   output_high(TX_ON);
   delay_ms(10);
} //void                                                                                   

void comms_TX_low() {
   delay_ms(10);
   output_low(TX_ON);     
} //void                                         
                       
//=========MAIN PROGRAM==============

void main() {  // main program                         

setup_wdt(WDT_ON);            //set watch dog timer
set_timer0(0);               //clear timers                 
set_rtcc(0);               //clear timers   
setup_timer_0(RTCC_INTERNAL | RTCC_8_BIT | T0_DIV_1); //set timer 0 parameters
setup_adc_ports(AN0_TO_AN5 | VSS_VDD); //set AD ports
setup_adc(ADC_CLOCK_INTERNAL | ADC_CLOCK_DIV_8);
setup_comparator(NC_NC_NC_NC);   //turn off comparators
enable_interrupts(int_ssp);      //enable serial i2c
enable_interrupts(INT_RTCC);   //enable TIMERs
enable_interrupts(int_rda);      //enable serial port
//enable_interrupts(GLOBAL);     
           
//=========BOOT UP===================   

         buffer[0]=0xFF; 
         buffer[1]=0xFF;
         buffer[2]=0xFF;
         buffer[3]=0xFF;
         buffer[4]=0xFF;
         
   while(true) { //loop program       
      //restart_wdt();
     
         output_low(STATUS);
         delay_ms(500);
                                                             
         if ((buffer[0] == 0x00) && (buffer[1] == 0x01)) {
            output_high(GOUT1);   
         }
         if ((buffer[0] == 0x01) && (buffer[1] == 0x01)) {
            output_low(GOUT1);   
         }
         if ((buffer[0] == 0x00) && (buffer[1] == 0x02)) {
            output_high(GOUT2);   
         }
         if ((buffer[0] == 0x01) && (buffer[1] == 0x02)) {
            output_low(GOUT2);   
         }
         if ((buffer[0] == 0x00) && (buffer[1] == 0x03)) {
            output_high(GOUT3);   
         }
         if ((buffer[0] == 0x01) && (buffer[1] == 0x03)) {
            output_low(GOUT3);   
         }                 
         if ((buffer[0] == 0x00) && (buffer[1] == 0x04)) {
            output_high(GOUT4);   
         }
         if ((buffer[0] == 0x01) && (buffer[1] == 0x04)) {
            output_low(GOUT4);   
         }
         if ((buffer[0] == 0x00) && (buffer[1] == 0x05)) {
            output_high(GOUT5);   
         }
         if ((buffer[0] == 0x01) && (buffer[1] == 0x05)) {
            output_low(GOUT5);   
         }
         if ((buffer[0] == 0x00) && (buffer[1] == 0x06)) {
            output_high(GOUT6);   
         }
         if ((buffer[0] == 0x01) && (buffer[1] == 0x06)) {                         
            output_low(GOUT6);   
         }
         if ((buffer[0] == 0x00) && (buffer[1] == 0x07)) {
            output_high(GOUT7);   
         }
         if ((buffer[0] == 0x01) && (buffer[1] == 0x07)) {
            output_low(GOUT7);   
         }
         if ((buffer[0] == 0x00) && (buffer[1] == 0x08)) {
            output_high(GOUT8);   
         }
         if ((buffer[0] == 0x01) && (buffer[1] == 0x08)) {
            output_low(GOUT8);           
         }                                                 
         
         comms_TX_high();           
         printf("%u:%u:%u:%u:%u%c",buffer[0],buffer[1],buffer[2],buffer[3],buffer[4],0x0D);
         comms_TX_low();
                     
         output_high(STATUS);
         delay_ms(500);
   } //while

} //void


_________________
Help "d" others and then you shell receive some help from "d" others.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Thu Feb 23, 2012 4:22 am     Reply with quote

INT_RDA without a handler present.
You must _never_ enable an interrupt and INT_GLOBAL, without a hanlder present. Will crash.

Best Wishes
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