Sunday, October 23, 2011

Password protect your microcontroller

Untitled Document
Hello friends, a good idea of privileging the execution of main program of microcontroller came in my mind after watching a Si-Fi movie……and I did it………

Password protecting your microcontroller is one of the coolest things you can do with it……


I created a routine that reads 5 keystrokes and then checks weather the inserted password is right or not.
the micro controller here used is  Atmega 8535
 Not discussing much more about logic, here is the program
----------------------------------------------------------------------------------------------

void main()

{
  unsigned char a,b,c,d,e;           
  DDRA=0x00;
  PORTA=0xff;
                               //ask for password

 while(1)
 {
  lcd_init();
  lcd_string("Enter a 5 digit");
  lcd_gotoxy(1,0);
  lcd_string("Password:");
                                             //read password
  while(PINA==0xff);                    //if all keys open stay here
  a=PINA;
  delayms(200);
    lcd_gotoxy(1,0);
            lcd_string("Password:*");
            while((PINA&0xf0)!=0xf0);      //wait to release the key
 while(PINA==0xff);        
             b=PINA;
  delayms(200);
    lcd_gotoxy(1,0);
            lcd_string("Password:**");
            while((PINA&0xf0)!=0xf0);     //wait to release the key
 while(PINA==0xff);        
             c=PINA;
  delayms(200);
    lcd_gotoxy(1,0);
            lcd_string("Password:***");
             while((PINA&0xf0)!=0xf0);   //wait to release the key
 while(PINA==0xff);        
 d=PINA;
  delayms(200);
    lcd_gotoxy(1,0);
            lcd_string("Password:****");
            while((PINA&0xf0)!=0xf0);   //wait to release the key
 while(PINA==0xff);        
             e=PINA;
  delayms(200);
    lcd_gotoxy(1,0);
            lcd_string("Password:*****");   //wait to release the key
              


                                   //checking the password
  if(a==0xef)
  {
  if(b==0xdf)
            {
            if(c==0xbf)
               {if(d==0x7f)
                        {
                                    if(e==0xef)
                                    {
                                    lcd_init();
                                    lcd_string("Welcome");
                                    goto wait;
                                    }
                                    else
                                    {
                                                            lcd_init();
                                                            lcd_string("Invalid password!!");
                                                            delayms(2000);
                                    }
                        }
                        else
                        {
                                                lcd_init();
                                                lcd_string("Invalid password!!");
                                                delayms(2000);
                        }

            }
            else
            {
            lcd_init();
            lcd_string("Invalid password!!");
            delayms(2000);
            }
            }
   else
  {
  lcd_init();
  lcd_string("Invalid password!!");
  delayms(2000);
  }
  }
 else
  {
  lcd_init();
  lcd_string("Invalid password!!");

  delayms(2000);

  }
  }
  wait:while(1);                      
            }

----------------------------------------------------------------------------------------------






and no post is completed without a video,




leave a comment if you want to ask any thing related to programming.

:)

Bidirectional Motor With 1 Pin of Microcontroller.....


How to drive a motor with a single pin of microcontroller?
A tough question but I have got the answer and the answer is “H-bridge connection”
Now you may be think that normally all the motors are driven by H-bridge connection so what is new here?
Here is a new configuration that can drive entire H-bridge with a single pin of micro controller, but for that you have to insert some extra hardware in that
A simple configuration is given below…


You can change the frequency of pulse to change the speed of motor and you can also vary PWM to generate asymmetric motor drive, and this entire thing at the cost of just one micro controller pin
Here I have just used a simple program to generate a 2 second delay and run motor in clockwise or anticlockwise,
The program is written for Atmega 8535(crystal frequency=16Mhz & I am using earlier made header file)
  
#include<avr/io.h>                     
#include <delay.h>

void main(void)
 {        
            DDRB=0x08;                                   // Port B data direction declaration as output.
            PORTB=0X00;
            while(1)                               //This is for the infinity loop.
            {         
             PORTB=0X08;
             delayms(2000);                //2 second delay
             PORTB=0X00;
             delayms(2000);                //2 second delay
            }
}

