0% found this document useful (0 votes)
117 views21 pages

Rudra 18

This document contains code for multiple embedded C programs including an LCD module, LED module, keypad module, ADC module, DAC module, and I2C communication modules. The LCD, keypad, and LED modules initialize ports and pins and demonstrate basic functionality. The ADC module reads an analog sensor value using I2C. The DAC module writes values to a DAC using I2C to generate an analog output. Common functions include delays, data transmission, and I2C communication routines.

Uploaded by

Ruthra Devi
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)
117 views21 pages

Rudra 18

This document contains code for multiple embedded C programs including an LCD module, LED module, keypad module, ADC module, DAC module, and I2C communication modules. The LCD, keypad, and LED modules initialize ports and pins and demonstrate basic functionality. The ADC module reads an analog sensor value using I2C. The DAC module writes values to a DAC using I2C to generate an analog output. Common functions include delays, data transmission, and I2C communication routines.

Uploaded by

Ruthra Devi
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
You are on page 1/ 21

lcd

#include<reg51.h>

sbit rs = P3^2;

sbit en = P3^3;

sbit d7 = P3^7;

sbit d6 = P3^6;

sbit d5 = P3^5;

sbit d4 = P3^4;

unsigned char a[16] ={" RUTHRA "};

unsigned char b[16]={" 18EE570 "};

unsigned char i;

void delay(int x)

int k,l;

for(k = x;k > 0;k--)

for(l = 0;l < x;l++);

void data_disp(unsigned char data_out) // LCD take 21ms of execution time.

d4 = data_out & 0x01;

d5 = (data_out & 0x02) >> 1;

d6 = (data_out & 0x04) >> 2;

d7 = (data_out & 0x08) >> 3;


}

void cmd_single(unsigned char cmds)

rs = 0;

data_disp((cmds & 0xf0)>>4);

en = 1;

delay(5);

en = 0;

delay(20);

data_disp((cmds & 0x0f));

en = 1;

delay(5);

en = 0;

void cmd_write()

int i;

unsigned char a[7]={0x33,0x32,0x28,0x01,0x06,0x0c,0x80};

for(i=0;i<7;i++)

cmd_single(a[i]);

delay(20);

}
}

lcd mod

#include<reg51.h>

sbit rs = P3^2;

sbit en = P3^3;

sbit d7 = P3^7;

sbit d6 = P3^6;

sbit d5 = P3^5;

sbit d4 = P3^4;

unsigned char a[16] = {" RUTHRADEVI "};

unsigned char b[16]= {" 19E084 "};

unsigned char i;

void delay(int x)

int k,l;

for(k = x;k > 0;k--)

for(l = 0;l < x;l++);

void data_disp(unsigned char data_out) // LCD take 21ms of execution time.

d4 = data_out & 0x01;

d5 = (data_out & 0x02) >> 1;

d6 = (data_out & 0x04) >> 2;


d7 = (data_out & 0x08) >> 3;

void cmd_single(unsigned char cmds)

rs = 0;

data_disp((cmds & 0xf0)>>4);

en = 1;

delay(5);

en = 0;

delay(20);

data_disp((cmds & 0x0f));

en = 1;

delay(5);

en = 0;

led mod

#include <reg51.h>

#define HIGH 1

#define LOW 0

sbit LEDSET1 = P0^0;

sbit LEDSET2 = P0^1;

void Delay(int x);

void main()
{

while(HIGH)

LEDSET1 = LOW;

LEDSET2 = LOW;

P1 = 0xF0;

LEDSET1 = HIGH;

Delay(100);

LEDSET1 = LOW;

P1 = 0X0F;

LEDSET2 = HIGH;

Delay(100);

LEDSET2 = LOW;

void Delay(int x)

int k,l;

for(k=x;k>0;k--)

for(l=0;l<x;l++);

keypad

#include<REG51.h>

#define HIGH 1
#define LOW 0

sbit LEDSET1 = P0^0;

sbit LEDSET2 = P0^1;

sbit SCANLINE_EN = P0^4;

sbit READLINE_EN = P0^5;

sbit rs = P3^2;

sbit en = P3^3;

sbit d7 = P3^7;

sbit d6 = P3^6;

sbit d5 = P3^5;

sbit d4 = P3^4;

unsigned char LcdData[16]={" RUTHRA "};

unsigned char LcdData1[16]={" DEVI "};

unsigned char LcdCommands[7]={0x33,0x32,0x28,0x01,0x06,0x0c,0x80};

void Delay_Function(int x);

void Bit_Conversion_Function(unsigned char data_out);

void Command_Write_Function(unsigned char cmd);


void Data_Write_Function(unsigned char dat);

void main()

int ReadVal, i;

for(i=0;i<7;i++)

Command_Write_Function(LcdCommands[i]);

Delay_Function(20);

for(i=0;i<16;i++)

Data_Write_Function(LcdData[i]);

Delay_Function(20);

while(HIGH)

P1 = 0x0D;

SCANLINE_EN = 1;
SCANLINE_EN = 0;

READLINE_EN = 0;

ReadVal = (P1 & 0xF0);

READLINE_EN = 1;

if(ReadVal == 0xb0)

for(i=0;i<16;i++)

Data_Write_Function(LcdData[i]);

P1 = 0xAA;

LEDSET1 = HIGH;

LEDSET2 = LOW;

Delay_Function(200);

LEDSET1 = LOW;

P1 = 0X55;

LEDSET2 =HIGH;

if(ReadVal == 0x70)

for(i=0;i<16;i++)

Data_Write_Function(LcdData1[i]);

}
}

void Delay_Function(int x)

int k,l;

for(k = x;k > 0;k--)

for(l = 0;l < x;l++);

