Showing posts with label Microcontroller. Show all posts

program to blink LED’s connected to a PORT of PIC18

Write a program to blink LED’s connected to a PORT of PIC18, also simulate it on Mplab-IDE simulator & “Proteous”.



#include
 LIST P=18F452, F=INHX32, MM=OFF
 CONFIG OSC=XT
 CONFIG WDT=OFF
MAIN:
COUNT1 EQU D'254'
COUNT2 EQU D'255'
R1 EQU 0x10
R2 EQU 0x30
R3 EQU 0x40
SAVE EQU 0x50
C1 EQU D'100'
C2 EQU D'1000'
C3 EQU D'10'

 ORG 00H
 ORG 20H
 BSF TRISC,2
 CLRF TRISB
 CLRF PORTB
 MOVLW 0

AGAIN1
 BSF TRISC,2
 BTFSS PORTC,2
 GOTO AGAIN1
 BCF TRISC,2
 BCF PORTC,2
  INCF WREG,W
 CALL DELAY
 MOVWF PORTB
 DECF COUNT2 ,F
 BNZ AGAIN1
 MOVLW 0
 MOVWF PORTB
 CLRF PORTC
 GOTO AGAIN1
 ORG   0x2000
DELAY          
 MOVWF SAVE
 MOVLW C3
 MOVWF R3
L3 MOVLW C2
 MOVWF R2
L2 MOVLW C1
 MOVWF R1
L1 DECF R1,F
 BNZ L1
 DECF R2,F
 BNZ L2
 DECF R3,F
 BNZ L3
 MOVF SAVE,WREG

RETURN




 END

Introduction to assembly language programming & an overview of MPLAB-IDE

Introduction to assembly language programming & an overview of MPLAB-IDE
Assembly Language:
·         Referred to as a low-level-language
·         deals directly with the internal structure of CPU
·         deals with registers, memory addresses and call stacks
·         all registers of CPU must be known by programmer
·         Each statement corresponds to a single machine code instruction
·         mnemonics or opcode (codes & abbreviations) are easier to remember than machine code
·         Translated into machine code by an Assembler

High Level Language:
·         C, C++ and Java are more structured & closer to spoken languages
·         Strong abstraction from the detail of the CPU
·         deals with variables, arrays, objects, functions, loops
·         No need to understand detailed architecture of CPU e.g. c=a+b
·         Translated into machine code by compiler

Structure of Assembly Language:
·         Consists of mnemonics (OPCODE), optionally followed by one or two operands
·         Mnemonics are commands to CPU, operands are data items being manipulated

[Label]             mnemonics      [operands]       [; comments]
Label: allows the program to refer to a line of code by name

HERE ADDLW          B’00000110’   ; W=W+6
               GOTO              HERE
MPLAB-IDE:
            A software program, also called an IDE which provides a single integrated “Environment” to develop code for “Microchip” microcontrollers.
Steps from design to implementation can be done flawlessly within the single environment.









Code is written, tested and then modified; it is also called development cycle


Components of MPLAB-IDE:
MPLAB-IDE includes:
Ø  Full-featured programmer’s text editor that also serves as a window into the debugger.
Ø  Project manager (visible as the Project window) that provides integration and communication between the IDE and the language tools.
Ø  Assembler converts instructions into machine code. Error file & object files are created.
Ø  Linker takes object files and produce hex file, list file, map files & debug files. (List file shows binary & source code, map file shows memory layout of used & unused memory locations)
Ø  Debugger engine (uses debug file) that provides breakpoints, single stepping, Watch windows and all the features of a modern debugger. The debugger works in conjunction with debug tools, both software and hardware.
Ø  The simulator is actually composed of several device-specific simulator executables. MPLAB X IDE decides which one to use based on your project’s device.

