Embedded C pulse counter

hey guys,
i have to design and implement the pulse counter and draw a function of a number of pulses read in one second on the screen.
i have draw function (below) to present a normalised number of pulses per second.
i have Timer interrupt function(below)that selects a single frequency for an interrupt.
Note that timer function should produce the interrupt from the range of 5-80Hz. than during the initialization of the timer interrupt, first i have initialize TRR and then TMR register. and then calculate the values of TMR and TRR registers
for the chosen frequency.


draw function
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
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <minos.h>
#include <video.h>
#include <math.h>
 
#define PI 3.14
 
void draw_func( int pulses );
 
int main( void ){
            draw_func( 20 ); //this is an example function call
            delay(100);
      return ( 0 );
}
 
void draw_func( int pulses )
{
      int s;
      int i;
      int p;
      int q;
      int n;
 
      int s_generator[40];
      float m;
 
      s = pulses;
      if( s == 0 )
            s = 20;
 
      for( i = 0; i < 20; i++ ) {
            m = 2 * i * s * PI;
            s_generator[i] = (int) (20 * sin(m/400.) );
      }
      tab( 2,1);
      printf("FREQUENCY = %d INTS/sec", pulses);
 
      for( i = 0; i < 20; i++ ) {
            n = (int) s_generator[i];
            q = 2 + i;
            tab(2, q);
            printf("i= %d[%d]", i, n );fflush(stdout);
            p = 40;
            tab( p, q);
            printf("|");fflush(stdout);
            p = 40 + n;
            tab( p, q);
            printf("*");fflush(stdout);
      }
}


and timer interrupt function
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <adio.h>
#include <minos.h>
#include <video.h>
 
//#define PRESCALER 0x0000
 
#define PRESCALER 0x3f00
 
unsigned char *pbcnt = (unsigned char *)0x120016; // Port B control
unsigned char *pbddr = (unsigned char *)0x120018; // Port B direction
unsigned char *pbdat = (unsigned char *)0x12001a; // Port B data
unsigned short *licr1 = (unsigned short *)0x120020; // Latched Interrupt Control Registers 1
unsigned short  *licr2 = (unsigned short *)0x120022; // Latched Interrupt Control Registers 2
 
int irqs[8]; 
int irq_timer1 = 0;
int irq_timer2 = 0;
 
void *oldvec1, *oldvec2, *oldvec3, *oldvec4, *oldvec5, *oldvec6, *oldvec7, *oldvec8;
void *oldvec_timer1;
void *oldvec_timer2;
 
 
// Timers memory map addresses
unsigned short *tmr1 = (unsigned short *) 0x120120;
unsigned short *trr1 = (unsigned short *) 0x120122;
unsigned short *tcr1 = (unsigned short *) 0x120124;
unsigned short *tcn1 = (unsigned short *) 0x120126;
unsigned char  *ter1 = (unsigned char *)  0x120129;
 
unsigned short  *tmr2 = (unsigned short *) 0x120130;
unsigned short  *trr2 = (unsigned short *) 0x120132;
unsigned short  *tcr2 = (unsigned short *) 0x120134;
unsigned short  *tcn2 = (unsigned short *) 0x120136;
unsigned char  *ter2 = (unsigned char *)   0x120139;
unsigned short *picr = (unsigned short *)  0x120024; 
 
