All microcontroller program
program using keil 4vision software
// 1) program for memory transfer
a] Internal memory transfer
org 0000h //reset 8051
sjmp main //short jump to main
org 0050h //program start from memory location 50h
main:mov r7,#5 //deci 5 assing to r7
mov r0,#30h // 30 is aasing to r0
mov r1,#40h // 40 is assing to r1
back:mov a,#00h // back is label,
mov a,@r0 //data of the mem loc cantain in r0( i.e. 30) , (at data 30 is 1) is copied to a
mov @r1,a //data of a (a=1) is copied to memory location cantain in r1 (r1=40)
inc r0 //r0 is increasing by 1
inc r1 //r1 is increasing bt 1
djnz r7,back //decreament and jump if r7 is not 0
end
b] External memory transfer
org 0000h
sjmp main
org 0050h
main:mov r1,#5
mov r0,#30h
mov dptr,#3000h
back:mov a,@r0
movx@dptr,a
inc r0
inc dptr
djnz r7,back
end
// 2) Interface of LED's and ALP of LED
org 0000h //reset 8051
sjmp main //short jump to main
org 30h //program start from mem 30h
main:mov a,#11111110B // main=label , data is asing to a
abc1:mov p2,a // abc=label , a is asing to p2
acall delay
rr a //data is rotate right
sjmp abc1 //short jump on abc
delay:mov r3,#7 // 7 decimal data is assing to r3 register
mov tmod,#10h //10h= tmod is set to 16 bit T/C
abc:mov tl1,#00h //tl1= 00000000
mov th1,#00h //th1=0000000
setb tr1 //start timer (bit=1)
xyz:jnb tf1,xyz //jump xyz untill tf1 become 1
clr tr1 // stop timer
clr tf1 //clear timer flag ( it become again 0)
djnz r3,abc //decrement and jump if not 0 ,r3
ret
end
// 3) Waveform generation
a] Square
org 0000h //reset 8051
sjmp st //short jump on st label
org 000040h //program start from mem loc 40
st:clr a //st is label, clear a
loop:mov p1,a //loop is label, a is assing to p1
acall delay
cpl a
sjmp loop //short jump on loop label
delay:mov tmod,#20h // work as 8 bit T/C, load tmod regi timer 1 mode 0
mov th1,#1ah // load th1, setting frq
setb tr1 //start timer
back:jnb tf1,back //jump untill tf1 is 0 , if tf1 become 1 , terminate loop
clr tr1 //stop timer
clr tf1 //clear timer flag (i.e. it become again 0)
ret
end
b] Sine
org 0000h // reset 8051
clr a //clear a
up:mov dptr,#sine //up is label,sine is assing to dptr register
mov r0,#24 //24 is assing to r0
label:movc a,@a+dptr //16 bit reg, data of mem loc cantain in dptr(i.e sine) is copied to a
mov p2,a //data at a is assing to p2
clr a //clear a
inc dptr //increment dptr
djnz r0,label //decrement and jump r0 untill it become 0 at label
sjmp up //short jump on up label
org 050h //program start from mem loc 50h
sine:
db 127,160,191,217,237,250,255,250,237,217,191,160,127,94,63,37,17,4,0,4,17,37,63,94,127
end
c] Triangular
org 0000h //reset 8051
sjmp main //short jump on main
main:mov a,#00h //main is label, 00 assing to a
abc:mov p1,a //abc is label,data in a is assing to p1
inc a //increment a
cjne a,#0ffh,abc //increment a till data reaches to maximum(ff)
xyz:mov p1,a //xyz is label, a is assing to p1
dec a //decrement a
cjne a,#00h,xyz //decrement a till data reaches to minimum(oo)
sjmp abc //short jump on abc
ret
end
// 5) Stepper motar
org 0000h //reset 8051
up:mov p1,#0h //up is label, 0 is assing to p1
acall delay
mov p1,#04h //04 is assing to p1
acall delay
mov p1,#02h //02 is assing to p1
acall delay
mov p1,#08h //08 is assing to p1
acall delay
sjmp up //short jump on up label
delay:
mov tmod,#20h //tmod is set as 8 bit T/C, lode tmod regi in timer 1 mode 0
mov th1,#1ah //load th1
setb tr1 //start the timer (bit=1)
back:jnb tf1,back //back is label, jump back untill th1 become 1 (i.e timer start from 00000000 to FFFFFFFF)
clr tr1 //stop timer
clr tf1 //clear timer flag (i.e. it become again 0)
ret
end
Program using MPLAB
project-->project wizard-->next-->select device-->PIC18F4550-->next-->active toolkit-->
microchip c18 toolsuite-->next-->create new project file (xyz) using browse-->
next-->finish.
file-->new-->write the program-->save-->using c extension-->right click on source file
(add file)-->project-->build all-->programmer(select programmer as pickit2)-->
programmer-->program(means program is downloded)-->programmer-->release from
reset(lit converte ISF mode into run mode).
// 6) Write a program for interfacing button, LED, relay & buzzer
with PIC18F as follows A. when button 1 is pressed relay and
buzzer is turned ON and LED’s start chasing from left to right.
B. when button 2 is pressed relay and buzzer is turned OFF and LED’s
start chasing from right to left.
#include <p18f4550.h>
#pragma config FOSC = HS // High-speed oscillator;20MHz
#pragma config WDT = OFF // Watchdog Timer disabled
#pragma config LVP = OFF // Low-voltage Programming disabled
#pragma config PBADEN = OFF
#define LEDS PORTD
#define BUZZER PORTAbits.RA3
#define BUTTON1 PORTBbits.RB0 // SW1 Switch
#define BUTTON2 PORTBbits.RB1 // SW2 Switch
void msdelay (unsigned int itime);
unsigned int flag=0;
void main()
{ TRISB=0XFF; //input port
TRISA=0X00; // output port or (TRISAbits.TRISA3=0;)
TRISD = 0x00; // output port
PORTD=0xFF; // all LEDs off
while(1)
{ if(BUTTON1==0) //checking if the button is pressed or not
flag=1;
if(BUTTON2==0)
flag=0;
if(flag==1)
{ BUZZER=1; // buzzer on
LEDS=0x00; //leds ON
msdelay(550);
}
if (flag==0)
{ BUZZER=0;
LEDS=0xFF; // leds off
msdelay(550);
}
}
}
void msdelay (unsigned int itime)
{ int i, j;
for(i=0; i< itime; i++)
for(j=0;j<135;j++);
}
// 7) Generate square wave using timer with interrupt using PIC18F.
#include <p18f4550.h>
//Configration bits setting//
#pragma config FOSC = HS //Oscillator Selection
#pragma config WDT = OFF //Disable Watchdog timer
#pragma config LVP = OFF //Disable Low Voltage Programming
#pragma config PBADEN = OFF //Disable PORTB Analog inputs
//Timer ISR Function Prototype
void timer_isr(void);
// Timer ISR Definition
#pragma interrupt timer_isr
void timer_isr(void)
{
TMR0H = 0X6D; //Reload the timer values after overflow
TMR0L = 0X82;
PORTBbits.RB0 = ~PORTBbits.RB0; //Toggle the RB0
INTCONbits.TMR0IF = 0; //Reset the timer overflow interrupt flag
}
//Interrupt Vector for Timer0 Interrupt
#pragma code _HIGH_INTERRUPT_VECTOR = 0x0008
void high_ISR (void)
{
_asm
goto timer_isr //The program is relocated to execute the ISR
_endasm
}
#pragma code
// Start of main Program
void main()
{
ADCON1 = 0x0F; //Configuring the PORTE pins as digital I/O
TRISBbits.TRISB0 = 0; //Configruing the RB0 as output
PORTBbits.RB0 = 0; //Setting the initial value
T0CON = 0x02; //Set the timer to 16-bit,Fosc/4,1:16 prescaler
TMR0H = 0x85; //load timer value to genearate delay 50ms
TMR0L = 0xEE;
INTCONbits.TMR0IF = 0; // Clear Timer0 overflow flag
INTCONbits.TMR0IE = 1; // TMR0 interrupt enabled
T0CONbits.TMR0ON = 1; // Start timer0
INTCONbits.GIE = 1; // Global interrupt enabled
while(1); //Program execution stays here until the timer overflow interrupt is generated
}
// 8) Interfacing serial port with PC using PIC18F.
#include <p18f4550.h>
//Function Prorotypes
void TXbyte(); //To transmit single character
void msdelay (unsigned int itime);
//Start of Main Program
void main()
{
TRISCbits.TRISC6=0; // TXD line as output
SPBRG = 0x07;
SPBRGH = 0x02; // 0x04E1 for 9600 baud
TXSTA = 0x24; // TX enable BRGH=1
RCSTA = 0x90; // continuous RX
BAUDCON = 0x08; // BRG16 = 1
TXbyte();
while(1); //loop forever
}
void TXbyte()
{ unsigned int i=0;
while(TXSTAbits.TRMT==0); //wait till transmit buffer is not empty
for (i=0;i<2;i++)
{
TXREG = 'S';
msdelay(550);
TXREG = 'R';
msdelay(550);
// Transmit Data
}
}
void msdelay (unsigned int itime)
{ int i, j;
for(i=0; i< itime; i++)
for(j=0;j<135;j++);
}
// 9) EXPT:: Interfacing DC Motor with PIC18F to Generate PWM signal.
#include <p18f4550.h>
#pragma config FOSC = HS //Oscillator Selection
#pragma config WDT = OFF //Disable Watchdog timer
#pragma config LVP = OFF //Disable Low Voltage Programming
#pragma config PBADEN = OFF //Disable PORTB Analog inputs
void MsDelay (unsigned int time) // Definition of delay subroutine
{
unsigned int i, j;
for (i = 0; i < time; i++) // Loop for itime
for (j = 0; j < 710; j++); // Calibrated for a 1 ms delay in MPLAB
}
void main()
{
TRISCbits.TRISC2 = 0 ; // Set PORTC, RC2 as output (CCP1)
TRISCbits.TRISC6 = 0 ; // Set PORTC, RC6 as output (DCM IN3)
TRISCbits.TRISC7 = 0 ; // Set PORTC, RC6 as output (DCM IN4)
PR2 = 0x4E; // set PWM Frequency 4KHz
CCP1CON = 0x0C; // Configure CCP1CON as PWM mode.
T2CON = 0x07; //Start timer 2 with prescaler 1:16
PORTCbits.RC6 = 1; // Turn ON the Motor
PORTCbits.RC7 = 0;
while(1) // Endless Loop
{
//-------Duty Cycle 80%-------//
CCP1CONbits.DC1B0 = 0;
CCP1CONbits.DC1B1 = 1;
CCPR1L = 0x3E;
MsDelay(2000);
//--------Duty Cycle 60%------//
CCP1CONbits.DC1B0 = 1;
CCP1CONbits.DC1B1 = 1;
CCPR1L = 0x2E;
MsDelay(2000);
//----------Duty Cycle 40%----//
CCP1CONbits.DC1B0 = 1;
CCP1CONbits.DC1B1 = 0;
CCPR1L = 0x1F;
MsDelay(2000);
//---------Duty Cycle 20%----//
CCP1CONbits.DC1B0 = 0;
CCP1CONbits.DC1B1 = 1;
CCPR1L = 0x0F;
MsDelay(2000);
}
}
// 1) program for memory transfer
a] Internal memory transfer
org 0000h //reset 8051
sjmp main //short jump to main
org 0050h //program start from memory location 50h
main:mov r7,#5 //deci 5 assing to r7
mov r0,#30h // 30 is aasing to r0
mov r1,#40h // 40 is assing to r1
back:mov a,#00h // back is label,
mov a,@r0 //data of the mem loc cantain in r0( i.e. 30) , (at data 30 is 1) is copied to a
mov @r1,a //data of a (a=1) is copied to memory location cantain in r1 (r1=40)
inc r0 //r0 is increasing by 1
inc r1 //r1 is increasing bt 1
djnz r7,back //decreament and jump if r7 is not 0
end
b] External memory transfer
org 0000h
sjmp main
org 0050h
main:mov r1,#5
mov r0,#30h
mov dptr,#3000h
back:mov a,@r0
movx@dptr,a
inc r0
inc dptr
djnz r7,back
end
// 2) Interface of LED's and ALP of LED
org 0000h //reset 8051
sjmp main //short jump to main
org 30h //program start from mem 30h
main:mov a,#11111110B // main=label , data is asing to a
abc1:mov p2,a // abc=label , a is asing to p2
acall delay
rr a //data is rotate right
sjmp abc1 //short jump on abc
delay:mov r3,#7 // 7 decimal data is assing to r3 register
mov tmod,#10h //10h= tmod is set to 16 bit T/C
abc:mov tl1,#00h //tl1= 00000000
mov th1,#00h //th1=0000000
setb tr1 //start timer (bit=1)
xyz:jnb tf1,xyz //jump xyz untill tf1 become 1
clr tr1 // stop timer
clr tf1 //clear timer flag ( it become again 0)
djnz r3,abc //decrement and jump if not 0 ,r3
ret
end
// 3) Waveform generation
a] Square
org 0000h //reset 8051
sjmp st //short jump on st label
org 000040h //program start from mem loc 40
st:clr a //st is label, clear a
loop:mov p1,a //loop is label, a is assing to p1
acall delay
cpl a
sjmp loop //short jump on loop label
delay:mov tmod,#20h // work as 8 bit T/C, load tmod regi timer 1 mode 0
mov th1,#1ah // load th1, setting frq
setb tr1 //start timer
back:jnb tf1,back //jump untill tf1 is 0 , if tf1 become 1 , terminate loop
clr tr1 //stop timer
clr tf1 //clear timer flag (i.e. it become again 0)
ret
end
b] Sine
org 0000h // reset 8051
clr a //clear a
up:mov dptr,#sine //up is label,sine is assing to dptr register
mov r0,#24 //24 is assing to r0
label:movc a,@a+dptr //16 bit reg, data of mem loc cantain in dptr(i.e sine) is copied to a
mov p2,a //data at a is assing to p2
clr a //clear a
inc dptr //increment dptr
djnz r0,label //decrement and jump r0 untill it become 0 at label
sjmp up //short jump on up label
org 050h //program start from mem loc 50h
sine:
db 127,160,191,217,237,250,255,250,237,217,191,160,127,94,63,37,17,4,0,4,17,37,63,94,127
end
c] Triangular
org 0000h //reset 8051
sjmp main //short jump on main
main:mov a,#00h //main is label, 00 assing to a
abc:mov p1,a //abc is label,data in a is assing to p1
inc a //increment a
cjne a,#0ffh,abc //increment a till data reaches to maximum(ff)
xyz:mov p1,a //xyz is label, a is assing to p1
dec a //decrement a
cjne a,#00h,xyz //decrement a till data reaches to minimum(oo)
sjmp abc //short jump on abc
ret
end
// 5) Stepper motar
org 0000h //reset 8051
up:mov p1,#0h //up is label, 0 is assing to p1
acall delay
mov p1,#04h //04 is assing to p1
acall delay
mov p1,#02h //02 is assing to p1
acall delay
mov p1,#08h //08 is assing to p1
acall delay
sjmp up //short jump on up label
delay:
mov tmod,#20h //tmod is set as 8 bit T/C, lode tmod regi in timer 1 mode 0
mov th1,#1ah //load th1
setb tr1 //start the timer (bit=1)
back:jnb tf1,back //back is label, jump back untill th1 become 1 (i.e timer start from 00000000 to FFFFFFFF)
clr tr1 //stop timer
clr tf1 //clear timer flag (i.e. it become again 0)
ret
end
Program using MPLAB
project-->project wizard-->next-->select device-->PIC18F4550-->next-->active toolkit-->
microchip c18 toolsuite-->next-->create new project file (xyz) using browse-->
next-->finish.
file-->new-->write the program-->save-->using c extension-->right click on source file
(add file)-->project-->build all-->programmer(select programmer as pickit2)-->
programmer-->program(means program is downloded)-->programmer-->release from
reset(lit converte ISF mode into run mode).
// 6) Write a program for interfacing button, LED, relay & buzzer
with PIC18F as follows A. when button 1 is pressed relay and
buzzer is turned ON and LED’s start chasing from left to right.
B. when button 2 is pressed relay and buzzer is turned OFF and LED’s
start chasing from right to left.
#include <p18f4550.h>
#pragma config FOSC = HS // High-speed oscillator;20MHz
#pragma config WDT = OFF // Watchdog Timer disabled
#pragma config LVP = OFF // Low-voltage Programming disabled
#pragma config PBADEN = OFF
#define LEDS PORTD
#define BUZZER PORTAbits.RA3
#define BUTTON1 PORTBbits.RB0 // SW1 Switch
#define BUTTON2 PORTBbits.RB1 // SW2 Switch
void msdelay (unsigned int itime);
unsigned int flag=0;
void main()
{ TRISB=0XFF; //input port
TRISA=0X00; // output port or (TRISAbits.TRISA3=0;)
TRISD = 0x00; // output port
PORTD=0xFF; // all LEDs off
while(1)
{ if(BUTTON1==0) //checking if the button is pressed or not
flag=1;
if(BUTTON2==0)
flag=0;
if(flag==1)
{ BUZZER=1; // buzzer on
LEDS=0x00; //leds ON
msdelay(550);
}
if (flag==0)
{ BUZZER=0;
LEDS=0xFF; // leds off
msdelay(550);
}
}
}
void msdelay (unsigned int itime)
{ int i, j;
for(i=0; i< itime; i++)
for(j=0;j<135;j++);
}
// 7) Generate square wave using timer with interrupt using PIC18F.
#include <p18f4550.h>
//Configration bits setting//
#pragma config FOSC = HS //Oscillator Selection
#pragma config WDT = OFF //Disable Watchdog timer
#pragma config LVP = OFF //Disable Low Voltage Programming
#pragma config PBADEN = OFF //Disable PORTB Analog inputs
//Timer ISR Function Prototype
void timer_isr(void);
// Timer ISR Definition
#pragma interrupt timer_isr
void timer_isr(void)
{
TMR0H = 0X6D; //Reload the timer values after overflow
TMR0L = 0X82;
PORTBbits.RB0 = ~PORTBbits.RB0; //Toggle the RB0
INTCONbits.TMR0IF = 0; //Reset the timer overflow interrupt flag
}
//Interrupt Vector for Timer0 Interrupt
#pragma code _HIGH_INTERRUPT_VECTOR = 0x0008
void high_ISR (void)
{
_asm
goto timer_isr //The program is relocated to execute the ISR
_endasm
}
#pragma code
// Start of main Program
void main()
{
ADCON1 = 0x0F; //Configuring the PORTE pins as digital I/O
TRISBbits.TRISB0 = 0; //Configruing the RB0 as output
PORTBbits.RB0 = 0; //Setting the initial value
T0CON = 0x02; //Set the timer to 16-bit,Fosc/4,1:16 prescaler
TMR0H = 0x85; //load timer value to genearate delay 50ms
TMR0L = 0xEE;
INTCONbits.TMR0IF = 0; // Clear Timer0 overflow flag
INTCONbits.TMR0IE = 1; // TMR0 interrupt enabled
T0CONbits.TMR0ON = 1; // Start timer0
INTCONbits.GIE = 1; // Global interrupt enabled
while(1); //Program execution stays here until the timer overflow interrupt is generated
}
// 8) Interfacing serial port with PC using PIC18F.
#include <p18f4550.h>
//Function Prorotypes
void TXbyte(); //To transmit single character
void msdelay (unsigned int itime);
//Start of Main Program
void main()
{
TRISCbits.TRISC6=0; // TXD line as output
SPBRG = 0x07;
SPBRGH = 0x02; // 0x04E1 for 9600 baud
TXSTA = 0x24; // TX enable BRGH=1
RCSTA = 0x90; // continuous RX
BAUDCON = 0x08; // BRG16 = 1
TXbyte();
while(1); //loop forever
}
void TXbyte()
{ unsigned int i=0;
while(TXSTAbits.TRMT==0); //wait till transmit buffer is not empty
for (i=0;i<2;i++)
{
TXREG = 'S';
msdelay(550);
TXREG = 'R';
msdelay(550);
// Transmit Data
}
}
void msdelay (unsigned int itime)
{ int i, j;
for(i=0; i< itime; i++)
for(j=0;j<135;j++);
}
// 9) EXPT:: Interfacing DC Motor with PIC18F to Generate PWM signal.
#include <p18f4550.h>
#pragma config FOSC = HS //Oscillator Selection
#pragma config WDT = OFF //Disable Watchdog timer
#pragma config LVP = OFF //Disable Low Voltage Programming
#pragma config PBADEN = OFF //Disable PORTB Analog inputs
void MsDelay (unsigned int time) // Definition of delay subroutine
{
unsigned int i, j;
for (i = 0; i < time; i++) // Loop for itime
for (j = 0; j < 710; j++); // Calibrated for a 1 ms delay in MPLAB
}
void main()
{
TRISCbits.TRISC2 = 0 ; // Set PORTC, RC2 as output (CCP1)
TRISCbits.TRISC6 = 0 ; // Set PORTC, RC6 as output (DCM IN3)
TRISCbits.TRISC7 = 0 ; // Set PORTC, RC6 as output (DCM IN4)
PR2 = 0x4E; // set PWM Frequency 4KHz
CCP1CON = 0x0C; // Configure CCP1CON as PWM mode.
T2CON = 0x07; //Start timer 2 with prescaler 1:16
PORTCbits.RC6 = 1; // Turn ON the Motor
PORTCbits.RC7 = 0;
while(1) // Endless Loop
{
//-------Duty Cycle 80%-------//
CCP1CONbits.DC1B0 = 0;
CCP1CONbits.DC1B1 = 1;
CCPR1L = 0x3E;
MsDelay(2000);
//--------Duty Cycle 60%------//
CCP1CONbits.DC1B0 = 1;
CCP1CONbits.DC1B1 = 1;
CCPR1L = 0x2E;
MsDelay(2000);
//----------Duty Cycle 40%----//
CCP1CONbits.DC1B0 = 1;
CCP1CONbits.DC1B1 = 0;
CCPR1L = 0x1F;
MsDelay(2000);
//---------Duty Cycle 20%----//
CCP1CONbits.DC1B0 = 0;
CCP1CONbits.DC1B1 = 1;
CCPR1L = 0x0F;
MsDelay(2000);
}
}
No comments: