Difference Between 32 bit and 64 bit operating system

We often come across the 32 bit and 64 bit operating systems in the computer world. Let us see the difference between those two.

What is 32bit and 64 bit?

32bit and 64 bit are the term whis is used to mention the bytes of data which can be processed by the RAM from processor in a single time. Simply we can say 32 bit OS means A processor can Read/Write 2^32 of bytes of data in RAM for a single machine cycle. and 64 bit OS means A processor can Read/Write 2^64 of bytes of data in RAM for a single machine cycle.

Let us go in deep

2^32 =   4294967296

So a 32 bit OS suits up to 4GB RAM, Using more than 4GB RAM for 32bit OS is useless and it will act as 4GB RAM only.

2^64 = 18446744073709551616

Yes a big number, 64 bit OS can be used for 16 exabyte of RAM. But this 16 exabyte is not for all it depends on the OS and processor.

Now a days all the OS and processors are upgrading to 64 bit only so the next think about the bit of OS processor and RAM before choosing it.

Cheers!!!!
-> Let Us Embed <-

This expression uses old style casts - How to avoid??

In C/C++ we often use a regular type of type casting i.e (expected_type) value.

This type of type casting is applicable in C only. You can use the same type in C++ also but it will show a warning This expression uses old style casts. If you want to over come this warning you need to use the new style of type casting and this is applicable only in C++. Let us see how can we over come this warning.!

New style of type casting

The new style of type casting comes with 4 types, they are

  1. static_cast
  2. dynamic_cast
  3. const_cast
  4. reinterpret_cast

1.static_cast

It is used as a simple type of cast which we often use (implicit conversion), byte to word, int to float, etc.
static_cast<float>(variable_name)

2.dynamic_cast 

It works only when source type is polymorphism. We can type cast any pointer or reference to any polymorphism type, also we can say it converts a pointer or reference within a hierarchy
dynamic_cast<expected_type *>(Source_Var_Name)

3.const_cast

You can use this when you want to strips away the const requirement.

4. reinterpret_cast

It is used when low level reinterpreting a bit pattern. It should be used with a bit pattern alignment order caution in your mind.

This is how you can overcome the warning "This expression uses old style casts"


Cheers!!!!
-> Let Us Embed <-

How to Set/Clear/Toggle multiple bit using C/C++

You may need to Set /Clear/Toggle multiple bit using C/C++ in your application. Let us see how we can achieve this in this tutorial.
Refer this before going down
Formula
Register_Value = Register_Value | (1<<X)
Register_Value = Register_Value & (~(1<<X))
Register_Value = Register_Value ^ (1<<X)
In all the listed formula above has common thing as 1<<X
here X is the position of the bit you want to set/clear/toggle
and 1 means binary one that is 1 bit

If you want to select two bit use 3<<X
3 in binary is 11

Cheers!!!!
-> Let Us Embed <-

Embedded interview questions - In general embedded - Part 2

1)How to achieve quadrature encoder in Software?
http://letusembed.blogspot.com/2015/11/quadrature-encoder-in-c-quadrature.html

2)Differentiate Prescalar and Postscalar or what is prescalar and postscalar
http://letusembed.blogspot.com/2015/11/what-is-prescalar-and-postscalar.html

3)How to check a particular bit in a register is set or not using C?
http://letusembed.blogspot.com/2015/08/how-to-check-bit-is-set-or-not-in.html

4)How to set a particular bit using C?
http://letusembed.blogspot.com/2015/12/how-to-set-clear-and-toggling.html

5)How to clear a particular bit in C?
http://letusembed.blogspot.com/2015/12/how-to-set-clear-and-toggling.html

6)How to toggle a particular bit in C?
http://letusembed.blogspot.com/2015/12/how-to-set-clear-and-toggling.html

7)How to set/Clear/Toggle multiple bit using C/C++?
How to set/clear/toggle multiple bit?

8)How to avoid This expression uses old style of cast?
http://letusembed.blogspot.com/2015/12/this-expression-uses-old-style-casts.html

9)What is a new style of cast?
http://letusembed.blogspot.com/2015/12/this-expression-uses-old-style-casts.html

10) Difference between 32 bit and 64 bit OS and Processors?
http://letusembed.blogspot.com/2015/12/difference-between-32-bit-and-64-bit.html

11)What is the Maximum limit of RAM you can choose for 32bit processor?
http://letusembed.blogspot.com/2015/12/difference-between-32-bit-and-64-bit.html

12)What is the Maximum limit of RAM you can choose for 64 bit processor?
http://letusembed.blogspot.com/2015/12/difference-between-32-bit-and-64-bit.html

Cheers!!!
-> Let us Emebed -<-

How to Set, Clear and toggling a particular bit in C, C++

In embedded world you may be need to set a particular bit while configuring a register, clear a bit when decoding a data and then toggle a bit in applications like external watchdog timer services. In this tutorial let us see how can we achieve this with C/C++ language.

Setting a particular bit

Bit wise OR (|) is used for setting a bit

Formula

