• Category Archives Programming
  • Early version of Arduino 1992

    For a future Arduino museum:

    This is one of the earliest developed arduinos dated 1992 based on a 8032 Micro controller. At that time the credit card controller was new and very expensive. I had gold in my hands but didn’t realize it.  Almost 20 years later an Italian engineer did recognize it and developed the first Arduino.  I want to say thanks to him and the Chinese production industry to allow me and many others work with this new type of LEGO. The LEGO of today keeps many engineers busy being creative and constructive doing the inventions of the future.

    Unfortunately for me I never recognized the potential of my design back in 1992. Call it stupid if you want, but it made me continue with inventing, designing and making.

    It had the following functions:

    • – Language used “C”, compiled with a Keil C compiler
    • – A boot loader loading the same hex files as today.
    • – 32K RAM data +program memory, 8K boot ROM,
    • – A Power LED
    • – RS232, Serial upload port
    • – A shield interface 8 x IO ports,
    • – Size Credit Card.
    • – PCB home made

    Later I decided to do it all over again but now better Goosduino 2.0. It is basically a PLC (Programmable  Logic Controller) with all interfaces integrated in one system. The enclosure is DIN rail base and easy to integrate in to existing DIN rail cabinets.

    The PLC has many functions and doesn’t need a shield. It’s for sale now.

    Availability Fig 1 : 1992, one of the first Credit Card Controllers
    Availability Fig 2 : 2015, An upgrade of and old idea.

  • TFT display with CLI Command Line Interface

    There is wide range of TFT displays available for ATmega processors. Displays distinguish them selves by resolution, dimensions and serie or parallel interfaces.

    Availability Fig 1 : TFT energy monitoring display

    For an ATmega328 processors an 8bit parallel display consumes all IO pins except the Series TTL interface and one Analog IO. The good thing is that with this processor its possible to build a relative simple low volume general purpose TFT display.

    Many controller interfaces require a combination of digital and Analog IOs interfacing to various sensors and a reasonable sized display to display results and status of the  sensors. By having a display with a Txt Rx communication interface space is saved on the IOS needed to control external sensors.

    The display on the right is a 3.5 Inch 320 x 480 resolution ILI9488 display controlled by a ATmega328 processor. The code exist out the following functions

    • The screen layout software
    • A Command Line Interpreter (CLI)
    • A real time clock.

    The CLI is responsible for reading values from the TTL Rx Tx interface and displaying them on the TFT screen. The screen is grouped in Battery, Grid/Net and Photovotaic related values. It can read all parameters defined on the TFT screen like:

    • Battery : voltage current, SoC, battery cycle count, battery health values
    • Grid/Net :  voltage current, power energy values and power factor
    • Photovotaic : voltage current, power Energy, panel temperature values 
    • Time and date values.

    The TFT libary used to implement the program consumes a reasonable percentage of all program memory on a ATmega 328 but after finalizing the code sufficient program and RAM space is left over for some extra functions. The code uses 27.7K program space and around 1.1K RAM code.

    For the ones interested below the basic code for the CLI copy it in your sketch and have fun.

    #define CR         0x0D	                // \r
    #define LF         0x0A                 // \n
    #define SPACE      0x20                 // " "
    #define OPT        0x25                 // %
    
    
    typedef struct cmds {
    	String      str;
    	void       (*pt2function)(char);
    } 	commands;
    
    	commands    cmd[] = {{"",},{"settm",settm}, {"batt",batt}, {"net",net}, {"pv",pv} };
    
    void readstream(byte state){
    uint8_t	n=1;
    	if (Serial.available()!=0){
    		while (Serial.peek()==LF || Serial.peek()==CR || Serial.peek()==SPACE) Serial.read();   // Read all Cr, Lf,spaces out of the stream until first characters 
    		cmd[0].str=Serial.readStringUntil(SPACE);                                               // Read Command string until first space
    		while( cmd[0].str!=cmd[n].str && n<=sizeof(cmd)/sizeof(cmd[0]))  n++;                   // Find Command in the cmd array list.
    		if (n>sizeof(cmd)/sizeof(cmd[0])) return;                                               // If command not found, terminate the cammand processing 
    		while ((Serial.available()!=0 && (Serial.peek()!=LF || Serial.peek()!=CR ))) {          // Check stream has no Cr/Lf characters 
    			if (Serial.read()==OPT) cmd[n].pt2function(Serial.read());                          // If stream is Option sign Call function pointed by cmd[0] and read option parameter
    			else Serial.read();                                                                 // IF not option characters read stream
    		}
    	}
    }
    
    void settm(char par){
    int8_t  n;
    	switch (par){
    		case 'T' :{ for (n=2; n>=0; n--) *(&RTC.time.sec+n)=Serial.parseInt();	}               // read three times data from the stream to the time struct sec, min, hr.
    		break;
    		case 'D' :{ for (n=0; n<=2; n++) *(&RTC.time.mday+n)=Serial.parseInt()%100; }            // read three times data from the stream to the time struct date, month, year (years needs MOD 100
    		break;
    		case 'C' :{ RTC.derror=RTC.derror+Serial.parseFloat()/24; }
    		break;
    	     
    	}
    }
    

    Examples of commands are : Set the time : settm %T 06:05:10 %D 11/12/1962
    Other commands are but not seen in this code : batt %V 12.56 %I 23.25 %E 2534.1
    net and PV


  • Read and Write to EEPROM memory

    I was programming for a CFE network monitor to check on the quality of the electricity grid in Mexico

    This program and hardware can start up generators and electricity backups in case the grid is out of specifications or specified electric limits. It also will reconnect to the grid under controlled conditions. So this Grid Guard protects your house or business form mall functioning grid conditions.

    The “grid guard” has a display showing voltage current, Power, Energy, Cos phi, frequency and network status. More options are possible but that is planned for future releases.

    One of the functions I needed was to secure data in case of disconnecting or changing the software in the Grid guard. To do this use the EEProm memory. The EEPROM.h library doesn’t have a general function that can write and update any desired variables to EEPROM. I wrote the two functions for this reason and you can use them without including the EEPROM.h libraries.

    After compiling by using the arduino IDE it indicated that the two functions use 444 byte of program memory and 9 byte of RAM.

    For the ones interested copy and past the code in your sketch and have fun.

    //******************************************************************************************//
    // Code is tested on a ATmega 328 micro controller.                                         //
    // This function is independent of data type. It copies all standard types but also string  //
    // and structs. This function reads [n] bytes from RAM pointed by ptr and writes them to    //
    // EEmemory at location EEaddress. This function checks if EEmemory contains the value to be// 
    // written. If it has it skips the programming section and runs for the next byte.          //
    //******************************************************************************************//
    
    void MEM2EE (unsigned int EEaddress, byte *ptr, unsigned int n) {
        n=n&0xFF    ;                        //safety statement can be removed
        while (n != 0) {
            while(EECR & (1<<EEPE));         // Wait for completion of previous write 
            EEAR  = EEaddress;               // Set up address register 
            EECR |= (1<<EERE);               // Start eeprom read by writing EERE 
            if (EEDR != *ptr) {              // skip write if ptr value is equal to EEmem
                EEDR  = *ptr;                // load data register with pointed byte
                EECR |= (1<<EEMPE);          // Write logical one to EEMPE
                EECR |= (1<<EEPE);           // Start eeprom write by setting EEPE 
            }
    //      else Serial.println("EQD");      // test if data ptr and EEmem equal print msg
            ptr++;
            EEaddress++;
            n--;
        } 
    }
     
    //******************************************************************************************//
    // This function is independent of data type. It copies all standard types but also string  //
    // and structs. This function reads n bytes from EEaddress in EEmemory and writes these to  //
    // pointed location ptr in RAM.                                                                 //              
    //******************************************************************************************//
     
    void EE2MEM (unsigned int EEaddress, byte *ptr, unsigned int n) {
         while (n != 0) {
                while(EECR & (1<<EEPE));       // Wait for completion of previous write
                EEAR  = EEaddress;             // Set up address register
                EECR |= (1<<EERE);             // Start eeprom read by writing EERE
                *ptr  = EEDR;                  // Assign EEDR data to memory location
                ptr++;
                EEaddress++;
                n--;
         } 
    }
    

    Examples of how to use these functions.
    Assume you want to write to EEPROM memory addresses 540 and 542
    You have the following variables :
    float fvar1=3.1416, fvar2;
    byte bvar1=66, bvar2;
    These are the calls MEM2EE( 540, (byte*)&bvar1, sizeof(bvar1));
    EE2MEM( 540, (byte*)&bvar2, sizeof(bvar2)); Serial.println(bvar2);
    MEM2EE( 542, (byte*)&fvar1, sizeof(fvar1));
    EE2MEM( 542, (byte*)&fvar2, sizeof(fvar2));
    Serial.println(fvar2);

    Your can write and read any variable, array or struct in this way to and from EEPROM memory

    Write a comment if you like it or want to contribute to this code.