Optional components can be acquired or purchased to work with the MPLAB X IDE:
• Compiler Language Tools
MPLAB C compilers from Microchip provide fully integrated, optimized code for PIC MCUs. Compiled code that is automatically loaded into the target debugger for instant testing and verification.
• Programmers
MPLAB PM3 programmer, PICkit™ 2, PICkit 3 and MPLAB ICD 3 in-circuit debuggers; and MPLAB REAL ICE™ in-circuit emulator can program code into target devices.
• In-Circuit Debuggers and Emulators

PICkit™ 2, PICkit 3 and MPLAB ICD 3 in-circuit debuggers, and MPLAB REAL ICE™ in-circuit emulator can be used to debug application code on target devices. By using some of the on-chip resources, they can download code into a target microcontroller inserted in the application, set breakpoints, single step and monitor registers and variables. The emulator includes additional debug features, such as trace.

Introduction to embedded system and overview of PIC18F452 microcontroller. part2

Introduction to embedded system and overview of PIC18F452 microcontroller.
Embedded System:
A computer based system designed to perform one or more dedicated functions.
OR
A device that include computer (hardware and software) but it is not itself a general purpose computer, which may expected to perform function without human interaction.

ü  Responds monitor and  control external environment using sensors and actuators
ü  May also contain user interface(create interaction between user and system)
Examples of Embedded System:
Cell phone, Mp3 player, digital watch and household applications
Automobile E.S:
ü  High end automobiles may have 100 of microcontrollers
ü  Tension in seatbelts
ü  Display services in dashboards
ü  EFI etc
Characteristics of E.S:
ü  Must be efficient in energy and low manufacturing cost
ü  Must meet real time constraints
ü  Frequently connected to environment through sensors and actuators
ü  Must be hybrid system i.e digital and analogue
ü  Must be reactive
ü  Sophisticated functionality
ü  Safe in operation
Comparison:
Embedded system:
ü  Few application that are known before design
ü  Mostly not programmable by end user
ü  Additional computing power is not useful
General purpose computing:
ü Broad Class of application.
ü Programmable by users
ü Faster is better.
PIC microcontroller:
ü  Stands for peripheral interface controller
ü  High performance RISC machine.
ü  Linear program memory 32K byte
ü  Linear data memory 1.5 k byte
ü  Speed upto 10 MIPs
ü  16 bit wide instruction, 8 bit wide data path
ü  Priority levels for interrupts
Peripheral features:
ü  High current sink/source 25 mA
ü  Three external interrupt pins
ü  Timer 0 module: 8 bit/16 bit timer/counter
ü  Timer 1 module: 8 bit timer/counter
ü  Timer 2 module: 16 bit timer/counter

MSSP(Master synchronous series ports) module:
Two modes of operation
ü  3 wire SPI
ü  I2C Master and Slave
ü  USART Module supports RS 485 RS 232
Analogue Features:
ü  Compatible 10 bit analog to digital converter module with
ü  Fast sampling rate
ü  Conversion available during sleep
ü  Programmable low voltage detection
ü  Support interrupts on low voltage detection
ü  Programmable Brown out reset
Special Features:
ü  100,000 erase/write cycle enhanced flash program memory
ü  1000,000 erase/write cycle data EEPROM
ü  Flash/Data EEPROM retention > 40 years
ü  Self programmable under software control
ü  Power on reset, power up timer Oscillator Start up timer
ü  Watch dog timer with its on chip oscillator
ü  Power saving sleep mode
ü  Single supply 5 v in circuit serial programming Via Two Pins

ü  In circuit debug 

Introduction to embedded system and overview of PIC18F452 microcontroller.

Introduction to embedded system and overview of PIC18F452 microcontroller.
Embedded System:
A computer based system designed to perform one or more dedicated functions.
OR
A device that include computer (hardware and software) but it is not itself a general purpose computer, which may expected to perform function without human interaction.