Register_Value = Register_Value | (1<<X)
here, X is the position of the bit which you want to set.
Example
Consider a register with 1 byte of size.
It has value of 11010001, If i want to set the 2nd bit in this register
Register_Value = 11010001 | (1<<2)
Register_Value = 11010001 | 00000100
Register_Value = 11010101

Clearing a particular bit

Bit wise AND(&) is used for clearing a bit

Formula

Register_Value = Register_Value & (~(1<<X))
here, X is the position of the bit which you want to clear.
Example
Consider a register with 1 byte of size.
It has value of 11010101, If i want to set the 2nd bit in this register
Register_Value = 11010101 & (~(1<<2))
Register_Value = 11010101 & (~00000100)
Register_Value = 11010101 & 11111011
Register_Value = 11010001

Toggling a particular bit

Bit wise XOR(^) is used for toggling a bit

Formula

Register_Value = Register_Value ^ (1<<X)
here, X is the position of the bit which you want to toggle.
Example
Consider a register with 1 byte of size.
It has value of 11010101, If i want to toggle the 2nd bit in this register
Register_Value = 11010101 ^ (1<<2)
Register_Value = 11010101 ^ 000000100
Register_Value = 11010101

Also refer
How to Check a particular bit is set or not ?
How to set/clear/toggle multiple bit?

Cheers!!!!
-> Let Us Embed <-

What is Prescalar and Postscalar

Prescalar and Postscalar:


  • Prescalar divides the input pulse before the counter and Postscalar divides the output of the counter. 
  • Both effectively increase the range of the counter. 
  • Both prescalar and postscalar used to get our desired frequency from basic reference. 
  • The prescalar divides the reference frequency by some specific factor (which is entered into clock register/PLL register) and thus helps in reducing frequency, Postscalar does the opposite function. 
Cheers!!!!
-> Let us Embed <-

Quadrature encoder in C || Quadrature encoder by Software

Let us See how can we achieve a quadrature encoder in c (by software) in this tutorial

What is a Quadrature encoder?

It is a encoder with two channels where any change(raise or fall) in one channel (Say Channel A) will result change (raise or fall) in the other channel (Say Channel A) with 90deg phase shift.

This picture will help you to understand the quadrature encoder better


Here for a clock wise rotation of the encoder Channel A is leading Channel B and vise-versa for the anti clock wise rotation. As explained you can see the phase differences of 90deg between two channels by probing two channels.

Main advantage of quadrature encoder is capturing both the edges (rising and falling) for two channels (Channel A and Channel B) as a interrupt will result more number of pulses from the encoder.

How to find the direction with software?

You can find the direction using the below table

Interrupt From Channel
Status of Channel A
Status of Channel B
Direction

Channel A
High
Low
Clock wise rotation
Channel A
Low
High
Clock wise rotation
Channel B
High
High
Clock wise rotation
Channel B
Low
Low
Clock wise rotation
Channel A
High
High
Anti-Clock wise rotation
Channel A
Low
Low
Anti-Clock wise rotation
Channel B
High
Low
Anti-Clock wise rotation
Channel B
Low
High
Anti-Clock wise rotation

Program implementation:

Configure both Channel A and Channel B interrupts
Inside_Channel_A_ISR
{
       Clear_the_interrupt_if_needed;
       if(Stats_of_Channel_A != Status_of_Channel_B)
       {
              increment_Clock_wise_counter ++;
       }
       else
       {
             increment_counter_Clock_wise_counter ++;
       }
}
Inside_Channel_B_ISR
{
       Clear_the_interrupt_if_needed;
       if(Stats_of_Channel_B == Status_of_Channel_A)
       {
              increment_Clock_wise_counter ++;
       }
       else
       {
             increment_counter_Clock_wise_counter ++;
       }
}

If you can able to find the type of interrupt like raising or falling it will be easy for configuration.


Cheers!!!!
-> Let us Embed <-

How to check a bit is Set or Not in Embedded C ??

In Embedded programming we may need to identify a particular bit is Set or Not in many places like Interrupt Service Routine. Let us See how we can do it in the C programming Language.

Click here to know more about Bitwise (&,~,|) operators in C before going to this tutorial.

#include <stdio.h>
int main()
{
int a,b;
a = 118;/*1110110*/
b = 8;/*1000*/
if(a & b)
{
printf("3rd bit is set");
}
else
{
printf("3rd bit is not set");
}
return 0;
}

here a = 11110110 and b = 1000 
11110110
00001000  &
-----------------
00000000

So it(a &b) will return 0 (False)

#include <stdio.h>
int main()
{
int a,b;
a = 118;/*1110110*/
b = 2;/*10*/
if(a & b)
{
printf("2nd bit is set");
}
else
{
printf("2nd bit is not set");
}
return 0;
}

here a = 11110110 and b = 10 
11110110
00000010  &
-----------------
0 0000010
So it (a&b) will return 2. In C anything which is not equal to 0 is considered as True so the above program will return in "2nd bit is set"

Also Check
How to Set a particular bit ?
How to toggle a particular bit?
How to Clear a particular bit?
How to set/clear/toggle multiple bit?


