Skip to content

Instantly share code, notes, and snippets.

@rbaas293
Last active November 13, 2017 17:09
Show Gist options
  • Save rbaas293/42db27dd6091a8275513bdd6d5a08f7a to your computer and use it in GitHub Desktop.
Save rbaas293/42db27dd6091a8275513bdd6d5a08f7a to your computer and use it in GitHub Desktop.
Lab Code for Embedded Systems

Lab Code

Lab 5 P1

void main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    while (1)
    {
        if(!shockSwitch_GetValue())
        {
            LED0_SetHigh();
        }
        else
        {
            LED0_SetLow();
        }

        if(!knockSwitch_GetValue())
        {
            LED1_SetHigh();
        }
        else
        {
            LED1_SetLow();
        }

        if(!tiltSwitch_GetValue())
        {
            LED2_SetHigh();
        }
        else
        {
            LED2_SetLow();
        }

        //__delay_ms(200);
    }
}
/**
 End of File
*/

Lab 5_1 Code

*/

#include "mcc_generated_files/mcc.h"

/*
                         Main application
 */
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();
    while (1)
    {
        if(motionSensor_GetValue())
        {
            buzzer_SetHigh();
        }

        if(!button_GetValue())
        {
            buzzer_SetLow();
        }
    }
}
/**
 End of File
*/

Lab5_2

// Lab 5-2 Code

long int counter = 0;
long int distance = 0;


int main()
{

  while (1)
  {
    trigger_Setlow();
    __delay_ms(2);
    trigger_SetHigh();
    __delay_ms(10);
    trigger_SetLow();

    while (echo_GetValue())
    {
      count = count + 1;
      __delay_us(1);

      distance = count*340;
    }
      distance = count*340;
    while (!echo_GetValue())
    {

    }
  }
}

}

Lab6_1

//Lab6_1

#include "mcc_generated_files/mcc.h"

void main(void)
{
    SYSTEM_Initialize();
    int conversionResult, highBits, lowBits, i, delay;
    ADC_SelectChannel(tempMeter); //ADC on pin RB4


    while (1)
    {

        ADC_StartConversion();
        while(!ADC_IsConversionDone())
        {
        }
        highBits = ADRESH; //Upper 2 Bits
        lowBits = ADRESL; //Lower 10 bits
        delay = (lowBits) * 10;

        // Select pin to light up based on highest 2 bits
        // Blink LED based on delay calculated from lowest 8 bits

        switch(highBits)
        {

            case 0 :
            LED0_SetHigh();         
            for (i=0;i<delay;i++)   
            {
                __delay_ms(1);
            }
            LED0_SetLow();   
            for (i=0;i<delay;i++)  
            {
                __delay_ms(1);
            }
            break;


            case 1 :
            LED1_SetHigh();
            for (i=0;i<delay;i++)
            {
                __delay_ms(1);
            }
            LED1_SetLow();
            for (i=0;i<delay;i++)
            {
                __delay_ms(1);
            }
            break;


            case 2 :
            LED2_SetHigh();
            for (i=0;i<delay;i++)
            {
                __delay_ms(1);
            }
            LED2_SetLow();
            for (i=0;i<delay;i++)
            {
                __delay_ms(1);
            }
            break;


            case 3 :
            LED3_SetHigh();
            for (i=0;i<delay;i++)
            {
                __delay_ms(1);
            }
            LED3_SetLow();
            for (i=0;i<delay;i++)
            {
                __delay_ms(1);
            }
            break;
        }
    }
}
/**
 End of File
*/

Lab6_2

//Lab6_2

#include "mcc_generated_files/mcc.h"

/*
                         Main application
 */
void main(void)
{
    SYSTEM_Initialize(); // Declare variables and select channel 0
    int result, i, pulse;
    double angle;
    ADC_SelectChannel(soundMeter);


    while (1)
    {
        ADC_StartConversion();

        while(!ADC_IsConversionDone()){}

        angle = ADC_GetConversionResult();

        pulse = ((angle*1000/180)-600)/2; //Convert angle to pulse delay equivalent

        servoPin_SetHigh(); //Send pulse to servo

        for (i=0; i<pulse; i++)
        {
            __delay_us(1);
        }

        servoPin_SetLow();
        __delay_ms(20); //pause between pulses
    }
}
/**
 End of File
*/

