Tuesday, July 3, 2012

A Journey By MicroChip...... (part 1)


Journey By MicroChip
( PIC based Micro controller )
                                                Part(1)

Intro
PIC family MicroController:
Microchip is the vendor of the PIC family micro controller which provide three range of micro controller , baseline micro controller(typically below PIC16x ), mid level micro controller, and high performance micro controller.
Baseline microcontroller provide at least an amount of component  which is enough to develop some range of peripheral application development such as temperature sensor.
It is a low power device.
Mid range Micro controller
Midrange micro controller has 14 bit program memory typically PIC16 and PIC12 group are consider as mid range micro controller .
High performance Micro controller
High performance micro controller has wide range of variation. It has a high performance ADC, 32 level  stack interrupt etc. depending on the hardware it has around 79 instruction set.

Fig: Pic Microcontroller
Processor Architecture :
PIC processor follows as Harvard architecture (RISC). According to that the Program and Data are stored in separate storage, accessible via different signals. So, CPU use different address bus, instruction bus  and data bus to execute instruction. Due to that reason it can not access both read write operation at the same time.

Fig: Oscillator
Oscillator :

PIC micro controller needs clock cycle to do its operation. The clock cycle should be  provided to micro controller as a external device  by means of  oscillator. In that sense oscillator is a extern device which provide clock cycle to the micro controller.

Oscillator mode is depend on the device. To select correct device oscillator one should look the device manual. Apart from that typically PIC micro controller select 8  oscillator mode which are as follows :

Id
Name
Meaning
1
LP
Low Frequency Crystal
2
XT
Crystal Resonator
3
HS
High Speed Crystal Resonator
4
RC
External Resistor/Capacitor
5
EXTRC
External Resistor/Capacitor
6
EXTRC
External Resistor/Capacitor with CLKOUT
7
INTRC
Internal 4 MHz Resistor/Capacitor

8
INTRC
Internal 4 MHz Resistor/Capacitor with CLKOUT


Ones who want to buy the oscillator should care of about the frequency and the oscillator type. When it is connected to oscillator micro controller should be configured with appropriate oscillator type. After the micro controller will operate with given oscillator frequency . Point to be noted that one instruction is executed after each 4 clock cycles.
So, micro controller speed is defined by the oscillator frequency.

Example:          Configuring Oscillator according to MPLAB C18 
                         #pragma config OSC = HS
                          or  _CONFIG _HS_OSC
 before using _CONFIG  processor should be define as
                          PROCESSOR pic18xxxx

Watchdog Timer :
Pic micro controller use watchdog timer to monitor the hardware continuously. If something unusual things happens the watchdog timer will restart the program. Eventually a watchdog timer is special timer which will incremented by one at every clock cycle of the internal RC clock pulse. When Watchdogtimer register has an Overflow , an interrupt is called which restart the micro controller. To prevent our program from continuously resetting , program should know the time interval that the watchdog timer needs to count 00 to FF. Knowing that , the program should reset the counter value to 00 before the overflow happens. If some unusual things happens and micro controller is running some part of program continuously (hang on a loop) the watchdog timer will not be reset by the program, In that case , watchdog timer finish its counting , interrupt will be called and the micro controller will be reset again.

          Example :
         
   Configuring Oscillator according to MPLAB C18 
                         #pragma config WDT= ON
                          or  _CONFIG _WDT_ON
   before using _CONFIG  processor should be define as
                          PROCESSOR pic18xxxx

Additional steps :
calculate the RC clock time needed to make overflow of the WDT
Reset the WDT timer register before it finish its counting.



Reliable computing / Malfunction Detection

Fig: Monitoring by watchdog Timer 
Watchdog timer is used to find possible program malfunction and reset the device to ensure reliable computing. It use monitor based scheme where a specific hardware (counter ) is used.
Before enter to specific function block the watchdog timer is set to zero. Then it enter to the functional block and start monitoring. If something is wrong or the functional block is take too much time to execute the WDT timer reset the program considering the programmer logic that this functional block will executed within the less amount of time that the WDT timer  register needs to fill up.


Reset Micro controller :
Reset Mechanism is a built in mechanism for the micro controller. It is used to start the program from the beginning and initialized all the components of the microcontroller such as stack , program counter.
Reset mechanism can be defined by the device module.
Most common reset mechanism is given as follows :

