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