Selasa, 14 Agustus 2012

Introduction to PIC Microcontroller

PIC microcontroller from microchip are very popular microcontroller. PICs are easily programmable cheap microcontroller. PICs is the name for the microchip microcontroller family (peripheral interface controller). Consisting of a microprocessor, I/O ports, timer(s), and other internal integrated hardware.  a wide range of chip sizes( from eight-pin up), great availability of compilers and source code and easy programming. Flash-type devices are re-programmable in-circuit, while OTP versions are very cheap to use at the final stage. A wide range of simple programmer hardware and software is downloadable from the net. PIC16f84 seems to be the “standard” for small projects. For the beginner with PICs, a PIC16f84A or PIC16f628 device is a good choice to start with. There are many different varieties of PICs that run at many frequencies, have different memory size, and different internal peripherals. The most famous MCU that microchip produces are: 12cxxx, 12fxxx, 16cxxx, 16fxxx, and 18fxxx.
  • Prefix 12 is for chips with 8 pins.
  • Prefix 16 is for 14-bit core chip with more than 8 pins.
  • Prefix 18 is for 16-bit core chip.
  • The letter after number tells the memory type:
    • C is for EPROM (OTP)
    • F is for flash chips
  • The number (2 or 3 digits) after this letter identifies specific chip version.
  • Improved new version of certain PICs types are identified by appending an (A) 

PIC16f84A microcontroller

  • 8-pin Enhanced FLASH/EEPROM 8-Bit microcontroller
High Performance RISC CPU Features:
  • Only 35 single word instructions to learn (instruction set)
  • All instructions single-cycle except for branches which are two-cycle
  • Operating speed: DC - 20 MHz clock input
                             DC - 200 ns instruction cycle

  • 1024 words of program memory
  • 68 bytes of Data RAM
  • 64 bytes of Data EEPROM
  • 14-bit wide instruction words
  • 8-bit wide data bus
  • Eight-level deep hardware stack
  • Four interrupt sources:
    • External RB0/INT pin
    • TMR0 timer overflow
    • PORTB<7:4> interrupt-on-change
    • Data EEPROM write complete
Peripheral Features:
  • 13 I/O pins with individual direction control
  • High current sink/source for direct LED drive
    • 25 mA sink max. per pin
    • 25 mA source max. per pin
  • TMR0: 8-bit timer/counter with 8-bit programmable prescaler.
Special Microcontroller Features:
  • 10,000 erase/write cycles Enhanced FLASH Program memory typical
  • 10,000,000 typical erase/write cycles EEPROM Data memory typical
  • EEPROM Data Retention >40 year
  • Selectable oscillator options
  1. RC oscillator : when using RC oscillator with CLKOUT
  2. HS oscillator : when using High speed Osc (> 4mhz)
  3. XT oscillator : when using external crystal oscillator with frequency <= 4Mhz
  4. LP oscillator : when using Low power osc < 200 khz
 

 

PORTA & PORTB

PORTA is a 5-bit wide, bi-directional port. The corresponding data direction register is TRISA. Setting a TRISA bit (= 1) will make the corresponding PORTA pin an input (i.e., put the corresponding output driver in a Hi-Impedance mode). Clearing a TRISA bit (= 0) will make the corresponding PORTA pin an output (i.e., put the contents of the output latch on the selected pin).
PORTB is an 8-bit wide, bi-directional port. The corresponding data direction register is TRISB. Setting a TRISB bit (= 1) will make the corresponding PORTB pin an input (i.e., put the corresponding output driver in a Hi-Impedance mode). Clearing a TRISB bit (= 0) will make the corresponding PORTB pin an output (i.e., put the contents of the output latch on the selected pin).
Now, let’s see how to write a program to microcontroller. Two methods are available to program the PIC, the first one is carefully study the memory organization, the function of each special register, and the 35 instruction set of this family. Then write the software in assembly language. This method is difficult and need good background in assembly programming. The second method is to write the code in HLL then compile this to machine code of PIC. Various HLL compilers are available as PICC, PICant, JAL, PICbasic, PICO-C and so on. We will use PICC compiler. After writing the C code using PICC compiler you must compile it to get hex filewhich will be loaded directly to PIC flash memory through programming circuit and software .

Simple Program : 