Cheers!!!!
-> Let us Embed <-

8051 Timers

8051 comes with 3 number of timers. All 3 timers can be configured individually. A timer in most of the controllers has the following 3 functionalities.

They are,


  1. Calculating time between the events
  2. Generating baud rates in serial communication
  3. Counting the events(Counter)
Using timers to generate precise time delays...
One of the most important use of Timers is to generate delays that are very precise.

Most of the 8051 family microcontrollers completes one machine cycle using 12 cpu cycles.

Meaning to say most of the instructions takes total of 12 clock pulses for their execution.

we are using master clock of 11.30592 MHz that means , time period for 1 machine cycle is 1.085uSec. How???

Fcpu=11.0592MHz

12 Cycles required for one machine cycle

Finstruction=(11.0592/12) MHz => 921.6 KHz

Time period of each machine cycle = 1/921.6 msec.

so... Tmachine cycle = 1.085 uSec

It needs 1.085 usec to execute one instruction.

Timer counts up by one in 1.085 uSec

so that Desired delay = No of counts x 1.085 usec

All we need to know is No of counts required to generate a particular amount of delay.

For example we need to generate a delay of 20 mSec. For that first we need to calculate amount of timer counts required to pass 20 msec.

Timer counts required = 20000 / 1.085 => 18433 counts

Now lets see how to implement this whole delay generation logic in a 'C' program.

First we need to know the various registers needed to control timer operations. 8051 derivative has total of 2, 16 - bit timers. while 8051 has 3 timers. But we are going to discuss 8051 only.

Timer SFR's

Both Timer0 and Timer1 shares two common SFR's known as TMOD and TCON which controls timer operation.
In addition, each timer also has two SFR dedicated to its own operation only.
TH0,TL0 for Timer0 and TH1,TL1 for Timer1.

SFR nameDescriptionSFR addressBit addressable
  TH0    Timer0 High Byte    0x8C    No  
  TL0    Timer0 Low Byte    0x8A    No  
  TH1    Timer1 High Byte    0x8D    No  
  TL1    Timer1 Low Byte    0x8B    No  
  TCON    Timer Control Register    0x88    Yes  
  TMOD    Timer Mode Control    0x89    No  
Lets first talk about our first control SFR: TMOD (Timer Mode). The TMOD SFR is used to control the mode of operation of both timers. Each bit of the SFR gives the microcontroller specific information concerning how to run a timer. The high four bits (bits 4 through 7) relate to Timer 1 whereas the low four bits (bits 0 through 3) perform the exact same functions, but for timer 0.

The individual bits of TMOD have the following functions:


13 Bit Timer Mode

When the timer is in 13-bit mode, TLx will count from 0 to 31. When TLx is incremented from 31, it will "reset" to 0 and increment THx. Thus, effectively, only 13 bits of the two timer bytes are being used: bits 0-4 of TLx and bits 0-7 of THx. This also means, in essence, the timer can only contain 8192 values. If you set a 13-bit timer to 0, it will overflow back to zero 8192 instruction cycles later.

16 Bit Timer Mode

Timer mode "1" is a 16-bit timer. This is a very commonly used mode. It functions just like 13-bit mode except that all 16 bits are used. TLx is incremented from 0 to 255. When TLx is incremented from 255, it resets to 0 and causes THx to be incremented by 1. Since this is a full 16-bit timer, the timer may contain up to 65536 distinct values. If you set a 16-bit timer to 0, it will overflow back to 0 after 65,536 machine cycles.

8 Bit Timer Mode

Timer mode "2" is an 8-bit auto-reload mode. What is that, you may ask? Simple. When a timer is in mode 2, THx holds the "reload value" and TLx is the timer itself. TLx starts counting up. When TLx reaches 255 and is subsequently incremented instead of resetting to 0 (as in the case of modes 0 and 1) it will be reset to the value stored in THx.

Split Timer Mode

Timer mode "3" is a split-timer mode. When Timer 0 is placed in mode 3, it essentially becomes two separate 8-bit timers. That is to say, Timer 0 is TL0 and Timer 1 is TH0. Both timers count from 0 to 255 and overflow back to 0. All the bits that are related to Timer 1 will now be tied to TH0 and all the bits related to Timer 0 will be tied to TL0.

While Timer 0 is in split mode, the real Timer 1 (i.e. TH1 and TL1) can be put into modes 0, 1 or 2 normally--however, you may not start or stop the real timer 1 since the bits that do that are now linked to TH0. The real timer 1, in this case, will be incremented every machine cycle no matter what.

The only real use I can see of using split timer mode is if you need to have two separate timers and, additionally, a baud rate generator. In such case you can use the real Timer 1 as a baud rate generator and use TH0/TL0 as two separate timers.



Cheers!!!!
-> Let us Embed <-

Embedded interview questions - In micro-controller - Part 1

1) What is SFR ?

http://letusembed.blogspot.com/2015/04/what-is-special-function-register-sfr.html

2) What is I/O ports in Microcontroller ?

http://letusembed.blogspot.com/2015/04/inputoutput-ports-in-micro-controller.html

