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 |
||
(Není zobrazeno 7 mezilehlých verzí od 3 dalších uživatelů.) | |||
Řádek 54: | Řádek 54: | ||
===example 7-4=== | ===example 7-4=== | ||
<source lang"asm"> | |||
#include <reg51.h> | #include <reg51.h> | ||
Řádek 68: | Řádek 68: | ||
} | } | ||
</source> | |||
===example 7-5=== | |||
<source lang"asm"> | |||
#include <reg51.h> | |||
sbit MYBIT = P1^0; //definování výstupního bitu | |||
void main (void) | |||
{ | |||
MYBIT = 0; | |||
MYBIT = 1; | |||
} | |||
</source> | |||
===example 7-5-2=== | |||
<source lang"asm"> | <source lang"asm"> | ||
#include <reg51.h> | |||
sbit MYBIT = P1^0; //definování výstupního bitu | |||
void main (void) | |||
{ | |||
MYBIT = ~MYBIT; | |||
} | |||
</source> | </source> | ||
===example 7-5-3=== | |||
<source lang"asm"> | |||
#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> | |||
</source> | |||
===example 7-6 (neověřeno)=== | |||
<source lang"asm"> | <source lang"asm"> | ||
#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++); | |||
} | |||
} | |||
</source> | </source> | ||
===example 7-7 (neověřeno)=== | |||
<source lang"asm"> | |||
#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++); | |||
} | |||
</source> | |||
===example 7-8 (neověřeno)=== | |||
<source lang"asm"> | |||
#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++); | |||
} | |||
</source> | |||
===example 7-9 (neověřeno)=== | |||
<source lang"asm"> | |||
#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 | |||
} | |||
} | |||
</source> | |||
===example 7-10 (neověřeno)=== | |||
<source lang"asm"> | |||
#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++); | |||
} | |||
</source> | |||
===example 7-11 (neověřeno)=== | |||
<source lang"asm"> | |||
#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 | |||
} | |||
} | |||
</source> | |||
===example 7-12 (neověřeno)=== | |||
<source lang"asm"> | |||
//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 | |||
} | |||
} | |||
</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> | |||
===Venkovní čidlo a bzučák=== | |||
<source lang"asm"> | |||
Zadání: Venkovní čidlo je připojeno k P1.1 a bzučák je připojen k P1.7. | |||
Čidlo sleduje zavřené dveře, pokud se dveře otevřou bzučák bude zvonit. | |||
Napište program v C | |||
#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> | |||
===Text na LCD=== | |||
<source lang"asm"> | |||
Zadání: Připojete LCD na P1 a napište program v jazyce C, | |||
který pošle na displej text: "Ahoj programatore" | |||
#include <reg51.h> | |||
#define LCDData P1 //LCDData declaration | |||
sbit En=P2^0; //the enable pin | |||
void main(void) | |||
{ | |||
unsigned char message [ ]="Ahoj programatore"; | |||
unsigned char z; | |||
for(z=0;z<28;z++) //send all the 28 characters | |||
{ | |||
LCDData=message[z]; | |||
En=1; //a high- | |||
En=0; //-to-low pulse to latch the LCD data | |||
} | |||
} |
Aktuální verze z 18. 5. 2011, 16:01
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;
}
}
Venkovní čidlo a bzučák
Zadání: Venkovní čidlo je připojeno k P1.1 a bzučák je připojen k P1.7.
Čidlo sleduje zavřené dveře, pokud se dveře otevřou bzučák bude zvonit.
Napište program v C
#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++);
}
Text na LCD
<source lang"asm"> Zadání: Připojete LCD na P1 a napište program v jazyce C,
který pošle na displej text: "Ahoj programatore"
- include <reg51.h>
- define LCDData P1 //LCDData declaration
sbit En=P2^0; //the enable pin void main(void)
{ unsigned char message [ ]="Ahoj programatore"; unsigned char z; for(z=0;z<28;z++) //send all the 28 characters { LCDData=message[z]; En=1; //a high- En=0; //-to-low pulse to latch the LCD data } }