![]() |
|||
Microcontroller Kits Simple
Mikrokontroller 89s51 Trainer
Super
Mikrokontroller Trainer 89s51
|
Driving 8 LED C Programming 8051 Microcontroller
/************************************************
* example of using WHILE loop construct *
* to drive 8 LEDS connected to port B *
* *
* * Compiler : MIDE-51
* *
************************************************/
#include < at89s51.h > /* Include 89s51 header file */
char const num[ ] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
void wait (void)
{ ; /* wait function */
}
void main( void )
{
unsigned int i;
unsigned char j;
P0 = 0; /* initialize ZERO to P0 */ while(1){
for( j = 0; j < 8; j++ )
{
P0 = num[ j ];
for ( i = 0; i < 10000; i++ )
{
wait(); /* delay for half second */
}
}
}
}
Wichit Sirichote, [email protected]
Test your board with myfirst.c, a simple c program /*
* myfirst.c
* First C program for 2051 experiment
* complement P1.7 every 0.5 sec
* Copyright (C) 1999 Wichit Sirichote
* compiled with Dunfield Micro-C for 8051 Release 3.2
*/
#include c:\mc\8051io.h /* include i/o header file */
#include c:\mc\8051reg.h
extern register char cputick; // cputick was incremented every 10ms register unsigned char sec100,flag1; #define n 50 task1(); // functions declarations
task2();
main()
{
flag1 = 0;
sec100 = 0;
serinit(9600); // set timer0 to be 16 bit counter
while(1){
while(cputick == 0) ; cputick = 0; task1(); task2(); } } task1() // set bit 0 of flag1 every n*10ms
{
sec100++; // increment sec100
if (sec100 >= n)
{sec100 = 0; // clear sec100
flag1 |= 0x01; // set bit 0 of flag1
}
}
task2()
{
if ((flag1 & 0x01) != 0) // execute below if bit 0 of flag1 is set
{
// P1 ^= 0x80; // exclusive or the latch bit 7 with 0x80
asm " CPL P1.7"; // complement P1.7
flag1 &= ~0x01; // clear bit 0 of flag1
}
}
Comments, questions and discussion about this topic
|
||