3) Explain Memory unit in micro controller ?

http://letusembed.blogspot.com/2015/04/memory-unit-in-microcontroller-embedded.html

4) Explain about ROM ?

http://letusembed.blogspot.com/2015/04/memory-unit-in-microcontroller-embedded.html

5) What is Masked ROM ?

http://letusembed.blogspot.com/2015/04/memory-unit-in-microcontroller-embedded.html

6) What is OTP ROM ?

http://letusembed.blogspot.com/2015/04/memory-unit-in-microcontroller-embedded.html

7) Explain UV EPROM ?

http://letusembed.blogspot.com/2015/04/memory-unit-in-microcontroller-embedded.html

Explain about flash Memory ?

8) Explain about RAM ?

http://letusembed.blogspot.com/2015/04/memory-unit-in-microcontroller-embedded.html

9) Explain about EEPROM ?

http://letusembed.blogspot.com/2015/04/memory-unit-in-microcontroller-embedded.html

10)What is Interrupt ?

http://letusembed.blogspot.com/2015/04/what-is-interrupt-embedded-interview.html

11) Explain about CPU in Microcontroller ?

http://letusembed.blogspot.com/2015/04/central-processor-unit-cpu-in.html

12) What is BUS in microcontroller ?

http://letusembed.blogspot.com/2015/04/what-is-bus-in-microcontroller-embedded.html

13) What is Serial Communication ? Explain about it.

http://letusembed.blogspot.com/2015/04/what-is-serial-communication-embedded.html

14) Explain about I2C ?

http://letusembed.blogspot.com/2015/04/what-is-i2c-inter-integrated-circuit.html

15) Explain about SPI ?

http://letusembed.blogspot.com/2015/04/what-is-spi-serial-peripheral-interface.html

16) What is UART ? Explain about it.

http://letusembed.blogspot.com/2015/04/what-is-uart-universal-asynchronous.html

17) What is the use of Oscillator in Microcontroller ?

http://letusembed.blogspot.com/2015/04/what-is-oscillator-in-microcontroller.html

18) What is Timer ?

http://letusembed.blogspot.com/2015/04/timerscounters-concepts-microcontroller.html

19) What is Counter ?

http://letusembed.blogspot.com/2015/04/timerscounters-concepts-microcontroller.html

20) Explain about ADC ?

http://letusembed.blogspot.com/2015/04/what-is-ad-converter-microcontroller.html


Cheers!!!!
-> Let Us Embed <-

7 segment LED display 0 to 9 using 8051 AT89S59 self learning development board - Video tutorial

Here We are going to interface 7 segment LED display to 8051 AT89S59. Then we will be displaying 0 to 9 aka it will start to count 0 to 9.

Hardware Connections:


P1_0 to A
P1_1 to B
P1_2 to C
P1_3 to E
P1_4 to F
P1_5 to G
P1_6 to DEC

0 to +5

Program:  

#include<at89x52.h>
int a = 100;
int i;
unsigned char displayValue[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
void delay() {
while(a--);
}
void main() {
while(1) {
for(i=0;i<10;i++) {
delay();
P1 = displayValue[i];
}
}
}

Explanation:

Delay function:

This will give some delay.
Need more explanation on delay function.

Main Loop:

As we know basic concept of displaying values in 7 segment LED display.
The algorithm for this 0 to 9 counting program is with delay and for loop.

Inside super loop,

for loop will be executed 10 times first it will give some delay. Then i'th value of displayValue array is assigned to Port 1 so we can see 0 to 9 is counting.

How displayValue array is initialized with hex values.

Working Video:

Will be available soon !! :)

Cheers!!!!
-> Let Us Embed <-

7 Segment LED display C program using 8051 AT89S52 self learning developer kit

This is a simple program on interfacing 7 segment LED display with 8051 AT89S52. In this program we will just move 0x07 to P1 port in AT89S52.  Which means 7 segment LED will display Number 7.

Hardware Connection:



P1_0 to A
P1_1 to B
P1_2 to C
P1_3 to E
P1_4 to F
P1_5 to G
P1_6 to DEC

0 to +5v

Program:

#include<at89x52.h>
void main() {
P1 = 0x07;
}

Explanation:

In the main function we are just assigning 0x07 to Pos=rt 1.

Hex value for 0 - 9 digits

Digit
Dp
g
f
e
d
c
b
a
Hex value
0
0
0
1
1
1
1
1
1
0x3f
1
0
0
0
0
0
1
1
0
0x06
2
0
1
0
1
1
0
1
1
0x5b
3
0
1
0
0
1
1
1
1
0x4f
4
0
1
1
0
0
1
1
0
0x66
5
0
1
1
0
1
1
0
1
0x6d
6
0
1
1
1
1
1
0
1
0x7d
7
0
0
0
0
0
1
1
1
0x07
8
0
1
1
1
1
1
1
1
0x7f
9
0
1
1
0
0
1
1
1
0x67

Output:



Cheers!!!!
-> Let Us Embed <-

Blink LED with single push button using 8051 AT89S52 self learning developer kit - Video tutorial

