Forum: Mikrocontroller und Digitale Elektronik takterzeugung timer0


von marten (Gast)


Lesenswert?

AT89S8253
microC


Hallo,

ich würde gerne eine Pulsspannung mit dem Timer0 erzeugen.
Ich habe die Routine so geschrieben das das Puls-Pause Verhältnis immer 
gleich ist. Der Reloadwert ist für 10ms berechnet.

10ms:       10ms / 1.2us = 8333.33     -> 65536-8333  = 57203
--> 57023 = 0xDF73

Kann mir jemand eine einfache Möglichkeit vorschlagen wie ich das 
Puls-Pause Verhältnis unabhängig von einander machen kann?
So wie ich es jetzt geschrieben habe ist es immer gleich.
Wie kann man das elegant lösen? Immer Reloadwert verändern?
1
sbit sb_LED at P2.B0;
2
3
unsigned char uc_waittime = 0x00;
4
5
void Timer0_ISR() org 0x000B ilevel 0{
6
7
  EA_bit = 0;        // Clear global interrupt enable flag
8
  TR0_bit = 0;       // Stop Timer1
9
10
  TH0 = 0xDF;        // Set Timer1 high byte
11
  TL0 = 0x73;        // Set Timer1 low byte
12
  
13
  if (uc_waittime == 50)
14
     {
15
      if (sb_LED == 0)
16
         sb_LED = 1;
17
      else
18
          sb_LED = 0;
19
     
20
     uc_waittime = 0;
21
     }
22
     else
23
         uc_waittime++;
24
25
26
  TR0_bit = 1;       // Start Timer 1
27
  EA_bit = 1;        // Set global interrupt enable flag
28
}
29
30
void main() {
31
32
  P0  = 0x00;           // Initialize PORT0 outout
33
  P1  = 0x00;           // Initialize PORT1 outout
34
  P2  = 0x00;           // Initialize PORT2 outout
35
  P3  = 0xFF;           // Initialize PORT3 input
36
  
37
  //TMOD = 0x01;     // Timer0 Mode 1
38
  GATE0_bit = 0;     // Set to enable timer/counter 0 only while the INT0# pin is high and the TR0 bit is set. -> TMOD Register (89h)
39
                     // Clear to enable timer 0 whenever the TR0 bit is set.
40
  C_T0_bit  = 0;     // Set for counter operation: timer 0 counts negative transitions on external pin T0. -> TMOD Register (89h)
41
                     // Clear for timer operation: timer 0 counts the divided-down system clock.
42
  M10_bit   = 0;     // M11_M01 = 01    =>   Mode 1(16-bit Timer/Counter) -> TMOD Register (89h)
43
  M00_bit   = 1;     //                                                   -> TMOD Register (89h)
44
  
45
  TH0 = 0xDF;        // Set Timer1 high byte
46
  TL0 = 0x73;        // Set Timer1 low byte
47
  
48
  TF0_bit = 0;       // Ensure that Timer0 interrupt flag is cleared -> TCON Register (88h)
49
  uc_waittime = 0x00;
50
  TR0_bit = 1;       // Run Timer0
51
  
52
  ET0_bit = 1;       // Enable Timer0 interrupt -> IE Register (A8h)
53
  EA_bit  = 1;       // Set global interrupt enable -> IE Register (A8h)
54
55
  while(1){
56
  ;
57
  }
58
}

: Bearbeitet durch User
von marten (Gast)


Lesenswert?

Habs jetzt mal so gelöst...
Puls 50ms...
Pause 200ms...
Gibt es Verbesserungsvorschläge...
1
void Timer0_ISR() org 0x000B ilevel 0{
2
3
  unsigned char uc_flag,
4
5
  EA_bit = 0;        // Clear global interrupt enable flag
6
  TR0_bit = 0;       // Stop Timer1
7
  TH0 = 0xDF;        // Set Timer1 high byte
8
  TL0 = 0x73;        // Set Timer1 low byte
9
10
  switch (uc_flag){
11
12
         case 0:   if (uc_waittime == 50)
13
                   {
14
                      sb_LED = 1;
15
                      uc_waittime = 0;
16
                      uc_flag = 1;
17
                      }
18
                      else
19
                      uc_waittime++;
20
                 
21
                   break;
22
23
         case 1:   if (uc_waittime == 200)
24
                   {
25
                      sb_LED = 0;
26
                      uc_waittime = 0;
27
                      uc_flag = 0;
28
                      }
29
                      else
30
                      uc_waittime++;
31
32
                   break;
33
34
         default: ;
35
36
         }
37
         
38
  TR0_bit = 1;       // Start Timer 1
39
  EA_bit = 1;        // Set global interrupt enable flag
40
}

: Bearbeitet durch User
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.