Generation of PWM signal using MSP430 Launch pad timers

Before going to generating PWM signal on MSP430 Launchpad, I would like to introduce some difference between the capture and compare modes of the Timer in MSP430G2553. We know that, there are two timers categories available In MSP430 family named as Timer_A and Timer_B. Both of these have got their importance based on its implementation.
  • In Timer_B, it is possible to program the bit length like 8-bit, 10-bit, 12-bit, 16-bit, which is available in Timer_A category.
  • Some Timers belongs to Timer_B have 7 CCR registers, but the Timer_A contains three capture/compare registers (CCR).
  • Timer_B contains double buffered capture/compare registers (CCR).
  • Remember MSP430G2553 contains two 16-Timers (Timer_A0 and Timer_A1) which belong to Timer_A category. 
Basically these timers may operate in two modes.
  •  Capture mode.
  •  Compare mode.
Whereas the counting modes for these two timers are
  • Up mode.
  •  Down mode.
  •  Up/down mode.
  •  Continuous mode.
Ok! Now we will look into capture mode and compare mode.

Capture mode: It is used to record/capture the time events for speed computations and time measurement. This will be very useful Robotics applications like Frequency measurement of a signal, motor speed calculations, degree of freedom in robotic arms, etc.,

Compare mode: It is used to generate Specific time periods which replaces the delay loops in software. By doing so, the timer allows CPU to sleep by consuming less power. It is also used to generate PWM signals which is more suitable in applications like MOTOR driving, LED display intensity control, etc.,

The Timer_A in MSP430G2553 contains mainly two sections as shown in the figure 1.1
  •  Timer Block
  •  Capture/compare block
Timer Block is mainly responsible for the basic configurations of the Timer like count mode selection, clock source selection, etc., for basic time intervals generation the user may simply concentrate on this block and can easily start his programming on timers. Here I am not going to discuss about the Timer block in details rather I will concentrate on Capture/compare block. I encouraged you read my tutorial  about “UNDERSTANDING TIMERS IN MSP430 LAUNCHPAD” .Capture/compare Block contains configuration settings for capture mode and compare mode operations. There are three capture/compare blocks (CCR0, CCR1, and CCR2) available in TIMER_A in MSP430G2553.
Figure 1.1 shows the basic Timer Block and three capture/compare blocks (CCR0, CCR1 and CCR2 respectively).
Capture mode:
  • If CAP = 1, capture mode is selected.
  • CCIxA and CCIxB are the input pins for capturing an event.
  •  CCISx bits are used to select one of the capture input pins.
  •  There are three types of triggering of capture input pins (raising edge, falling edge, both) and these can selected by using CMx bits.
  • If capture occurs, Timer value is copied into the TACCRx register and the interrupt flag CCIFG is set automatically.
NOTE: The input level signal can be read at any time via the CCI bit. Therefore the capture signal is asynchronous to the timer clock and cause race condition. Setting the SCS bit will sync the capture with the next timer clock.

Compare mode:
  • Each capture/compare block contains an output unit and it is used to generate output signals such as PWM signals.
  •  To select Compare mode, CAP should be ‘0’ (CAP = 0).
  • When the count value in the ‘TAR’ register reaches the value in a TACCRx, then, CCIFG          interrupt flag is set.
  • Internal signal EQUx = 1, the EQUx affects the output according to the output mode.
  • Each output unit has eight operating modes  as shown in the figure 1.2 that generate signals based     on the 
      EQU0 and EQUx signals and it can be selected by using OUTMODx.
Figure 1.2 shows that eight operating modes of output unit and its corresponding OUTMODx bits.
Figure 1.3 shows that the generation of different output signals by configuring the output unit in different modes and the timer operates in UP mode.

FIGURE 1.3 shows different output signals by configuring output modes in UP mode operation of the timer.
Now we will switch into the program to generate the PWM output on P1.6 pin of the MSP430 Launch pad, so that green LED will flash according to duty cycle provided in the program. Here the Timer0 is source from ACLK which is sourced from VLO (12 KHz) oscillator.

Step 1: Stop the watch dog timer, to avoid unnecessary resets of the microcontroller. We will discuss more 
            on watch dog timer later.

            WDTCTL = WDTPW + WDTHOLD; // stop watch dog timer
Step 2: make cpu to run with MCLK from DCO source. You can use any clock to drive cpu.

            BCSCTL1 = CALBC1_1MHZ;            
            DCOCTL = CALDCO_1MHZ;

Step 3:  BCSCTL3 is used to low frequency crystal. LFXT1S_2 --> LFXT1 = VLO oscillator is selected.

             BCSCTL3 |= LFXT1S_2;                     

Step 4:  Configure the MCLK and SMCLK by setting proper bits BCSCTL2. SELM_0 -- selects MCLK 
             source from DCOCLK, DIVM_3 --> MCLK = DCO/8,
             We should remember that "Up on reset, MCLK and SMCLK are source from 1.1MHz DCO  
             clock source" DIVS_3 --> SMCLK = DCO/8.

             BCSCTL2 |= SELM_0 + DIVM_3 + DIVS_3;

Step 5:  Configure the GPIO port to blink the LED according to the PWM output signal. In MSP430G2553  
             port pins has multiple functionalities (like GPIO, UART, Timers, PWM, SPI, I2C, etc.,). To choose  
             a special functionality out of these many functionalities of a pin, there is a special function registers  
             P1SEL and P1SEL2 as shown in figure 1.4. Here we are using P1.6 (green LED) which has 
             “P1.6/TA0.1/UCB0SOMI/UCB0SCL/A6/CA6/TDI/TCLK” and we have to use P1.6 as 
             TA0.1. To do this, make the 6th bit of Port1 in P1SEL as ‘1’ and in P1SEL2 as ‘0’ as shown in 
             figure 1.4.
Figure 1.4 shows port pin multiplexing capability and its selection.

P1SEL2 |= ~BIT6;  
             P1SEL |= BIT6;                     // TA1 option is select, P1SEL2 and P1SEL combination is ‘01’
             Make the direction of the bit6 as output.
             P1DIR |= BIT6;                             // MAKE P1.6 (TA0.1) as output port

Step 6: Configure the TimerA to operate with ACLK which is source from VLO (12KHz) - (TASSEL = 1) 

            and in up mode (MC = 1). TACLR bit clears the TAR register, clock divider and count direction.

           TACTL = TASSEL_1 + MC_1 + TACLR;

           Load the count 12000 in counter compare register to generate 1 second delay (12000 * T) = 12000  
           * (1/12000) = 1sec; T = 1/F = 1/12 kHz. This count define the PWM period (Ton + Toff).

               CCR0 = 12000; // PWM period.
               CCTL1 = OUTMOD_7; // RESET/SET
               CCR1 = 6000-1;   // 50% duty cycle (12000/6000 = 0.5).

              And the PWM waveform will generate depend upon the duty cycle as shown in the figure 1.5
Figure 1.5 shows the PWM waveform based on the duty cycle using CCR0 and CCR1 registers.
Step 7: use infinite loop or Lower power mode (LPM). We haven’t discussed about the low power mode, 
            so I use infinite loop even though it wastages the CPU time.
             While (1);

code:
#include 

void Config_TimerA(void);
void Config_GPIO(void);


void main(void)
{

	WDTCTL = WDTPW + WDTHOLD; // Stop the Watch dog timer

	BCSCTL1 = CALBC1_1MHZ;                    // make cpu to run with MCLK from DCO source
	DCOCTL = CALDCO_1MHZ;
	BCSCTL3 |= LFXT1S_2;                      // BCSCTL3 is used to low frequency crystal. LFXT1S_2 --> LFXT1 = VLO oscillator is selected

	//In the basic clock system by default OFIFG (oscillator-fault flag) bit is set and force the MCLK to use DCO as its source.
	//IFG1 &= ~OFIFG;                           // Clear OSCFault flag not used here

	BCSCTL2 |= SELM_0 + DIVM_3 + DIVS_3;      // SELM_0 -- selects MCLK source from DCOCLK, DIVM_3 --> MCLK = DCO/8,
	// We should remember that "Up on reset, MCLK and SMCLK are source from 1.1MHz DCO clock source"
	//DIVS_3 --> SMCLK = DCO/8
	Config_GPIO();
	Config_TimerA();

	while(1);

}

