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

how to write and read 16bit variable to 16f628A chip?

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



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

how to write and read 16bit variable to 16f628A chip?
PostPosted: Sun Sep 05, 2021 4:19 am     Reply with quote

hello,

I'm trying to store my variable "time" to eeprom and get it back from eeprom ... but no success ...

I'll be very pleased if you can advise how to do that Smile

have fun

Code:

#include<16F628A.h>
#use delay(clock=11059200)               
#fuses NOWDT,HS                           //must be HS
#zero_ram                                 // OK maybe must be here ?

//#define A_encoder         (PIN_B0);    //rotary encoder make interrupts this pin
//#define B_encoder         (PIN_B1);    //rotary encoder direction  pin

#define sleep             (PIN_B2);   //SLEEP pin of A4988 stepper drive module#define dir
#define step              (PIN_B3);   //every pulse stepper takes one step,200 steps/rev.
#define dir               (PIN_B4);   //DIR pin of A4988 stepper drive module
#define stepper_ON_OFF    (PIN_A0);
#define speed_to_mem      (PIN_A2);                 
#define speed_from_mem    (PIN_A1);


void change_step_time__isr(void) ;    //this for rotary encoder_A interrupts
void start_demo(void);

int16 time,time_step;                //one rev. = 200 steps... min. speed is 8 s/rev. ... one step last 40000 mms
                                     //pulse 20000 mms high 20000 mms low
                                     //filament going 11 x PI / 8 = 4.3 mm/s
                                     
                                     //max. speed is 1 s/rev. ... one step last 1/200 s = 5000 mms
                                     //pulse 2500 mms high 2500 mms low
                                     //filament going 11 x PI / 1 = 34.5 mm/s

int8  lower_8bits,upper_8bits;       //variable time to 2 8 bits for eeprom
     

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

void main(void){
   
   ext_int_edge(L_to_H);
   enable_interrupts(INT_EXT);   
   enable_interrupts(GLOBAL);
   
   delay_ms(100);                  //start delay
   
   output_high dir;                //first dir is forwards   
   time = 20000;                   //this: begin speed = 8 s / rev.   
   
   time_step = 100;
   
   start_demo();
   
   output_high sleep;               //first sleep
   delay_ms(2);
   
   
   while(1){
   
    if(input (PIN_A0)){                  //read sleep pin, HIGH when sleeping 
       output_low sleep;
    }
   
    if(!input(PIN_A0)){                  //read sleep pin, LOW when running       
       
       output_high sleep;
   
   
    if(!input(PIN_A2)){                 //A2 is speed_to_mem, active LOW       

/*     
int32 x;
int y;
y = make8(x,3); // Gets MSB of x
*/     
     
      //lower_8bits = time & 0xff;
      //upper_8bits = (time >> 8) & 0xff;
     
      lower_8bits = make8(time,0);     
      upper_8bits = make8(time,1);       
     
     
      write_eeprom(lower_8bits,0);
      delay_ms(50);
      write_eeprom(upper_8bits,1);
      delay_ms(50);

      while(!input(PIN_A2)){                 //wait PIN A2 to release
         ;
      }
     
      delay_ms(50);

    }


    if(!input(PIN_A1)){                 //A1 is speed_from_mem, active LOW       

     
      lower_8bits = read_eeprom(0);
      delay_ms(50);
      upper_8bits = read_eeprom(1);
      delay_ms(50);     
     
//lower_8bits = 0x20;     
//upper_8bits = 0x4E;       
 
      time = make16(upper_8bits,lower_8bits);             
   
//x = make16(hi,lo);     
     
     
      //time = 0x4E20;
     
      while(!input(PIN_A1)){                 //wait PIN A1 to release
         ;
      }
     
      delay_ms(50);

    }

   
   output_high step;
   delay_us(time); 
   output_low step;
   delay_us(time); 
   }
   
 }
}
//*********************************************************

#INT_EXT
void change_step_time__isr(void){
                                                       //all values are microseconds 
   
    if (!input (PIN_B1) & (time > 2500) ) {              //max. speed 1 second / rev. full step
       time = time - time_step ;                       //increase stepper speed
       return;
    }
   
   if (input(PIN_B1)  & (time < 20000)){               //min. speed 8 second / rev. full step
      time = time + time_step ;                        //decrease stepper speed     
      return;
   }
   
}

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

void start_demo(void)  {

     
      output_low(pin_B5);
      delay_ms(500);
      output_high(pin_B5);
     
     
     
      output_low(pin_A4);
      delay_ms(100);
      output_high(pin_A4);

      output_low(pin_A3);
      delay_ms(100);
      output_high(pin_A3);

      output_low(pin_A2);
      delay_ms(100);
      output_high(pin_A2);

      output_low(pin_A1);
      delay_ms(100);
      output_high(pin_A1);
     

      output_low(pin_A0);
      delay_ms(100);
      output_high(pin_A0);


}

//**************************************************************************************************************
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Sep 05, 2021 4:39 am     Reply with quote

artohautala wrote:

write_eeprom(lower_8bits,0);
delay_ms(50);
write_eeprom(upper_8bits,1);
delay_ms(50);

Look in the CCS manual at the order of parameters for the
write_eeprom() function. See page 610 in the manual.
Compare it to what you are doing above.

CCS manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
artohautala



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

PostPosted: Sun Sep 05, 2021 4:51 am     Reply with quote

PCM programmer wrote:
artohautala wrote:

write_eeprom(lower_8bits,0);
delay_ms(50);
write_eeprom(upper_8bits,1);
delay_ms(50);

Look in the CCS manual at the order of parameters for the
write_eeprom() function. See page 610 in the manual.
Compare it to what you are doing above.

CCS manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf


Allright ... Smile lot of thanks to solve my stupid error ... now it works fine Smile

so order is first address and then variable


all the best for you !

-arto-
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Sep 06, 2021 5:13 pm     Reply with quote

If you're using MPLAB, pressing F11, will automatically open up the CCS C Manual..... don't know if this works for other IDEs though...
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Sep 14, 2021 6:17 am     Reply with quote

There are instances where you need to make sure that the data you wrote is valid when read and not uninitialized eeprom. As in valid after a reset. Consider adding two bytes of checksum following your two bits of data. Then you know if you wrote it - or if it's just default. Also EEPROM can degrade from too many writes. Saying for a friend.
Ttelmah



Joined: 11 Mar 2010
Posts: 19510

View user's profile Send private message

PostPosted: Tue Sep 14, 2021 6:22 am     Reply with quote

Yes, a very important point.
Using a 'flag' value to say the data has actually been updated, allows you
to handle the 'uninitialised' state by testing if the byte is 0xFF (this is for
most types of EEPROM and flash, the 'erased' value. When this is seen force
the user to put in sensible values, and then change the flag to reflect this.
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Sep 14, 2021 8:27 am     Reply with quote

hmmm...
is this legal ?

#define sleep (PIN_B2); //SLEEP pin of A4988 stepper drive

enclosing the PIN_B2 with brackets ??

I'd think the compiler would flag as an error ?

jes curious...
Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19510

View user's profile Send private message

PostPosted: Tue Sep 14, 2021 9:23 am     Reply with quote

A pin definition is only a number, and extra brackets round a number
should not matter at all.
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