You can download source code file for this project and ISIS Proteus design HERE.

DAC by PWM

As i was reading a book "Embedded systems" by Rajkamal I found some thing interesting on Page 15 that was "DAC using PWM". Then I tried to implement it on my own as it was an interesting idea to generate Analog voltage from only one pin of micro controller.

Normally for DAC we have to use 8-pin and it is always good to use less pins.

Here we have to use only one pin to generate PWM and then Integrate the wave to make analog output.I have used an op-amp integrator(low-pass filter) .

I have used program made by me earlier that generates PWM according to potentiometer varied.
Just adding a next stage to it i gave that pwm pulse to an Op-amp based integrator.
Here is the rar file given which contains Proteus ISIS design file and source code to generate PWM in AVR atmega 8535.

The design is made for test purpose only to check the concepts, hardware is not built and tested  so enough care must be taken before converting this design file to actual hardware.

Hope the new concept is good for implementing.

:)

Sunday, October 16, 2011

Fast Pwm omode of AVR timers


Hello folks, I once needed to generate PWM wave  in my project related to power inverter design, I had to generate PWM waveforms according to the potentiometer varied by the user.
For that first method which I adopted was discussed earlier post but now here is another sophisticated method which I came across while studying the datasheet of mine microcontroller (mine favorite Atmega 8535).I came across the PWM modes given inbuilt in AVR series.
There are three modes of generating PWM in Atmega8535
1)Clear timer on compare(CTC) mode.

2)fast PWM mode

3)Phase correct PWM mode.


You can select any of them according to your requirement as in my requirement I had to select fast PWM mode.

Here are the steps given to configure your timer in fast PWM mode…..

Step 1) set Port in output mode
                                As the PWM generated would be given to output pin first of all that pin must be taken to output mode.the PIN where PWM is to be generated depends on the timer which you use to generate PWM. The pin OCn would be your output pin if you are using Timer-n. you can find the location of pin from datasheet.

Step 2) Set TCCRn register
                                You have to configure your timer in prescaler mode, OCn pin in inverting or non-inverting mode, selection of PWM mode etc. can be configured just within one register.
Step 3) Change the value of OCRn register in run time to generate PWM whatever you want…
Now your timer is configured to work with PWM mode
As I discussed earlier if we want to have a variable resistor  that can generate variable PWM for that we have to access ADC given in Atmega.

For adc initialization you have to do following steps
Step 1)  Initialize adc
                                You can do it by ADCSRA register you have to Enable, prescale and set interrupt .
Step 2)give ADC channel no
                                This no is given in ADMUX register.
Step 3)start conversion
                                Setting ADSC bit in ADCSRA register will do this thing.
Step 4) give it some time to convert and then read the result from ADCH and ADCL register.


Now you are done to interface your ADC with Timer

A sample code which I had used is given below.
#include&lt;avr/io.h>
#include&lt;delay.h>
void adc_init()
 {
ADCSRA=0X86;                              //ADC enable, ADC interrupt enable, set prescaller to 64
 }
 unsigned char getdata(unsigned char chno)        
  {
    ADMUX=0X60;                                       //right align the ADC result
    ADMUX|=chno;                                      //select the ADC channel
    ADCSRA|=0X40;                                     //start ADC convertion
    delayms(1);                                             //give some time delay to complit the ADC convertion
            return ADCH;
  }

void main()
{          unsigned char pot=0;
            adc_init();
             SREG=SREG|0x80;//global interrupt enable
             DDRB=0XFF;          //set data direction as output
TCCR0=TCCR0|0x7A;       //fast pwm,inverting mode,8 prescaler
while(1)
             {
             delayms(10);
             OCR0= getdata(0);          //read value from pot to OCR0
            }
}

Link to .zip file containing source code and Proteus ISIS design is this.
and datasheet of Atmega 8535 can be found from here.


if any question  then you can post a comment. 

