#include<reg51.h> #define led P0 unsigned char i=0; void delay (int);
void delay (int d) { unsigned char i=0; for(;d>0;d--) { for(i=250;i>0;i--); for(i=248;i>0;i--); } }
void main() { while(1)//// led blink { led=0xff; delay(1000); led=0x00; delay(1000); ++i; if(i==7) { i=0; break; } } while(1)//// binary equivalent representation of 1byte data { led=i++; if(i==256) { i=0; break; } delay(500); }
while(1); }
|
/ Program for LCD Interfacing with 8051 Microcontroller (AT89S52) #include<reg51.h> #define display_port P2 //Data pins connected to port 2 on microcontroller sbit rs = P3^2; //RS pin connected to pin 2 of port 3 sbit rw = P3^3; // RW pin connected to pin 3 of port 3 sbit e = P3^4; //E pin connected to pin 4 of port 3 void msdelay(unsigned int time) // Function for creating delay in milliseconds. { unsigned i,j ; for(i=0;i<time;i++) for(j=0;j<1275;j++); } void lcd_cmd(unsigned char command) //Function to send command instruction to LCD { display_port = command; rs= 0; rw=0; e=1; msdelay(1); e=0; } void lcd_data(unsigned char disp_data) //Function to send display data to LCD { display_port = disp_data; rs= 1; rw=0; e=1; msdelay(1); e=0; } void lcd_init() //Function to prepare the LCD and get it ready { lcd_cmd(0x38); // for using 2 lines and 5X7 matrix of LCD msdelay(10); lcd_cmd(0x0F); // turn display ON, cursor blinking msdelay(10); lcd_cmd(0x01); //clear screen msdelay(10); lcd_cmd(0x81); // bring cursor to position 1 of line 1 msdelay(10); } void main() { unsigned char a[15]="CIRCUIT DIGEST"; //string of 14 characters with a null terminator. int l=0; lcd_init(); while(a[l] != '\0') // searching the null terminator in the sentence { lcd_data(a[l]); l++; msdelay(50); } }
|
start: mov a,#00h mov p1,a ;making all rows of port p1 zero mov a,#0fh mov p1,a ;making all rows of port p1 high press: mov a,p2 jz press ;check until any key is pressed
after making sure that any key is pressed
mov a,#01h ;make one row high at a time mov r4,a mov r3,#00h ;initiating counter next: mov a,r4 mov p1,a ;making one row high at a time mov a,p2 ;taking input from port A jnz colscan ;after getting the row jump to check column mov a,r4 rl a ;rotate left to check next row mov r4,a mov a,r3 add a,#08h ;increment counter by 08 count mov r3,a sjmp next ;jump to check next row
after identifying the row to check the column following steps are followed
colscan: mov r5,#00h in: rrc a ;rotate right with carry until get the carry jc out ;jump on getting carry inc r3 ;increment one count jmp in out: mov a,r3 da a ;decimal adjust the contents of counter before display mov p2,a jmp start ;repeat for check next key.
|
#include<reg51.h> sbit LED_pin = P2^0; //set the LED pin as P2.0 void delay(int ms){ unsigned int i, j; for(i = 0; i<ms; i++){ // Outer for loop for given milliseconds value for(j = 0; j< 1275; j++){ //execute in each milliseconds ; } } } void main(){ int rot_angle[] = {0x0C,0x06,0x03,0x09}; int i; while(1){ //infinite loop for LED blinking for(i = 0; i<4; i++){ P0 = rot_angle[i]; delay(100); } } }
|
#include <reg52.h> #include <intrins.h> /* Define value to be loaded in timer for PWM period of 20 milli second */ #define PWM_Period 0xB7FE sbit PWM_Out_Pin = P2^0; /* PWM Out Pin for speed control */ sbit Speed_Inc = P1^0; /* Speed Increment switch pin */ sbit Speed_Dec = P1^1; /* Speed Decrement switch pin */ sbit Change_Dir = P1^2; /* Rotation direction change switch pin */ sbit M1_Pin1 = P1^6; /* Motor Pin 1 */ sbit M1_Pin2 = P1^7; /* Motor Pin 2 */ unsigned int ON_Period, OFF_Period, DutyCycle, Speed; /* Function to provide delay of 1ms at 11.0592 MHz */ void delay(unsigned int count) { int i,j; for(i=0; i<count; i++) for(j=0; j<112; j++); } void Timer_init() { TMOD = 0x01; /* Timer0 mode1 */ TH0 = (PWM_Period >> 8);/* 20ms timer value */ TL0 = PWM_Period; TR0 = 1; /* Start timer0 */ } /* Timer0 interrupt service routine (ISR) */ void Timer0_ISR() interrupt 1 { PWM_Out_Pin = !PWM_Out_Pin; if(PWM_Out_Pin) { TH0 = (ON_Period >> 8); TL0 = ON_Period; } else { TH0 = (OFF_Period >> 8); TL0 = OFF_Period; }
}
/* Calculate ON & OFF period from PWM period & duty cycle */ void Set_DutyCycle_To(float duty_cycle) { float period = 65535 - PWM_Period; ON_Period = ((period/100.0) * duty_cycle); OFF_Period = (period - ON_Period); ON_Period = 65535 - ON_Period; OFF_Period = 65535 - OFF_Period; }
/* Initially Motor Speed & Duty cycle is zero and in either direction */ void Motor_Init() { Speed = 0; M1_Pin1 = 1; M1_Pin2 = 0; Set_DutyCycle_To(Speed); }
int main() { EA = 1; /* Enable global interrupt */ ET0 = 1; /* Enable timer0 interrupt */ Timer_init(); Motor_Init(); while(1) { /* Increment Duty cycle i.e. speed by 10% for Speed_Inc Switch */ if(Speed_Inc == 1) { if(Speed < 100) Speed += 10; Set_DutyCycle_To(Speed); while(Speed_Inc == 1); delay(200); } /* Decrement Duty cycle i.e. speed by 10% for Speed_Dec Switch */ if(Speed_Dec == 1) { if(Speed > 0) Speed -= 10; Set_DutyCycle_To(Speed); while(Speed_Dec == 1); delay(200); } /* Change rotation direction for Change_Dir Switch */ if(Change_Dir == 1) { M1_Pin1 = !M1_Pin1; M1_Pin2 = !M1_Pin2; while(Change_Dir == 1); delay(200); } } }
|
#include<reg51.h> #include<stdio.h> #include<string.h> #include <stdlib.h> #include "LCD16x2_4bit.h"
sbit DHT11=P2^1; /* Connect DHT11 output Pin to P2.1 Pin */ int I_RH,D_RH,I_Temp,D_Temp,CheckSum;
void timer_delay20ms() /* Timer0 delay function */ { TMOD = 0x01; TH0 = 0xB8; /* Load higher 8-bit in TH0 */ TL0 = 0x0C; /* Load lower 8-bit in TL0 */ TR0 = 1; /* Start timer0 */ while(TF0 == 0); /* Wait until timer0 flag set */ TR0 = 0; /* Stop timer0 */ TF0 = 0; /* Clear timer0 flag */ }
void timer_delay30us() /* Timer0 delay function */ { TMOD = 0x01; /* Timer0 mode1 (16-bit timer mode) */ TH0 = 0xFF; /* Load higher 8-bit in TH0 */ TL0 = 0xF1; /* Load lower 8-bit in TL0 */ TR0 = 1; /* Start timer0 */ while(TF0 == 0); /* Wait until timer0 flag set */ TR0 = 0; /* Stop timer0 */ TF0 = 0; /* Clear timer0 flag */ }
void Request() /* Microcontroller send request */ { DHT11 = 0; /* set to low pin */ timer_delay20ms(); /* wait for 20ms */ DHT11 = 1; /* set to high pin */ }
void Response() /* Receive response from DHT11 */ { while(DHT11==1); while(DHT11==0); while(DHT11==1); }
int Receive_data() /* Receive data */ { int q,c=0; for (q=0; q<8; q++) { while(DHT11==0);/* check received bit 0 or 1 */ timer_delay30us(); if(DHT11 == 1) /* If high pulse is greater than 30ms */ c = (c<<1)|(0x01);/* Then its logic HIGH */ else /* otherwise its logic LOW */ c = (c<<1); while(DHT11==1); } return c; }
void main() { unsigned char dat[20]; LCD_Init(); /* initialize LCD */
while(1) { Request(); /* send start pulse */ Response(); /* receive response */
I_RH=Receive_data(); /* store first eight bit in I_RH */ D_RH=Receive_data(); /* store next eight bit in D_RH */ I_Temp=Receive_data(); /* store next eight bit in I_Temp */ D_Temp=Receive_data(); /* store next eight bit in D_Temp */ CheckSum=Receive_data();/* store next eight bit in CheckSum */
if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum) { LCD_String_xy(0,0,"Error"); }
else { sprintf(dat,"Hum = %d.%d",I_RH,D_RH); LCD_String_xy(0,0,dat); sprintf(dat,"Tem = %d.%d",I_Temp,D_Temp); LCD_String_xy(1,0,dat); LCD_Char(0xDF); LCD_String("C"); memset(dat,0,20); sprintf(dat,"%d ",CheckSum); LCD_String_xy(1,13,dat); } delay(100); } }
|