Outline of program for Lab 13

#include <hc11.h>

#define TRUE  1
#define FALSE 0

#define PWM_PERIOD 1000   /* Pulse width modulation period in timer ticks */
                          /* For a 500 Hz PWM and prescaler divide by 4   */

unsigned int period;
unsigned char voltage;
int calc_dc_flag, print_flag;
unsigned int dc_inc;


main()
{

    float speed;      /* Actual speed*/
    float desired;    /* Desired speed from pot */
    float fdc;        /* Fractional duty cycle (0.1 to 1.0) */
    float k;          /* Control proportionality constant */
    float m;          /* Slope to calculate desired speed */
    float b;          /* Intercept to calculate desired speed */
    int i;            /* Temp variable */

    TMSK2 = 0x01;     /* Prescaler divide by 4 */

    /* Setup stuff */

    fdc = 0.5;
    dc_inc = fdc * (PWM_PERIOD - 1);

    enable();
    xenable();

    /* Wait for 10 seconds (40 TOI periods) for motor to come to speed */

    i = 0;
    while (i < 40)
    {
        if (calc_dc_flag)
        {
            i = i + 1;
            calc_dc_flag = FALSE;
        }
    }
 
    while(TRUE)
    {
        speed = 3.0e7/(float) period;
        desired = m * (float) voltage + b;
        if (calc_dc_flag)
        {
            fdc = fdc + k * (desired - speed);
            if (fdc < 0.1) fdc = 0.1;
            if (fdc > 1.0) fdc = 1.0;
            dc_inc = fdc * (PWM_PERIOD - 1);
            calc_dc_flag = FALSE;
        }
        if (print_flag)
        {
            printf("Speed = %f\n",speed);
            .
            .
            .
            print_flag = FLASE;
        }
    }
}

#pragma tof_isr

void tof_isr(void)
{
    /* Determine pattern to put on LEDS */
    PIA_B = pattern;
    calc_dc_flag = TRUE;
    TFLG2 = 0x80;
}