void Bit_Conversion_Function(unsigned char data_out) // LCD take 21ms of execution time.

d4 = data_out & 0x01;

d5 = (data_out & 0x02) >> 1;

d6 = (data_out & 0x04) >> 2;

d7 = (data_out & 0x08) >> 3;

void Command_Write_Function(unsigned char cmd)

rs = 0;

Bit_Conversion_Function((cmd & 0xf0)>>4);

en = 1;

Delay_Function(5);

en = 0;
Delay_Function(10);

Bit_Conversion_Function((cmd & 0x0f));

en = 1;

Delay_Function(5);

en = 0;

void Data_Write_Function(unsigned char dat)

rs = 1;

Bit_Conversion_Function((dat & 0xf0)>>4);

en = 1;

Delay_Function(5);

en = 0;

Delay_Function(10);

Bit_Conversion_Function((dat & 0x0f));

en = 1;

Delay_Function(5);

en = 0;

keypad mod

#include<reg51.h>

sbit en=P3^3;
sbit rs=P3^2;

sbit en_scanline=P0^4;//edge trigger

sbit en_readline=P0^5;//active low

sbit d4=P3^4;

sbit d5=P3^5;

sbit d6=P3^6;

sbit d7=P3^7;

unsigned int i;

unsigned char read_value;

unsigned char a[16]={" RUTHRA "};

unsigned char b[16]={" 19E084 "};

void delay(int x);

void data_display(unsigned char data_out);

void command_bit_conversion(unsigned char cmd);

void cmd_write();

void data_bit_conversion(unsigned char da);

void main()

{
cmd_write();

while(1)

P1=0x0D; //1011

en_scanline=1;

en_scanline=0;

en_readline=0;

read_value=(P1&0xF0);

en_readline=1;

if(read_value==0xE0)

command_bit_conversion(0x80);

for(i=0;i<15;i++)

data_bit_conversion(a[i]);

command_bit_conversion(0xC0);

for(i=0;i<15;i++)

data_bit_conversion(b[i]);

}
void data_display(unsigned char data_out)

d4 = data_out & 0x01;

d5 = (data_out & 0x02) >> 1;

d6 = (data_out & 0x04) >> 2;

d7 = (data_out & 0x08) >> 3;

void cmd_write()

int i;

unsigned char c[7]={0x33,0x32,0x28,0x01,0x06,0x0C,0x80};

for(i=0;i<7;i++)

command_bit_conversion(c[i]);

delay(20);

void data_bit_conversion(unsigned char da)

rs=1;

data_display((da & 0xf0)>>4);


en=1;

delay(5);

en=0;

delay(20);

data_display((da & 0x0f));

en=1;

delay(5);

en=0;

void command_bit_conversion(unsigned char cmd)

rs=0;

data_display((cmd & 0xf0)>>4);

en=1;

delay(5);

en=0;

delay(100);

data_display((cmd & 0x0f));

en=1;

delay(5);

en=0;

void delay(int x)
{

int i, k;

for(i=0;i<x;i++)

for(k=0;k<x;k++);

adc

#include<REG51.h>

#include<stdio.h>

#define HIGH 1

#define LOW 0

sbit SCL = P2^0;

sbit SDA = P2^1;

void Uart_Initialize_Function();

void I2c_Start_Function();

void I2c_Write_Function(int WriteData);

char I2c_Read_Function();

void main()
{

int I2cReadData;

Uart_Initialize_Function();

I2c_Start_Function();

I2c_Write_Function(0x9C);

I2c_Write_Function(0x00);

while(HIGH)

I2c_Start_Function();

I2c_Write_Function(0x9D);

I2cReadData = I2c_Read_Function();

if(I2cReadData<0)

I2cReadData = I2cReadData + 256;

printf("\n\r The ADC Value is %d", I2cReadData);

void Uart_Initialize_Function()

{
TMOD = 0x20;

TH1 = 0xFD;

TL1 = 0x00;

TCON = 0x40;

SCON = 0x52;

void I2c_Start_Function()

SDA = HIGH;

SCL = HIGH;

SDA = LOW;

SCL = LOW;

void I2c_Write_Function(int WriteData)

unsigned char index;

for(index = 0x80; index != 0; index >>= 1)

if (WriteData & index)

SDA = HIGH;
else

SDA = LOW;

SCL = HIGH;

SCL = LOW;

SDA = HIGH;

SCL = HIGH;

while (SDA);

SCL = HIGH;

SCL = LOW;

char I2c_Read_Function()

unsigned char X;

unsigned char index;

X = LOW;

for(index = 0x80; index != 0; index >>= 1)

SCL = HIGH;

if (SDA)

X |= index;

SCL = LOW;
}

return(X);

dac

#include<REG51.h>

#include<stdio.h>

#define HIGH 1

#define LOW 0

sbit SCL = P2^0;

sbit SDA = P2^1;

void I2c_Start_Function();

void I2c_Write_Function(int WriteData);

void main()

int i;

while(HIGH)

I2c_Start_Function();

I2c_Write_Function(0x9C);//Address

I2c_Write_Function(0x40);//Control byte

for(i=0xff;i>0;i--)

I2c_Write_Function(i);

}
}

void I2c_Start_Function()

SDA = HIGH;

SCL = HIGH;

SDA = LOW;

SCL = LOW;

void I2c_Write_Function(int WriteData)

unsigned char index;

for(index = 0x80; index != 0; index >>= 1)

if (WriteData & index)

SDA = HIGH;

else

SDA = LOW;

SCL = HIGH;

SCL = LOW;

//For ACK

SDA = HIGH;

SCL = HIGH;

while(SDA); //Wait until low signal(ACK) is through SDA by slave IC8591


SCL = HIGH;

SCL = LOW;

You might also like