easyavr.h Header file for pin/port operations

Here I am sharing a header file called easyavr.h. This is a pretty basic header file with some helper functions related to input output pins of AVR

#ifndef __EASYAVR_H_
#define __EASYAVR_H_


//  Sets pin of port to 1
//
//  Example for PD2: PIN_ON(PORTD, 2)
#define PIN_ON(port,pin) ((port) |= (1 << (pin)))


//  Sets pin of port to 0
//
//  Example for PD2: PIN_OFF(PORTD, 2)
#define PIN_OFF(port,pin) ((port) &= ~(1 << (pin)))


//  Sets pin of port to value
//
//  Example for PD2: PIN_SET(PORTD, 2, 1)
#define PIN_SET(port,pin,val) (((val) > 0) ? PIN_ON((port),(pin)) : PIN_OFF((port),(pin)))

//  Sets all of port pins to OUTPUT mode
//
//  Example for PORTD: PORT_AS_OUTPUT(PORTD)
#define PORT_AS_OUTPUT(port) ((port) = 0xFF)

//  Sets all of port pins to INPUT mode
//
//  Example for PORTD: PORT_AS_INPUT(PORTD)
#define PORT_AS_INPUT(port) ((port) = 0x00)

//  Sets pin of port to OUTPUT mode
//
//  Example for PD1: PORT_AS_OUTPUT(DDRD, 1)
#define PIN_AS_OUTPUT(ddr,pin) ((ddr) |= (1 << (pin)))


//  Sets pin of port to INPUT mode
//
//  Example for PD1: PORT_AS_INPUT(DDRD, 1)
#define PIN_AS_INPUT(ddr,pin) ((ddr) &= ~(1 << (pin)))

//  Checks pin's value of port
//  Returns 1 or 0
//
//  Example for PD2: CHECK_PIN(PIND, 2)
#define CHECK_PIN(pinreg,pin) (((pinreg) & (1 << (pin))) != 0)


#endif
 

convert a decimal value to its binary representation ( string )

Wanted to share this snippet of code which I used to display a decimal numbers binary representation. It is quite self explanatory and easy to understand.

/**
  * Turns a decimal value to its binary representation
  */
char* dec2binWzerofill(unsigned long Dec, unsigned int bitLength){
    return dec2binWcharfill(Dec, bitLength, '0');
}

char* dec2binWcharfill(unsigned long Dec, unsigned int bitLength, char fill){
  static char bin[64];
  unsigned int i=0;
unsigned int j;
  while (Dec > 0) {
    bin[32+i++] = ((Dec & 1) > 0) ? '1' : fill;
    Dec = Dec >> 1;
  }

  for ( j = 0; j< bitLength; j++) {
    if (j >= bitLength - i) {
      bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];
    }else {
      bin[j] = fill;
    }
  }
  bin[bitLength] = '';

  return bin;
}
 

TCP connection over GPRS using SIM900 and similar modems using AT commands

GSM/GPRS modems are getting very common these days, as prices are getting cheaper and cheaper. Apart from providing SMS and call functions to my projects I also wanted to communicate via TCP.

Although there are many documents and blog posts to help but I have always found that they either are answers to specific problem faced by someone or not providing complete details.  In this post I would first explain the AT commands used in brief. You may connect your SIM900 to your computer via a serial/usb and test these commands. In the later part of this post I would include arduino example code.

AT commands for TCP/UDP Connection with example response and a brief description are given in the table below. Refer to the AT commands manual of your modem for details

AT command Response Description
AT  OK test command. reply is OK
AT+CGATT?  +CGATT:n checks if GPRS is attached? n=1 if attached
AT+CIPMUX=n  OK use n as 0 for single connection
or use 1 for multiple connections
AT+CSTT=”apn”,”username”,”pass” OK Sets APN, user name and password
AT+CIICR  OK Brings up wireless connection
AT+CIFSR  ip address Get local IP address if connected
AT+CIPSTART=“TYPE” , “domain”, “port”  Connected Establishes a connection with a server. Type can be UDP or TCP
AT+CIPSEND  > Sends data when the a connection is established.
AT+CIPCLOSE  OK Closes the connection
AT+CIPSHUT  SHUT OK resets IP session if any

how to make a connection:

  1. Send ATr and wait for a response from the modem. You should recieve OK
    if everything is set.
  2. Make sure that the Modem has registered to network and that PIN code is disabled on the SIM. Send AT+CGATT?r to check if GPRS is attached or not.  +CGATT: 1 indicates that GPRS is attached.
  3. Send AT+CIPSHUTr . Although its optional this will be helpful as it resets IP session if any. you will get a response SHUT OK .
  4. Send AT+CIPMUX=0 to set a single connection mode, response would be OK
  5. Now set APN settings by AT+CSTT= “apn ”, “username”, “password”r . replace apn, username and password to match APN (Access Point Name) ,username and password for your service provider.
  6. Now send AT+CIICRr , this will bring up the wireless connection. OK is received on successful connection
  7. Send AT+CIFSRr , this will reply with the IP address the modem has been assigned.
  8. Send AT+CIPSTART=”TCP”,”server domain name or ip”,”port”r, replace the domain name/ip and port with appropriate values, on connection modem will reply with CONNECT OK
  9. Now you can send your data using AT+CIPSENDr  AT command. modem will respond with > indicating it is ready to receive data to be sent. Type in your data.
  10. Now the modem is waiting for the ASCII 26  that is control+z on keyboard. Depending on the terminal software used you can either press control and Z together on keyboard or send hex value 0x1A. The modem will then send the response from server.
  11. Now send AT+CIPSHUT to shut down the connection. Modem will reply with SHUT OK 
  12. cheers 🙂

ARDUINO CODE :

Below is example code for single and multiple connection using arduino and sim900

int8_t answer;
int onModulePin= 2;
char aux_str[50];
char ip_data[40]="Test string from GPRS shieldrn";
void setup(){
    pinMode(onModulePin, OUTPUT);
    Serial.begin(115200);
    Serial.println("Starting...");
    power_on();
    delay(3000);
    // sets the PIN code
    sendATcommand2("AT+CPIN=****", "OK", "ERROR", 2000);
    delay(3000);
    Serial.println("Connecting to the network...");
    while( sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 1000)== 0 );
}
void loop(){
    // Selects Single-connection mode
    if (sendATcommand2("AT+CIPMUX=0", "OK", "ERROR", 1000) == 1)
    {
        // Waits for status IP INITIAL
        while(sendATcommand2("AT+CIPSTATUS", "INITIAL", "", 500)  == 0 );
        delay(5000);

        // Sets the APN, user name and password
        if (sendATcommand2("AT+CSTT="APN","user_name","password"", "OK",  "ERROR", 30000) == 1)
        {
            // Waits for status IP START
            while(sendATcommand2("AT+CIPSTATUS", "START", "", 500)  == 0 );
            delay(5000);

            // Brings Up Wireless Connection
            if (sendATcommand2("AT+CIICR", "OK", "ERROR", 30000) == 1)
            {
                // Waits for status IP GPRSACT
                while(sendATcommand2("AT+CIPSTATUS", "GPRSACT", "", 500)  == 0 );
                delay(5000);

                // Gets Local IP Address
                if (sendATcommand2("AT+CIFSR", ".", "ERROR", 10000) == 1)
                {
                    // Waits for status IP STATUS
                    while(sendATcommand2("AT+CIPSTATUS", "IP STATUS", "", 500)  == 0 );
                    delay(5000);
                    Serial.println("Openning TCP");

                    // Opens a TCP socket
                    if (sendATcommand2("AT+CIPSTART="TCP","IP_address","port"",
                            "CONNECT OK", "CONNECT FAIL", 30000) == 1)
                    {
                        Serial.println("Connected");

                        // Sends some data to the TCP socket
                        sprintf(aux_str,"AT+CIPSEND=%d", strlen(ip_data));
                        if (sendATcommand2(aux_str, ">", "ERROR", 10000) == 1)
                        {
                            sendATcommand2(ip_data, "SEND OK", "ERROR", 10000);
                        }

                        // Closes the socket
                        sendATcommand2("AT+CIPCLOSE", "CLOSE OK", "ERROR", 10000);
                    }
                    else
                    {
                        Serial.println("Error openning the connection");
                    }
                }
                else
                {
                    Serial.println("Error getting the IP address");
                }
            }
            else
            {
                Serial.println("Error bring up wireless connection");
            }
        }
        else
        {
            Serial.println("Error setting the APN");
        }
    }
    else
    {
        Serial.println("Error setting the single connection");
    }

    sendATcommand2("AT+CIPSHUT", "OK", "ERROR", 10000);
    delay(10000);
}

void power_on(){

    uint8_t answer=0;

    // checks if the module is started
    answer = sendATcommand2("AT", "OK", "OK", 2000);
    if (answer == 0)
    {
        // power on pulse
        digitalWrite(onModulePin,HIGH);
        delay(3000);
        digitalWrite(onModulePin,LOW);

        // waits for an answer from the module
        while(answer == 0){     // Send AT every two seconds and wait for the answer
            answer = sendATcommand2("AT", "OK", "OK", 2000);
        }
    }

}

int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
        char* expected_answer2, unsigned int timeout){

    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;

    memset(response, '', 100);    // Initialize the string

    delay(100);

    while( Serial.available() > 0) Serial.read();    // Clean the input buffer

    Serial.println(ATcommand);    // Send the AT command

    x = 0;
    previous = millis();

    // this loop waits for the answer
    do{
        // if there are data in the UART input buffer, reads it and checks for the asnwer
        if(Serial.available() != 0){
            response[x] = Serial.read();
            x++;
            // check if the desired answer 1  is in the response of the module
            if (strstr(response, expected_answer1) != NULL)
            {
                answer = 1;
            }
            // check if the desired answer 2 is in the response of the module
            else if (strstr(response, expected_answer2) != NULL)
            {
                answer = 2;
            }
        }
    }
    // Waits for the asnwer with time out
    while((answer == 0) && ((millis() - previous) < timeout));

    return answer;
}
int8_t answer;
int onModulePin= 2;
char aux_str[50];

char ip_data[40]="Test string from GPRS shieldrn";

void setup(){

    pinMode(onModulePin, OUTPUT);
    Serial.begin(115200);

    Serial.println("Starting...");
    power_on();

    delay(3000);

    // sets the PIN code
    sendATcommand2("AT+CPIN=****", "OK", "ERROR", 2000);

    delay(3000);

    Serial.println("Connecting to the network...");

    while( sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 1000) == 0 );

}


void loop(){


    // Selects Multi-connection mode
    if (sendATcommand2("AT+CIPMUX=1", "OK", "ERROR", 1000) == 1)
    {
        // Waits for status IP INITIAL
        while(sendATcommand2("AT+CIPSTATUS", "INITIAL", "", 500)  == 0 );
        delay(5000);

        // Sets the APN, user name and password
        if (sendATcommand2("AT+CSTT="APN","user_name","password"", "OK",  "ERROR", 30000) == 1)
        {
            // Waits for status IP START
            while(sendATcommand2("AT+CIPSTATUS", "START", "", 500)  == 0 );
            delay(5000);

            // Brings Up Wireless Connection
            if (sendATcommand2("AT+CIICR", "OK", "ERROR", 30000) == 1)
            {
                // Waits for status IP GPRSACT
                while(sendATcommand2("AT+CIPSTATUS", "GPRSACT", "", 500)  == 0 );
                delay(5000);

                // Gets Local IP Address
                if (sendATcommand2("AT+CIFSR", ".", "ERROR", 10000) == 1)
                {
                    // Waits for status IP STATUS
                    while(sendATcommand2("AT+CIPSTATUS", "IP STATUS", "", 500)  == 0 );
                    delay(5000);
                    Serial.println("Openning TCP");

                    // Opens a TCP socket with connection 1
                    if (sendATcommand2("AT+CIPSTART=1,"TCP","IP_address","port"",
                                    "CONNECT OK", "CONNECT FAIL", 30000) == 1)
                    {
                        Serial.println("Connected");

                        // Sends some data to the TCP socket
                        sprintf(aux_str,"AT+CIPSEND=1,%d", strlen(ip_data));
                        if (sendATcommand2(aux_str, ">", "ERROR", 10000) == 1)
                        {
                            delay(500);
                            sendATcommand2(ip_data, "SEND OK", "ERROR", 10000);
                        }

                        // Closes the socket
                        sendATcommand2("AT+CIPCLOSE=1", "CLOSE OK", "ERROR", 10000);
                    }
                    else
                    {
                        Serial.println("Error openning the connection 1");
                    }

                }
                else
                {
                    Serial.println("Error getting the IP address");
                }
            }
            else
            {
                Serial.println("Error bring up wireless connection");
            }
        }
        else
        {
            Serial.println("Error setting the APN");
        }
    }
    else
    {
        Serial.println("Error setting the multi-connection");
    }

    sendATcommand2("AT+CIPSHUT", "OK", "ERROR", 10000);
    delay(10000);
}

void power_on(){

    uint8_t answer=0;

    // checks if the module is started
    answer = sendATcommand2("AT", "OK", "OK", 2000);
    if (answer == 0)
    {
        // power on pulse
        digitalWrite(onModulePin,HIGH);
        delay(3000);
        digitalWrite(onModulePin,LOW);

        // waits for an answer from the module
        while(answer == 0){     // Send AT every two seconds and wait for the answer
            answer = sendATcommand2("AT", "OK", "OK", 2000);
        }
    }

}