Now after this introduction we are going to write our first program PICC compiler.The program is to flash a LED while a push button is pressed.and we are going to explain how to use PICC in a nutshell.
when you run PICC for the 1st time you will see a window as below :
Now press the folder icon in the top left of the window then select new then select source file as shown in the figure below :
Then save it in your desired place and an editor will be opened in this editor we are going to write our program which is :

#include<16f84a.h>          // include library of microcontroller we are going to use
#fuses xt                           // tell the compiler that we are going to use external
                                   //crystal oscillator as a clock source for our microcntroller
#use delay(clock=4000000)   // tell the compiler the frequency of the clock
                                 // source is to be 4MHZ (used in calculation of delay time)

void main( )
{
   set_tris_a(0xff);               // set PORTA as input    
   set_tris_b(0xfe);              //set PORTB as input except PB0 which is
                                           // output for LED
   output_low(pin_b0);        // set PB0 output to 0v
   while(1)                      // main program
   {
     if(input(pin_a0))    // when switch is pressed >> true
                                   // and when released >> false
{     
     output_high(pin_b0);     //LED ON
     delay_ms(10);              // wait for 10ms doing nothing
     output_low(pin_b0);    // LED off
     delay_ms(10);            // wait for 10ms doing nothing
   }
}
}

Then after writing the code  go to the tab compile and press compile as shown below :


If there is no errors you will find that .hex file is generated which can be loaded in proteus for simulation :


Next Page >>

You can download the code and the similuation HERE
You can download this introduction in PDF HERE

Sabtu, 11 Agustus 2012

DAC using ATmega 16

DAC stands for digital to analog converter . In ATmega 16 there is no module which converts digital value to ana analog voltage it only contains ADC (analog to digital converter ) .
Analog voltage from microcontroller can be used in many applications such as speed control of a dc motor and dimmer of a light or LED .
In this tutorial we are going to use the timer module of the ATmega 16 to make DAC (i.e. to obtain analog voltage from microcontroller) .
Timers in ATmega 16 works in 4 mode and there are two modes can be used for DAC :
  1. Fast PWM (Pulse Width Modulation) .
  2. Phase correct PWM .
In our example we will use Fast PWM but Phase correct PWM can be used in similar manner .

In Fast PWM the OCR register is always compared with the value of the timer and when they are equal a signal is cleared (Non inv. mode) at the OC pin of the microcontroller and this signal is set whe the timer reaches to the max (255 for timer0 or timer2) and is cleared and starts counting again and the sequence is repeated again .
The average of the signal generated at the OC pin (represent the analog value) will correspond to the digital value stored in the OCR register .



In the above figure the OCR value is 64 ( small value ) and the output pulse width is small so it's average value is small and will be equal two ( (64 / 256)*5 =1.25 v approximately )

In the above figure the OCR value is 200 ( large value ) and the output pulse width is large so it's average value is large and will be equal two ( (200 / 256)*5 =3.9 v approximately )

So the analoge output will range from 0 v to 5v .

Timer Adjustment :

In the code we used timer0 (counts from 0 to 255 ) adjusted in Fast PWM.
The OC pin is connected to Non inv. signal .
OCR value will take it's value from PINA (switches connected to PORTA ) .

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 8000.000 kHz
// Mode: Fast PWM top=FFh
// OC0 output: Non-Inverted PWM
TCCR0=0x69;
TCNT0=0x00;
OCR0=0x00;

Now there is a problem we want the average value of the output signal from OC pin . How we can do this ?
It's so simple we want average i.e. DC value i.e the zero frequency component of the output signal so we connect RC low pass filter at the output of the OC pin with a small cut off frequency for example (50 HZ). 



Fc (cut off frequency ) = 1/ (2*pi*R*C ) .

Fc =50 HZ .

we choose R=1k .

we calculate C =3.2uf .





Results 



In the above example the input digital value = " 1000 0000 " which corresponds to 128 which is half  256 so the analog output as we see is half 5 v which is 2.5 v and so on we can change the digital input .

Download the project and it's simulation HERE
Download this tutorial in PDF HERE

Jumat, 10 Agustus 2012


This Course is very simple,very easy and very interesting to begin with avr microcontrollers .