DC motor interfacing with microcontroller



 #include<reg51.h>
        
   sbit sw1=P2^0;
    sbit sw2=P2^1;
        
  sbit  L293D_A = P1^0;             //Make positive of motor
    sbit    L293D_B = P1^1;             //Make negative of motor 0
     sbit   L293D_E = P1^2;             //Enable L293D



void rotate_f(void);         //Forward run funtion
void rotate_b(void);         //Backward run function
void breaks(void);           //Motor stop function
void delay(void);            //Some delay

void main(){ 
P2=0xFF;
P1=0x00;
                //Our main function
    while(1){                //Infinite loop
                              
                             
               if(sw1==1 && sw2==0)
               {
                rotate_f();          //Run forward
                delay();             //Some delay
                breaks();            //Stop
                delay();             //Some delay
                }
                
                if(sw2==1 && sw1==0)
                {
                rotate_b();          //Run Backwards
                delay();             //Some delay
                breaks();            //Stop
                delay();             //Some delay
               }                
}
}

void rotate_f()                  //rotate forward
{
        L293D_A = 1;             //Make positive of motor 1
        L293D_B = 0;             //Make negative of motor 0
        L293D_E = 1;             //Enable L293D
}

void rotate_b()                      //rotate backward
{
        L293D_A = 0;             //Make positive of motor 0
        L293D_B = 1;             //Make negative of motor 1
        L293D_E = 1;             //Enable L293D
}

void breaks()                   //stop motor
{
        L293D_A = 0;             //Make positive of motor 0
        L293D_B = 0;             //Make negative of motor 0
        L293D_E = 0;             //Disable L293D
}

void delay()                //Some delay...
{               
    unsigned char i,j,k;
    for(i=0;i<0x20;i++)
        for(j=0;j<255;j++)
                for(k=0;k<255;k++);

    }
 you can use these DC motors


but if you want to do such easy task with your microcontroller then you are wasting your resources and money so do take this program as an example only. if you want to drive motor directly with switch you can refer my earlier post.

Saturday, October 15, 2011

A new circuit element Memoristor



As all science students know, there are only three basic elements that make up an electrical circuit:the resistor, the capacitor and the inductor.
Sorry guys!It may be time to tear up your textbooks and write the new ones:scientists have realised physical samples of a fourth fundamental element which they call a memristor - short for memory resistor.
In a paper published in the latest issue of NATURE MAGAZINE(”The missing memristor found”,may 1st ,2008;vol no.453 ; pp 80-83), researchs at hewlett packard labs, report that the missing fourth element of the circuitry that professor Leon Chua of the University of California in Berkeley predicted in 1971 is indeed realisable.
PRACTICAL UNITS :
The team, led by R.Stanley Williams, believes that using nano technology one can soon build practical units of the resistor-with-memory that cannot be created by a mere combination of the three basic circuit elements.
Such element could fuel a new class of computer memory that would ‘remember’, even if the machine were switched off……in other words, tomorrow’s PCs could boot up and spring to life instantly.
The engineers are busy building memristors using Titanium Dioxide and have already realised a few hybrid versions in silicon.
Memory banks built using memristors could be a thousand times faster than todays magnetic disk systems , and consume a fraction of the power, the scientists suggest.
In electrical circuit theory, the memristor is a passice circuit element. It has been described as the fourth basic type of passive circuit element, alongside the well-known capacitor, resistor, and inductor. The name is a portmanteau of memory resistor.
Although the memristor was predicted and described in 1971 by Leon Chua of UC Berkeley, in a paper in IEEE Transactions on Circuit Theory, it was a hypothetical device for 37 years, with no known physical examples. According to Chua, the “era of nanoscale electronics will be enabled by the memristor. This is not just an invention, it is a basic scientific discovery.
In April 2008, a working device with similar characteristics to a memristor was announced by a team of researchers at HP Labs. The new circuit element may enable the development of a new class of high-density non-volatile digital memory. Performance of memristors improves as they are scaled down, and they generate less heat than transistors. The memristor also has unique analog properties that may lead to the invention of other devices.
The memristor is an element in which the magnetic flux Φm is a function of the accumulated electric charge q in the device. The rate of change of flux with charge
M(q)=\frac{\mathrm d\Phi_m}{\mathrm dq}
is known as memristance. This is comparable to the other three fundamental circuit elements:
  • Resistance: R(I)=\frac{\mathrm dV}{\mathrm dI}
  • Inductance: L(I) = \frac{\mathrm d\Phi_m}{\mathrm dI}
  • Capacitance: \frac{1}{C(q)}=\frac{\mathrm dV}{\mathrm dq}