In this tutorial We are going to use a single push button. When it is first pressed LED will be turned ON. When it is pressed next LED will be turned off.
How to turn on and off LED with 2 push buttons?

Hardware Connection:


The i/o connections are,

Input

P0_0 to Gnd, +5 to SW1

Output

P1_0 to LED

Program:

#include<at89x52.h>
void main() {
P0_0 = 0;
P1_0 = 0;
while(1) {
if(P0_0 == 1) {
while(P0_0 != 0);
P1_0 = ~ P1_0;
}
}
}

Explanation:

P0_0 for input i.e push button.
P1_0 for output i.e LED.

Inside the super loop, 

if P0_0 is equal to 0 aka push button is pressed toggle the led state. So if the LED is Off then turn On the LED and vice versa.

while(P0_0 != 0) 

It is added for debounce. This is a software debounce it simply means waiting for push button to release.

How to turn on and off LED with 2 push buttons?

Working Video:

Will be available soon!! :)


Cheers!!!!
-> Let Us Embed <-

Push button check program using 8051 AT89S52 self learning developer kit

In this tutorial we are going to turn on the LED when first push button is pressed, turn off the LED when the other push button is pressed. In 8051 AT89S52 self learning developer kit.

Hardware connection


Input

P0_0 to Gnd, +5 to SW1
P0_1 to Gnd, +5 to SW2

Output

P1_0 to LED

Program


#include<at89x52.h>
void main() {
P0 = 0x00;
P1 = 0;
while(1) {
while(P0_0 == 1) {
P1 = 1;
}
while (P0_1 == 1){
       P1 = 0;
}
}
}

Explanation:

P0 port is dedicated for input i.e push button.
P1 port is dedicated for output i.e LED.

First it is set to 0 aka LOW.

Then While(1) aka super loop starts. It has two while loops. They are,
what is super loop ??

 while(P0_0 == 1)

Inside this P1 = 1 is written which means when the first push button is pressed light or turn on the LED.

while (P0_1 == 1)

Inside this P1 = 0 is written which means when the first push button is pressed turn off the LED.

Working video:

Will be available soon!! :)


Cheers!!!!
-> Let Us Embed <-

Analog to Digital Converter (ADC) in PIC16C73/73A/74/74A PIC Microcontroller

A/D Converter

Apart from a large number of digital I/O lines, the PIC16C7X contains 14 analog inputs. They enable the microcontroller to recognize, not only whether a pin is driven to logic zero or one (0 or +5V), but to precisely measure its voltage and convert it into a numerical value, i.e. digital format. The whole procedure takes place in the A/D converter module which has the following features:
·       The converter generates a 10-bit binary result using the method of successive approximation and stores the conversion results into the ADC registers (ADRESL and ADRESH);
·       There are 14 separate analog inputs;
·       The A/D converter allows conversion of an analog input signal to a 10-bit binary representation of that signal; and
·       By selecting voltage references Vref- and Vref+, the minimal resolution or quality of conversion may be adjusted to various needs.

ADC Mode and Registers
Even though the use of A/D converter seems to be very complicated, it is basically very simple, simpler than using timers and serial communication module, anyway.
Fig. 1 ADC Mode and Registers
The module is under the control of the bits of four registers:
·       ADRESH - Contains high byte of conversion result;
·       ADRESL - Contains low byte of conversion result;
·       ADCON0 - Control register 0; and
·       ADCON1 Control register 1

ADRESH and ADRESL Registers
When converting an analog value into a digital one, the result of the 10-bit A/D conversion will be stored in these two registers. In order to deal with this value easier, it can appear in two formats- left justified and right justified. The ADFM bit of the ADCON1 register determines the format of conversion result (see figure 2). In the event that A/D converter is not used, these registers may be used as general-purpose registers.
Fig. 2 ADRESH and ADRESL Registers
A/D Acquisition Requirements
For the ADC to meet its specified accuracy, it is necessary to provide a certain time delay between selecting specific analog input and measurement itself. This time is called "acquisition time" and mainly depends on the source impedance. There is an equation used for accurately calculating this time, which in the worst case amounts to approximately 20uS. Briefly, after selecting (or changing) the analog input and before starting conversion it is necessary to provide at least 20uS time delay to enable the ACD maximal conversion accuracy.
ADC Clock Period
Time needed to complete a one-bit conversion is defined as TAD. The required TAD must be at least 1,6 uS. One full 10-bit A/D conversion is a bit longer than expected and amounts to 11 TAD periods. However, since both the conversion clock frequency and source are determined by software, one of the available combination of bits ADCS1 and ADCS0 should be selected before voltage measurement on some analog input starts. These bits are stored in the ADCON0 register.
ADC Clock Source
ADCS1
ADCS0
Device Frequency (Fosc)
20 Mhz
8 Mhz
4 Mhz
1 Mhz
Fosc/2
0
0
100 nS
250 nS
500 nS
2 uS
Fosc/8
0
1
400 nS
1 uS
2 uS
8 uS
Fosc/32
1
0
1.6 uS
4 uS
8 uS
32 uS
Frc
1
1
2 - 6 uS
2 - 6 uS
2 - 6 uS
2 - 6 uS
Table 1 ADC Clock Period
Any change in the system clock frequency will affect the ADC clock frequency, which may adversely affect the ADC result. Device frequency characteristics are shown in the table above. The values in the shaded cells are outside of recommended range.
How to Use A/D Converter?
In order to enable the A/D converter to run without problems as well as to avoid unexpected results, it is necessary to consider the following:
·       A/D converter does not differ between digital and analog voltages. In order to avoid errors in measurement or chip damage, the pins should be configured as analog inputs before conversion starts. The bits used for this purpose are stored in the TRIS and ANSELH registers;
·       When the port with analog inputs marked as CH0-CH13 is read, the corresponding bits will be driven to logic zero (0); and
·       Roughly speaking, voltage measurement in the converter is based on comparing input voltage with internal scale which has 1024 marks (210=1024). The lowest scale mark stands for the Vref- voltage, whilst the highest mark stands for the Vref+ voltage. Figure 7-3 below shows selectable referent voltages and their minimum and maximum values as well.
Fig. 3 How to Use The A/D Converter
ADCON0 Register

