gedas 0 Junior Poster

hey everyone,
i been set a coursework which i am totally confused about, i have been struggling to even understand it so i hope someone can at least lead me to the right direction.
i have been asked to design and implement the pulse counter and draw a function(draw function does not require any changes) of a number of pulses read in one second on the screen. to
present a normalised number of pulses per second (10 pulses/sec - 1/2 period
of sinusoid; 20 pulses/sec – 1 period of sinusoid; 40 pulses/sec – 2 periods of
sinusoid)

Draw Function

#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);
      }
}

i can use timer interrupt function to Select
a single frequency for an interrupt. Note that timer should produce the
interrupt from the range of 5-80Hz (see TRR register). Hint: during
initialisation of the timer interrupt, first initialise TRR and then TMR
register. Explain how you calculated the values of TMR and TRR registers
for your chosen frequency.


timer interrupt function

#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;
 
// end
 
void _interrupt mytimer1(void)
{
            ibegin;
            *ter2 = (unsigned char) 0x02;                            // Clear interrupt condition
            irq_timer1 = 1;
            ireturn;
}
void _interrupt mytimer2(void)
{
            ibegin;
            *ter2 = (unsigned char) 0x02;                            // Clear interrupt condition
            irq_timer2 = 1;
            ireturn;
           
}
 
void _interrupt intSrv1(void)
{
            ibegin;
            *licr1 = 0xa222;                         // clear PIR1 bit
            irqs[1] = 1;                                          // set bit in array
            ireturn;
           
}
 
 
/*
void _interrupt intSrv2(void)
{
            ibegin;
            *licr1 = 0x2a22;                         // clear PIR2 bit
            irqs[2] = 1;                                          // set bit in array
            ireturn;
}
 
void _interrupt intSrv3(void)
{
            ibegin;
            *licr1 = 0x22a2;                         // clear PIR3 bit
            irqs[3] = 1;                                          // set bit in array
            ireturn;
}
*/
 
 
void _interrupt intSrv4(void)
{
            ibegin;
            *licr1 = 0x222a;                         // clear PIR4 bit
            irqs[4] = 1;                                          // set bit in array
            ireturn;
 
}
 
 
/*
void _interrupt intSrv5(void)
{
            ibegin;
            *licr2 = 0xa223;                         // clear PIR5 bit
            irqs[5] = 1;                                          // set bit in array
            ireturn;
}
 
void _interrupt intSrv6(void)
{
            ibegin;
            *licr2 = 0x2a23;                         // clear PIR6 bit
            irqs[6] = 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 _interrupt intSrv8(void)
{
            ibegin;
            *licr2 = 0x222b;                         // clear PIR8 bit
            irqs[8] = 1;                                          // set bit in array
            ireturn;
}
*/
 
void IRQsetup( void )
{
            oldvec1 = _getvect(0x42);             // Save INT1 handler address
 
 
            //oldvec2 = _getvect(0x43);             //Save INT2 handler address
            //oldvec3 = _getvect(0x44);             //Save INT3 handler address
 
 
            oldvec4 = _getvect(0x45);             // Save INT4 handler address
 
 
            //oldvec5 = _getvect(0x46);             // Save INT5 handler address
            //oldvec6 = _getvect(0x47);             // Save INT6 handler address
 
 
            oldvec7 = _getvect(0x48);             // Save INT7 handler address
 
            //oldvec8 = _getvect(0x49);             // Save INT8 handler address
           
            _setvect(0x42,intSrv1);                          // Set up new vector
 
//            _setvect(0x43,intSrv2);                          // Set up new vector
//            _setvect(0x44,intSrv3);                          // Set up new vector
 
            _setvect(0x45,intSrv4);                          // Set up new vector
 
//            _setvect(0x46,intSrv5);                          // Set up new vector
//            _setvect(0x47,intSrv6);                          // Set up new vector
 
            _setvect(0x48,intSrv7);                          // Set up new vector
 
//            _setvect(0x49,intSrv8);             // set up new vector       
 
 
            //oldvec_timer1 = _getvect(0x4a);            // Save current handler address for timer1
            //_setvect(0x4a,mytimer1);
           
            //*tmr1 = (unsigned short) 0x0f3d; //0x7f3d
            //*trr1 = (unsigned short) 0xffff;
            //*tcn1 = (unsigned short) 0x0001;
           
 
            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(0x43,oldvec2);                         // Restore the old vectors
//            _setvect(0x44,oldvec3);                         // Restore the old vectors
 
            _setvect(0x45,oldvec4);                         // Restore the old vectors
 
//            _setvect(0x46,oldvec5);                         // Restore the old vectors
//            _setvect(0x47,oldvec6);                         // Restore the old vectors
 
            _setvect(0x48,oldvec7);                         // Restore the old vectors
 
//            _setvect(0x49,oldvec8);                         // Restore the old vectors
 
 
 
            //*tmr1 = (unsigned short) 0;
            *tmr2 = (unsigned short) 0;
 
            //_setvect(0x4a,oldvec_timer1);
            _setvect(0x4b,oldvec_timer2);
 
}
 
int main( void )
{
            int i;
           
 
            cls();
            tab(16,23);
            printf("All interrupt inputs are falling EDGE triggered\n");
            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,3);
//            printf("INT2 - Digital I/O (CH 17-32)"); fflush(stdout);
//            tab(1,4);
//            printf("INT3 - Led (not used)"); fflush(stdout);
            tab(1,5);
            printf("INT4 - Real Time Clock"); fflush(stdout);
//            tab(1,6);
//            printf("INT5 - Module Bus (I6)"); fflush(stdout);
//            tab(1,7);
//            printf("INT6 - Module Bus (I5)"); fflush(stdout);
            tab(1,8);
            printf("INT7 - Keypad (CH 0-16)"); fflush(stdout);
//            tab(1,9);
//            printf("INT8 - Serial Port S1 & S2"); 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;
                        //delay(30);
 
                        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);             // STOP TIMER
                                                                        tab(33,12);
                                                                        printf("%X", *tcn2);fflush(stdout);
                                                                       
                                                                         writech(0,1);    // Write a logic low to channel
                                                                        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;
}

if someone at least understands what needs to be done please let me know.
if you were wondering this code has to be executed on embedded environment of FM-400 System.
i am software engineer and this is electronics therefore i am finding this very complicated.
If something is unclear please ask , i might be able to give clearer specification
any help or suggestions will be greatly appreciated

THANK YOU