Freelancer

Thursday, March 13, 2014

Simple Counter with PIC16F877A

Config Proteus like this
7 Segments leds counter pic16f877a
And here is the source code
;; -----------------------------------------------------------------------------
;; File name: 7SegmentsLedx4_Counter.asm
;; Email: dmtmd2010@yahoo.com.vn
;; Blog: tdmblogging.blogspot.com
;; Date: March 3rd, 2014
;; Description: a simple counter from 0000 to 9999 using PIC16F877A and 7 segments leds.
;; -----------------------------------------------------------------------------

include P16F877A.inc

; define constants

num_0 equ  20
num_1 equ  21
num_2 equ  22
num_3 equ  23

time_delay  equ  24
time_delay1  equ  25
time_delay2  equ  26

counter  equ  27
rate  equ  28
tmp   equ  29


org 0
goto 100

org 10
table ; look up for 7 segments code
movfw counter
addwf PCL
retlw H'3F'  ; 0
retlw H'06'  ; 1
retlw H'5B'  ; 2
retlw H'4F'  ; 3
retlw H'66'  ; 4
retlw H'6D'  ; 5
retlw H'7D'  ; 6
retlw H'07'  ; 7
retlw H'7F'  ; 8
retlw H'6F'  ; 9

org 100
start
; config and init ports
bsf  STATUS, 5    ; select bank 1

clrw
movwf TRISB    ; mark port B as an output port
movwf TRISC    ; mark port C as an output port

bcf  STATUS, 5    ; select bank 0

clrf PORTB
clrf num_0
clrf num_1
clrf num_2
clrf num_3
clrf rate

movlw H'FF'
movwf PORTC

; main program start here
loop
; control the counting speed
incf rate   ; increase rate
btfsc rate, 6   ; if rate equals 01000000
call incNum0   ; do count
btfsc rate, 6   ; and then clear rate
clrf rate

call display   ; scan the leds
goto loop


display
bsf  PORTC, 0   ; port C equals 11111111

movf num_0, w   ; look for the 7 segments code of num_0 value
movwf counter
call table
movwf PORTB   ; display to port B
bcf  PORTC, 3   ; port C equals 11110111
call delay

bsf  PORTC, 3

movf num_1, w
movwf counter
call table
movwf PORTB
bcf  PORTC, 2
call delay

bsf  PORTC, 2

movf num_2, w
movwf counter
call table
movwf PORTB
bcf  PORTC, 1
call delay

bsf  PORTC, 1

movf num_3, w
movwf counter
call table
movwf PORTB
bcf  PORTC, 0
call delay
return

incNum0
incf num_0   increase num_0
; test if num_0 equals 10 (1010 in binary)
clrf tmp
btfsc num_0, 1   ; xxxxxx1x
bsf  tmp, 0
btfsc num_0, 3   ; xxxx1xxx
incf tmp
btfsc tmp, 1
call incNum1   ; num_0 = 10 -> increase the next
return

incNum1
clrf num_0
incf num_1
clrf tmp
btfsc num_1, 1
bsf  tmp, 0
btfsc num_1, 3
incf tmp
btfsc tmp, 1
call incNum2
return

incNum2
clrf num_1
incf num_2
clrf tmp
btfsc num_2, 1
bsf  tmp, 0
btfsc num_2, 3
incf tmp
btfsc tmp, 1
call incNum3
return

incNum3
clrf num_2
incf num_3
clrf tmp
btfsc num_3, 1
bsf  tmp, 0
btfsc num_3, 3
incf tmp
btfsc tmp, 1
clrf num_3
return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
delay
movlw H'5'
movwf time_delay2
delay3
movlw H'5'
movwf time_delay1
delay2
movlw H'5'
movwf time_delay
delay1
decfsz time_delay
goto delay1
decfsz time_delay1
goto delay2
decfsz time_delay2
goto delay3
return

END

Tuesday, December 10, 2013

Textbox with placeholder by C#

It's here. A textbox with placeholder. Look like input control in html. Create a file named STextBox.cs and paste this code:

// -----------------------------------------------------------------------------
// File name: STextBox.cs
// Author: Smart Goat
// Email: dmtmd2010@yahoo.com.vn
// Blog: knowledgesharez.net
// Date: 2013-12-09
// Description: a text box with placeholder, similar the web input control.
//   - Properties:
//      + Placeholder: string - The content of the placeholder
//   - Methods:
//      + IsEmpty(): bool - Check if the text box is empty (recommended)
// -----------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Windows.Forms;

namespace FooBar
{
    class STextBox : TextBox
    {
        private bool textInputted;
        private Color backupForeColor = Color.Empty;
        private string placeholder = string.Empty;

        public string Placeholder
        {
            get { return placeholder; }
            set
            {
                placeholder = value;
                SetPlaceHolder();
            }
        }

        public bool IsEmpty()
        {
            return string.IsNullOrEmpty(Text) || !textInputted;
        }

