Embedded Systems Lab 2 – Objective, blink a morse code message on PIC Controller LED using C language. Upcoming… Lab 3- Play a song on PIC Controller using C

Written by

×

Current Workings as of September

Embedded Systems Lab 2 – Objective, blink a morse code message on PIC Controller LED using C language.




//*****************************************************************
// Name:    Caden Nubel
// Date:    September 9, 2023
// Lab:     02
//*****************************************************************

#include <xc.h>
#include <stdint.h>
#include <stdbool.h>

// Configuration bits
#pragma config FOSC = INTIO67    // Oscillator Selection bits -> Internal oscillator block
#pragma config PLLCFG = OFF       // 4X PLL Enable -> Oscillator used directly
#pragma config PRICLKEN = ON      // Primary clock enable bit -> Primary clock enabled

void initPIC(void);
void milliSecondDelay(uint16_t ms);
void microSecondDelay(uint16_t us);
void blink(char letter);
uint8_t convert(char letter);

#define BUTTON_PIN  PORTAbits.RA2
#define BUTTON_TRIS TRISAbits.TRISA2
#define BUTTON_ANG  ANSELAbits.ANSA2

#define LED_PIN     LATBbits.LATB5
#define LED_TRIS    TRISBbits.TRISB5
#define LED_ANG     ANSELBbits.ANSB5

#define INPUT       1
#define OUTPUT      0

#define ANALOG      1                   // Page PIC18(L)F2X/4XK22 Data Sheet
#define DIGITAL     0

#define TIME_UNIT               200
#define DOT_DELAY               1 * TIME_UNIT
#define IMD                     1 * TIME_UNIT
#define DASH_DELAY              3 * TIME_UNIT
#define INTER_LETTER_DELAY      3 * TIME_UNIT
#define INTER_WORD_DELAY        7 * TIME_UNIT
#define DOT                     0
#define DASH                    1
#define END                     2

char msg[] = "sos help"; // Define the message here
uint8_t morseCode[26][5] = {
    {DOT, DASH, END},               // a
    {DASH, DOT, DOT, DOT, END},     // b
    {DASH, DOT, DASH, DOT, END},    // c
    {DASH, DOT, DOT, END},          // d
    {DOT, END},                     // e
    {DOT, DOT, DASH, DOT, END},     // f
    {DASH, DASH, DOT, END},         // g
    {DOT, DOT, DOT, DOT, END},      // h
    {DOT, DOT, END},                // i
    {DOT, DASH, DASH, DASH, END},   // j
    {DASH, DOT, DASH, END},         // k
    {DOT, DASH, DOT, DOT, END},     // l
    {DASH, DASH, END},              // m
    {DASH, DOT, END},               // n
    {DASH, DASH, DASH, END},        // o
    {DOT, DASH, DASH, DOT, END},    // p
    {DASH, DASH, DOT, DASH, END},   // q
    {DOT, DASH, DOT, END},          // r
    {DOT, DOT, DOT, END},           // s
    {DASH, END},                    // t
    {DOT, DOT, DASH, END},          // u
    {DOT, DOT, DOT, DASH, END},     // v
    {DOT, DASH, DASH, END},         // w
    {DASH, DOT, DOT, DASH, END},    // x
    {DASH, DOT, DASH, DASH, END},   // y
    {DASH, DASH, DOT, DOT, END}     // z
};
//*****************************************************************

//*****************************************************************

void main(void) {

    initPIC();
    
    while (1) {
        uint8_t i = 0; // Initialize i to 0
        LED_PIN = 1;

        while (BUTTON_PIN == 1); // While the button is not pressed, wait
        while (BUTTON_PIN == 0); // The button is being held down
        while (msg[i] != '\0') { 
            if (msg[i] == ' ') {
                milliSecondDelay(INTER_WORD_DELAY);
            }
            else {
                blink(msg[i]);
            }
            i++; // Increment i
        }
    }
}

//*****************************************************************
// Initialize initPIC
//*****************************************************************

void initPIC(void) {

    // ---------------Configure Oscillator------------------
    OSCCONbits.IRCF2 = 1; // Internal RC Oscillator Frequency Select bits
    OSCCONbits.IRCF1 = 1; // Set to 16MHz
    OSCCONbits.IRCF0 = 1;
    OSCTUNEbits.PLLEN = 1; // Enable the 4xPLL, wicked fast 64MHz

    BUTTON_ANG = DIGITAL;
    BUTTON_TRIS = INPUT;

    LED_ANG = DIGITAL;
    LED_TRIS = OUTPUT;
}

//*****************************************************************
// Call milliseconds(1000) a number of times
//*****************************************************************

void milliSecondDelay(uint16_t ms) {

    uint16_t i;

    for (i = 0; i < ms; i++) microSecondDelay(1000);

}

//*****************************************************************
// Create a delay of 1uS and loop a number of times
//*****************************************************************

void microSecondDelay(uint16_t us) {

    uint16_t i;

    for (i = 0; i < us; i++) {
        asm("NOP"); // 1
        asm("NOP"); // 2
        asm("NOP"); // 3
        asm("NOP"); // 4
        asm("NOP"); // 5
        i = i;
    }
}

void blink(char letter) {
    uint8_t index = convert(letter);

    for (uint8_t i = 0; morseCode[index][i] != END; i++) {
        if (morseCode[index][i] == DOT) {
            // Blink DOT (You need to implement the LED control here)
            LED_PIN = 0; // Turn on the LED (you may need to adjust this)
            milliSecondDelay(DOT_DELAY); // Delay for DOT_DELAY milliseconds
            LED_PIN = 1; // Turn off the LED (you may need to adjust this)
            milliSecondDelay(IMD);
        } else if (morseCode[index][i] == DASH) {
            // Blink DASH (You need to implement the LED control here)
            LED_PIN = 0; // Turn on the LED (you may need to adjust this)
            milliSecondDelay(DASH_DELAY); // Delay for DASH_DELAY milliseconds
            LED_PIN = 1; // Turn off the LED (you may need to adjust this)
            milliSecondDelay(IMD);
        }
    }
    milliSecondDelay(INTER_LETTER_DELAY); // End of letter delay
}

uint8_t convert(char letter) {
    return (letter - 'a');
}

Upcoming… Lab 3- Play a song on PIC Controller using C

Leave a comment