Usefull PIC programming tips for PIC16f877A

This article totally depends on my experiences when I doing some microcontroller projects based on PIC16f877A. Further more following article describe the answers which I found for following questions.

Make RA4(4th bit of PORTA) as a digital output.

All the bits on all the ports in PIC16f877A microcontroller can be configure as digital input or output except RA4. All other bits on ports can be configure as a digital I/O by changing its TRIS register's values and configuring ADCON1 register(for analog input pins). But if we consider about RA4 pin, its behavior different than other I/O pins.
RA4 is an open collector I/O pin. Therefore when we want to use RA4 as a digital output, we have to pull up RA4 by a resistor. This approach can be understand by following example.
This is an example program to use RA4 as a digital output using Hi-Tech C.

#include <htc.h>
#define _XTAL_FREQ 4000000
void main(){
    TRISA = 0;//PORTA is output
   
    //configure ADCON1 to set PORTA as digital I/O
    PCFG0 = 0;
    PCFG1 = 1;
    PCFG2 = 1;
    PCFG3 = 0;
   
    ADON = 0;//ADC modulde is off
   
    while(1){
        RA4 = 1;//Now LED will off
        __delay_ms(100);
        __delay_ms(100);
        __delay_ms(100);
        RA4 = 0;//Now LED will on
        __delay_ms(100);
        __delay_ms(100);
        __delay_ms(100);
    }
}

Finally what we have to do is, supply a current to LED attached to RA4 and change the logic to on and off the LED.

Status on RA4 LED status
1 (High) Off
0 (Low) On

References and further readings-
The best source for understand the behavior of the PIC is "Data sheet".

This is a cropped part from PIC16f877A data sheet.



All the bits on PROTB can be used as digital input except RB3. Because by default the Low-Voltage ICSP Programming (LVP) is enabled in PIC16f877A which lead to disable RB3 I/O function. If you do not use LVP, you can disable LVP by following these steps in MPLAB IDE.

  1. Configure
  2. Configuration bit.
  3. Uncheck Configuration Bits set in code.
  4. Select disable on LVP field.
  5. Close and recompile the code.
Now you can use RB3's input function by changing TRISB's 3rd bit into 1.(TRISB3 = 1)

References and further readings-
The best source for understand the behavior of the PIC is "Data sheet".

  This is a cropped part from PIC16f877A data sheet.
Continue Reading...

Create your own ant build file to create a jar file.


Apache Ant is a build tool mainly focus on Java to build Java applications. Ant uses the xml script file to perform tasks such as,
  • compiling source code, 
  • running software tests, 
  • creating jar files, 
  • creating Javadocs, and 
  • keeping your own file structure within the project folder.
 As I earlier explained Ant uses simple xml file to perform its tasks. Therefore let's see how to write a simple xml file for Ant to compile and create a jar file using Java source classes.
I have to mention here there are 2 kind of jar files,
  1. jar file which can be use as a library to another program.
  2. jar file which can be run (runnable) on the operating system.
Ant can use to either create jar file or runnable jar file. First let's create a jar file(not a runnable jar file).

Structure of the xml file.
Ant xml file includes mainly  three elements,
  1. Property - can be consider as a variable of the xml file.
  2. Target - can be consider as a function in C which can be reuse and modularized.
  3. Task - can be consider as a command on the terminal or C.
Initially let's start with a simple task.

First create a file named 'build.xml' and type following codes.


<project default="final" name="yourProjectName">

</project>

Code explanation........
name in project tag-
Name of your project should be here
default in project tag-
Default target script will start to run(Explained later)
Now create a simple target(function) to execute task(command). As a first step let's echo some text to terminal.


<project default="final" name="yourProjectName">
           <target name="final" description="this is a function to echo some text">
                    <echo message="Hello world..."/>
           </target>
</project>

