0% found this document useful (0 votes)
6 views13 pages

Lab2 C Programming

Uploaded by

21020601
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Lab2 C Programming

Uploaded by

21020601
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

VietNam National University

University of Engineering and Technology

PROGRAMMING
EMBEDDED AND REAL-TIME SYSTEMS
(INT3108, LẬP TRÌNH NHÚNG VÀ THỜI GIAN THỰC)

Dr. Nguyễn Kiêm Hùng


Email: [email protected]

Laboratory for Smart Integrated Systems


Introduction to VietNam National University
Week
Embedded Systems 1-2
University of Engineering
Introduction to CandWeek
Technology
Languague 3

CPU: Week
ARM Cortex-M 4

Curriculum Memory
and Interfaces
Week
5-6

Path ARM-based Week


7
Embedded System

Embedded Software Week


8-9

Real-time Week
Operating systems 10-12

Interfacing Embedded Week


With Real-World 13-14

Project Week
Laboratory for Smart Integrated Systems 15
Objectives

In this lecture you will be introduced to:

– Use the functions written by the vendor to test C


program on platform FRDM-KL46Z

3
Outline
• Hello World.
• Development Tool chain Overview
(KEIL).

4
First Program: Hello World
- In Pack Installer, select a device on the Devices tab. Click on the
Examples tab and select “Hello World (FRDM-KL46Z)”
-Press Copy to install the selected example project on your machine.
µVision opens up and you can start working with the project.

5
First Program: Hello World
- On PC, Open “Device manager” find COM NUMBER of “OpenSDA – CDC
Serial port

6
First Program: Hello World
- In Putty Configuration, select Session on the Category tab. Check on the
Serial and enter COM NUMBER on Serial line and 115200 on Speed
-Press Open

7
Program #2: Static variables

Insert bellow code into file “hello world.c”


/* function prototype */
int a(void);

int a(void)
{
static int i = 10;
i++;
return(i);
}

int main()
{
int p, n;
for (n=1; n < 10; n++) {
p = a();
PRINTF("Invocation %i, return value %i\n", n, p );
}
} 8
Program #3: Array

Insert bellow code into file “hello world.c”


int main() {
int x[8];
int i;
int *p; /* p is pointer to an int */
for (i=0; i<8; i++)
x[i] = i;
for (i=0; i<8; i++)
PRINTF("element %i = %i\n", i, x[i]);
for (i=0; i<8; i++) {
p = x + i;
*p = 8-i;
}
for (i=0; i<8; i++)
PRINTF("element %i = %i\n", i, x[i]);

9
Program #4: Struct

Insert bellow code into file “hello world.c”


#include <stdio.h>

int main() {

typedef struct {
int hours;
int mins;
int secs;
} time;

time clock;
time *ptime; /* pointer to time struct */

/* accces struct field via dot notation */


clock.hours = 1;
clock.mins = 10;
clock.secs = 59;

printf("The time is %i : %i : %i \n", clock.hours, clock.mins, clock.secs);

/* access struct field via pointer */


ptime = &clock;
ptime->hours = (clock.hours+1)%12;
ptime->mins = (clock.mins+1)%60;
ptime->secs = (clock.secs+1)%60;

printf("The time is %i : %i : %i \n", clock.hours, clock.mins, clock.secs);

10
Program #5: Add Two integers

Insert bellow code into file “hello world.c”

11
Program #6: pass-by-value vs pass-by-reference

Insert bellow code into file “hello world.c”

• Lets write void swap(int a, int b)


{
int main()
{
swap(a, b) int temp; int x = 1, y = 2;

as a function. temp = a; swap(x,y);


printf("x = %i, y = %i\n", x, y);
a = b;
b = temp; }
• This doesn’t work...why? }

void swap(int *a, int *b) int main()


• Fixed...how? { {
int temp; int x = 1, y = 2;

swap(&x,&y);
temp = *a; printf("x = %i, y = %i\n", x, y);
*a = *b; }
*b = temp;
}

12
Summary

13

You might also like