int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
        char* expected_answer2, unsigned int timeout){

    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;

    memset(response, '', 100);    // Initialize the string

    delay(100);

    while( Serial.available() > 0) Serial.read();    // Clean the input buffer

    Serial.println(ATcommand);    // Send the AT command

    x = 0;
    previous = millis();

    // this loop waits for the answer
    do{
        // if there are data in the UART input buffer, reads it and checks for the asnwer
        if(Serial.available() != 0){
            response[x] = Serial.read();
            x++;
            // check if the desired answer 1  is in the response of the module
            if (strstr(response, expected_answer1) != NULL)
            {
                answer = 1;
            }
            // check if the desired answer 2 is in the response of the module
            else if (strstr(response, expected_answer2) != NULL)
            {
                answer = 2;
            }
        }
    }
    // Waits for the asnwer with time out
    while((answer == 0) && ((millis() - previous) < timeout));

    return answer;
}

 

make your android app boot at device start-up

If you want your app to start-up automatically when Android boots up you need to the following

#1: add the following to your android manifest file


<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

view raw

gistfile1.xml

hosted with ❤ by GitHub

This will register for a boot complete receiver event and ask for its permission.

#2 Add a the following java class


public class BootUpReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}

 

Converting UNIX timestamp to seconds, minutes, hours, days, weeks, month, years, decades ago

If you have UNIX time-stamps and you want convert it to show how may seconds, minutes, hours, weeks, months, years or decades ago it is from now you may use the following snippet of code. It accepts UNIX time-stamp in long format, In case you are getting UNIX time-stamps in String format you have to convert it to long. It will return a string with either seconds, minutes , hours , days, weeks, months, years , or decades ago. The code is written in java for an android app but can be ported to any language. Here is the code which is also available on github.


public static String caluculateTimeAgo(long timeStamp) {
long timeDiffernce;
long unixTime = System.currentTimeMillis() / 1000L; //get current time in seconds.
int j;
String[] periods = {"s", "m", "h", "d", "w", "m", "y", "d"};
// you may choose to write full time intervals like seconds, minutes, days and so on
double[] lengths = {60, 60, 24, 7, 4.35, 12, 10};
timeDiffernce = unixTime – timeStamp;
String tense = "ago";
for (j = 0; timeDiffernce >= lengths[j] && j < lengths.length – 1; j++) {
timeDiffernce /= lengths[j];
}
return timeDiffernce + periods[j] + " " + tense;
}

 

 

 

Soft-Uart Tx only using software delays only

I was working on a code for a module on my GSM gateway today , for which I had given 1 pin of micro-controller to use as software Tx. Software UARTs usually uses timers to make them robust, but I had already used them all. So I decided to write a code using software delays.

The UART logic is inverted , so to send logic 1 you have to send low signal  and vice versa . Here is my code , hope it might help someone else.

/*
* soft-uart Tx only without any timmer uses software delays
* the baud rate depends on the delay in us , here I am using
* 4800 with a 1 start bit, 8 databits and 1 stop bit
* if you wanna change the baud rate calculate it by 1/baud and
* modify the _delay_us();
*
* Created: 11/21/2012 1:52:37 PM
* Author: AbuUmar
*/
#include <avr/io.h>
#include <util/delay.h>
#define portlow PORTC&=~0x01
#define porthigh PORTC|=0x01
void putchar_soft(char data_soft)
{
 char bit_count=10; // 1+8+1SB
 data_soft=~data_soft;
 char secc=1;char0:
 if (secc=1)
 portlow;
 else
 porthigh;
 _delay_us(208);
 //_delay_us(208);
 for ( char i = 0; i < 8; i++ ) {
 if(data_soft & 1)
 portlow;
 else
 porthigh;
 data_soft=data_soft>>1;
 _delay_us(208);
 }
 porthigh;
 _delay_us(208);
 _delay_us(208);
 return;
}
int main(void)
{
 DDRC|=0b00000001;
 porthigh;
char inte=0;
while(1)
 {
 // example use, initializing a var to 0 and sending the data
 // with 1 sec delays
 putchar_soft(inte) ;
 _delay_ms(250);
 _delay_ms(250);
 _delay_ms(250);
 _delay_ms(250);
 inte++;
 }
}
 

writing and reading from AVR EEPROM in Block.

EEPROM can be used to store non volatile data of the program , sometimes you need to write arrays even multidimensional. The way I do it is by using EEMEM attribute. EMMEM is used to allocate space in EEPROM.

