ATMEL 8051 programming in C, example

OP #4373029
I need to write only numbers (every second) in string on display (memory 
mapped on 0x8001 adress) which are divding with 3 .

I must use counter not delay () for interrupt ...

I got with code below only to printing all numbers in one second.
I need 1 number per second, not all.

Anybody help?
1
#include <reg51.h> 
2
#include <math.h>                  
3
typedef unsigned char byte;
4

5
byte a[8]= {7,5,16,3,1,4,11,9}; 
6

7
byte counter;
8
byte xdata display _at_ 0x8001;
9
byte started = 0;
10
int i=0;
11

12
sbit switcher=P0^0;
13
 
14
void Inic(void) 
15
{
16
    EA=1;         
17
    TMOD=1;       
18
    TH0=0x3C;     
19
    TL0=0xAF;    
20
    TR0=1;
21
    counter=1;    
22
}
23

24
 
25
void timer0(void) interrupt 1 using 2 
26
{    
27
    TH0=0x3C;  
28
    TL0=0xAF;                
29
    
30
  counter--; 
31
    if(counter == 0)         
32
          {
33
            counter = 20;        
34
       if(started == 1)  
35
         {
36
         if(i < 8)              
37
            if(!(a[i]%3) )       
38
               display=a[i];                            
39
         i++;             
40
         if(i == 8)                            
41
            {
42
            ET0 = 0;      
43
            TR0 = 0;     
44
            started = 0;  
45
            }
46
         }
47
    }   
48
}
49

50
void main(void) 
51
{
52
   Inic();        
53
   switcher = 1;  
54
   while(1)       
55
     {                    
56
     if(switcher == 0)    
57
       if(started == 0)   
58
          {         
59
          started = 1;    
60
          i = 0;          
61
          counter = 20;    
62
                         
63
          TH0=0x3C;       
64
          TL0=0xAF;      
65
          ET0=1;        
66
          TR0=1;            
67
          }
68
     }            
69
}

Reply

A question of your own? This thread is over 10 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.