void _interrupt mytimer1(void){
            ibegin;
            *ter2 = (unsigned char) 0x02;                            
            irq_timer1 = 1;
            ireturn;
}
void _interrupt mytimer2(void){
            ibegin;
            *ter2 = (unsigned char) 0x02;                            
            irq_timer2 = 1;
            ireturn;
}
void _interrupt intSrv1(void){
            ibegin;
            *licr1 = 0xa222;                        
            irqs[1] = 1;                                         
            ireturn;
           
}
void _interrupt intSrv4(void){
            ibegin;
            *licr1 = 0x222a;                         
            irqs[4] = 1;        // set bit in array
            ireturn;
}
void _interrupt intSrv7(void){
            ibegin;
            *licr2 = 0x22a3;    // clear PIR7 bit
            irqs[7] = 1;      // set bit in array
            ireturn;
}
void IRQsetup( void )
{
            oldvec1 = _getvect(0x42);  // Save INT1 handler address
            oldvec4 = _getvect(0x45);  // Save INT4 handler address
            oldvec7 = _getvect(0x48);  // Save INT7 handler address
            _setvect(0x42,intSrv1);                         

            _setvect(0x45,intSrv4);         // Set up new vector
            _setvect(0x48,intSrv7);         // Set up new vector
            oldvec_timer2 = _getvect(0x4b); // Save current handler address for timer2
            _setvect(0x4b,mytimer2);
 
            *tmr2 = (unsigned short) (0x0035 | PRESCALER); //ff35
            *trr2 = (unsigned short) 0x07ff; //0x07ff
            *tcn2 = (unsigned short) 0x0000;
            *picr = *picr | (unsigned short) 0x0100; 
            *pbcnt = 0xff; 
            *licr1 = 0xaaaa;                         // all interrupts level 2
            *licr2 = 0xaaab;                         // INT8 left as level 3
}
 
void IRQstop( void )
/* This code will disable the interrupt and leave the system in a safe state */
{
            *pbcnt = 0x80;                // turn off interrupt enables
            *licr1 = 0x8888;              // disable interrupts
            *licr2 = 0x888b;              // disable interrupts except INT8
            _setvect(0x42,oldvec1);       // Restore the old vectors 
            _setvect(0x45,oldvec4);       // Restore the old vectors
            _setvect(0x48,oldvec7);       // Restore the old vectors
 
            *tmr2 = (unsigned short) 0;
            _setvect(0x4b,oldvec_timer2);
 
}
 
int main( void )
{
int i;
cls();
tab(16,23);
printf("All interrupt inputs are falling EDGE triggeredn");
fflush(stdout);
outch(0); // Set channel 0 to be outputs
outch(1); // Set channel 1 to be outputs
outch(2); // Set channel 2 to be outputs
IRQsetup(); // set up the interrupt services
tab(1,1);
printf(" Interrupt Inputs  "); fflush(stdout);
tab(1,2);
printf("INT1 - Mode Switch"); fflush(stdout);
tab(1,5);
printf("INT4 - Real Time Clock"); fflush(stdout);
tab(1,8);
printf("INT7 - Keypad (CH 0-16)"); fflush(stdout);
tab(1,10);
printf("Timer1"); fflush(stdout);
tab(1,11);
printf("Timer2"); fflush(stdout);
tab(1,12);
printf("Number of pulses in TCN2"); fflush(stdout);
while(!ready(fileno(stdin))) {
      tab(33,10);
      if( irq_timer1 == 1 ) {
         colour(green);
         printf("1");
      }else{
         colour(red);
         printf("-");
      }
         fflush(stdout);
         irq_timer1 = 0;
         tab(33,11);                   
         if( irq_timer2 == 1 ) {
             printf("1");
             writech(2,1); // Write a logic low to channel
             delay(1);   // wait for 0.5 second
             writech(2,0); // Write a logic high to channel
          } else {
             printf("-");
          }
          fflush(stdout);
          irq_timer2 = 0;
          for (i=1;i<9;i+=3) {
               tab(33,i+1);
                 if (irqs[i] == 1) {
                     switch(i) {
                     case 4:
                     *tmr2 = (unsigned short) (0x0031 | PRESCALER);                                                                                                                                             printf("%X", *tcn2);fflush(stdout);                                                                       writech(0,1);    // Write a logic low to chanel                                  delay(5);           // wait for 0.5 second
writech(0,0);    // Write a logic high to channel
 *tmr2 = (unsigned short) (0x0034 | PRESCALER);  // RESET TIMER
 *tmr2 = (unsigned short) (0x0035 | PRESCALER);  // START TIMER
                       break;
                       }
                       tab(33,i+1);
                       printf("1");
                       } else {
                         printf("-");
                       } 
                         fflush(stdout);
                         irqs[i] = 0;
                        }
                        delay(5);
            }
            colour(white);
            IRQstop();
            window(0);      
            ontop(0);      // disable interrupts & leave safe
            cls();
            return 0;
}


any help would be greatly appreciated.
suggestions are more than welcome
THANK YOU
Topic archived. No new replies allowed.