Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] 8051 blinking led code

Status
Not open for further replies.

sherryliu

Member level 1
Joined
Jan 17, 2011
Messages
32
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,535
Hi guys,i want to modified code to show blink led difference,how to optimized the code.when i click one show one way and click again show other way.

#include <reg51.h>
#include<intrins.h>

unsigned char tmod=0;
unsigned char i ,j,tmp;

void delay_1ms(void)
{
unsigned int i;
for (i=1;i<(unsigned int)(1144-2);i++);
}


void delay_ms(unsigned int n)
{
unsigned int i=0;
while(i<n)
{
delay_1ms();
i++;
}
}


void main(void)
{
ET0=1;
ET1=1;
EX0=1;
EX1=1;
EA=1;
while(1)
P0=0x00;
}

void int0(void) interrupt 0
{

switch(tmod)
{
case 0:
tmp=0x01;
for(j=8; j>0; j--)
{
P0=~tmp;
delay_ms(50);
tmp =tmp<<1;
}
break;
case 1:
tmp=0x80;
for(j=8; j>0; j--)
{
P0=~tmp;
delay_ms(50);
tmp = tmp>>1;
}
break;
case 2:
break;

}
tmod+=1;
if(tmod==2)tmod=0;

} 20131225150846.jpg
 

Hi jayanth
I used interrupt 0 to control blinking led,i want click it show one style,and click it again show other style.
 

mikroC Code for P1. Not tested. Test in hardware.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
unsigned char togglemode = 0, i = 0;
 
void Interrupt() {
     if(IE0_bit)
     togglemode = ~togglemode;
     
     IE0_bit = 0;
}
 
void main() {
 
     P1 = 0x00;
     P3.F2 = 1;
 
     EX0_bit = 1;
     IE0_bit = 1;
     IT0_bit = 1;
     EA_bit = 1;
       
     while(1){
 
             if(togglemode){
                   P1 = 0x01;
                   for(i = 0; i < 8; i++) {
                          P1 <<= 1;
                          Delay_ms(500);
                   }
             }
             else if(!togglemode){
                   P1 = 0x80;
                   for(i = 0; i < 8; i++) {
                          P1 >>= 1;
                          Delay_ms(500);
                   }
             }
     }
}

 

I have modified code ,can do it.thanks!!
 

Attachments

  • blinking led.rar
    17.4 KB · Views: 43

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top