Fig. 4 ADCON0 Register
ADCS1, ADCS0 - A/D Conversion Clock Select bits select clock frequency used for internal synchronization of A/D converter. It also affects duration of conversion.
ADCS1
ADCS2
Clock
0
0
Fosc/2
0
1
Fosc/8
1
0
Fosc/32
1
1
RC *
Table 2 A/D Conversion Select Bits
* Clock is generated by internal oscillator which is built in converter.
CHS3-CHS0 - Analog Channel Select bits select a pin or an analog channel for conversion, i.e. voltage measurement:
CHS3
CHS2
CHS1
CHS0
Channel
Pin

0
0
0
0
0
RA0/AN0

0
0
0
1
1
RA1/AN1

0
0
1
0
2
RA2/AN2

0
0
1
1
3
RA3/AN3

0
1
0
0
4
RA5/AN4

0
1
0
1
5
RE0/AN5

0
1
1
0
6
RE1/AN6

0
1
1
1
7
RE2/AN7

1
0
0
0
8
RB2/AN8

1
0
0
1
9
RB3/AN9

1
0
1
0
10
RB1/AN10

1
0
1
1
11
RB4/AN11

1
1
0
0
12
RB0/AN12

1
1
0
1
13
RB5/AN13

1
1
1
0
CVref
1
1
1
1
Vref = 0.6V
Table 3 Analog Channel Status Bits
GO/DONE - A/D Conversion Status bit determines current status of conversion:
·       1 - A/D conversion is in progress; and
·       0 - A/D conversion is complete. This bit is automatically cleared by hardware when the A/D conversion is completed.
ADON - A/D On bit enables A/D converter.
·       1 - A/D converter is enabled; and
·       0 - A/D converter is disabled.

ADCON1 Register

Fig. 5 ADCON1 Register
ADFM - A/D Result Format Select bit
·       1 - Conversion result right justified. Six most significant bits of the ADRESLH are not used; and
·       0 - Conversion result left justified. Six least significant bits of the ADRESL are not used.
VCFG1 - Voltage Reference bit selects negative voltage reference source needed for A/D converter operating.
·       1 - Negative voltage reference is applied on the Vref- pin; and
·       0 - Voltage power supply Vss is used as negative voltage reference source.
VCFG0 - Voltage Reference bit selects positive voltage reference source needed for A/D converter operating.
·       1 - Positive voltage reference is applied on the Vref+ pin; and
·       0 - Voltage power supply Vdd is used as positive voltage reference source.

In Short:
In order to measure voltage on an input pin by A/D converter the following should be done:
Step 1 - Configuring port:
·       Write logic one (1) to the corresponding bit of the TRIS register to configure it as input; and
·       Write logic one (1) to the corresponding bit of the ANSEL register to configure it as analog input.
Step 2 - Configuring ADC module:
·       Configure voltage reference in the ADCON1 register;
·       Select ADC conversion clock in the ADCON0 register;
·       Select one of input channels CH0-CH13 of the ADCON0 register;
·       Select data format using the ADFM bit of the ADCON1 register; and
·       Enable A/D converter by setting the ADON bit of the ADCON0 register.
Step 3 - Configuring ADC interrupt (optionally):
·       Clear the ADIF bit; and
·       Set the ADIE, PEIE and GIE bits.
Step 4 - Wait for the required acquisition time (approximately 20uS) to pass.
Step 5 - Start conversion by setting the GO/DONE bit of the ADCON0 register.
Step 6 - Wait for ADC conversion to complete.
·       It is necessary to check in program loop whether the GO/DONE pin is cleared or wait for an A/D interrupt (must be previously enabled).
Step 7 - Read ADC results:
·       Read the ADRESH and ADRESL registers.