void Config_GPIO(void)
{
	P1DIR |= BIT6;                             // MAKE P1.0  and P1.2 (TA0.1) as output port
	P1SEL |= BIT6; // TA1 option
}

void Config_TimerA(void)
{
	// Configure the TimerA to operate with ACLK which is source from VLO (12KHz) - (TASSEL = 1) and in up mode (MC = 1)
	TACTL = TASSEL_1 + MC_1 + TACLR;
	CCR0 = 1000-1; // PWM Period
	CCTL1 = OUTMOD_7;// Reset/set
	CCR1 = 200-1; // 50% Duty cycle
}

Understanding Timers in MSP430 Launch pad


In this tutorial, I assume that you have an idea about the basic timers in 8051 or any other microcontroller. I am not going to define the timer’s definition and its applications. Here I just concentrate on programming aspects of the timers in MSP430 microcontroller.

Generally, first question arises in our mind about the timer’s in a microcontroller is
“How many timers are there in MSP430G2553”?
There are two 16-bit timers are available in MSP430G2553, excluding watch dog timer.
  •  Timer_A0
  •  Timer_A1


While doing applications using timers, I was a bit confused about the Timer_A and Timer_B. Finally a lot of search I found some important points regarding these timers.
Generally MSP430 family contains two categories of timers
  •  Timer_A
  •  Timer_B.

What is the difference between Timer_A and Timer_B ?
Both are same in its operation, but Timer_B is more sophisticated than Timer_A and it has many features available than compared with Timer_A. They are.

·         Bit-length of the timer is programmable as 8-bit, 10- bit, 12-bit, 16-bit.
·        Some Timers in B category have 7 CCR registers whereas the Timer_A contains three capture/compare registers.
·         It contains double-buffered CCR register.
·         CCR register can be grouped.

Note: MSP430G2553 contains only Timer_A as Timer_A0 and Timer_A1.

Each 16-bit timer starts counts from 0 to 0x0FFFF (0 to 65536) and they operate in four different modes.

·         Stop     --- Timer is in halt state or stops the timer.
·         UP        --- Timer counts up from zero to value stored in TACCR0 register (other than 0xFFFF) and roll 
                         over to zero after it reached the count value as shown in figure 1.1.
                         Generally this mode used to produce time delays.
·         Continuous --- it is same as UP mode but here Timer counts up from zero to maximum value 0xFFFFh and 
                                rolls over to zero after it reached 0xFFFF and keep going as shown in figure 1.1.
·         Up/Down --- in this mode time counts up from 0 to TACCR0 register and then counts down back to zero 
                            as shown in figure. It is good for generating PWM’s and driving motors.

Figure 1.1 shows the Different timing modes in MSP430 microcontroller.




Ok! We will switch over to Programming concepts in MSP430G2553. First of all we need to know the available special functions registers and bits that are used to configure the Timers in MSP430.

Figure 1.2 Shows the Timer register and its configurable bits.
 Three compare/capture registers CCR0, CCR1, CCR2 are used load the timer count. These registers can    
 also be referred as TACCR0, TACCR1, and TACCR2,  But in this tutorial we referred simply as CCR0,  
 CCR1 and CCR2.
·         TAR is the 16-bit timer register in which the count start increment/decrements value depends
                upon the timer mode settings.
·         CCIFG interrupt flag is set when the timer counts to the value stored in CCR0 register.
·         TAIFG interrupt flag is set when the timer count from CCR0 to zero.
·         TASSELx are the bits used to select one of the clock signals as shown in the figure 1.2.
·         IDx bit are used to divide the clock signal applied to timer as shown in the figure 1.2.
·         MCx bits are used select count mode as shown in figure 1.2.
·         TACLR bit clears the TAR register, clock divider and count direction (mode).

There are two interrupt flags (CCIFG and TAIFG) and its corresponding two interrupt vectors (TACCR0 and TAIV) available for Timers in MSP430 as shown in the figure 1.3.

Figure 1.3 shows Timer interrupts and its corresponding interrupt vectors in MSP430

C Program to generate 1 sec delay using Timer_A0 which is driven by ACLK and it is sourced by low frequency oscillator (VLO). Here in this program LED blinks for every one second.

           Step 1: Stop the watch dog timer, to avoid unnecessary resets of the microcontroller. We will discuss 
                       more on watch dog timer later.

                WDTCTL = WDTPW + WDTHOLD; // stop watch dog timer.

Step 2: make cpu to run with MCLK from DCO source

    BCSCTL1 = CALBC1_1MHZ;            
                DCOCTL = CALDCO_1MHZ;

Step 3: BCSCTL3 is used to low frequency crystal. LFXT1S_2 --> LFXT1 = VLO oscillator is 
            selected.

BCSCTL3 |= LFXT1S_2;                     

Step 4: Configure the MCLK and SMCLK by setting proper bits BCSCTL2. SELM_0 -- selects 
            MCLK source from DCOCLK, DIVM_3 --> MCLK = DCO/8, We should remember  
            that "Up on reset, MCLK and SMCLK are source from 1.1MHz DCO clock source"  
            DIVS_3 --> SMCLK = DCO/8.

BCSCTL2 |= SELM_0 + DIVM_3 + DIVS_3;

Step 5: Configure the TimerA to operate with ACLK which is source from VLO (12KHz) - 
            (TASSEL = 1) and in up mode (MC = 1)

                TACTL = TASSEL_1 + MC_1;

            Enable compare register interrupt in the CCTLO (capture compare control register)

    CCTL0 = CCIE;

Load the count 12000 in counter compare register  to generate 1 second delay (12000 * T) 
= 12000 * (1/12000) = 1sec;  T = 1/F = 1/12khz

 CCR0 = 12000;

Step 6:  Configure the GPIO port to blink the LED.
Make port1 bit 0 as output, which is connected to RED LED on the LAUNCH PAD board. 
This can be done by placing 1 on direction register (P1DIR).

                P1DIR = 0x01; // (output – 1, input – 0)

 place zero on the P1.0 pin, to make output zero.

                P1OUT = 0x00; // all port1 pins are at logic low.


Step 7: Enable the global interrupt in status register (SR) via Assembly call from C using following 
            instruction.

__bis_SR_register(GIE);         

            Step 8: Use infinite loop or Lower power mode (LPM). We haven’t discussed about the low 
                        power mode, so I use infinite loop even though it wastages the CPU time.

            while(1);

Step 9: write ISR function for Timer_A0,

#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A(void)
{
              P1OUT |= 0x01;
              _delay_cycles(1000);
              P1OUT &= ~0x01;
}     



code:

A program generate 1 sec time day using Timers in MSP430 launch pad.

#include 

void Config_TimerA(void);
void Config_GPIO(void);


void main(void)
{

  WDTCTL = WDTPW + WDTHOLD; // Stop the Watch dog timer

 BCSCTL1 = CALBC1_1MHZ;
 DCOCTL = CALDCO_1MHZ;
 BCSCTL3 |= LFXT1S_2;                      // LFXT1 = VLO
 IFG1 &= ~OFIFG;                           // Clear OSCFault flag
 BCSCTL2 |= SELM_0 + DIVM_3 + DIVS_3;      // MCLK = DCO/8 , SMCLK = DCO/8
 Config_GPIO();
 Config_TimerA();
 __bis_SR_register(GIE);         


 while(1);
}


void Config_GPIO(void)
{
 P1DIR = 0x01;                             // MAKE P1.0 as output port
 P1OUT = 0x00;                             // MAKE ALL pins to logic low
}

void Config_TimerA(void)
{

 TACTL = TASSEL_1 + MC_1;
 CCTL0 |= CCIE;  
 CCR0 = 12000;  

}


#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A(void)
{
 CCR0 =+ 12000;
 P1OUT |= 0x01;
 _delay_cycles(1000);
 P1OUT &= ~0x01;
}

Understanding of Basic Clock System in MSP430 Launch pad