        private void SetPlaceHolder()
        {
            if (string.IsNullOrEmpty(Text))
            {
                Text = placeholder;
                ForeColor = Color.Gray;
                textInputted = false;
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            textInputted = !string.IsNullOrEmpty(Text);
        }

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            if (!textInputted)
            {
                Text = string.Empty;
                ForeColor = backupForeColor;
            }
        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            SetPlaceHolder();
        }

        protected override void OnForeColorChanged(EventArgs e)
        {
            base.OnForeColorChanged(e);
            if (backupForeColor == Color.Empty)
            {
                backupForeColor = ForeColor;
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.Control && e.KeyCode == Keys.A)
            {
                SelectAll();
            }
        }
    }
}

Sunday, December 8, 2013

Simple photoshop script

This is my simple script to join images using photoshop:

// create a new document size 30000x500 pixels at 72ppi
var doc = app.documents.add (30000, 500, 72);

// prepare the real width of the joined image
var wid = new UnitValue(0, "px");

// loop through all images
// 3 is the number of images
// images are store in c:\img folder
// images' file name: img1.jpg, img2.jpg, ...
for (i = 1; i <= 3; i++) {
    // take a file
    var fileRef = File("C:\\imgs\\img" + i + ".jpg");

    // open the image in a new document
    var imgdoc = app.open(fileRef);

    // resize the opened image to height 500px
    var ws = 500 * imgdoc.width / imgdoc.height;
    imgdoc.resizeImage(ws, 500);

    // copy the resized image to working document
    imgdoc.selection.selectAll();
    imgdoc.selection.copy();
    app.activeDocument = doc;
    doc.paste();

    // get the top layer
    var pslayer = doc.layers[0];

    // move the pasted image to the right position
    var position = pslayer.bounds;
    position[0] = wid - position[0];
    position[1] = 100 - position[1];
    pslayer.translate(position[0], -position[1]);

    // close the opened image and update the real width
    app.activeDocument = imgdoc;
    wid += imgdoc.width;
    imgdoc.close(SaveOptions.DONOTSAVECHANGES);
}

Monday, December 2, 2013

[Tutorial] Move form with no border