Analog Comparator
In addition to A/D converter, there is one more module, which until quite recently has been embedded only in integrated circuits, belonging to so called analog electronics. Owing to the fact that it is hardly possible to find any more complex automatic device which in some way does not use these circuits, two high quality comparators along with additional electronics are integrated into the microcontroller and connected to its pins.
How does a comparator operate? Basically, the analog comparator is an amplifier which compares the magnitude of voltages at two inputs. Looking at its physical features, it has two inputs and one output. Depending on which input has a higher voltage (analog value), a logic zero (0) or logic one (1) (digital values) will appear on its output:
Fig. 6 Analog Comparator
·       When the analog voltage at Vin- is higher than the analog voltage at Vin+, the output of the comparator is a digital low level; and
·       When the analog voltage at Vin+ is higher than the analog voltage at Vin-, the output of the comparator is a digital high level.
The PIC16C7X microcontroller has two such voltage comparators whose inputs are connected to I/O pins RA0-RA3, whereas the outputs are connected to the pins RA4 and RA5. In addition there is also a referent voltage internal source on chip itself, but it will be discussed later.
These two circuits are under control of the bits stored in the following registers:
CM1CON0 is in control of comparator C1;
CM2CON0 is in control of comparator C2; and
CM2CON1 is in control of comparator C2.

