Ultrasonic module for PIC

I was searching for a mikroC code written for ultrasonic module HC-SR04 ultrasonic module, but had to write it from scratch. Sharing it.

RB3 pin is used as an input and is connected to echo pin of ultrasonic module  RB4 pin is used as an output and is connected to trig pin of ultrasonic module. PortC is used as a indicator, Connect some LEDs

float temp; //This variable is used for calculations
unsigned cnt; //This variable is used to store the number of TMR0 overflows.

void interrupt() {

if(intcon.t0if) // TMR0 has been overflowed
    cnt++;                    // Interrupt causes cnt to be incremented by 1
INTCON = 0b10100000;   // Clearing flags

void main()
    INTCON = 0b10100000;  //GIE set, TMR0 Overflow Interrupt Set
    //TMR0 Settings
    OPTION_REG.T0CS = 0;
    OPTION_REG.PS2 = 0;
    OPTION_REG.PS1 = 0;
    OPTION_REG.PS0 = 0;

  TRISB.F3 = 1; //RB3 pin is used as an input and is connected to echo pin of ultrasonic module
  TRISB.F4 = 0; //RB4 pin is used as an output and is connected to trig pin of ultrasonic module
  TRISC=0; //PortC is used as a indicator, Connect some LEDs

  PORTB.F4 = 1; //set ultrasonic trig pin to high state
  PORTB.F4 = 0; //clear ultrasonic trig pin to low state
  while(PORTB.F3 == 0); //wait till echo pin gets high.

  cnt = 0;
  TMR0 = 0;  // start the stop watch
  while(PORTB.F3 == 1);     // wait till the echo is off

  temp = cnt*256; //the counter increments at TMR0 overflow(256)
  temp = temp + TMR0; //total time in cycles
  //multiplying with velocity of sound, 
  //But needs to divide by to because the total time is for the signal to hit some object and reflect
  temp = temp * 340 /2;  
  //Oscilator used is 20MHz, Instruction cycle is 20MHz/4 = 5MHz, 
  //To get the time in seconds devide by 5*10^6
  temp = temp *0.2;   
  temp = temp/1000000 ; //By now temp has distance in meters 

  //The following code is just a indicator. For 30cm. 
  if(temp<0.3) PORTC = 255; 
  if(temp>0.3) PORTC = 0; 



Atmel AVR USBASP Programming

IMG_0796I thought to try another microcontroller family, Atmel. Got a USBASP Programmer and messed my first Atmel microcontroller by setting wrong fuses. And after some googling I found that I can’t fix it without HVPP(high voltage parallel programming).

Atmel microcontrollers support 3 programming modes.

  1. ISP (In System Programming)
  2. High Voltage Programming
  3. JTAG programming

USBASP uses ISP method. ISP requires the reset pin. In atmel microcontrollers the reset pin can be configured to be used as a I/O pin by setting a fuse. But if you do that you will need high voltage programming circuit to reprogram the microcontroller.

Also for ISP, once the fuses are set corresponding for a clock source, you are required to provide the same clock source for future programming. Otherwise you will need to use high voltage programming. So be careful when setting the fuses.

Connect the USBASP signals (Reset, SCK, MISO, MOSI, VCC, GND) referring to the pin out in the datasheet. AREF and AVCC pins of the microcontroller should be connected to VCC even if you are not using ADC.

I tried several softwares to use with USBASP. Progisp 1.72 seems to be ok.


Ref – http://getavr.wordpress.com/download/

For Atmega328p using 20MHz crystal set the fuses as follows

CKDIV8  1    //not using internal oscillator anymore
CKOUT   1    //disable clock out on PB0
SUT1    1    //selecting start-up time
SUT0    1    //selecting start-up time
CKSEL3  0    //selecting clock source
CKSEL2  1    //selecting clock source
CKSEL1  1    //selecting clock source
CKSEL0  1    //selecting start-up time

For fuses “1” means unprogrammed while “0” means programmed.
Ref – http://www.atmel.com/Images/doc8271.pdf

Comment if you have problems.

Verilog HDL

Hardware descriptive languages are useful when describing circuits consisting thousands or more transistors (LSI, VLSI integration). As we can describe a sequential program with C, hardware can be described with a hdl.

Design Process of Hardware

  • Abstraction of the intended design
  • Schematics and hardware description


  • Simulation
  • Synthesize a netlist
  • Input the synthesized netlist to the tools that

build the final device.

Verilog Simulation tools

  • Xilinx (Windows)
  • Icarus Verilog (Linux)

Hello World!

module main;
$display(“Hello, World”);
$finish ;

Compiling using iverilog (icarus verilog)
% iverilog -o hello hello.v

% vvp hello
Hello, World

Examples can be found in following links,



I didn’t know about Rainmeter until I saw awesome customized desktop of a friend of mine. Rainmeter is very light and open source. This screenshot is of my desktop (yeah mine is simple 😛 ). Google “rainmeter themes” to find more. Only problems I have are that I can’t find a Start menu with search option to replace the Windows 7 Start menu and I can’t get real-time previews from rainmeter task bar. I’m currently searching for a solution and trying with Emerge Desktop which is also light and open source 😛 .


Screenshot 2

USBPICPROG – Replaced MOSFETs with locally available transistors

I get 12V VPP and tested with 16F877A. Let me know if there’s any other modifications necessary.

Click Here – USBPICPROG Modified Schematic WW

BC847, BS170 replaced with BC547.

TSM2301CX replaced with BC557.

USBPICPROG – An open source PIC programmer

My old PIC programmer supports Windows XP only. I was thinking to buy a Windows 7 supported programmer before I came across usbpicprog. It supports Linux, Mac OS and Windows. So I built it on a vero board. I had to replace some SMD components in the original circuit design with components locally available. I replaced BC847 with BC547. And TSM2301cx with BC557. Then I programmed the bootloader to 18F2550 using my old programmer. Connected usbpicprog. Windows recognized the programmer. I programmed the firmware using usbpicprog software. Then I tried to test it with 16F877A but failed. The software didn’t detect the microcontroller and couldn’t read or write to it. I used IOTEST provided with the software but I didn’t get 12V or 5V VPP . Checked everything and all seemed to be correct. So I thought to replace components in VPP circuit. Replaced BS170 with BC547 since I didn’t had any extra BS170. And boom, everything works perfectly 😀 .

Click here to check the modified usbpicprog schematic.


Arch Linux

Arch linux “A simple lightweight linux distribution” simply provides you a command processor, no useless applications, not even a desktop environment. Installing a desktop environment is your choice. Extremely customizable. For a beginner I recommend using a virtual machine software, otherwise when you get stuck you will have to use another PC to find answers. I myself is a  newbie to linux. But starting with Arch linux was entertaining.


Fig 1. Boot Arch linux from a disc