0% found this document useful (0 votes)
4 views9 pages

Assignment 4

The document contains code snippets for microcontroller applications using the 8051 architecture. It includes implementations for generating a waveform, a counter displayed on an LCD, and a digital clock with time display. Each section demonstrates the use of various functions for delays, LCD commands, and data handling.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Assignment 4

The document contains code snippets for microcontroller applications using the 8051 architecture. It includes implementations for generating a waveform, a counter displayed on an LCD, and a digital clock with time display. Each section demonstrates the use of various functions for delays, LCD commands, and data handling.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment 4

Name: Parth Kurhade

Branch: ICE
Subject: Microcontroller & Application
Year: 2nd
1.
#include <reg51.h>

sbit WAVE_PIN = P1^0;

void delay_10ms() {
TMOD = 0x01;

TH0 = 0xDC;
TL0 = 0x00;

TR0 = 1;

while (TF0 == 0);

TR0 = 0;

TF0 = 0;
}

void main() {
WAVE_PIN = 0;

while (1) {
WAVE_PIN = ~WAVE_PIN;
delay_10ms();
}
}
2.
#include <reg51.h>

#define LCD_DATA P2
sbit RS = P3^0;
sbit RW = P3^1;
sbit EN = P3^2;

void timer_delay_1sec();
void lcd_cmd(unsigned char cmd);
void lcd_data(unsigned char dat);
void lcd_init();
void lcd_string(char *str);
void ms_delay(unsigned int ms);

void main() {
unsigned char count;
unsigned char tens, units;

lcd_init();
lcd_cmd(0x80);
lcd_string("Counter: 00");

for (count = 0; count <= 99; count++) {


tens = count / 10;
units = count % 10;

lcd_cmd(0x89);

lcd_data(tens + '0');
lcd_data(units + '0');

timer_delay_1sec();
}

while(1);
}
void timer_delay_1sec() {
int i;
TMOD = 0x01;

for(i = 0; i < 20; i++) {


TH0 = 0x4C;
TL0 = 0x00;
TR0 = 1;

while (TF0 == 0);

TR0 = 0;
TF0 = 0;
}
}

void lcd_cmd(unsigned char cmd) {


LCD_DATA = cmd;
RS = 0;
RW = 0;
EN = 1;
ms_delay(2);
EN = 0;
}

void lcd_data(unsigned char dat) {


LCD_DATA = dat;
RS = 1;
RW = 0;
EN = 1;
ms_delay(2);
EN = 0;
}

void lcd_init() {
ms_delay(20);
lcd_cmd(0x38);
lcd_cmd(0x0C);
lcd_cmd(0x01);
lcd_cmd(0x06);
}
void lcd_string(char *str) {
int i;
for(i = 0; str[i] != 0; i++) {
lcd_data(str[i]);
}
}

void ms_delay(unsigned int ms) {


unsigned int i, j;
for(i = 0; i < ms; i++)
for(j = 0; j < 112; j++);
}
3.
#include <reg51.h>

#define LCD_DATA P2
sbit RS = P3^0;
sbit RW = P3^1;
sbit EN = P3^2;

unsigned char hours = 0;


unsigned char minutes = 0;
unsigned char seconds = 0;

void timer_delay_1sec();
void lcd_cmd(unsigned char cmd);
void lcd_data(unsigned char dat);
void lcd_init();
void lcd_string(char *str);
void lcd_display_2digits(unsigned char val);
void ms_delay(unsigned int ms);

void main() {
lcd_init();
lcd_cmd(0x80);
lcd_string(" Digital Clock ");

while (1) {
lcd_cmd(0xC4);

lcd_display_2digits(hours);
lcd_data(':');
lcd_display_2digits(minutes);
lcd_data(':');
lcd_display_2digits(seconds);

timer_delay_1sec();

seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
if (minutes == 60) {
minutes = 0;
hours++;
if (hours == 24) {
hours = 0;
}
}
}
}
}

void lcd_display_2digits(unsigned char val) {


unsigned char tens, units;
tens = val / 10;
units = val % 10;
lcd_data(tens + '0');
lcd_data(units + '0');
}

void timer_delay_1sec() {
int i;
TMOD = 0x01;

for(i = 0; i < 20; i++) {


TH0 = 0x4C;
TL0 = 0x00;
TR0 = 1;

while (TF0 == 0);

TR0 = 0;
TF0 = 0;
}
}

void lcd_cmd(unsigned char cmd) {


LCD_DATA = cmd;
RS = 0;
RW = 0;
EN = 1;
ms_delay(2);
EN = 0;
}

void lcd_data(unsigned char dat) {


LCD_DATA = dat;
RS = 1;
RW = 0;
EN = 1;
ms_delay(2);
EN = 0;
}
void lcd_init() {
ms_delay(20);
lcd_cmd(0x38);
lcd_cmd(0x0C);
lcd_cmd(0x01);
lcd_cmd(0x06);
}
void lcd_string(char *str) {
int i;
for(i = 0; str[i] != 0; i++) {
lcd_data(str[i]);
}
}
void ms_delay(unsigned int ms) {
unsigned int i, j;
for(i = 0; i < ms; i++)
for(j = 0; j < 112; j++);
}

You might also like