I use the Macros given below to write or read to EEPROM. you have to use #include I would be precise . below is the Code.

#include

//////////////////////////////////////////////////////////////////////////
//        Macros and # Defines
//write block to EEPROM
#define eepw(message,EEADDR,BLKSIZE) eeprom_write_block((const void*)message,(void*)EEADDR,BLKSIZE);
//read block from EEPROM
#define eepr(readblck,EEADDR,BLKSIZE) eeprom_read_block((void*)readblck,(const void*)EEADDR,BLKSIZE);

uint8_t EEMEM eepstring[15];

Example use

eepw("sample test 1",eestring, 15);  // "writes sample test1" to eestring in EEprom ,
char d[15];   //array in ram
eeprom_read(d, eestring[0],15); // reads the data and puts it in d[]

 

Disable PIN code using Gsm modem AT commands

If you have a PIN code enabled SIM card and want to remove /disable PIN code using AT commands follow these commands,
suppose 9546 is the current PIN code , Replace 9546 with your PIN code, >>> shows the response from modem.

AT+CPIN?
>>> +CPIN: SIM PIN // pin codes need to be entered
>>> OK

AT+CPIN="9546"
>>>; OK

AT+CLCK="SC",0,"9546" // disable pin code
>>> OK

AT+CPIN?
>>> +CPIN: READY

 

 

Keeping track of time in embedded applications: millis();

Its very handy to keep track of time in embedded programs. In this post I will implement a function called millis() which can be used to track time.  Arduino users will be familiar with this one. I would be doing it for AVR MCUs you can easily port it for others. this function returns the number of milliseconds since the MCU began running the current program. This number will overflow (go back to zero), after approximately 50 days.
It uses a hardware timer , in this post i will use timer0 . The first step is to initialize timer0 and interupts. lets start.

void timer0(){
  // To set clock:
  // 1MHZ is 1,000,000 ticks per second
  // 1000 milli in 1 second
  // xMHZ = 1000millis
  // so MHZ/millis gives # HZ per millis
  // (HZ/millis)/prescaler= Top counter number

  // EG:for 8MHZ clock
  // 8000000/1000
  // 8000.0000000000
  // 8000/256
  // 31.2500000000 TOP counter

  //set CTC (clear timer on compare match mode)
  TCCR0A = (1< <WGM01);
  //sets prescaler clkIO/256  ***THIS MIGHT CAUSE ISSUES SETS FOR ALL CLOCKS**!!!!
  TCCR0B = (1<<CS02);
  //sets interrupt enable for OCF0A (TIMER_COMPA_vect)
  TIMSK0 = (1<<OCIE0A);
  //sets TOP counter match A at 31
  OCR0A = 31;
}


volatile uint32_t millis()
{
 uint32_t mill;
 uint8_t oldSREG = SREG;
 // remember last value of interrupts
 // disable interrupts while we read timer0_millis or we might get an
 // inconsistent value (e.g. in the middle of a write to timer0_millis)
 cli();
 mill = millis_count;
 SREG = oldSREG; // rewrite reg value to turn back on interrupts
 return mill;
}

In the code shown above we have initialized timer/counter 0 to make an interrupt after every millisecond. Next we have to update our millisecond count.

//interrupt declaration
ISR(TIMER0_COMPA_vect)
{
  ++millis_count;
  //OCR0A = 10; //sets upper breakpoint A
}

That’s it. Lets see how to use it! First we copy the current value in milis() to a variable.

uint32_t starttime=millis();

and later we compare the new values with the start value. Here’s an example of a 25 second.

if(millis()-starttime > 25000)
{
  // some code here
}

Note that the parameter for millis() is an unsigned long, errors may be generated if a programmer tries to do math with other datatypes such as ints.

There are a number of ways you can use this. Hope this post will help you

 

Add Voice /Audio to projects using WTV020SD

Adding sound to your projects is great, there can be several methods to do it. In this post i will show how we can use wtv020-sd module to get this task done. I wont go in detailed description of the module and will keep the post short and to the point. The module can be operated in a number of modes including pushbutton modes but the one we are going for is the 3 wire serial mode. its actually SDA,SCL and reset wires that we use. The module would play back the ad4 files stored on uSD Card. and spk+ and spk- pins can be connected directly to a speaker. More details about the module can be found in the datasheet and the webpage link.

The interface is simple. you only need to connect Supply , DI ,CLK and reset Pins to get it working although you may also connect some other pins also but this is what is really needed to get the module working.

connections …