A clock system is a heart of microcontroller. If we know the functioning of this heart system then it is easier to study remaining modules (like Timers, communication interface, etc.,) of the microcontroller. You people may have an idea about the clock system in 8051 and other microcontrollers, off course they are simple and easy to configure. But in MSP430 there is a highly sophisticated and accurate clock generation system is available. This clock system makes the CPU (of MSP430) may operate in different  speeds  and also helps to reduce the power consumption of it by operating in different low power modes. The aim of this tutorial is to make the clock system as simple as that easily understandable for the beginners.
Basically the MSP430 device contains four clock sources (oscillators) that are used to generate three different clock signals. Because the MSP430 classifies the peripherals into two categories (slow and fast) based upon their operating speeds. This categorization of peripherals is mainly to reduce the power consumption of CPU and the peripherals, i.e., high frequency operations require more power that compared with low frequency.

MSP430G2553 Clock Sources:
·         VLOCLK –    VERY LOW FREQUENCY – LOW POWER OSCILLATOR.
·         LFXT1CLK – LOW FREQUENCY OR HIGH FREQUENCY OSCILLATOR.
·         DCOCLK –    DIGITALLY CONTROLLED OSCILLATOR.
·         XT2CLK (optional) – HIGH FREQUENCY OSCILLATOR.

MSP430G2553 Clock signals:
·         ACLK  –    for Slow Peripherals.
·         MCLK  --  to CPU and few peripherals.
·         SMCLK -- for Fast Peripherals.





FIGURE 1.1 MSP430 CLOCK SYSTEMS WITH FOUR CLOCK SOURCES AND THREE CLOCK SIGNALS.

·         VLO: Very low power – low frequency oscillator. It is especially for to generate low frequency signals and comes as internal feature to MSP430. It provides a frequency of 12KHz without any need of external crystal. It provides 500nA as stand by current.

·          LFXT1:  Low frequency or High frequency oscillator. This is operated in two modes.
o   Low Frequency (LF) mode – (XTS = 0) – 32768KHz.
o   High Frequency (HF) mode – (XTS = 1) – 400KHz – 16MHz.
o   There is an in-built OSC-Fault mechanism available in MSP430. If OSC – fails to work then it switches to alternate clock source available in the device.
o   The frequency can change by the changing the programmable capacitors present internally in the MSP430.

·    DCO: Digital Controlled Oscillator – It generates a frequency range from 1MHz – 16MHz. generally beginners may like this, because it’s easy configure than compared to other.

·         XT2: it is an optional high frequency oscillator similar to LFXT1 in HF mode. The frequency range is 400 KHz – 16MHz.  Generally some device these optional clock sources

You can use any one of the oscillator to generate the three different clock sources based on the power and speed requirement.
·         ACLK – used for slower devices and it can generate from one of either two clock sources (LFXT1 or VLO).
·         MCLK – used for CPU and some peripherals. It can generate from either DCO or VLO or Low/High frequency crystals. Hence a mux is used to multiplex both oscillators as shown in the figure 1.1.
·         SMCLK – used for High speed peripherals and it can generate from DCO oscillator only.

·     Up on reset, MCLK and SMCLK are source from 1.1MHz DCO clock source. And ACLK is sourced from LFXT1 oscillator in LF mode with 6pF load capacitance.
·         If LFXT1 fails, then the ACLK is source from the VLO oscillator as shown in the figure 1.1.
·    Clock speed should match with the required operating VCC to achieve the very lower power possible otherwise unreliable execution results if Vcc < the minimum required value for the selected frequency.

Coming to the MSP430 programming, there are three special Basic Clock Control registers are available to configure the different clocks to different modules in the MSP430 microcontroller.

BASIC CLOCK SYSTEM CONTROL REGISTERS:

·         BCSCTL1 – Used to ACLK frequency select (DIVAx bits) and LFXT1 mode select (LF and HF).
·         BCSCTL2 – Used to select MCLK and SMCLK sources (either DCO or LFXT1 or VLO).
      And also provides frequency range selection bits for MCLK (DIVMx bits) and SMCLK (DIVSx bits).