1. Reset during power on (POR).
2. !MCLR reset during normal operation.
3. Reset during SLEEP mode.
4. Watchdog timer reset (WDT).
5. Brown-out reset (BOR).
6. Stack full reset
7. Stack Underflow reset  
  • Reset During  Power On

When ever the device is power on and the supply voltage reaches a certain level an power on reset is occurred.
An external POR reset circuit may be need depends on device design. According to microchip
“ External Power-on Reset circuit is required only if the VDD power-up slope is too slow. The diode D helps discharge the capacitor quickly when VDD powers down ”



  • MASTER CLEAR !MCLR
Fig: !MCLR 
Master clear pin gives a freedom to the user of the device to do forcefully reset during its normal operation. To use this feature an  external push button is associated with that pin of the micro controller. When that push button pin goes low, it reset the micro controller.












Sunday, July 1, 2012

Micro controller part(3)


                                              Microcontroller  Overview
                                                    Part (3)


Intro:
Microcontroller part (2) deals with the definition of microcontroller (what it is ) and some basic components of the microcontroller (ports , register and ROM).
This blog will explain some other elements of the micro controller.
Elements of Micro Controller (continue ...)



Communication mechanism of micro controller
Serial communication
Serial communications take place by transmitting and receiving data in a stream of consecutive electrical pulses that represent data bits and control codes.In an single time
A single data stream will be send or receive.
Serial communication can be divide by asynchronous serial communication and synchronous serial communication
Asynchronous communication :
When device send something it must be receive by some device also. Asynchronous communication established when the time (clock) between receiver and transmitter is not the same.
Synchronous communication :
When the transmitter and receiver clock is synchronous (having same clock, it does not mean the identical clock ) then the communication topology is said synchronous communication .

Asynchronous communication is much more complicated than the synchronous communication. To make a communication successfully a protocol should be defined first between the transmitter and the receiver.

Communication speed which is measured number of bits per second , known as baud rate, important characteristics to establish a communication.


Serial communication can be done with micro controller by using the following system
• USART (universal synchronous/asynchronous receiver/transmitter)
• SPI (serial peripheral interface)
• I2C (inter-integrated circuit)
• CAN (controller area network)


USART:
Serial communication such as RS-232 , RS-485 can be done with USART subsystem of the micro controller.
When it is in synchronous mode additional clock information is to send with the packet data.
For asynchronous  communication it is not needed only the data packet following defined protocol (baud rate, start bit , stop bit , handshake).Baud rate should same with the transmitting/receiving devices.

SPI :
Micro Controller device can work with master salve architecture where each master/ salve may be another micro controller . To obtain these topology micro controller use serial peripheral interface. By using these interface micro controller is able to exchange data and not only that it is also able to select its slave to whom it wants to communicate,
To do that is use slave select (ss) i/o , Serial data out (sdo), serial data in(sdi)and serial clock (sck).

I2C :
Inter integrated circuit communication (I2C bus) is used to interconnect  at least two microcontroller or other integrated circuit devices. This is a synchronous protocol that allow slave to communicate with master controlled by the master.
Due to it is a synchronous communication clock is send with data , for this feature it does not matter if the clock is changed later. Serail clock line (SCL) is used for this purpose.
Data is send over the Serial data (SDA) line which is bi- directional line controlled by SCL.

Can Bus System:
CAN bus system is a serial communication bus system for multi master high speed system that is used around 1 Mega bit per second communication.
In can bus system the communication is usually broadcast over the network.

Oscillator :
Micro controller used oscillator for frequency stabilization. This oscillator is an external device that produce clock cycle for the micro controller which is required for its operation. Traditionally MCU perform each instruction for every 4 clock cycles. There are around eight oscillator mode is defined by the micro controller. The operating mode of the oscillator is defined by the program (code) and written into the non volatile memory. Which loaded as a system configuration when MCU start .


Interrupt:
Interrupt is way of communication between microcontroller itself with CPU. While CPU is busy in executing some programs in meanwhile if something happens such as a pin state change then  interrupt is called which inform the CPU to halt its execution and go the Interrupt service routine to do something according to the interrupt type.

