#define MCU_8051 // for 8051
#define MCU_PIC // for PIC
Method 1) To control 8 LEDs, scrolling on/off, define function, void FlashLightOne(void),
**AT89S52 : use P1
**PIC16F877A : use PORTB
Method 2) To control 8 LEDs, 2 LEDs on at the same time, while the first LED is off, the next LED will be on, define funciton, void FlashLightTwo(void),
**AT89S51 : use P1
**PIC16F877A : use PORTB
main program:
/*******************************************************************************
Copyright (c) 2008 Wizign Ltd.
All Rights Reserved.
main.c: Main program for demo flash light
Date: Aug 27, 2008
Author: YenHung Chen
E-mail: yhchen@wizign.com
Revision:
---------- -----------------------------------------------------------------
2008/08/27 Created by YenHung Chen, demo flash light for both 8051 and PIC
******************************************************************************/
/*******************************************************************************
<>For AT89S52
There are 8 LEDs, which are connected P1.
Each pins of P1 is connected to one LED ligth.
<>For PIC16F877A
There are 8 LEDs, which are connected PORTB
Each pins of PORTB is connected to one LED ligth.
******************************************************************************/
#define MCU_8051
//#define MCU_PIC
#ifdef MCU_PIC
#include
#else // default is MCU_8051
#include
#endif
#include "delay.h"
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// define IO and global variables
#ifdef MCU_PIC
// Configurations
typedef unsigned int config;
config at 0x2007 __CONFIG = _RC_OSC & _PWRTE_ON & _BODEN_OFF & _WDT_OFF & _LVP_OFF;
#define gLEDs PORTB
#else // default is MCU_8051
#define gLEDs P1
#endif
int gCount = 0;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// define functions
/* Initilize parameters before run the main loop */
void InitParams(void)
{
gLEDs = 0xFF;
#ifdef MCU_PIC
TRISB = 0x00; // output
#endif
}
/* Make a LED ON each time */
void FlashLightOne(void)
{
unsigned char status;
status = 1 << gCount++;
if(gCount >= 8)
gCount = 0;
gLEDs = ~status;
}
/* Make two LED ON each time,
while the preview LED is OFF, the next new LED is ON */
void FlashLightTwo(void)
{
unsigned char status;
status = 1 << gCount++;
if(gCount >= 8)
gCount = 0;
if(status == 0x80){
status |= 1;
}else{
status |= (status << 1);
}
gLEDs = ~status;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// main program
void main(void)
{
// Initilize parameters
InitParams();
/* do loop */
while(1){
FlashLightOne();
DelayMs(1000);
}
}
No comments:
Post a Comment