Lab6_3

//lab 6_3

#include "mcc_generated_files/mcc.h"

/*
                         Main application
 */
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    uint8_t result;

    while (1)
    {
        ADC_SelectChannel(waterMeter);

        result = ADC_GetConversion(waterMeter);

        //water_level = WaterMeter/4.3 - 65;

        __delay_ms(20);
        int abc = 0;

    }
}
/**
 End of File
*/

Lab6_4

//Lab6_4

#include "mcc_generated_files/mcc.h"

/*
                         Main application
 */
void main(void)
{
    SYSTEM_Initialize(); // Declare variables
    double xResult, yResult;
    int x = 200;
    int y = 200;

    while (1)
    {
        //get xPos ADC
        ADC_SelectChannel(xPin);
        ADC_StartConversion();
        while(!ADC_IsConversionDone())
        {
        }
        xResult = ADC_GetConversionResult();

        //get yPos ADC
        ADC_SelectChannel(yPin);
        ADC_StartConversion();
        while(!ADC_IsConversionDone())
        {
        }
        yResult = ADC_GetConversionResult();

        int abc = 0;

        //Determine direction of x and y change based on position of joystick - down, up, or no movement

        if (xResult < 500)
        {
            x--;
        }
        else if (xResult > 600)
        {
            x++;
        }       

        if (yResult < 500)
        {
            y--;
        }
        else if (yResult > 600)
        {
            y++;
        }

        // This set of nested conditionals determines which LED to light up based on any changes in the x and y variables

        if ( x % 2 == 1)
        {
            if ( y % 4 == 1 )
            {
                LED3_SetHigh();
                __delay_ms(200);
                LED3_SetLow();
            }
            else if ( y % 4 == 2 )
            {
                LED2_SetHigh();
                __delay_ms(200);
                LED2_SetLow();
            }
            else if ( y % 4 == 3 )
            {
                LED1_SetHigh();
                __delay_ms(200);
                LED1_SetLow();
            }
            else
            {
                LED0_SetHigh();
                __delay_ms(200);
                LED0_SetLow();
            }
        }
        else
        {
            if ( y % 4 == 1 )
            {
                LED4_SetHigh();
                __delay_ms(200);
                LED4_SetLow();
            }
            else if ( y % 4 == 2 )
            {
                LED5_SetHigh();
                __delay_ms(200);
                LED5_SetLow();
            }
            else if ( y % 4 == 3 )
            {
                LED6_SetHigh();
                __delay_ms(200);
                LED6_SetLow();
            }
            else
            {
                LED7_SetHigh();
                __delay_ms(200);
                LED7_SetLow();
            }
        }

    }
}
/**
 End of File
*/

Lab7_1

//Lab7_1

#include "mcc_generated_files/mcc.h"

/*
                         Main application
 */
void main(void)
{
    SYSTEM_Initialize();

    uint8_t lower, upper;
    upper = lower = 0;

    //Set all LEDs to LOW
    LED0_LAT = LED1_LAT = LED2_LAT = LED3_LAT = LED4_LAT = LED5_LAT = LED6_LAT = LED7_LAT = 0;


    // Start the timer
    TMR1_StartTimer();

    while (1)
    {
        //Wait for flag to be set to show that 1 second has passed
        while (!TMR1IF) {}
        lower = lower + 1;

        //Light up correct LEDs
        LED7_LAT = lower & 1;
        LED6_LAT = (lower & 2) >> 1;
        LED5_LAT = (lower & 4) >> 2;
        LED4_LAT = (lower & 8) >> 3;
        LED3_LAT = (lower & 16) >> 4;
        LED2_LAT = (lower & 32) >> 5;

        LED1_LAT = upper & 1;
        LED0_LAT = (upper & 2) >> 1;

        TMR1_Reload();
        TMR1IF = 0;

        if(lower == 59) //count from 0 to 59 for one minute
        {
            if(upper == 3) //stop timer if 4 minutes is reached
            {
                TMR1_StopTimer();
                upper = lower = 0;
                TMR1_Reload();
                TMR1IF = 0;
                TMR1_StartTimer();
            }
            else //Reset timer, iterate minute timer
            {
                upper++;
                lower = 0;
                TMR1_StopTimer();
                TMR1_Reload();
                TMR1IF = 0;
                TMR1_StartTimer();
            }
        }
    }
}