·         BCSCTL3 – used to select LFXT1 oscillator frequency range.
·         DCOCTL – DCO control register – used to select DCO frequency (1MHz, 4MHz, 8MHz, and 16MHz).

Status register contains some control bits (SCG0, SCG1, OSCOFF, and CPUOFF) used to enable or disable some portions of the Basic clock module and also used to configure the MSP430 operating modes.

C Program to generate 1 sec delay using low frequency oscillator (VLO). Here in this program LED blinks for every one second and also we can see the lowest frequency of operation. This can be achieved by taking the VLO oscillator which is the very low frequency oscillation (~12KHz) and divide it with maximum dividend which is 8 in this context.

Step 1: Stop the watch dog timer, to avoid unnecessary resets of the microcontroller. 
            We will discuss more on watch dog timer later
                WDTCTL = WDTPW + WDTHOLD; // stop watch dog timer

Step 2: Select the VLO as clock source.
                BCSCTL3 |= LFXT1S_2; // LFXT1 = VLO, check out theMSP430x2xx family 
                                                         // user  guide page number 290.

Step 3: In the basic clock system by default OFIFG (oscillator-fault flag) bit is set and force the 
           MCLK to use DCO as its source. But our intension is to make the MCLK should use VLO 
           as its source. So we need to clear the OFIFG bit.
                IFG1 &= ~OFIFG;                           // Clear OSCFault flag

Step 4: The OSC_Fault flag takes 50 milliseconds to react, so single instruction is enough to produce 
            that delay since the clock speed is 12KHz. That single instruction is used to block the DCO 
            source for MCLK. It is better to disable the DCOCLK and this can be done by setting the  
            bits ‘SCG0’ and ‘SCG1’ in the status register.
__bis_SR_register(SCG1 + SCG0);  // Stop DCO clock and this ‘__’ indicates that                                                      
                                                     // this instruction is an assembly level call from c language.

                Step 5: Now source the MCLK with the VLO oscillator and divide the value by 8 why because 
                            here we are trying to achieve the lowest frequency of operation in MSP430 (~ 1.5KHz)
                                BCSCTL2 |= SELM_3 + DIVM_3;               // MCLK = LFXT1/8 = VLO/8

   Step 6: Make port1 bit 0 as output, which is connected to RED LED on the LAUNCH PAD 
               board. This can be done by placing 1 on direction register (P1DIR)
                P1DIR = 0x01; // (output – 1, input – 0)

    Step 7: place zero on the P1.0 pin, to make output zero.
                P1OUT = 0x00; // all port1 pins are at logic low.

     Step 8: Blinking LED code using _delay_cycles() functions, this function produces delay equal to 
               that the number passed to that function. For example, if the operating clock frequency is 
               1MHz then _delay_cycles(1) produce 1microsecond delay. Likewise _delay_cycles(10) 
               produce 10 microseconds. In this program, we are operating with 1.5KHz, hence one 
               _delay_cycles(1) may take (1/1.5KHz = 0.666 milliseconds) Here one question arises that   
            how many delay cycles that are needed to produce 1 second with our clock source. Answer 
             is 1500 (1/0.66m = 1500).

While(1)              
{
P1OUT = 0X01;
_delay_cycles(1);
P1OUT = 0X00;
_delay_cycles(1);
}
               

 code:


#include  

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer
  BCSCTL3 |= LFXT1S_2;                      // LFXT1 = VLO
  IFG1 &= ~OFIFG;                           // Clear OSCFault flag
  __bis_SR_register(SCG1 + SCG0);           // Stop DCO and to wait 50 milliseconds
  BCSCTL2 |= SELM_3 + DIVM_3;               // MCLK = LFXT1/8
  P1DIR = 0x01                              // make P1.0 as output and remaing all are output
  P1OUT = 0x00;                             // All P1.x reset
  
  for (;;)
  {
    P1OUT |= 0x01;                          // P1.0 set
    _delay_cycles(100);
    P1OUT &= ~0x01;                         // P1.0 reset
    _delay_cycles(1400);
  }
}


video:

IR sensor based Distance measurement system


In this tutorial, I am going show how we can measure the distance over a short range using infrared sensors. Such application is very much useful in robotics, transportation, etc.,

For example, A robotic car which take diversions based upon the distance measure between the obstacle and the car. An application like, snake robot can measure the diameter of the hole and make a decision according to that. Similarly in transportation, inter-vehicular-distance is very important for a safe driving. The inter-vehicular-distance can be measured by using IR or UV rays sensors.

1.      Construction:

1.1  simple IR transmitter:
The IR transmitter can be designed by using simple IR diode and a 220 ohms resistor, which is shown on the circuit diagram figure 1.

1.2  Simple IR receiver:
In this application, the main objective is to measure the distance between obstacle and the IR receiver. Due to that the analog variation of the IR receiver output is taken and then connected it to an Analog to Digital converter. Here the receiver diode and a 10k ohms resistor are connected in series between supply and ground to form a voltage divider network which is shown in the circuit diagram. The principle of operation is very simple; whenever the IR light is falls on the receiver diode, the resistance offered by the IR receiver is decreases and allow more current through it. The IR receiver offers more resistance when no IR light is falls on it. Keep in mind that both IR transmitter and receiver is connected side by side, so that the reflected light from the obstacle is easily catch by the receiver.

1.3  Analog to Digital converter connections:
The IR receiver produces different voltage variations as output, depend upon the amount of IR light is reflected back by the obstacle. By using an Analog to digital converter the analog voltage variations of the IR receiver are converted into digital values. Here I used a single channel ADC (ADC0804) which contains single input channel. The output of the IR receiver is connected to pin6 (input channel VIN (+)) of the ADC0804 IC and digital output is taken from the pin 11(LSB) to 18(MSB). And pin7 VIN (-) is connected to ground.
A clock circuit is made by using simple resistor and capacitor connected to 4th and 9th pins of IC, similarly an external clock can be connected instead of R and C circuit between 4 and 19th pins of the ADC IC. Here, the product of R and C values should result 600 kHz at 5V check the data sheet, hence R = 10k and c = 150pF is used in the clock circuit. The maximum clock frequency that ADC0804 can operate at 5V is 800 kHz, whereas at 6v the clock frequency is 1200khz.

Frequency      F = 1/(1.1RC).

A 10k pot is used as external reference voltage for the ADC IC, if no external reference is used then the reference voltage = VREF/2 (or Vcc/2). This is useful to adjust the step size. If the VREF = 5V (vcc) then the step size is VREF/MAXIMUM DIGITAL OUTPUT value.

                                                            Step size = 5v/255 = 19.33mV.

The internal conversion process is completed in 64 clock cycles, but 1 to 8 clock periods are used after the write pin low to high transition and before conversion starts. And also 1 clock period is taken before INTR pin goes low. Approximately the maximum conversion time taken by the ADC is equal to (t conversion = 8 clock cycles+ 64 clock cycles + 1 clock cycle = 73 clock cycles).

Therefore in this example, t conversion = 73 * 1/(606khz) = 120 microseconds.

Finally we three control pins for the ADC operation Read (2nd pin of ADC), Write (3rd pin of ADC), INTR (5th pin of ADC). All these three pins are active low signals. By providing a high to low logic on the Write pin will START the conversion process. If a low to high logic will eventually set the INTR pin to high. The INTR pin is used to indicate the END of conversion process. Finally a low signal on Read pin will latch the internal converted data on to the data port of the ADC.


1.4  Steps to perform the conversion process on ADC:

·      Provide a high to low logic (>200ns) on write pin to start the conversion process.
·      Again provide a low to high logic on write pin to set the INTR pin to high.
·      Check for INTR pin, if it is low means the conversion process is completed.
·     Finally provide a logic low on the read pin, to read the converted data from the ADC.
·       After all the steps are completed make read pin.
·    Make sure that CS, AGND, v- and GND are grounded before performing all the above steps.


1.5  16x2 LCD interface:
The LCD connections and its interfacing programming with 8051 is already discussed on this website. Therefore, here I encourage you to read the related topics on LCD provided in this site.


1.6  Ciruit Diagram for IR sensor based Distance measurement system:

Figure1  Ciruit Diagram for IR sensor based Distance measurement system