Timer Module:
Timer is a special register which is associated with oscillator. For each and every pulse that comes from the Oscillator the value of the Timer register incremented by one. When the register is full another increment causes an overflow interrupt which inform CPU , used to design clock .

Watchdog Timer:
Fig: WatchDog Timer
Hardware can have fault due to many reason. Considering a Embedded Engineer when the hardware is in prototype phase , if something wrong as a example  microcontroller stop working and hanged in that  circumstances an embedded engineer usually restart the micro controller board . But if somebody does not exist at that moment micro controller should restarted by it self  , and that is happens by the  Watchdog timer .

A/D Converter:
Analog to Digital converter module is used to convert  the analog data to digital conversion. A/D module is multiplexed with the analog channels . So before Using the Analog conversion appropriate channel should be selected first. More over that analog to digital conversation involve sampling acquisition and conversion . For this case Sampling rate is important .

 To summarize all the basics components of the micro controller I would like to introduce the block diagram of the components added below. 


Fig: Basics Components inside A microController



Tuesday, June 26, 2012

MicroController Part(2)





                                              Microcontroller  Overview
                                                    Part (2)

Intro:
Microcontroller part (1) deals with the definition of microcontroller (what it is ) and some basic components of the microcontroller (ports,register and rom).
This blog will explain some other elements of the micro controller.

Elements of Micro Controller (continue ...)

RAM : Random Access Memory 

Register , As we see in my  the blog micro-controller-part-1 , a small memory unit which has a faster access by the CPU. Where these register resides? 
RAM is a temporary memory storage of microcontroller, usaully in size of Kbytes which hold the program temporary data ,the results comes from arithmetic logic unit etc. It contains the general purpose register and special function register 
RAM is volatile memory unit , means whenever the power goes off it will lost it contents. The memory will be cleared. 

Program Memory :


The program which will be save somewhere inside the micro controller is called program memory. every micro controller has internal program memory. But if the program is too long reference to program memory it is possible to add external program memory. 

Program Counter :

Program counter is a special register which hold the instruction address of  program memory that will fetch next.The value of this register will be incremented automatically each time when a instruction will be executed. Working procedure of the program counter is like that , CPU will call for next instruction that will be executed. The address of the instruction  is obtained from the program counter and It will be incremented. 
Instruction will be placed into Instruction Register for most of the micro processor and micro controller.

Fig: Program Counter
Program counter register and instruction register ins typically involve for the instruction fetching. After the early production of 8051 Micro controller , most of the micro controller contain program memory which is a internal memory block that hold the instruction for the CPU. Instruction fetching involve typically to obtain the appropriate address of the program memory which hold the appropriate instruction that should be executed now and execute the instruction.
Instruction Register
Instruction register is a register that will store instruction. Operation code is extracted from the instruction set and it will executed in processing unit.
                       
Stack Pointer


A block of memory storage is called stack that will store temporary storage of data.To save data of general-purpose register during execution of a subroutine (function) or when an interrupt is serviced. Push and Pop mechanism is use to save the content of a register and to retrieve data into register. Stack is use for the faster access by the CPU.


Central Processor Unit (CPU)
CPU is unit that actually deals with the data , perform some operation and gives some output . CPU unit consist of the following units
a) Instruction Decoder :
Each and every instruction has opcode (operation code) that must be parsed and executed.
Instruction decoder is a electronics components that will take the opcode from the instruction and regenerate code so that the electronics components can execute it.
b)Arithmetical Logical Unit (ALU)
The arithmetic logic unit (ALU) is the most sophisticated components of any processing unit. It performs the entire integer arithmetic and bit-wise logical operations of the microprocessor.
Operation bit can be 8 bit arithmetic , 16 bit arithmetic or 32 bit arithmetic.
c) Accumulator is an SFR closely related to the operation of ALU. It is a kind of working desk used for storing all data upon which some operations should be executed (addition, shift etc.). It also stores the results ready for use in further processing. One of the SFRs, called the Status Register, is closely related to the accumulator, showing at any given time the "status" of a number stored in the accumulator (the number is greater or less than zero etc.).


Saturday, June 16, 2012

Micro-Controller part (1)