ü  Responds monitor and  control external environment using sensors and actuators
ü  May also contain user interface(create interaction between user and system)
Examples of Embedded System:
Cell phone, Mp3 player, digital watch and household applications
Automobile E.S:
ü  High end automobiles may have 100 of microcontrollers
ü  Tension in seatbelts
ü  Display services in dashboards
ü  EFI etc
Characteristics of E.S:
ü  Must be efficient in energy and low manufacturing cost
ü  Must meet real time constraints
ü  Frequently connected to environment through sensors and actuators
ü  Must be hybrid system i.e digital and analogue
ü  Must be reactive
ü  Sophisticated functionality
ü  Safe in operation
Comparison:
Embedded system:
ü  Few application that are known before design
ü  Mostly not programmable by end user
ü  Additional computing power is not useful
General purpose computing:
ü Broad Class of application.
ü Programmable by users
ü Faster is better.
PIC microcontroller:
ü  Stands for peripheral interface controller
ü  High performance RISC machine.
ü  Linear program memory 32K byte
ü  Linear data memory 1.5 k byte
ü  Speed upto 10 MIPs
ü  16 bit wide instruction, 8 bit wide data path
ü  Priority levels for interrupts
Peripheral features:
ü  High current sink/source 25 mA
ü  Three external interrupt pins
ü  Timer 0 module: 8 bit/16 bit timer/counter
ü  Timer 1 module: 8 bit timer/counter
ü  Timer 2 module: 16 bit timer/counter

MSSP(Master synchronous series ports) module:
Two modes of operation
ü  3 wire SPI
ü  I2C Master and Slave
ü  USART Module supports RS 485 RS 232
Analogue Features:
ü  Compatible 10 bit analog to digital converter module with
ü  Fast sampling rate
ü  Conversion available during sleep
ü  Programmable low voltage detection
ü  Support interrupts on low voltage detection
ü  Programmable Brown out reset
Special Features:
ü  100,000 erase/write cycle enhanced flash program memory
ü  1000,000 erase/write cycle data EEPROM
ü  Flash/Data EEPROM retention > 40 years
ü  Self programmable under software control
ü  Power on reset, power up timer Oscillator Start up timer
ü  Watch dog timer with its on chip oscillator
ü  Power saving sleep mode
ü  Single supply 5 v in circuit serial programming Via Two Pins

ü  In circuit debug 

Temperature and Pressure Sensor part 2 (C++ code)

Voltage regulator:

            It is used to regulate the voltage. We are using 7805which gives us 5 voltage at output that is supply to microcontroller. These ripples can be removed by using voltage regulator it provides us regulated constant voltages. The purpose of a voltage regulator is to keep the voltage in a circuit relatively close to a desired value. Voltage regulators are one of the most common electronic components, since a power supply frequently produces raw current that would otherwise damage one of the components in the circuit. Voltage regulators have a variety of specific functions, depending on their particular application
Buzzer, Oscillor and capacitors:
          We are using buzzer if the input value in greater than the room temperature. We are using 8MHz oscillator and 2 capacitors of 30 microf.
Keypad:
            We are using 3x4 keypad in this project to enter digits.
Potentiometer:
This is used for variable resistance.

C code:
#include
#pragma config OSC = HS, OSCS = OFF
#pragma config PWRT =OFF,LVP =OFF
#pragma config DEBUG = OFF, STVR = OFF
#pragma config WDT=OFF
#define ldata PORTD
#define rs PORTCbits.RC5
#define rw PORTCbits.RC6
#define en PORTCbits.RC7
far rom const char mycom[] = {0x01,0x06,0x0C,0x80,'\0'};
far rom const char mydata13[]="Temperature and\0";
far rom const char mydata14[]="Pressure Sensor\0";
far rom const char mydata15[]="PreSet Temp: \0";
far rom const char mydata10[]="Temperature: \0";   //0=end of stirng
far rom const char mydata2[]="Pressure: \0";
far rom const char Unit[]="kPa \0";
void LCDMSDelay(unsigned int);
void lcddata(unsigned char);
void lcdcmd(unsigned char);
void MSDelay(unsigned char);
unsigned char RBIF_ISR();
void code_enter();
void chk_isr ();
unsigned char z ,cod, code[4] , temp[4], x , i , m ,l, a , b,j ,g, o,cnt=0;
unsigned int nol ,noh , nom , nohh ,c, d, e, h, f, L, p;
unsigned char keypad [4][3]={0x31,0x32,0x33,
0x34,0x35,0x36,
0x37,0x38,0x39,
'*',0x30,'#'};
void chk_isr ();
#pragma code my_hiprio_int =0x0008
void my_hiprio_int(void)
{
_asm
GOTO chk_isr
_endasm
}
#pragma code
#pragma interrupt chk_isr
void chk_isr ()
{
if (INTCONbits.RBIF==1)
RBIF_ISR ();
}
#pragma code
    unsigned char L_Byte,H_Byte;
    unsigned char Bin_Temp,Bin_Pres,m;
    unsigned char z,y = 'l';
void main (void)
{
    i=1;
    TRISAbits.TRISA0=1;
    TRISD=0;
    TRISC=0;
    TRISB=0xF0;
    PORTB=0xF0;
    PORTCbits.RC2 = 0;
    TRISAbits.TRISA0=1;
    TRISAbits.TRISA1=1;
    TRISAbits.TRISA3=1;
    en=0;
    LCDMSDelay(150);
    lcdcmd(0x38);
    LCDMSDelay(150);
 for(z=0;mycom[z]!='\0';z++)
    {
        lcdcmd(mycom[z]);
        LCDMSDelay(10);
    }
 lcdcmd(0x8F);
    for(z=0;mydata13[z]!='\0';z++)
    {
         lcdcmd(0x07);
         lcddata(mydata13[z]);
         MSDelay(60);
    }
   lcdcmd(0x01);
   lcdcmd(0x8F);
    for(z=0;mydata14[z]!='\0';z++)
    {
        lcdcmd(0x07);
        lcddata(mydata14[z]);
        MSDelay(60);
    }
   lcdcmd(0x01);
    for(z=0;mycom[z]!='\0';z++)
    {
        lcdcmd(mycom[z]);
        LCDMSDelay(50);
    }
 lcdcmd(0x01);
 for(z=0;mydata15[z]!='\0';z++)
    {
        lcddata(mydata15[z]);
        MSDelay(6);
    }
    MSDelay(700);
for(j=0;j<2 j="" o:p="">
{
    code_enter();
}
    for(z=0;mycom[z]!='\0';z++)
    {
        lcdcmd(mycom[z]);
        LCDMSDelay(10);
    }
MSDelay(250);
    for(z=0;mydata10[z]!='\0';z++)
    {
        lcddata(mydata10[z]);
        MSDelay(6);
    }
    lcdcmd(0xC0);
    LCDMSDelay(10);
   for(z=0; mydata2[z]!= '\0';z++)
    {
        lcddata(mydata2[z]);
        MSDelay(3);
    }
    while(1)
    {
        lcdcmd(0x8C);
        LCDMSDelay(3);
        ADCON0=0x81;
        ADCON1=0x85;
        MSDelay(10);
        ADCON0bits.GO=1;
        while(ADCON0bits.DONE==1);
        L_Byte=ADRESL;
        H_Byte=ADRESH;
        L_Byte>>=2;
        L_Byte&=0x3F;
        H_Byte<<=6;
        H_Byte&=0xC0;
        Bin_Temp= L_Byte|H_Byte;
        z=Bin_Temp;
        l=Bin_Temp;
        l=Bin_Temp/10;
        l=l+48;
        lcddata(l);
        Bin_Temp=Bin_Temp%10;
        l=Bin_Temp+48;
        MSDelay(1);
        lcddata(l);
        lcdcmd(0x8F);
        MSDelay(1);
        lcddata('C');

        ADCON0bits.GO=0;
        if(code[1] > z/10)
           PORTCbits.RC2=1;
        else
        {
            if(code[1] == z/10)
                if(code[2] > (z % 10))
                    PORTCbits.RC2 = 1;
                else;
        }
        MSDelay(9);
        lcddata(l);
        MSDelay(9);
        lcdcmd(0xCA);
        LCDMSDelay(3);
        ADCON0=0x89;
        ADCON1=0x80;
        MSDelay(1);
        ADCON0bits.GO=1;
        while(ADCON0bits.GO==1);
        L_Byte=ADRESL;
        H_Byte=ADRESH;
        L_Byte>>=2;
        L_Byte&=0x3F;
        H_Byte<<=6;
        H_Byte&=0xC0;
        Bin_Pres= L_Byte|H_Byte;
        m=Bin_Pres;
        m=Bin_Pres/100;
        m=m+48;
        lcddata(m);
        Bin_Pres=Bin_Pres%100;
        m=Bin_Pres/10;
        m=m+48;
        MSDelay(1);
        lcddata(m);
        m=Bin_Pres%10;
        m=m+48;
        lcddata(m);
        MSDelay(1);
for (z=0;Unit[z]!='\0';z++)
{
        lcddata(Unit[z]);
        MSDelay(1);
}

        ADCON0bits.GO=0;
    }}

void lcdcmd(unsigned char value)
{
    ldata= value;
    rs=0;
    rw=0;
    en=1;
    MSDelay(10);
    en=0;
}
void lcddata(unsigned char k)
{
    ldata= k;
    rs=1;
    rw=0;
    en=1;
    MSDelay(10);
    en=0;
}
 void MSDelay(unsigned char itime)
    {
        unsigned char i;
        unsigned char j;
    for(i=0;i
        for(j=0;j<165 j="" o:p="">
    }
 void LCDMSDelay(unsigned int itime)
 {
     unsigned int i,j;
     for(i=0;i
         for(j=0;j<135 j="" o:p="">
 }
 void code_enter()
{
    INTCON2bits.RBPU=0;
    while (PORTB !=0xF0)
{};
INTCONbits.RBIE=1;
INTCONbits.GIE=1;
while(!x)
{};
code[i]=x;
lcddata(code[i]);
code[i] = code[i] - 48;
x=0;
i++;
MSDelay(15);
}
unsigned char RBIF_ISR()
{
unsigned char temp , COL=0, ROW=4 ;
temp=PORTB;
temp ^=0xF0;
if (!temp) return;
while (temp<<=1) COL++;
PORTB = 0xFE;
if(PORTB !=0xFE)
ROW=0;
else
{
PORTB = 0xFD;
if(PORTB !=0xFD)
ROW=1;
else
{
PORTB = 0xFB;
if(PORTB !=0xFB)
ROW=2;
else
{
PORTB = 0xF7;
if(PORTB !=0xF7)
ROW=3;
}
}
}
if(ROW<4 o:p="">
x=keypad [ROW][COL];
MSDelay(250);
while(PORTB !=0xF0) PORTB=0xF0;
INTCONbits.RBIF=0;
} 





Applications of Sensors:
Sensors are used in everyday objects such as touch-sensitive elevator buttons and lamps which dim or brighten by touching the base. There are also innumerable applications for sensors of which most people are never aware. Applications include cars, machines, aerospace, medicine, manufacturing and robotics. Some more applications are in
ü  Pump/motor controller
ü  Level indicator
ü  Medical diagnostics
ü  Pressure switching
ü  Barometers
ü  Altimeters


Project Cost:

Sr. No.
Componets
Rs/-
1
Pic18F452
360/-
2
LM35
65/-
3
3-Potentio Meter
15/-
4
Base for Pic18F452
50/-
5
16x2 LCD
450/-
6
3x4 Keypad
150/-
7
Buzzer
10/-
8
Voltage Regulator 7805
10/-
9
MPx2100
350/-
10
Battery
90/-
11
PCB
60/-
12
FeCl3
200/-
13
Slodering wire, paste
100/-
14
Oscillator,Capacitors
30/-
15
Total Cost
1940/-