Here q is electrical charge, I is electrical current, V is electrical potential and Φm is magnetic flux. The differential forms of these equations are used because we are comparing non-linear circuit elements; a linear memristor would be uninteresting, as explained below.
Applying Faraday’s law of electromagnetic induction and the chain rule to the equation defining the memristance, one obtains that the voltage V across a memristor is related to the current I by the instantaneous value of the memristance:
V(t) = M(q(t)) I(t) \,
Thus at any given instant, a memristor behaves like an ordinary resistor. However, its “resistance” M(q) is a value which depends on the charge accumulated in the device. This differs from ordinary resistors where the resistance is determined by fixed physical properties and transistors where the resistance is controlled by either the voltage at or current through a gate electrode. A linear memristor (one for which M is constant) would thus be indistinguishable from an ordinary linear resistor (one for which R is constant), with M = R. Memristance can be said to depend on the history of the charge that has flowed through the device in the same way that the voltage of capacitors does.
The memristor is capable of “remembering” how much electrical charge most recently passed through it in one direction versus another. Thus, it can “remember” the state it was last in.
Fabrication :
Interest in the memristor revived in 2007 when an experimental solid-state version was reported by Stanley Williams of hewlett packard. A solid-state device could not be constructed until the unusual behavior of nanoscale materials made it possible. The device does not use magnetic flux as the theoretical memristor suggested, nor stores charge as a capacitor does, but instead achieves a resistance dependent on the history of current using a chemical mechanism.
The HP device is composed of a thin (5 nm) titanium dioxide film between two electrodes. Initially, there are two layers to the film, one of which has a slight depletion of oxygen atoms. The oxygen vacancies act as charge carriers, meaning that the depleted layer has a much lower resistance than the non-depleted layer. When an electric field is applied, the oxygen vacancies drift, changing the boundary between the high-resistance and low-resistance layers. Thus the resistance of the film as a whole is dependent on how much charge has been passed through it in a particular direction, which is reversible by changing the direction of current.
Samsung has a pending U.S. patent application for a memristor similar to that described by Williams.

Potential applications :
Williams’s solid-state memristors can be combined into devices called crossbar latches, which would replace transistors in future computers, taking up a much smaller area. They can also be fashioned into non-volatile solid-state memory, which would allow greater data density than hard drives with access times potentially similar to DRAM, replacing both components. HP prototyped a crossbar latch memory using the devices that can fit 100 gigabits in a square centimeter. For comparison, the highest-density flash memories at this time (2008) hold 16 gigabits. HP has reported that its version of the memristor is about one tenth the speed of DRAM.
The devices’ resistance would be read with alternating current so that they do not affect the stored value.

Obstacle sensor with TSOP 1738

 




This is a simple yet effective IR proximity sensor built around the TSOP 1738 module. The TSOP module is commonly found at the receiving end of an IR remote control system; e.g., in TVs, CD players etc. These modules require the incoming data to be modulated at a particular frequency and would ignore any other IR signals. It is also immune to ambient IR light, so one can easily use these sensors outdoors or under heavily lit conditions.
Such modules are available for different carrier frequencies from 32 kHz to 42 kHz.They cost some thing around 20Rs.
In this particular proximity sensor, we will be generating a constant stream of square wave signal using IC555 centered at 38 kHz and would use it to drive an IR led. So whenever this signal bounces off the obstacles, the receiver would detect it and change its output. Since the TSOP 1738 module works in the active-low configuration, its output would normally remain high and would go low when it detects the signal (the obstacle).


you can see other post related to IR sensors here, where direct transistors are used for switching