Voltage Reference Internal Source
One of two analog voltages provided on the comparator inputs is usually stable and unchangeable. Because of those features it is called "voltage reference"(Vref). To generate it, both external and special internal voltage source can be used. After selecting voltage source, Vref is derived from it by means of ladder network consisting of 16 resistors which form voltage divider. The voltage source is selectable through both ends of that divider through the VRSS bit of the VRCON register.
In addition, the voltage fraction provided by resistor ladder network may be selected through the bits VR0-VR3 and used as voltage reference. See figure below.
Fig. 7 VREF
The comparator voltage reference has 2 ranges with 16 voltage levels in each range. Range selection is controlled by the VRR bit of the VRCON register. The selected voltage reference may be output to the RA2/AN2 pin.
Even though the main idea was to obtain varying voltage reference for the operation of analog modules, a simple A/D converter is obtained in that way too. This converter is very useful in some situations.
It's operation is under control of the VRCON register.
Comparators and Interrupt Operation
The flag bit CMIF of the register PIR is set on every change of logic state on any comparator's output. The same changes also cause an interrupt if the following bits are set:
CMIE bit of the PIE register;
PEIE bit of the INTCON register; and
GIE bit of the INTCON register.
If interrupt is enabled, any change on the comparator's output can wake up the microcontroller from sleep mode if it is setup in that mode.
CM1CON0 Register
Fig. 8 CM1CON0 Regsiter
Bits of this register are in control of the comparator C1. It mainly affects configuration of its inputs. To understand it better, look at figure 7-9 below which shows only a part of electronics directly affected by the bits of this register.
Fig. 9 Comparator C1 Enable Bit
C1ON - Comparator C1 Enable bit enables comparator C1.
·       1 - Comparator C1 is enabled; and
·       0 - Comparator C1 is disabled.
C1OUT - Comparator C1 Output bit is comparator C1 output bit.
If C1POL = 1 (comparator output is inverted)
·       1 - Analog voltage at C1Vin+ is lower than analog voltage at C1Vin-; and
·       0 - Analog voltage at C1Vin+ is higher than analog voltage at C1Vin-.
If C1POL = 0 (comparator output is non-inverted)
·       1 - Analog voltage at C1Vin+ is higher than analog voltage at C1Vin-; and
·       0 - Analog voltage at C1Vin+ is lower than analog voltage at C1Vin-.
C1OE Comparator C1 Output Enable bit.
·       1 - Comparator C1OUT output is connected to the C1OUT pin.*; and
·       0 - Comparator output is internal only.
* In order to enable the C1OUT bit to be present on the pin, two conditions must be met: C1ON = 1 (comparator must be on) and the corresponding TRIS bit = 0 (pin must be configured as output).
C1POL - Comparator C1 Output Polarity Select bit enables comparator C1 out put state to be inverted.
·       1 - Comparator C1 output is inverted; and
·       0 - Comparator C1 output is non-inverted.
C1R - Comparator C1 Reference Select bit
·       1 - Non-inverting input C1Vin+ is connected to reference voltage C1Vref; and
·       0 - Non-inverting input C1Vin+ is connected to the C1IN+ pin.
C1CH1, C1CH0 - Comparator C1 Channel Select bit
C1CH1
C1CH0
Comparator C1Vin- input
0
0
Input C1Vin- is connected to the C12IN0- pin
0
1
Input C1Vin- is connected to the C12IN1- pin
1
0
Input C1Vin- is connected to the C12IN2- pin
1
1
Input C1Vin- is connected to the C12IN3- pin
Table 4 Comparator C1
CM2CON0 Register
Fig. 10 CM2CON0 Regsiter
Bits of this register are in control of comparator C2. Similar to the previous case, the figure 7-11 shows a simplified schematic of the circuit affected by the bits of this register.
Fig. 11 Comparator C2 Schematic Diagram
C2ON - Comparator C2 Enable bit enables comparator C2.
·       1 - Comparator C2 is enabled; and
·       0 - Comparator C2 is disabled.
C2OUT - Comparator C2 Output bit is comparator C2 output.
If C2POL = 1 (comparator output inverted)
·       1 - Analog voltage at C1Vin+ is lower than analog voltage at C1Vin-; and
·       0 - Analog voltage at C1Vin+ is higher than analog voltage at C1Vin-.
If C2POL = 0 (comparator output non-inverted)
·       1 - Analog voltage at C1Vin+ is higher than analog voltage at C1Vin-; and
·       0 - Analog voltage at C1Vin+ is lower than analog voltage at C1Vin-.
C2OE - Comparator C2Output Enable bit
·       1 - Comparator C2OUT output is connected to the C2OUT pin.*; and
·       0 - Comparator output is internal only.
* In order to enable the C2OUT bit to be present on the pin, two conditions must be met: C2ON = 1 (comparator must be on) and the corresponding TRIS bit = 0 (pin must be configured as output).
C2POL - Comparator C2 Output Polarity Select bit enables comparator C2 out put state to be inverted.
·       1 - Comparator C2 output is inverted; and
·       0 - Comparator C2 output is non-inverted.
C2R - Comparator C2 Reference Select bit
·       1 - Non-inverting input C2Vin+ is connected to reference voltage C2Vref; and
·       0 - Non-inverting input C2Vin+ is connected to the C2IN+ pin.
C2CH1, C2CH0 Comparator C2 Channel Select bit
C2CH1
C2CH0
Comparator C2Vin- input
0
0
Input C2Vin- is connected to the C12IN0- pin
0
1
Input C2Vin- is connected to the C12IN1- pin
1
0
Input C2Vin- is connected to the C12IN2- pin
1
1
Input C2Vin- is connected to the C12IN3- pin
Table 5 Comparator C2 Channel Select Bit
CM2CON1 Register
Fig. 12 CM2CON1 Register
MC1OUT Mirror Copy of C1OUT bit
MC2OUT Mirror Copy of C2OUT bit
C1RSEL Comparator C1 Reference Select bit
·       1 - Selectable voltage CVref is used in voltage reference C1Vref source; and
·       0 - Fixed voltage reference 0.6V is used in voltage reference C1Vref source.
C2RSEL - Comparator C2 Reference Select bit
·       1 - Selectable voltage CVref is used in voltage reference C2Vref source; and
·       0 - Fixed voltage reference 0.6V is used in voltage reference C2Vref source.
T1GSS - Timer1 Gate Source Select bit
·       1 - Timer T1gate source is T1G; and
·       0 - Timer T1gate source is comparator SYNCC2OUT.
C2SYNC - Comparator C2 Output Synchronization bit
·       1 - Comparator C2 output is synchronized to falling edge of Timer TMR1 clock; and
·       0 - Comparator output is asynchronous signal.
VRCON Register
Fig. 13 VRCON Register
VREN Comparator C1 Voltage Reference Enable bit
·       1 - Voltage reference CVref source is powered on; and
·       0 - Voltage reference CVref source is powered off.
VROE Comparator C2 Voltage Reference Enable bit
·       1 - Voltage reference CVref is connected to the pin; and
·       0 - Voltage reference CVref is disconnected from the pin.
VRR - CVref Range Selection bit
·       1 - Voltage reference source is set to low range; and
·       0 - Voltage reference source is set to high range.
VRSS - Comparator Vref Range selection bit
·       1 - Voltage reference source is in the range of Vref+ to Vref-; and
·       0 - Voltage reference source is in the range of Vdd - Vss (power supply voltage).
VR3 - VR0 CVref Value Selection
If VRR = 1 (low range)
Voltage reference is calculated using the formula: CVref = ([VR3:VR0]/24)Vdd
If VRR = 0 (high range)
Voltage reference is calculated using the formula: CVref = Vdd/4 + ([VR3:VR0]/32)Vdd

In Short:

In order to properly use built in Comparators, it is necessary to do the following:
Step 1 - Configuring module:
·       In order to select the appropriate mode, bits of the registers CM1CON0 and CM2CON0 should be configured. Interrupt should be disabled on any change of mode.
Step 2 - Configuring internal voltage reference Vref source (only when used). In the VRCON register it is necessary to :
·       Select one of two voltage ranges using the VRR bit;
·       Configure necessary Vref using bits VR3 - VR0;
·       Set the VROE bit if needed; and
·       Enable voltage Vref source by setting the VREN bit.
Formula used to calculate voltage reference:
VRR = 1 (low range)
 CVref = ([VR3:VR0]/24)VLADDER

VRR = 0 (high range)
CVref = (VLADDER/4) + ([VR3:VR0]VLADDER/32)

Vladder = Vdd or ([Vref+] - [Vref-]) or Vref+
Step 3 - Starting operation:
·       Enable interrupt by setting bits CMIE (PIE register), PEIE and GIE (both in the INTCON register);
·       Read bits C1OUT and C2OUT of the CMCON register; and
·       Read flag bit CMIF of the PIR register. After being set, this bit must be cleared in software




Cheers!!!!
-> Let Us Embed <-