Basic Embedded C Program Structure

// I/O Port / register names / address for STM32L1xx microcontroller
#include "STM32L1xx.h"

// Global variables.. accessible by all functions
int count;

// Function definitions
void increment(int n){
    count = count + n;
    // Instructions to be implement the function
}

// Main program
int main(void){
    // Declare local variables
    int i;

    // Endless loop
    while (1){ // Can also use for(;;) or while(1) or while(true)
    // instructions to be repeated
    }
}

<aside> ⚠️ لو حفظتهم هتلبس .. افهمهم

</aside>

1. Central Heating Controller Application

Untitled

void main(void){
    // Init the system
    C_HEAT_Init();
    while (1){ // 'for ever' (Super Loop)
        // Find out what temperature the user requires (via the user interface)
        C_HEAT_Get_Required_Temperature();
        // Find out what the current room temperature is (via temperature sensor)
        C_HEAT_Get_Actual_Temperature();
        // Adjust the gas burner, as required
        C_HEAT_Control_Boiler();
    }
}

2. Write an 8051 C program to toggle only bit P2.4 continuously without disturbing the rest of the bits of P2.

#include <reg51.h>  // Include header file for 8051 registers

sbit mybit = P2^4;  // Define a bit variable for P2.4

void main() {
    while (1) {  // Infinite loop to continuously toggle P2.4
        mybit = 1;  // Turn on P2.4
        mybit = 0;  // Turn off P2.4
    }
}

3. Write an 8051 C program to toggle bits of P1 ports continuously with a 250ms

#include <reg51.h>  // Include header file for 8051 registers

void delay(unsigned int ms) {  // Delay function
    unsigned int i, j;
    for (i = 0; i < ms; i++) {
        for (j = 0; j < 1275; j++);  // Adjust for accurate delay based on crystal frequency
    }
}

void main() {
    while (1) {  // Infinite loop to continuously toggle bits
        P1 = 0; // turn on P2
        delay(250);  // Delay for 250ms
        P1 = 1; // turn off P2
        delay(250);  // Delay for 250ms
    }
}

4. Write an 8051 C program to get the status of bit P1.2, save it, and send it to P2.5 continuously.

#include <reg51.h>  // Include header file for 8051 registers

void main() {
    sbit inbit = P1^2;  // Define a bit variable for Pin 3 port 2
    sbit outbit = P2^5;  // Define a bit variable for Pin 6 port 3
    bit membit;         // Declare a bit variable to store the status

    while (1) {  // Infinite loop to continuously monitor and send
        membit = inbit;  // Read the status of Pin 3 port 2 and save it in membit
        outbit = membit;  // Send the saved status to Pin 6 port 3
    }
}