Course Videos :


  1. Intro to the AVR Atmega32
  2. AVR Program Transfer Part 1
  3. Continuation of SPI to AVR IC interface for the AVR Atmega32 Microcontroller
  4. AVR Program Transfer Part 2 (Drivers)
  5. AVR (WinAVR) Programming Environment WinAVR Install
  6. Testing the programmer and building the first circuit
  7. Writing our first program and transferring it to the MCU
  8. Revising the LED program to make the LED blink
  9. Adding a Button to the Microcontroller
  10. Understanding Button Debouncing
  11. Button Debouncing through Software
  12. The button Game
  13. Timers/Counters and Binary/Hex Numbers
  14. Intro to Interrupts
  15. Intro to LCD... Meet Mr. LCD and check out his crib.
  16. Writing our First LCD Program
  17. Passing a String to the LCD
  18. Displaying Number and Finding Positions
  19. Separating Reusable Code to Form a Library
  20. LCD Creating a New Library Routine (Integer to Display Location)
  21. Using other Power Sources
  22. AC to DC Power Supply in Detail and Why Capacitors are Used
  23. Soldering a Wire to a Male Header
  24. Assembling and Crimping - Female Header
  25. Potentiometers and Voltage Dividing
  26. Introduction to ADC (Analog to Digital Conversion)
  27. ADC (Analog to Digital Converter) - First Program Using Interrupts
  28. Getting the Full 10-bits from the ADC (Analog to Digital Converter)
  29. Accelerometers and ADC
  30. Measuring the ADC Noise and Intro to the ADC Noise Reduction Mode
  31. Using Multiple Channels on the Analog to Digital Converter
  32. Introduction to Servos and Understanding Torque
  33. Introduction to PWM (Pulse Width Modulation)
  34. Controlling a Hobby Servo Using the Internal PWM
  35. Controlling a Servo Using Fast PWM with Non-Inverted Pulses
  36. Controlling Multiple Servos Using Standard Available Output Pins
  37. Part 1 - Real World Example of Create a Vacuum Source
  38. Part 2 - Real World Example of Create a Vacuum Source
  39. Part 3 - Real World Example of Create a Vacuum Source
  40. Part 4 - Real World Example of Create a Vacuum Source
  41. Part 5 - Real World Example of Create a Vacuum Source
  42. Part 6 - Real World Example of Create a Vacuum Source

 FOR MORE DETAILS :

    Altera VHDL basics online course

    Altera is a leading corporation in the manufacture of FPGA's (Field Programmable Gate Array) , CPLD's (Complex Programmmable Logic Devices ) and ASIC's (Application Specific Integrated Circuit ) .

    VHDL which stands for (VHSIC hardware description lanaguage ) is one of the hardware description languages used to program (synthesis) digital circuits on FPGA,CPLD and ASIC chips.

    Altera made a nice presentation for learning the basics of the VHDL language in just 1.5 hours and it's for free , you need only to register and you can run the course online .

    Objectives of the course :

    • Construct complete VHDL models .
    • Generate Logic functions using the VHDL .
    • Create hierachical VHDL designs

    Course Outlines : 

    • VHDL introduction .
    • Design units .
    • Architecture modeling fundamentals .
    • VHDL logic synthesis .
    •  Hierachical designing .
    You can start your course HERE (Select register at the bottom of the page to start your course )

    Now USB became in almost laptops and the serial port has gone and all made programmers use serial ports.in this post we will make a simple usb avr programmer connected through usb (no need for serial port).

    This programmer uses a Atmega8 microcontroller with a few external passive components. The good thing is you don’t need any USB controller because it is implemented in the firmware inside Atmega8. Yes, you are right, you need an AVR programmer once to load the firmware inside Atmega8.

    Features

    • Works under multiple platforms. Linux, Mac OS X and Windows are tested.
    • No special controllers or smd components are needed.
    • Programming speed is up to 5kBytes/sec.
    • SCK option to support targets with low clock speed (< 1,5MHz).

    Download

    Firmware and circuit

    The following packages include circuit and firmware.
    Firmware is the software which going to be burn into programmer's microcontroller, this firmware have code to enable the programmer to communicate with pc via usb and target microcontroller.

    Download here

    COMPONENTS

    Part                      Descirption                 Qty
    ATMEGA8                  Microcontroller                1
    3.6 Volts                    Zener Diode                       2
    22pf                            Capacitor                             2             
    100nf                          Capacitor                             1    
    4.7uf                           capacitor                              1
    12Mhz                        Crystal                                   1 
    10K                              Resistance                           1
    1K                                Resistance                           3
    68ohm                       Resistance                           2
    2.2K                            Resistance                           1
    LED                             Led  (Red & Geen)            2
    USB connector                                                        1
    USB Cable                                                                 1    

    For details on how to build the programmer READ ME 



    Note:-
    Make sure you burn the right fuse bit configration along with the firmware into atmega8 microcontroller
    for atemega8   the fuse bit are
    atmega8        HFUSE=0xC9     LFUSE=0xEF

    Software


    source :

    Senin, 06 Agustus 2012

    VHDL Tutorial - LESSON 6


    2.2 Sequential Circuit Design Example :

    The circuit under study will be a Counter. We will design three counters, a
    synchronous one using JK Flip-Flops and counts from 0 up to 6 .

    2.2.1 Counter using JK Flip-Flops

    2.2.1.1 Excitation Tables and Karnaugh Maps

     
    Karnaugh Maps:
    "J0" Map

    "K0" Map

    "J1" Map

    "K1" Map
     
    "J2" Map

    "K2" Map

    2.2.1.3 Boolean Expression
     
    2.2.1.3 VHDL Behavioral Code of Inverter

    library ieee;
    use ieee.std_logic_1164.all;
    Entity invert is
    Port (a : in std_logic;
    b:out std_logic);
    end invert
    Architecture invbehave of invert is
    Begin
    b <= not (a);
    end invbehave;

    2.2.1.4VHDL Behavioral Code of 2-input AND

    library ieee;
    use ieee.std_logic_1164.all;
    Entity and2i is
    Port (a,b : in std_logic;
    c:out std_logicl;
    end and2i
    Architecture andbehave of and2i is
    Begin
    c <= a and b;
    end andbehave;

    2.2.1.5 VHDL Behavioral Code of 2-input OR

    library ieee;
    use ieee.std_logic_1164.all;
    Entity or2i is
    Port (a,b : in std_logic;
    c:out std_logic);
    end or2i
    Architecture orbehave of or2i is
    Begin
    c <= a or b;
    end orbehave;

    2.2.1.6 VHDL Behavioral Code of JK Flip-Flop

    library ieee;
    use ieee.std_logic_1164.all:
    Entity jkff is
    Port (j,k,clk : in std_logic;
    q:out std_logic);
    end jkff
    Architecture behavelikethis of jkff is
    signal q_int : std jogic := '0';
    Begin
    Process (clk)
    Begin
    If clk='1' and clk 'eventthen
    If j='1' and k='0' then
    q_int<='1';
    elsif j='0' and k='1' then
    q_int<='0';
    elsif j='1' and k='1' then
    q_int<=not (q_int);
    end if;
    end if;
    end process;
    q<=q_int;
    end behavelikethis

    2.2.1.7 VHDL Structural Code of the JK Flip-Flops Counter
    library ieee;
    use ieee.std_logic_1164.all;
    Entity jkcount is
    Port (clk: in std_logic;
    q0,q1,q2:out std_logic);
    end jkcount
    Architecture Structural of jkcount is
    signalq0int,q1int,q2int,q1i,q2i,j0,k0,j1,k1,j2,k2 : std_logic := '0';
    Component invert is
    Port (a : in std_logic;
    b:out std_logic);
    end Component
    Component and2i is
    Port (a,b : in std_logic;
    c:out std_logic);
    end Component
    Component or2i is
    Port (a,b : in std_logic;
    c.out std_logic);
    end Component
    Component jkff is
    Port (j,k,clk : in std_logic;
    q:out std_logic);
    end Component
    begin
    inv0: invert
    port map (q1int,q1i)
    inv1 : invert
    port map(q2int,q2i)
    and0 : and2i
    port map(q0int,q1int,j2)
    or0: or2i
    port map(q1i,q2i,j0)
    or1 : or2i
    port map(q0int,q2int,k1)
    jk0 : jkff
    port map(j0,l,clk,q0int)
    jk1 : jkff
    port map(q0int,k1,clk,q1int)
    jk2 : jkff
    port map(j2,q1int,cIk,q2int)
    q0 <= q0int;
    q1 <= q1int;
    q2 <= q2int;
    end Structural