把这个程序看懂后自己修改一下。
89c51的p1口上接八个led,经电阻后接5v电源,用c周期控制p1口电平的高低。
这个程序和89c51是通用的,改一下头文件就可以用的。
89c51的 I/O端口没有weak pull-ups等状态。
//------------------------------------------------------------------------------------
// F00x_Blinky.c
//------------------------------------------------------------------------------------
// Copyright (C) 2007 Silicon Laboratories, Inc.
//
// This program flashes the green LED on the C8051F000 target board about five times
// a second using the interrupt handler for Timer3.
// Target: C8051F000, C8051F010
//
// Tool chain: KEIL Eval 'c'
//
//#pragma CD OE DB SB // Compilation directives
//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include
#include
//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------
sbit LED = P1^6; // green LED: '1' = ON; '0' = OFF
#define TC_100ms 16000 // approx number of counts of 1.9MHz/12
// to get 100ms.
//------------------------------------------------------------------------------------
// Function PROTOTYPES
//------------------------------------------------------------------------------------
void Timer3_ISR (void);
//------------------------------------------------------------------------------------
// MAIN Routine
//------------------------------------------------------------------------------------
void main (void) {
// disable watchdog timer
//WDTCN = 0xde;
//WDTCN = 0xad;//89c51没有内部看门狗。
// set up Crossbar and GPIO
//XBR2 = 0x40;//89c51无此功能 // Enable crossbar and weak pull-ups
//PRT1CF |= 0x40; // enable P1.6 (LED) as a push-pull output
// set up Timer3 for auto-reload to generate interrupts at 100ms intervals
TMR3CN = 0x00; // stop Timer3
TMR3RLH = ((-TC_100ms >> 8) & 0xff); // init Timer3 reload value
TMR3RLL = (-TC_100ms & 0xff);
TMR3H = 0xff; // init Timer3 to reload immediately
TMR3L = 0xff;
EIE2 = 0x01; // enable Timer3 OVR interrupt
TMR3CN = 0x04; // start Timer3
EA = 1; // enable global interrupts
while (1) {
PCON |= 0x01; // set IDLE mode
}
}
//------------------------------------------------------------------------------------
// Interrupt Service Routines
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// Timer3_ISR
//------------------------------------------------------------------------------------
// This routine changes the state of the LED.
//
void Timer3_ISR (void) interrupt 14
{
TMR3CN &= ~(0x80); // clear TF3
P1= ~P1;//在此取反实现8个led周期性亮灭。
//LED = ~LED; // change state of LED
}
#define LED P0 //----定义的是P0口驱动LED,当P0口为低时LED点亮,如果实际硬件是其他IO口请更改
#define LIGHT 1 //---指示灯的方向,Light表示由全灭到逐个点亮
#define SHUT 0 //---指示灯的方向,SHUT表示由全亮到逐个熄灭
void Led(void)
{
static unsigned char Led_Num = 0;
bit Led_Direct = LIGHT ;
unsigned short Delay;
if(Led_Direct == LIGHT )
{
P0 &= ~(1<
if(Led_Num >=8) //Led_Num大于8表示已经全亮,所以把Led_Direct赋值成SHUT
{
Led_Direct = SHUT ;
Led_Num = 0;
}
}
else
{
P0 |= (1<
if(Led_Num >=8)//Led_Num大于8表示已经全灭,所以把Led_Direct赋值成LIGHT
{
Led_Direct = LIGHT ;
Led_Num = 0;
}
}
//----需要延迟,不然眼睛观察不到
for(Delay = 0;Delay<1000;Delay++);
}
很简单的程序 不至于这么高的分吧!最简单的一次点亮 ,可以利用循环实现哦!
#include
#define uchar unsigned char
#define uint unsigned int
void delay(void) //1s
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
main()
{
while(1)
{
P0=0x00;//0b00000000
delay();//延时
P0=0x01;//0b00000001
delay();
P0=0x03;//0b00000011
delay();
P0=0x07;//0b00000111
delay();
P0=0x0f;//0b00001111
delay();
P0=0x1f;//0b00011111
delay();
P0=0x3f;//0b00111111
delay();
P0=0x7f;//0b01111111
delay();
P0=0xff;//0b11111111
delay();
P0=0xfe;//0b11111110
delay();
P0=0xfc;//0b11111100
delay();
P0=0xf8;//0b11111000
delay();
P0=0xf0;//0b11110000
delay();
P0=0xe0;//0b11100000
delay();
P0=0xc0;//0b11000000
delay();
P0=0x80;//0b10000000
delay();
}
}
汇编的如下:
ORG 0000H
MOV R2, #16
L1: MOV A, R2
ADD A, #(D_TAB - $ - 3)
MOVC A, @A + PC
MOV P1, A
CALL DL_500MS
DJNZ R2, L1
SJMP 0000H
D_TAB:
DB 0, 0, 1, 3, 7, 15, 31, 63, 127, 255, 127, 63, 31, 15, 7, 3, 1
;-------------------------------延时子程序
DL_500MS:
MOV R5, #19
DL1: MOV R6, #128
DL2: MOV R7, #215
NOP
DL3: DJNZ R7, DL3
DJNZ R6, DL2
DJNZ R5, DL1
RET
;------------------------------------
END