Note: 1. White color reflects more IR light than any other colour 
          2. One major problem with the IR distance measure is going below the minimum sensor          range means  an object is so close the sensor cannot get an accurate reading. And this theory is better explained in the URL http://www.societyofrobots.com/sensors_sharpirrange.shtml



1.7 software code:
/******************************************************************************************************************************/
// http://www.npeducations.com
// IR_Distance_Measurement.c - measuring the obstacle distance using infrared sensors. the output will show the distance upto 9 cm, the video is taken during testing
// so don't confuse with the video output.
// Author - lovakiranvarma, M.tech
/******************************************************************************************************************************/
#include

#define LCD_PORT P2 
#define ADC_DATA P1

// function prototypes
void converttochar(void);
void lcd_data_string(unsigned char *);
void delay(unsigned int );
void lcd_cmd(unsigned char );
void lcd_data(unsigned char );
void LCD_Init(void);

// LCD control pins declaration
sbit RS = P3^7;     // Register Select line
sbit RW = P3^5;  // Read/ADC_WRITEite line
sbit ENABLE = P3^6; // Enable line

// ADC control pins declaration
sbit ADC_READ= P3^0;
sbit ADC_WRITE= P3^1;
sbit ADC_INTR= P3^2;

int value1=0;

void main()
{
ADC_DATA = 0xff; // make P1 as input port
LCD_PORT = 0x00; //make P2 as outport

LCD_Init();
lcd_cmd(0x85);  // Setting cursor location at 5th position of the first line
delay(2);

lcd_data_string("IR BASED DISTANCE MEASURE");
delay(100);
lcd_cmd(0x01);
delay(5);
lcd_cmd(0x80);
delay(5);
lcd_data_string("DISTANCE:");
while(1)
{
  delay(5);
  ADC_READ=1;
  ADC_WRITE=0;  // make WRITE to high to low for start of conversion
  delay(5);
  ADC_WRITE=1;
  while(ADC_INTR==1); // check for END of conversion
  ADC_READ=0;         // read the converted digital data form the ADC port
  value1=ADC_DATA;
  delay(5);
  ADC_INTR=1;
  converttochar();
}
}



void LCD_Init()
{
lcd_cmd(0x38);  //2 Line, 5X7 Matrix
delay(5);
lcd_cmd(0x0E);  //Display On, CuRSor Blink
delay(5);
lcd_cmd(0x06);
delay(5);
lcd_cmd(0x01);  // Clear Screen
delay(5);
}

void lcd_cmd(unsigned char Command) // LCD Command Sending Function declaration
{
LCD_PORT = Command;
RS= 0;
RW=0;
ENABLE = 1;
delay(1);
ENABLE = 0;
return;
}

void lcd_data(unsigned char data_value)  // LCD data Sending Function declaration
{
LCD_PORT = data_value;
RS= 1;
RW=0;
ENABLE = 1;
delay(1);
ENABLE = 0;
return;
}

void lcd_data_string(unsigned char *string) // LCD Command Sending String declaration
{
int i=0,j=0;
while(string[i]!='\0')
{
 if(i>=9)
 { 
  // If the number of characters in the string > 16, then the below command automatically 
 lcd_cmd(0xc0+j++);  // Shift the display right side 
 }
  lcd_data(string[i]);
  i++;
  delay(2);
}
return;
}

void converttochar()
{
int M;

M=value1/100;
M=M/10;
value1=value1%100;
value1=value1%10;

lcd_cmd(0x8a);
if(M!=0)
lcd_data(M+48);
else
lcd_cmd(0x06);
M=value1/10;
value1=value1%10;
lcd_data(M+48);
lcd_data(value1+48);
lcd_data(' ');
delay(5);
}

void delay(unsigned int DELAY_VALUE ) // delay function
{
int i ,j ;
for(i=0;i<=DELAY_VALUE;i++)
  for(j=0; j<1000 data-blogger-escaped-j="" data-blogger-escaped-pre="">


If you really like this tutorial, Don't forget to give the comment or please subscribe to the RSS feed by submitting your E-mail.
Related Posts Plugin for WordPress, Blogger...
Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger