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

Eeprom problems

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



Joined: 07 Aug 2007
Posts: 24

View user's profile Send private message

Eeprom problems
PostPosted: Tue Oct 02, 2007 3:23 am     Reply with quote

Hey,

I really need help, i have been struggeling with programming a eeprom to function as follow. It must only log data when the motion sensor is high. And it must read the data when there is 10 values logged.

But it doesn't want to work. Please help me, i posted the code below

Code:



#include "16f883.h"

#use DELAY(clock=4000000)
#fuses HS,NOWDT,NOPROTECT,PUT,NOLVP,NOBROWNOUT,NOWRT
#use rs232(baud=9600, xmit=PIN_C6, rcv= PIN_C7)
#use i2c(master,sda=PIN_C4, scl = PIN_C3)

#define MAX_TICKS 3
#define address 0x08
#bit RELAY = PORTC.2

//Components
#define EE_BLOCK   8  //5 was vir ekstra coils
#define COIL_1  PIN_C2

#case
// GLOBAL VARIABLES


int8 PIR = 0;
int8 POT = 0;
int8 LDR = 0;
int8 SWITCH_TIME;
int8 RecordNr = 0;  // Moved the variable declarations out of the loop.
unsigned int16 recordNumber = 0;
unsigned int16 timeout =0;
 

// Structures
struct time
{
   unsigned int ms;                  // Milliseconds
   unsigned char ss ;                  // Seconds
   unsigned char mm;                  // Minutes
   unsigned char hh;                  // Hours
   unsigned char day ;              //day
   unsigned char month;           //Months
   
};

//********************************************************************************************************************
//                         Data structure for logging and modbus functions
//********************************************************************************************************************

typedef struct
{
    int8         PIR_mod,          // Sensor motion detection
              POT_mod,          // Sensitivity adjustments
              LDR_mod,          // Light intensity   
                temp_mod,        // temprature value
              coil_mod[3],        // relay status
              minute,
              hour,
              day,
              month;
         
}ilINFO;// Intelli light data structure

ilINFO intelliINFO; // Stores info to be logged to EEPROM

//********************************************************************************************************************

 
struct time now = {0};

//****************************************************************************************************************

//*********************************************************************************************************************************
//                              Stel die A na D op
//*********************************************************************************************************************************

void setupADC ()

{
 
ADCON1 = 0x80; // moet in setup kom
ADCON0 = 0x00;

}


void setChannel ( unsigned char Channel )

{

 unsigned char temp;
 Channel = Channel & 0x0F;
 Channel = Channel << 2;
 ADCON0  = 0x03 | Channel;
 
}

unsigned int16 readADC ()

{

  unsigned int16 Value;
  ADCON0 = ADCON0 | 0x01; // turn on ADC
  delay_ms(5);
  ADCON0 = ADCON0 | 0x02;  //start conversion stel 2de bus
  while ( ADCON0 & 0x02 );
  Value = ADRESH;
  Value = ( Value << 8 ) | ADRESL;
 
  return Value;

}


//*********************************************************************************************************************************
//                                          LEES PIR
//*********************************************************************************************************************************
unsigned int8 readSensor()
{
   unsigned int8 adcValue;

   
   setChannel(0);
   delay_ms(15);
   adcValue = readADC();         // get ADC value from the sensor;
   
   return adcValue;
}


//*********************************************************************************************************************************
//                              skryf na eeprom   
//********************************************************************************************************************************

// Writes to the slave board from the master.
void write_ext_eeprom(int16 ADDRESS, unsigned char data)
{

   int8 status;
   i2c_start();
   i2c_write(0xAE);
   i2c_write((int8)(ADDRESS>>8));
   i2c_write((int8)ADDRESS);
   i2c_write(data); 
   i2c_stop();
   //delay_ms(5);   delay is boarderline to the maximum waiting period - delay only if...do
      do
           {
             i2c_start();
             status=i2c_write(0xAE);
              }   while (status != 0);
}   

//**************************************************************************************************************************************
//                              LEES
//***************************************************************************************************************************************

// Read from the slave board and display the data.
unsigned char read_ext_eeprom( int16 ADDRESS )
{

   unsigned char data_read;

   i2c_start();
   i2c_write(0xAE);
   i2c_write( (int8)(ADDRESS>>8));
   i2c_write( (int8)ADDRESS);
   i2c_start();
   i2c_write(0xAF);
   data_read = i2c_read(0); 
   i2c_stop();
   delay_ms(5);


   return data_read;

}

//**********************************************************************************************************************************
//                This function will read a single log from the EEPROM and store it in a intelli light structure
//**********************************************************************************************************************************

void readEE(unsigned int16 ee_address , ilINFO *currentEntry)
{
   // Move to correct block
   ee_address = ee_address * EE_BLOCK ;

   // Read EEPROM
   currentEntry->PIR_mod  = read_ext_eeprom(ee_address);                       // Day
   printf(read_ext_eeprom(ee_address));                   
      currentEntry->LDR_mod  = read_ext_eeprom(ee_address+1);
   printf(read_ext_eeprom(ee_address+1));
   currentEntry->temp_mod = read_ext_eeprom(ee_address+2);
    printf(read_ext_eeprom(ee_address+2));
   currentEntry->coil_mod[0] = read_ext_eeprom(ee_address+3);
   printf(read_ext_eeprom(ee_address+3));
   currentEntry->minute = read_ext_eeprom(ee_address+4);                     //Stuur minute
   printf(read_ext_eeprom(ee_address+4));
   currentEntry->hour = read_ext_eeprom(ee_address+5);                        //Stuur ure
   printf(read_ext_eeprom(ee_address+5));
   currentEntry->day = read_ext_eeprom(ee_address+6);                        //Stuur dae
   printf(read_ext_eeprom(ee_address+6));   
   currentEntry->month = read_ext_eeprom(ee_address+7);                     //Stuur maande
   printf(read_ext_eeprom(ee_address+7));


}



//***********************************************************************************************************************************
//                         This function will log a single intelli light structure to EEPROM
//**********************************************************************************************************************************


void logtoEE(unsigned int16 ee_address, ilINFO *currentEntry)
{
   // Move to correct block
   ee_address = ee_address * EE_BLOCK;


   // Write EEPROM
   write_ext_eeprom(ee_address,   currentEntry->PIR_mod);                // Day
   write_ext_eeprom(ee_address+1, currentEntry->LDR_mod);              // Hour
   write_ext_eeprom(ee_address+2, currentEntry->temp_mod);           // Temprature(High byte) >> 8
   write_ext_eeprom(ee_address+3, currentEntry->coil_mod[0]);
   write_ext_eeprom(ee_address+4, currentEntry->minute);
   write_ext_eeprom(ee_address+5, currentEntry->hour);      
   write_ext_eeprom(ee_address+6, currentEntry->day);
   write_ext_eeprom(ee_address+7, currentEntry->month);      


}



//*************************************************************************************************************************************
//                     Hier binne moet dit log as switch time verander
//************************************************************************************************************************************

void light_on(int8 motionsensor)
{


   if(motionsensor > 0)
            {

            output_high(PIN_C2);
            intelliINFO.coil_mod[0] = 1;
            ++RecordNr;                              //Record number increment to save continiously   
             logtoEE(RecordNr, &intelliINFO);                // A '&' indicates the compiler we want to pass the address of the struct
   
            }

   if(motionsensor == 0)
         {
            output_low(PIN_C2);
            intelliINFO.coil_mod[0] = 0;   
   
          }

   
      
}
             



//***********************************************************************************************************************************************************************************


void readReference( unsigned int16 recordNUMBER )
{

ilINFO currentEntry;
readEE (recordNUMBER, &currentEntry);                                                                                        

}


         
                                                   
void setup()
{



 set_tris_a(0x27);               // Port A Direction
   // RA0 : Sensor Input (1)
   // RA1 : Sensitivity Input (1)
   // RA2 : Sensitivity (1)
   // RA3 : Not used (0)
   // RA4 : Not used (0)
   // RA5 : LDR (1)

   set_tris_b(0x00);               // Port B Direction
   // RB0 : Not used (0)
   // RB1 : RS-485 Direction Output (0)
    // RB2 : LED1 Output (0)
   // RB3 : LED2 Output (0)
   // RB4 : LED3 Output (0)
   // RB5 : RUN LED Output (0)
    // RB6 : Not used (0)
   // RB7 : Not used (0)

   set_tris_c(0x80);               // Port C Direction

   
   // ADC Setup

    setup_adc_ports( AN0_AN1_AN2_AN3_AN4 );                            //sets port A to analogue
    setup_adc( ADC_CLOCK_INTERNAL );                        //enables the a/d module

    setupADC();


   setup_timer_2(T2_DIV_BY_4,155,2);



   setup_timer_2(T2_DIV_BY_4,155,2);


   enable_interrupts(INT_TIMER2);
   enable_interrupts(GLOBAL);
   
   
}


//*****************************************************************************************************************
//                                       Main Program!!!!
//*****************************************************************************************************************

// Main Program
void main()
{
 
   setup();
   SWITCH_TIME = 5;

   // Main Loop
   while(1)
   {


   PIR = readSensor();                             //Lees die bewegings sensor
   light_on(PIR);

   intelliINFO.PIR_mod  = PIR;                    // PIR sensor
      intelliINFO.POT_mod  = 2;
   intelliINFO.LDR_mod =  3;
     intelliINFO.temp_mod = 4;
   intelliINFO.coil_mod[0] = RELAY;


   if( RecordNr == 10)
      {
         readReference(1);

      }
         


   }
     
}   

//*********************************************************************************************************************************
//                                          TIMEOUT!!!!!!
//*********************************************************************************************************************************


#INT_TIMER2
void isr_TIMER2()      //interrupt routine when overflow occurs in TIMER2. ====> VIR TIMEOUT
{

 static unsigned char ticks = 0;

   if(++ticks == 3)         // 1 millisecond
   {
 
      // Software Clock
      if(++now.ms == 1000)    // One second elapsed
      {
         now.ms = 0;
         if(++now.ss == 60)   // One minute elapsed
         {
           now.ss = 0;
   

            if(++now.mm == 60)   // One hour elapsed
            {
               now.mm = 0;


               if(++now.hh == 24)   // One day elapsed
               {
                  now.hh = 0;
            if((++now.day == 31 && (now.month==4 || now.month==6 || now.month==9 || now.month==11 ))
              || (now.day == 32)
              ) {now.month++; now.day = 0;}

               }
            }
         }
      }

      ticks = 0;            // Reset ticks
   }


   intelliINFO.minute = now.mm;
   intelliINFO.hour = now.hh;
   intelliINFO.month = now.month;
   intelliINFO.day = now.day;



 
}

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