Příklady s časovým zpožděním v C pro 8051: Porovnání verzí
Skočit na navigaci
Skočit na vyhledávání
Bez shrnutí editace |
Bez shrnutí editace |
||
Řádek 269: | Řádek 269: | ||
void main (void) | void main (void) | ||
{ | { | ||
while(1) | |||
{ | { | ||
mybit=1; //turn on P2.4 | |||
mybit=0; //turn off P2.4 | |||
} | |||
} | |||
} | </source> | ||
===example 7-13 (neověřeno)=== | |||
<source lang"asm"> | |||
#include <reg51.h> | |||
sbit mybit = P1^5; //notice the way single bit is declared | |||
void main (void) | |||
{ | |||
mybit=1; //make mybit an input | |||
while(1) | |||
{ | |||
if(mybit==1) | |||
P0=0x55; | |||
else | |||
P2=0xAA; | |||
} | |||
} | |||
</source> | |||
===example 7-14 (neověřeno)=== | |||
<source lang"asm"> | |||
#include <reg51.h> | |||
sbit Dsensor = P1^1; //notice the way single bit is defined | |||
sbit Buzzer = P1^7; | |||
void main (void) | |||
{ | |||
Dsensor=1; //make P1.1 an input | |||
while(Dsensor==1) | |||
{ | |||
buzzer=0; | |||
MSDelay(200); | |||
buzzer=1; | |||
MSDelay(200); | |||
} | |||
} | |||
void MSDelay(unsigned int itime) | |||
{ | |||
unsigned int i, j; | |||
for(i=0,i<itime;i++) | |||
for(j=0;j<1275;j++); | |||
} | |||
</source> | |||
===example 7-15 (neověřeno)=== | |||
<source lang"asm"> | |||
#include <reg51.h> | |||
sbit mybit = P1^5; //notice the way single bit is declared | |||
void main (void) | |||
{ | |||
mybit=1; //make mybit an input | |||
while(1) | |||
{ | |||
if(mybit==1) | |||
P0=0x55; | |||
else | |||
P2=0xAA; | |||
} | |||
} | } |
Verze z 18. 5. 2011, 11:51
zdroj informací: http://what-when-how.com/8051-microcontroller/data-types-and-time-delay-in-8051-c/
Unsigned char
example 7-1
#include <reg51.h>
void main (void)
{
unsigned char z;
for (z=0;z<=255;z++)
P1=z;
}
example 7-2
#include <reg51.h>
void main (void)
{
unsigned char mynum [] = "012345ABCD";
unsigned char z;
for (z=0;z<=10;z++)
P1 = mynum[z];
}
example 7-3
#include <reg51.h>
void main (void)
{
for (;;) //opakovani cyklu
P1=0x55;
P1=0xAA;
}
Unsigned char
example 7-4
#include <reg51.h>
void main (void)
{
char mynum [] = {+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
for (z=0;z<=8;z++)
P1 = mynum[z];
}
example 7-5
#include <reg51.h>
sbit MYBIT = P1^0; //definování výstupního bitu
void main (void)
{
MYBIT = 0;
MYBIT = 1;
}
example 7-5-2
#include <reg51.h>
sbit MYBIT = P1^0; //definování výstupního bitu
void main (void)
{
MYBIT = ~MYBIT;
}
example 7-5-3
#include <reg51.h>
sbit MYBIT = P1^0; //definování výstupního bitu
void main (void)
{
unsigned int z;
for (z=0;z<=5;z++)
{
MYBIT = 0;
MYBIT = 1;
}
}
</source>
example 7-6 (neověřeno)
#include <reg51.h>
void main (void)
{
unsigned int x;
for (;;) //repeat forever
{
P1=0x55;
for(x=0;x<40000;x++); //delay size unknown
P1=0xAA;
for(x=0;x<40000;x++);
}
}
example 7-7 (neověřeno)
#include <reg51.h>
void MSDelay(unsigned int);
void main (void)
{
while(1) //opakování cyklu
{
P1=0x55;
MSDelay(250);
P1=0xAA;
MSDelay(250);
}
}
Void MSDelay(unsigned int itime)
{
unsigned int i, j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}
example 7-8 (neověřeno)
#include <reg51.h>
void MSDelay(unsigned int);
void main (void)
{
while(1) //another way to do it forever
{
P0=0x55;
P2=0x55;
MSDelay(250);
P0=0xAA;
P2=0xAA
MSDelay(250);
}
}
Void MSDelay(unsigned int itime)
{
unsigned int i, j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}
example 7-9 (neověřeno)
#include <reg51.h>
#definice LED P2 //notice how we can define P2
void main (void)
{
P1=00; //clear P1
LED=0; //clear P2
for(;;) //Repeat forever
{
P1++; //increment P1
LED++; //increment P2
}
}
example 7-10 (neověřeno)
#include <reg51.h>
void MSDelay (unsigned int);
void main (void)
{
unsigned char mybyte;
P1=0xFF; //make P1 an input port
while(1)
{
mybyte=P1; //get a byte from P1
MSDelay(500);
P2=mybyte; //send it to P2
}
}
void MSDelay(unsigned int itime)
{
unsigned int i, j;
for i=0,i<itime;i++}
for(j=0;j<1275,jj++);
}
example 7-11 (neověřeno)
#include <reg51.h>
void main (void)
{
unsigned char mybyte;
P0=0xFF; //make P0 an input port
while(1)
{
mybyte=P0; //get a byte from P0
if(mybyte<100)
P1=mybyte; //send it to P1 if less than 100
else
P2=mybyte; //send iu to P2 if more than 100
}
}
example 7-12 (neověřeno)
//toggling an individal bit
#include <reg51.h>
sbit mybit = P2^4; //notice the way single bit is declared
void main (void)
{
while(1)
{
mybit=1; //turn on P2.4
mybit=0; //turn off P2.4
}
}
example 7-13 (neověřeno)
#include <reg51.h>
sbit mybit = P1^5; //notice the way single bit is declared
void main (void)
{
mybit=1; //make mybit an input
while(1)
{
if(mybit==1)
P0=0x55;
else
P2=0xAA;
}
}
example 7-14 (neověřeno)
#include <reg51.h>
sbit Dsensor = P1^1; //notice the way single bit is defined
sbit Buzzer = P1^7;
void main (void)
{
Dsensor=1; //make P1.1 an input
while(Dsensor==1)
{
buzzer=0;
MSDelay(200);
buzzer=1;
MSDelay(200);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i, j;
for(i=0,i<itime;i++)
for(j=0;j<1275;j++);
}
example 7-15 (neověřeno)
<source lang"asm">
- include <reg51.h>
sbit mybit = P1^5; //notice the way single bit is declared void main (void)
{ mybit=1; //make mybit an input while(1) { if(mybit==1) P0=0x55; else P2=0xAA; } }