Sie sind auf Seite 1von 11

8051 PWM C Program | Embed4u

1 of 11

http://www.embed4u.com/8051-pwm-c-program/

Embed4u
All About Embedded Hardware and Software
Home
Code Library
DOWNLOAD
Drivers
Electronics
Projects
Related Videos
Schematics
Technology
Tutorial

Ads by Google

PWM

Timer

Timer Circuit

Clock Timer Circuit

Home > Code Library > 8051 PWM C Program

8051 PWM C Program


May 29th, 2010 admin Leave a comment Go to comments

#include<reg51.h>
sbit x=P1^1;
void scan_col();
void scan_row();
void init_timer();
void runtimer();

5/23/2012 3:50 PM

8051 PWM C Program | Embed4u

2 of 11

http://www.embed4u.com/8051-pwm-c-program/

void pwn_generator();
void toggle();
void delay();
void busy_check();
void init_lcd();
void display();
void ton_array();
void toff_array();
void init_array();
char a[4][3];
int ton[4][3];
int toff[4][3];
int i = 0,r,c;
xdata char *ptr=(xdata char*)0xfffc;

void init_array()
{
a[0][0]=1;
a[0][1]=2;
a[0][2]=3;
a[1][0]=4;
a[1][1]=5;
a[1][2]=6;
a[2][0]=7;
a[2][1]=8;
a[2][2]=9;
a[3][0]=*';
a[3][1]=0;

5/23/2012 3:50 PM

8051 PWM C Program | Embed4u

3 of 11

http://www.embed4u.com/8051-pwm-c-program/

a[3][2]=#';

void display()
{
init_lcd();
*(ptr+1)=001;
*(ptr) = a[r];
busy_check();
}
void init_lcd()
{
*(ptr+1)=000;
*(ptr)=038;
busy_check();
*(ptr+1)=000;
*(ptr)=00e;
busy_check();
*(ptr+1)=000;
*(ptr)=080;
busy_check();
*(ptr+1)=000;
*(ptr)=001;
busy_check();
}
void busy_check()
{

5/23/2012 3:50 PM

8051 PWM C Program | Embed4u

4 of 11

http://www.embed4u.com/8051-pwm-c-program/

*(ptr+1)=002;
while((*ptr&080)!=000);
}

void delay()
{
i=30000;
while(i);
}

void ton_array() // high


{
ton[0][0]=0xd0;
ton[0][1]=0xc0;
ton[0][2]=0xb0;
ton[1][0]=0xa0;
ton[1][1]=090;
ton[1][2]=080;
ton[2][0]=070;
ton[2][1]=0xff;
ton[2][2]=011;
ton[3][0]=0xff;
ton[3][1]=0xff;
ton[3][2]=0xff;
}
void toff_array() // low
{
toff[0][0]=080;

5/23/2012 3:50 PM

8051 PWM C Program | Embed4u

5 of 11

http://www.embed4u.com/8051-pwm-c-program/

toff[0][1]=090;
toff[0][2]=0xa0;
toff[1][0]=0xb0;
toff[1][1]=0xc0;
toff[1][2]=0xd0;
toff[2][0]=0xe0;
toff[2][1]=000;
toff[2][2]=0xff;
toff[3][0]=0xff;
toff[3][1]=0xff;
toff[3][2]=0xff;
}
void scan_col()
{
P2=0068;
while((P2&0068)==0068);
if((P2&060)==060)
{
c=0;
scan_row();
}
if((P2&048)==048)
{
c=1;
scan_row();
}
if((P2&028)==028)
{

5/23/2012 3:50 PM

8051 PWM C Program | Embed4u

6 of 11

http://www.embed4u.com/8051-pwm-c-program/

c=2;
scan_row();
}
}
void scan_row()
{
P2=096;
while((P2&096)==096);
if((P2&086)==086)
{
r=0;
display();
pwn_generator();
}
if((P2&092)==092)
{
r=1;
display();
pwn_generator();
}
if((P2&094)==094)
{
r=2;
display();
pwn_generator();
}
if((P2&016)==016)
{

5/23/2012 3:50 PM

8051 PWM C Program | Embed4u

7 of 11

http://www.embed4u.com/8051-pwm-c-program/

r=3;
display();
pwn_generator();
}
}

void pwn_generator()
{

TR0 = 0;
if (r != 2 && c != 1)
{
TH0 = ton[r];
}
else
{
TH0 = 0xfe;
}
x = 1;
TL0=000;
TR0 = 1;
}

void runtimer() interrupt 1


{
TR0=0;
toggle();
if(x == 1)

5/23/2012 3:50 PM

8051 PWM C Program | Embed4u

8 of 11

http://www.embed4u.com/8051-pwm-c-program/

{
TH0 = toff[r];
TL0 = 000;
}
else
{
TH0 = ton[r];
TL0 = 000;
}
TR0 = 1;
}

void toggle()
{
x = ~x;
}

void init_timer()
{

TMOD=TMOD & 0xf0;


TMOD=TMOD | 001;
IE=IE & 07f;
IE=IE | 002;
IE=IE | 080;
}

main()

5/23/2012 3:50 PM

8051 PWM C Program | Embed4u

9 of 11

http://www.embed4u.com/8051-pwm-c-program/

{
x = 000;
init_lcd();
init_array();
init_timer();
ton_array();
toff_array();
while(1)
{
scan_col();
delay();
}
}
Related posts:
1.
2.
3.
4.
5.

8051 password C Program


DS1820 C PROGRAM WITH 8051
8051 Embedded C Program for Toggle LEDs
8051 LCD CLOCK
8051 2 DIGIT CALCULATOR C PROGRAM

Categories: Code Library Tags: 8051 c code for pwm, 8051 PWM C Program|embed4u.com, Pulse Width
Modulation (PWM) tutorials: : 8051 Microcontroller ..., pwm c program for microcontroller|embed4u.com, pwm
using 805, using micro pwm to generate pure sine wave
Comments (0) Trackbacks (0) Leave a comment Trackback

5/23/2012 3:50 PM

8051 PWM C Program | Embed4u

10 of 11

http://www.embed4u.com/8051-pwm-c-program/

1. No comments yet.
1. No trackbacks yet.
You must be logged in to post a comment.
8051 External Interrupt c program: 8051 serial program

RSS

Recent Posts
stepper motor driver circuit using microcontroller

5/23/2012 3:50 PM

8051 PWM C Program | Embed4u

11 of 11

http://www.embed4u.com/8051-pwm-c-program/

IR Sensor based visitor counter


mcp3202 interface with 8051
SERVO MOTOR INTERFACE USING PWM
Final Year Embedded IEEE Projects 2010-2011

Categories
Code Library
DOWNLOAD
Drivers
Electronics
Projects
Related Videos
Schematics
Technology
Tutorial

Blogroll
HEXsource Technologies

Pages
About Us
Contact us
Privacy Policy
Top
Copyright 2009-2011 Embed4u

5/23/2012 3:50 PM

Das könnte Ihnen auch gefallen