/**
 End of File
*/

Lab7_2

#include "mcc_generated_files/mcc.h"

/*
                         Main application
 */
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    LED0_SetLow();
    LED1_SetLow();
    LED2_SetLow();
    LED3_SetLow();
    LED4_SetLow();
    LED5_SetLow();
    LED6_SetLow();
    LED7_SetLow();

    int x = 1;    
    int speed = 0;
    ADC_SelectChannel(pot);    
    while (1)
    {
        speed = ADC_GetConversion(pot);   
        if (x == 1)
        {
            LED0_SetLow();
            LED1_SetHigh();
        }
        else if (x == 2)
        {
            LED1_SetLow();
            LED2_SetHigh();
        }
        else if (x == 3)
        {
            LED2_SetLow();
            LED3_SetHigh();
        }
        else if (x == 4)
        {
            LED3_SetLow();
            LED4_SetHigh();
        }
       else if (x == 5)
        {
            LED4_SetLow();
            LED5_SetHigh();
        }
        else if (x == 6)
        {
            LED5_SetLow();
            LED6_SetHigh();
        }
        else if (x == 7)
        {
            LED6_SetLow();
            LED7_SetHigh();
        }
        else
        {
            LED7_SetLow();
            LED0_SetHigh();
            x = 0;    
        }
        for (int i = 0; i < speed; i++)
        {
            __delay_ms(1);
            if (LATCbits.LATC5 == 1)
            {
                INT_ISR();
            }
        }
        x++;
    }
}

/**
 End of File
*/

Lab8

#include "mcc_generated_files/mcc.h"

/*
                         Main application
 */
void main(void)
{
    SYSTEM_Initialize();
    unsigned int duty = 0;
    int lightValue = 0;

    PWM1_LoadDutyValue(12);
    TMR2_Start();

    while	(1)
    {  
        lightValue = ADC_GetConversion(lightSensor);

        if (lightValue>300)
        {
            for	(duty = 12; duty <= 17; duty++)
            {
                PWM1_LoadDutyValue(duty);
                __delay_ms(40);		
            }

            __delay_ms(10);

            for	(duty = 17; duty >= 12; duty--)
            {
                PWM1_LoadDutyValue(duty);
                __delay_ms(40);
            }
            __delay_ms(10);
        }
        else if (lightValue>200)
        {
            for	(duty = 12; duty <= 17; duty++)
            {
                PWM1_LoadDutyValue(duty);
                __delay_ms(30);		
            }

            __delay_ms(10);

            for	(duty = 17; duty >= 12; duty--)
            {
                PWM1_LoadDutyValue(duty);
                __delay_ms(30);
            }

            __delay_ms(10);
        }
        else if (lightValue>75)
        {
            for	(duty = 12; duty <= 17; duty++)
            {
                PWM1_LoadDutyValue(duty);
                __delay_ms(20);		
            }

            __delay_ms(10);

            for	(duty = 17; duty >= 12; duty--)
            {
                PWM1_LoadDutyValue(duty);
                __delay_ms(20);
            }
            __delay_ms(10);
        }
        else
        {
            for	(duty = 12; duty <= 17; duty++)
            {
                PWM1_LoadDutyValue(duty);
                __delay_ms(10);		
            }

            __delay_ms(10);

            for	(duty = 17; duty >= 12; duty--)
            {
                PWM1_LoadDutyValue(duty);
                __delay_ms(10);
            }

            __delay_ms(10);

        }
    }
}
/**
 End of File
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment