View previous topic :: View next topic |
Author |
Message |
artohautala
Joined: 17 Nov 2011 Posts: 187
|
how to write and read 16bit variable to 16f628A chip? |
Posted: Sun Sep 05, 2021 4:19 am |
|
|
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
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
|
|
Posted: Sun Sep 05, 2021 4:39 am |
|
|
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
|
|
Posted: Sun Sep 05, 2021 4:51 am |
|
|
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 ... lot of thanks to solve my stupid error ... now it works fine
so order is first address and then variable
all the best for you !
-arto- |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Mon Sep 06, 2021 5:13 pm |
|
|
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
|
|
Posted: Tue Sep 14, 2021 6:17 am |
|
|
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
|
|
Posted: Tue Sep 14, 2021 6:22 am |
|
|
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
|
|
Posted: Tue Sep 14, 2021 8:27 am |
|
|
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
|
|
Posted: Tue Sep 14, 2021 9:23 am |
|
|
A pin definition is only a number, and extra brackets round a number
should not matter at all. |
|
|
|