Just add the below snippet to your code (C#):

private const int WM_NCHITTEST = 0x84; // Mouse capture.
private const int HTCLIENT = 0x1;      // Client area.
private const int HTCAPTION = 0x2;     // Title bar.

// This function intercepts all the commands sent to the application.
// It checks if user click (WM_NCHITTEST) on the form area (HTCLIENT)
// It fakes the result as the title bar (HTCAPTION)
// This makes the form thinks user clicked on the title bar.
// So we can click and move form by (fake) title bar.
protected override void WndProc(ref Message message)
{
    base.WndProc(ref message);

    if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
    {
        message.Result = (IntPtr)HTCAPTION;
    }
} 

Thursday, October 17, 2013

[Tutorial] Add offline javadoc to eclipse

If you are working with eclipse, the javadocs come from internet. And eclipse takes a bit delay to show the docs. You can config your eclipse to use offline javadocs. This will make eclipse faster and you can see the docs without internet. These are the step:
1. Download javadocs:
Java 6: http://docs.oracle.com/javase/6/docs/
Java 7: http://docs.oracle.com/javase/7/docs/
Download the javadocs zip file and put in any where that you want.
2. Config eclipse:
add offline javadoc
Select Preferences
add offline javadoc to eclipse
Select Installed JREs
add offline javadoc to eclipse
Select the java version that you want to add docs and then select Edit
add offline javadoc to eclipse
Select rt.jar package and then select Javadoc Location
Select Javadoc in archive and browse the zip that you downloaded
Done

Sunday, October 6, 2013

How the CPU works

Every computer, smartphone, ... has a CPU inside. It's the brain of these devices. But how it works? After surfing around for a while, I found this. Take a look at how a CPU works:


Summary:
First, all devices have their own machine code to tell them what to do.
CPU takes a string of bits (machine code - designed by the manufacturer) and output another string of bits that do some special tasks or tell the devices what to do.
'The strings of bits' - intructions, are stored in RAM (random access memory). CPU read (input) these instructions and execute an output. The output maybe tell the hard disk to write somethings. Maybe store somethings to RAM again.

Wednesday, September 25, 2013

[Tip] Show all sheets in Excel 2013

If you have tried Excel 2013, you would see a new style of manage sheets. For somebody, it's strange and hard to use if there are many sheets.
Today I will show you how to view all sheets and move to any sheet you want. It's very easy!

Excel sheets
Excel sheets



Right click on the arrow

excel sheets tooltip

And here is what you'll see!

excell all sheets

Wednesday, August 21, 2013

[Tip] Set static ARP

For Windows XP

1. Open Command Prompt
2. At the command prompt, type:
arp -s ip_address mac_address
Example:
arp -s 192.168.2.1 00-34-A3-7D-66-FF
3. In order to delete the static arp entry, type:
arp -d ip_address
Example:
arp -d 192.168.2.1

For Windows 7, Windows 8

1. Open Command Prompt
2. At the command prompt, type:
netsh -c "interface ipv4" set neighbors "connection_name" "ip_address" "mac_address"
Example:
netsh -c "interface ipv4" set neighbors "Wi-fi" "192.168.1.1" "00-A3-F7-C0-AE-32"
3. In order to delete the static arp entry, type:
netsh interface ipv4 delete neighbor "connection_name" "ip_address" "mac_address"
Example:
netsh interface ipv4 delete neighbor "Wi-fi" "192.168.1.1" "00-A3-F7-C0-AE-32"
When you restart the computer, if the arp entry is still displayed, go to this link for more information.

For Linux

1. Open Terminal
2. At the terminal prompt, type:
sudo arp -s ip_address mac_address
Example:
sudo arp -s 10.0.0.2 00:0C:29:C0:94:BF
3. In order to delete the static arp entry, type:
sudo arp -d ip_address
Example:
sudo arp -d 10.0.1.0

Tuesday, August 20, 2013

How much time you surfing in a day

Today I found some firefox extensions to track you browsing time. These extensions are good for someone who is easy to be distract in working. It let you know how much time you spend for each page. Here they are:


Mind the Time



mind the time, browsing time, surfing time


Rescue Time



rescue time, browsing time, surfing time

Monday, August 19, 2013

[Cooking] Poached pork with egg

Today, I give a guide to cook "poached pork with eggs" - an Asia food.

pork with egg, pork and egg, thịt kho trứng

Ingredients:

  • 600 grams pork ham
  • 4 duck eggs
  • 1 liter of coconut water
  • Star anise
  • Fish sauce
  • Monosodium glutamate (MSG)
  • Colored water
  • Sugar
  • Chopped onion
  • Chopped garlic
  • Pepper
  • Cooking oil

Directions:

  1. Cleaned pork, cut into bite size pieces, marinate with 1 tablespoon of chopped garlic, 1 tbsp. of chopped onion, 1 cup of fish sauce, 4 tbsp. of sugar, 1 tbsp. of MSG, 1 teaspoon of ground pepper, 1 tbsp of colored water, 2 tbsp of cooking oil, for about 15 minutes for the spices are absorbed.
  2. Boiled duck eggs for about 10 minutes, let cool, peel. Then put into a heat frying pan of oil, fry until yellow evenly.
  3. Put pork to the pot star anise, add fresh coconut water. Cook over medium heat until the meat is ready, just cooked, remove foam. Then add the duck eggs and cook with eggs in small fire until the meat is tender, done.

Saturday, August 17, 2013

[Tutorial] Install Damn Small Linux (DSL) 2.4 on Virtualbox

Step 1: Install Virtualbox (DIY)
Step 2: Download dsl iso install file
Access ftp://distro.ibiblio.org/pub/linux/distributions/damnsmall/ select the archive folder and download the file dsl-2.4.iso (if you want the newest version, select the current folder)

dsl, virtualbox, linux, install

Step 3: Install and setting
Open virtualbox and click New

dsl, virtualbox, linux, install
dsl, virtualbox, linux, install
dsl, virtualbox, linux, install
dsl, virtualbox, linux, install
dsl, virtualbox, linux, install
dsl, virtualbox, linux, install
dsl, virtualbox, linux, install
dsl, virtualbox, linux, install
dsl, virtualbox, linux, install

Now config the created virtual machine

dsl, virtualbox, linux, install
dsl, virtualbox, linux, install

Replace SATA controller by IDE controller:

dsl, virtualbox, linux, install
dsl, virtualbox, linux, install

Choose dsl.vdi file >> Open >> Ok

dsl, virtualbox, linux, install

Now start the virtual machine and install dsl

dsl, virtualbox, linux, install
dsl, virtualbox, linux, install
dsl, virtualbox, linux, install

Select the dsl-2.4.iso file downloaded before

dsl, virtualbox, linux, install
dsl, virtualbox, linux, install
dsl, virtualbox, linux, install

When the boot screen appear, press Enter key and wait for dsl to start


dsl, virtualbox, linux, install

Here is what we got:
(If you have problem with mouse, select Machine >> Disable Mouse Integration

dsl, virtualbox, linux, install

Now, right click on desktop >> Xshell >> Root Access >> Transparent

dsl, virtualbox, linux, install

The shell window appear, type fdisk /dev/hda

dsl, virtualbox, linux, install

Press 'm' key for help menu:

dsl, virtualbox, linux, install

Type commands as the following image:
(n >> p >> 1 >> 520)

dsl, virtualbox, linux, install

 Continue with these commands: t >> 83 >> w


dsl, virtualbox, linux, install

Now, close the shell window, right click on desktop >> Apps >> Tools >> Install to Hard Drive


dsl, virtualbox, linux, install

A window appear, type hda1 >> n >> n >> y


dsl, virtualbox, linux, install

Type 'y' and press Enter:

dsl, virtualbox, linux, install

Type 'g' to make Grub boot loader:


dsl, virtualbox, linux, install

After this, type 'y' to restart the virtual machine, and then you got DSL installed.