Code explanation........
name in target tag-
Name of this target(function) this name can be use to recall the target
echo-
Use to print text on terminal
Now run the xml file
  • In terminal,
    • change the directory to directory which include the build.xml file(using 'cd' command) and type ant and press enter.
  • In Eclipse,
    • Right click on source file, select Run as -> Ant Build file
It will print Hello world... on the terminal/console screen.

Now let's consider about two targets (functions).

<project default="final" name="yourProjectName">
           <target name="initial" description="this is a function to echo some text">
                    <echo message="Hello world..."/>
           </target>

          <target name="final" depends="initial" description="this is a function to echo some text">
                    <echo message="Hello world..."/>
           </target>

</project>


Code explanation........
depends in target tag-
This is very important attribute to make our program flow correctly. Here first, program will direct to default target which is defined in project tag. Now program wait until the 'initial' target finish its tasks at 'final' target. ('final' target depends on 'initial' target therefore after finishing 'initial' target's tasks, the target 'final' can be started). Finally program flow will be firstly 'initial' target and then 'final' target.
Now let's try to crate jar file using Ant

Create jar file using ant

In order to create jar file there should be some requirements,
  • Basically, Java source code file(s).
  • compile versions of these Java source files.
  • well understood file structure within the project folder.

Therefore first organize our project folder by creating folders and placing our files into correct locations.

src folder use to place Java source files.
lib folder use to place all necessary external jar files.
build folder use to place temporary class files(compiled files)
dist folder use to place final jar file which is created by Ant


After organizing the project folder, place your build.xml file in the directory which all the above folders included.

Initially, let's compile our Java source files to create class files. To accomplish that the task(command) 'javac' can be used. javac is a task which is defined in Ant(more details can be found at Ant reference manual)

syntax-
<javac srcdir="sourceDir" destdir="destinationDir"/>
srcdir-
full path to the directory which include Java source file(s)
destdir-
full path to the directory, compiled file(s) to be created(destination to create .class files)
Now full code to compile Java source file(s) looks like follow,

<project default="final" name="yourProjectName">
           <target name="final" description="this is a function to compile Java source files">
                    <javac srcdir="sourceDir" destdir="destinationDir"/>
           </target>
</project>


 Code explanation........
Here, javac task simply embed into a target in order to make program more clear. This code can be expect some exception, generating destination directory not found or something like that. To prevent that exception we should create a destination folder(because destination folder does not exist in the project folder)

mkdir task can be used to create a new directory.

<project default="compileMe" name="yourProjectName">
           <target name="compileMe" description="this is a function to compile Java source files">
                     <mkdir dir="destinationDir"/>
                    <javac srcdir="sourceDir" destdir="destinationDir"/>
           </target>
</project>


Code explanation........
mkdir-
make a new directory, name defined by dir attribute

As you can see here, still we did not use any real path for codes. Because the path is depend on user's disk names, disk letter, user names, etc.
The best way to give a path is give it as a relative path to the task. Now property element take place to create relational variable based on paths(locations on the disk). So if we declare an relative variable who is the referrer? OR the variable relative to what?
Therefore we have to give base directory to the program to refer that location and create relative variables for location.

Declare properties and compile Java source file(s)

<project default="compileMe" name="yourProjectName" basedir=".">
    
            <property name="srcDir" location="src"/>
            <property name="buildDir" location="build"/>

           <target name="compileMe" description="this is a function to compile Java source files">
                     <mkdir dir="${buildDir}"/>
                    <javac srcdir="${srcDir}" destdir="${buildDir}"/>
           </target>
</project>


Code explanation........
basedir in project tag-
base directory of all the location property(Here it is '.' that is the directory  itself which save the build.xml file)
name in property tag-
Name of the property(can give any meaningful name)
location in property tag-
Relative location of the disk. As an example, let's say my basedir is '/home/milinda/workspace/AntTest' when Ant assign value to srcDir it concatenate base directory and relative property's location to make absolute path of the directory. Finally srcDir = '/home/milinda/workspace/AntTest/src'
${buildDir}-
This is the way to access to a property in Ant build file(here ${buildDir}=/home/milinda/workspace/AntTest/build and ${srcDir}=/home/milinda/workspace/AntTest/src)

Now this Ant file will run correctly on any directory with the src (java source file directory) directory.


Create jar file using Ant

jar is the task which is defined by Ant to create jar file by using Java class files and other configuration files.

Syntax-
<jar jarfile="destinationDir/jarfileName.jar" basedir="baseOnWhatDir">

destinationDir/jarfileName.jar-
full path of jar file to be created.
basedir-
full path to the directory which compiled file(s) are located.
Now we can create another directory for save the jar file which can be created by using compiled Java files from 'build' directory. Finally code for making jar file is looks like.


<project default="makeJar" name="yourProjectName" basedir=".">
    
            <property name="srcDir" location="src"/>
            <property name="buildDir" location="build"/>
            <property name="distDir" location="dist"/>

           <target name="compileMe" description="this is a function to compile Java source files">
                     <mkdir dir="${buildDir}"/>
                     <javac srcdir="${srcDir}" destdir="${buildDir}"/>
           </target>

           <target name="makeJar" depends="compileMe" description="this is a function to make jar file">
                      <mkdir dir="${distDir}"/>
                      <jar jarfile="${distDir}/finalRelease.jar" basedir="${buildDir}"/>
            </target>
</project>





Continue Reading...

Set custom pictures for slide show in ubuntu desktop wallpapers

Is that possible to change your desktop wallpaper automatically after some specific time period in Ubuntu environment?
Yes, it is.
To do that you should create xml file that contain all the information about,
  • picture files which you going to add for slide show,
  • time duration for each wallpaper can appear,
  • time for transition from one wallpaper to another.
This xml file should be written manually when you want to create custom slide show using your own picture files and your own configurations(such as durations).

Don't worry it is not a difficult thing since I have a script file to create that xml file easily.

Follow these steps to create your own fully customized slide show for Ubuntu desktop wallpaper.

Step 1 - Download my 'Script file' which can generate the xml file automatically.

Step 2 - Copy downloaded script file to folder which contain picture files(jpg or png or bmp format).

Step 3 - Make sure your picture files those you want to add to your slide show and script file is located in the same folder.

Step 4 - Double click on the script file and run it in terminal.
OR
you can use following commands to run script file in terminal
  1. Open terminal (By pressing Alt+Ctrl+T or selecting 'Terminal' from application)
  2. Direct your working directory to folder which contain downloaded script file and picture files.(By using cd command ex:- cd '/home/milinda/Desktop/My show')
  3. Run the script file by using ./Script command.(if any permission problem occur use chmod 700 Script command to set permission of the file before calling ./Script command)
Step 5 - Enter relevant durations when request by the terminal.(firstly time duration for each wallpaper can appear, and secondly time for transition from one wallpaper to another.)

Now you can see xml file which created by the script on the relevant directory. Named background-1.xml.

Step 6 - Right click on the desktop select Change Desktop Background from the pop up menu.

Step 7 - Navigate to Background tab and click on Add.. button.

Step 8 - Open the generated xml file(background-1.xml file).

Now you can see your slide show appear in the preview area. you can set this slide show as your current desktop slide show by clicking on it.

Finally you have your own fully customized slide show for Ubuntu desktop.


Note -
  • Script file involve only for generate the xml file it self. It does not affect to your system.
  • When you want to add picture files or change your picture files which run on slide show, you should respectively copy or delete and just rerun the script file.
Bugs -
  • In order to create xml file, any picture file's name should not include spaces. Therefore make sure any of your picture files does not include space(s).[Bug fixed in 1.1 version you can now download newest version]
  • This script can only handle maximum of 255 picture files. Therefore do not copy more then 255 number of picture files into the folder.

 Downloads
Continue Reading...