Jump to content
43oh

superpanky

Members
  • Content Count

    2
  • Joined

  • Last visited

Posts posted by superpanky

  1. Maybe someone can try to tell, what could be wrong in these few lines? I don't understand, how these constructions with pointers and "unsigned char/int" datatypes do work.


    int pos=0;
    int counter=0;
    int p=0;

    void loop() {

                Flash.read(flash+(pos * sizeof(int)), (unsigned char*)&p, sizeof(int));

    counter=p;

                            if (.............) {
                              counter=++counter;
                              } else {
                               counter=--counter;

                             }

                 Flash.erase(flash);

                 Flash.write(flash+(pos * sizeof(int)), (unsigned char*)&counter, sizeof(int));

    }

     

    Regards, Superpanky

  2. Hello.
    I have wrote an Energia program for digital potentiometer control. I want to save variable "counter" (it is an integer number from 0-255) in non-volatile memory using mspflash.h library in case of reset or power interrupt. But it doesn't saves.
    I am allmost sure there is something wrong with initialization of variables or pointers inside my code. Can anyone help? Any improvements in code?

    My code:

    #include <SPI.h>
    #include "MspFlash.h"

    #define outputA P2_1
    #define outputB P2_2

    #define flash SEGMENT_D

     int aState;
     int aLastState;  

     const int slaveSelectPin = SS;
     const int shutDownPin = P1_4;

    int pos=0;
    int counter=0;
    int p=0;

    void setup() {

        pinMode (outputA,INPUT_PULLUP);
        pinMode (outputB,INPUT_PULLUP);
        aLastState = digitalRead(outputA);
     
      pinMode (slaveSelectPin, OUTPUT);
      pinMode (shutDownPin, OUTPUT);
        SPI.begin();
      digitalWrite(shutDownPin, HIGH);
      digitalPotWrite(1, 0);
    }

    void loop() {
     
    Flash.read(flash+(pos * sizeof(int)), (unsigned char*)&p, sizeof(int));

    counter=p;

          aState = digitalRead(outputA);
       if (aState != aLastState){     
         if (digitalRead(outputB) != aState) {
            counter=++counter;
         } else {
            counter=--counter;
         }
       }
       if (counter <= 255) {
       if (counter < 0) {
       digitalPotWrite(1, 0);
       counter = 0;
       Flash.erase(flash);
       Flash.write(flash+(pos * sizeof(int)), (unsigned char*)&counter, sizeof(int));
       }
       else {
       digitalPotWrite(1, counter);
       Flash.erase(flash);
       Flash.write(flash+(pos * sizeof(int)), (unsigned char*)&counter, sizeof(int));
       }
       }
        if (counter > 255) {    
       digitalPotWrite(1, 255);
       counter = 255;
       Flash.erase(flash);
       Flash.write(flash+(pos * sizeof(int)), (unsigned char*)&counter, sizeof(int));  
       }
           aLastState = aState;
    }
    int digitalPotWrite(int address, int value) {
      digitalWrite(slaveSelectPin,LOW);
      SPI.transfer(address);
      SPI.transfer(value);
      digitalWrite(slaveSelectPin,HIGH);
    }

     

    Regards, Superpanky

     

×
×
  • Create New...