Intro:
Whenever we are talking about a micro controller we eventually talk about some lump of silicon inside a chip and of course programmable.
So, Micro controller is just a chip having having lots of logical gates which perform some arithmetic operation.Most of the micro controller actually execute some predefined assembly level operation  such as increment,move , copy , add etc.As an example most of the pic (vendor microchip) based micro controller execute only 30 instructions.
so, technically a micro controller can be define as a single chip having a processing unit (microprocessor), a define size of memory (that will depend on the device model) and a sort of peripheral interfaces (IO port,ADC,Serial etc).Firmware is the program that run inside a micro controller.

Depending on how to accesses the program memory hardware structure is defined by two possible architectures 
(1) Von Neumann, and 
(2) Harvard.
It will be describe later in the next blogs..


Elements of Micro Controller:

Fig: MicroController Structure


PORT:
When a device need to communicate out side the world, it need some structural interface through that it can communicate. Port is the physical interface through that it communicate with peripheral devices or outside of the world i.e with sensor.The communication can be done in bidirectional way means micro controller exchange some information as a passing out some information and taking in some information .(I/O ).So, 
these input output communication peripheral are called PORT which are use to receive some inputs from the outside world (port is configured for input )and manipulate data within inside the processing unit and give the output through PORT (port is configured for output )

REGISTER:

Fig: Register(D-FlipFlop)


Register is a digital electronic memory where 8bit (for 8 bit MCU)  data is saved.Data saved into register is volatile means whenever the microcontroller rebooted register will be cleared. Register is part of Central Processing Unit of the micro controller that means CPU has faster access to the register data.
Perhaps before any operation (before enter to the arithmetic logic unit ALU)
data are placed into register.

According to the functional definition registers are two types 
1. Register 
2.Special Function Register (SFR)

Register :
I always consider it is as an general type register because of it is a register (by definition) and its function is not defined. It eventually used to store some data and program may use those data for further processing according to the programmer. As an example PORTA register

SFR :
Unlike register , it also temporary save data but their functional behavior is defined by the manufacturer.Every bits of the SFR register has a defined function when ever it holds some bits (0 or 1 ) it will perform some function. It is a controller register.
As a example TRISA register is a SFR register associated to the PORTA register. But every bits of TRISA register means something special which is defined.For PIC based micro controller TRISA register can contain 0 or 1. 
but if TRISA contain 0 corresponding PORTA register bit will be configured as a OUTPUT pin and if it is 1 corresponding PORTA register bit will be configured as a INPUT pin

ROM :
ROM stand for read only memory which is a structured array of memory.The data which is written into the memory is not volatile.

the structure of the ROM is organised into three modules 
1) An array of memory with a fixed size 
2) A address decoder 
3) A buffer


The address decoder is use to decode the address of the memory block 
As an example we have 1024x8 block of memory array.So there are 1024 row and 8 column in the memory array. Memory array contain the memory cells i.e TTL Memory cell.The word size of the block memory that we are considering guess that it is 8 bit long. So, to address the column cells only 3 bit Colum address decoder is need to address the memory array.on the other hand to select 1024 bit of the row cells 10 bit row address decoder is sufficient. When the row and column decoder decode an address (i.e row 512, column 1 , row 512 colum 2 ) associated data which reside inside the memory is place into the buffer. Then the data is available to the data bus in the next clock cycle. 

ROM



The whole operation need some time to complete. This is know as a ROM speed.

according to it programming (data write inside the memory , the way we put data into the ROM memory cell)  ROM can defined as 
1. Mask Programmed ROM

The programming is done on manufacturer side according the customer demands. Once it is done it can not be modified.We known term is OTP ( one time programming.)

2.Programmable ROM 
The programming is done on customer side. It is like burn the memory cell to have value 0 / 1 but once it is done it can not be reprogrammed.

3.Erasable Programmable ROM
(nonvolatile read/write, re programmable )
The programming is done on customer. It can be reprogrammed. The programming is done by ultra violet ray or electrically (EEPROM , in-circuit electrical erasability ).

4.Flash 

re programmable memories are high density. it combines the low cost and high density features of an UV EPROM /EEPROM.It is called a flash memory because of its rapid erase and write times.
Most flash memory devices use a ‘bulk erase’ operation in which all the memory cells on the chip are erased simultaneously. Some flash memory devices offer a ‘sector erase’ mode in which specific sectors of the memory device can be erased at a time .