You are on page 1of 37

C Programming

Department of Electrical Engineering,


www.ee.ntou.edu.tw
National Taiwan Ocean University

3/14/2013

Richard Kuo
Assistant Professor
Outline
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
► C Programming
3. C_programming.ppt
– Computer Architecture
– Introduction and Basic C Features
– Data Type and Operators
– Functions and Macro

► Exercise : GPIO to control on-board LEDs (Smpl_GPIO_LED)


► Exercise : GPIO using macro (Smpl_GPIO_LED1_macro)
► Exercise : GPIO to control RGB LED (Smpl_GPIO_RGBled)
► Exercise : GPIO to control Buzzer on/off (Smpl_GPIO_Buzzer)
► Exercise : GPIO to control 7-segment display (Smpl_7seg)
Computer Architecture
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

► Von Neumann Architecture


C Compilation Stage
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

source code (c )

IDE

Compiler
object code (obj)
Libraries(lib)

Linker

executable code (bin)

C Compilation Model
GCC Compilation Stage
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
C Compilation Stage
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

Integrated Development Environment (IDE) : Editor + Compiler + Linker +


Debugger + Downloader
►Preprocessor : removing comments & interpreting special preprocessor
directives denoted by #.
#include -- includes contents of a named file. Files usually called header files. e.g
– #include <math.h> -- standard library maths file.
– #include <stdio.h> -- standard library I/O file
#define -- defines a symbolic name or constant. Macro substitution.
– #define MAX_ARRAY_SIZE 100
►C compiler : translate source code to assembly code
►Assembler : generate object code
►Linker : combine library files and link functions from other source files
Example of C codes
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
#include <stdio.h> #include <stdio.h>
main() /* print a few numbers, to illustrate a simple
{ loop */
printf("Hello, world!\n"); main()
return 0; {
} int i;
for(i = 0; i < 10; i = i + 1) printf("i is
%d\n", i);
return 0;
}

The C Programming Language, by Brian Kernighan and Dennis Ritchie, 1995


2nd Edition published in 1988 by Prentice-Hall, ISBN 0-13-110362-8
C/C++ Keywords ( _ are Microsoft-specific )
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
Keil show C/C++ Keywords in blue
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
Basic Data Types & Operators
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
► Types ► Operators
– char, int, long int, float, long float + : addition
► Constant - : subtraction
* : multiplication
► Declarations (for variables)
/ : division
char c; % : modulus (remainder)
int i;
float f; ~ : not
& : and
int i1, i2;
| : or
int j1=10, j2= 20; ^ : exclusive or
>> shift to right
<< shift to left
ANSI C/C++ Standard Data Types and Sizes
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
Operators
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
► Bitwise operators &, (and), | (or), ^ (xor), ~ (one’s complement)
► Shift operators << (left shift), >> (right shift)
► Increment/Decrement ++, --
► Arithmetic operators +, -, *, /, % (modulus)
► Assignment operators =
► Compound assignment +=, -=, *=, /=
► Relational operators ==, !=, >, <, <=
► Logical operators && (and), || (or), ! (not)
typedef in stdint.h
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

/* exact-width signed integer types */


typedef signed char int8_t;
typedef signed short int int16_t;
typedef signed int int32_t;
typedef signed __int64 int64_t;

/* exact-width unsigned integer types */


typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned __int64 uint64_t;
Struct in stdint.h
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
typedef struct union { union {
{
union {
__IO uint32_t u32OFFD; __IO uint32_t u32DBEN;
__IO uint32_t u32PMD; __IO uint32_t OFFD; __IO uint32_t DBEN;
struct { }; };
__IO uint32_t PMD0:2;
__IO uint32_t PMD1:2;
__IO uint32_t PMD2:2; union { union {
__IO uint32_t PMD3:2; __IO uint32_t u32DOUT; __IO uint32_t u32IMD;
__IO uint32_t PMD4:2;
__IO uint32_t DOUT; __IO uint32_t IMD;
__IO uint32_t PMD5:2;
__IO uint32_t PMD6:2; }; };
__IO uint32_t PMD7:2;
__IO uint32_t PMD8:2;
union { union {
__IO uint32_t PMD9:2;
__IO uint32_t PMD10:2; __IO uint32_t u32DMASK; __IO uint32_t u32IEN;
__IO uint32_t PMD11:2; __IO uint32_t DMASK; __IO uint32_t IEN;
__IO uint32_t PMD12:2;
}; };
__IO uint32_t PMD13:2;
__IO uint32_t PMD14:2;
__IO uint32_t PMD15:2; union { union {
} PMD; __IO uint32_t u32PIN; __IO uint32_t u32ISRC;
};
__IO uint32_t PIN; __IO uint32_t ISRC;
}; };
} GPIO_T;
Operator Precedence Levels
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

From Highest To Lowest


Standard C and C++ Libraries
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream.h>
#include <memory.h>
Program Control : if, if-else
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
► If ► If -Else
if ( input >4095) if ( input >4095)
input = 0; input = 0;
else
if (keypad_number!=0) { input = input – 2048;
GPA0=1;
GPA1=1; if (keypad_number!=0) {
GPA2=1; GPA0=1;
} GPA1=1;
} else {
GPA0=0;
GPA1=0;
}
Program Control : switch-case
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

switch (number) { switch (number) {


case 1 : GPA1=1; break; case ‘1’ : GPA1=1; break;
case 2 : GPA2=1; break; case ‘2’ : GPA2=2; break;
case 3 : GPA3=1; break; case ‘3’ : GPA3=3; break;
case 4 : GPA4=1; break; case ‘4’ : GPA4=1; break;
case 5 : GPA5=1; break; case ‘5’ : GPA5=1; break;
case 6 : GPA6=1; break; case ‘6’ : GPA6=1; break;
case 7 : GPA7=1; break; case ‘7’ : GPA7=1; break;
case 8 : GPA8=1; break; case ‘8’ : GPA8=1; break;
case 9 : GPA9=1; break; case ‘9’ : GPA9=1; break;
default: GPA0=1; default: GPA0=1;
} }
Loop Controls
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
 for  while
for (i=0; i<max; i++) { while(1) {
DrvGPIO_SetBit (E_GPA, i); no = Scankey();
printf(“%d\n”, i); if (no==0) break;
} else printf (“%d\n”, no);
}

 do-while
do {
no = Scankey();
if (no==0) break;
else printf (“%d\n”, no);
} while (flag);
Function
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
int32_t DrvGPIO_GetBit(E_DRVGPIO_PORT port, int32_t i32Bit)
{
volatile uint32_t u32Reg;

if ((i32Bit < 0) || (i32Bit > 16))


{
return E_DRVGPIO_ARGUMENT;
}

u32Reg = (uint32_t)&GPIOA->PIN + (port*PORT_OFFSET);

return ((inpw(u32Reg)>>i32Bit) & 0x1);


}
Function – pointer input
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
void print_lcd(unsigned char line, char *str)
{
int i=0;
do{
Show_Word(line,i,*str++);
i++;
if(i>15)
break;
}
while(*str!='\0');
}
Smpl_GPIO_LED1
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
// int main (void)
// Smpl_GPIO_LED : GPC12 to control on-board LEDs {
// output low to enable red LEDs
// UNLOCKREG(); // unlock register
#include <stdio.h> DrvSYS_Open(48000000); // running at 48MHz
#include "NUC1xx.h" LOCKREG(); // lock register
#include "Driver\DrvGPIO.h"
#include "Driver\DrvUART.h" // Initialize LEDs
#include "Driver\DrvSYS.h" DrvGPIO_Open(E_GPC, 12, E_IO_OUTPUT);
DrvGPIO_SetBit(E_GPC, 12); // output Hi to turn off LED
// Initial GPIO pins (GPC 12,13,14,15) to Output mode
void Init_LED() while (1) {
{ // GPC12 pin set to output mode DrvGPIO_ClrBit(E_GPC, 12); // output Low to turn on LED
DrvGPIO_Open(E_GPC, 12, E_IO_OUTPUT); DrvSYS_Delay(1000000); // delay
// GPC12 pin output Hi to turn off LED DrvGPIO_SetBit(E_GPC, 12); // output Hi to turn off LED
DrvGPIO_SetBit(E_GPC, 12);} DrvSYS_Delay(1000000); // delay
}
}
Function – DrvGPIO_Open
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
int32_t DrvGPIO_Open(E_DRVGPIO_PORT port, int32_t i32Bit, E_DRVGPIO_IO mode)
{ volatile uint32_t u32Reg;
if ((i32Bit < 0) || (i32Bit > 16))
{ return E_DRVGPIO_ARGUMENT; }
u32Reg = (uint32_t)&GPIOA->PMD + (port*PORT_OFFSET);
if ((mode == E_IO_INPUT) || (mode == E_IO_OUTPUT) || (mode == E_IO_OPENDRAIN))
{ outpw(u32Reg, inpw(u32Reg) & ~(0x3<<(i32Bit*2)));
if (mode == E_IO_OUTPUT)
{ outpw(u32Reg, inpw(u32Reg) | (0x1<<(i32Bit*2))); }
else if (mode == E_IO_OPENDRAIN)
{ outpw(u32Reg, inpw(u32Reg) | (0x2<<(i32Bit*2))); }
}else if (mode == E_IO_QUASI)
{ outpw(u32Reg, inpw(u32Reg) | (0x3<<(i32Bit*2)));
}else
{ return E_DRVGPIO_ARGUMENT; }
return E_SUCCESS;
}
DrvGPIO macro using #define
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
#define DrvGPIO_Open_GPC12_OUTPUT DrvGPIO_Open(E_GPC, 12, E_IO_OUTPUT)
#define DrvGPIO_Set_GPC12 DrvGPIO_SetBit(E_GPC, 12)
#define DrvGPIO_Clr_GPC12 DrvGPIO_ClrBit(E_GPC, 12)
#define DrvSYS_Delay_1Mcyc DrvSYS_Delay(1000000)

void Init_LED()
{
DrvGPIO_Open_GPC12_OUTPUT; // GPC12 pin set to output mode
DrvGPIO_Set_GPC12; // GPC12 pin output Hi to turn off LED
}
Smpl_GPIO_LED1_macro
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
// directly programming GPIO register bits

#define PORT_OFFSET 0x40


#define u32Reg (uint32_t)&GPIOA->PMD
#define u32RegD (uint32_t)&GPIOA->DOUT
#define DrvGPIO_Open_GPC12_OUTPUT outpw(u32Reg+ (E_GPC*PORT_OFFSET),
inpw(u32Reg + (E_GPC*PORT_OFFSET))| (0x1<<(12*2)))
#define DrvGPIO_Set_GPC12 outpw(u32RegD+ (E_GPC*PORT_OFFSET),
inpw(u32RegD+ (E_GPC*PORT_OFFSET))| (0x1 << 12 ))
#define DrvGPIO_Clr_GPC12 outpw(u32RegD+ (E_GPC*PORT_OFFSET),
inpw(u32RegD+ (E_GPC*PORT_OFFSET))& ~(0x1<< 12 ))
Smpl_GPIO_RGBled
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

RGB LED drived


by GPA12,13,14
Smpl_GPIO_RGBled
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
int main (void)
{
UNLOCKREG(); // unlock register for programming
DrvSYS_Open(48000000); // set System Clock to run at 48MHz
LOCKREG(); // lock register from programming
while (1)
{
// Initial GPIO GPA12,13,14 to output mode
DrvGPIO_Open(E_GPA, 12, E_IO_OUTPUT); // initial GPIO pin GPA 12 to output mode
DrvGPIO_Open(E_GPA, 13, E_IO_OUTPUT); // initial GPIO pin GPA 13 to output mode
DrvGPIO_Open(E_GPA, 14, E_IO_OUTPUT); // initial GPIO pin GPA 14 to output mode
// set RGBled to Blue
DrvGPIO_ClrBit(E_GPA,12); // GPA12 = Blue, 0 : on, 1 : off
DrvGPIO_SetBit(E_GPA,13);
DrvGPIO_SetBit(E_GPA,14);
// set RGBled to off
DrvGPIO_SetBit(E_GPA,12); // GPA12 = Blue, 0 : on, 1 : off
DrvGPIO_SetBit(E_GPA,13); // GPA13 = Green, 0 : on, 1 : off
DrvGPIO_SetBit(E_GPA,14); // GPA14 = Red, 0 : on, 1 : off
Delay(1000000);
}
}
Buzzer schematic
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

Note: R1 may need to be shorted if buzzer doesn’t have enough current to buzz
Smpl_GPIO_Buzzer
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
int main (void)
{
UNLOCKREG(); // unlock register for programming
DrvSYS_Open(48000000); // set System Clock to run at 48MHz
LOCKREG(); // lock register from programming

DrvGPIO_Open(E_GPB, 11, E_IO_OUTPUT); // initial GPIO pin GPB11

DrvGPIO_ClrBit(E_GPB,11); // GPB11 = 0 to turn on Buzzer


Delay(1000000); // Delay to keep Buzz
DrvGPIO_SetBit(E_GPB,11); // GPB11 = 1 to turn off Buzzer

}
7-segment display
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
Control Pins used for 7-segment Display
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
GPC4~7 control which 7-segment to turn on (1 = on, 0 = off)
► GPC4 : First 7-segment (LSB)
► GPC5 : Second 7-segment
► GPC6 : Third 7-segment
► GPC7 : Forth 7-segment (MSB)
GPE0~7 control each segment to turn on (0 = on, 1 = off)
► GPE0 : c
► GPE1 : dot
► GPE2 : f
► GPE3 : a
► GPE4 : b
► GPE5 : d
► GPE6 : e
► GPE7 : g
7-Segment LED Driver
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
NUC100SeriesBSP/NuvotonPlatform_Keil/Src/NUC1xx-LB002/Seven_Segment.c
► #define SEG_N0 0x82 // define segment led on/off for digit 0
► #define SEG_N1 0xEE // define segment led on/off for digit1

► SEG_BUF[10] // array of segment led on/off value for digit 0~9


► show_seven_segment (no, number)
– no : select 1st/2nd/3rd/4th 7-segment LED
– number : value of the digit (0~9)

► close_seven_segment (void)
– // turn off all four 7-segment LEDs
– DrvGPIO_ClrBit(E_GPC,4);
– DrvGPIO_ClrBit(E_GPC,5);
– DrvGPIO_ClrBit(E_GPC,6);
– DrvGPIO_ClrBit(E_GPC,7);
Smpl_7seg
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

Counting
7 segment LEDs
seg_display routine
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
// display 4-digit value (0~9999) on 7-segment LEDs
void seg_display(int16_t value)
{
int8_t digit;
digit = value / 1000;
close_seven_segment();
show_seven_segment(3,digit);
delay(5000);

value = value - digit * 1000;


digit = value / 100;
close_seven_segment();
show_seven_segment(2,digit);
delay(5000);

value = value - digit * 100;


digit = value / 10;
close_seven_segment();
show_seven_segment(1,digit);
delay(5000);

value = value - digit * 10;


digit = value;
close_seven_segment();
show_seven_segment(0,digit);
delay(5000);
}
Smpl_7seg
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
int32_t main (void)
{
int32_t i =0;

UNLOCKREG();
DrvSYS_Open(48000000);
LOCKREG();

while(i<10000)
{
seg_display(i); // display i on 7-segment display
DrvSYS_Delay(10000); // delay for keeping display
i++; // increment i
}
}
General Disclaimer
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
The Lecture is strictly used for educational purpose.

MAKES NO GUARANTEE OF VALIDITY


►The lecture cannot guarantee the validity of the information found here. The lecture may recently have been
changed, vandalized or altered by someone whose opinion does not correspond with the state of knowledge in the relevant
fields. Note that most other encyclopedias and reference works also have similar disclaimers.
No formal peer review
►The lecture is not uniformly peer reviewed; while readers may correct errors or engage in casual peer review, they have
no legal duty to do so and thus all information read here is without any implied warranty of fitness for any purpose or use
whatsoever. Even articles that have been vetted by informal peer review or featured article processes may later have been
edited inappropriately, just before you view them.
No contract; limited license
►Please make sure that you understand that the information provided here is being provided freely, and that no kind of
agreement or contract is created between you and the owners or users of this site, the owners of the servers upon which it is
housed, the individual Wikipedia contributors, any project administrators, sysops or anyone else who is in any way
connected with this project or sister projects subject to your claims against them directly. You are being granted a limited
license to copy anything from this site; it does not create or imply any contractual or extracontractual liability on the part of
Wikipedia or any of its agents, members, organizers or other users.
►There is no agreement or understanding between you and the content provider regarding your use or modification of
this information beyond the Creative Commons Attribution-Sharealike 3.0 Unported License (CC-BY-SA) and the GNU
Free Documentation License (GFDL);
General Disclaimer
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
Trademarks
►Any of the trademarks, service marks, collective marks, design rights or similar rights that are mentioned, used or cited in
the lectures are the property of their respective owners. Their use here does not imply that you may use them for any
purpose other than for the same or a similar informational use as contemplated by the original authors under the CC-BY-
SA and GFDL licensing schemes. Unless otherwise stated , we are neither endorsed by nor affiliated with any of the
holders of any such rights and as such we cannot grant any rights to use any otherwise protected materials. Your use of any
such or similar incorporeal property is at your own risk.
Personality rights
►The lecture may portray an identifiable person who is alive or deceased recently. The use of images of living or recently
deceased individuals is, in some jurisdictions, restricted by laws pertaining to personality rights, independent from their
copyright status. Before using these types of content, please ensure that you have the right to use it under the laws which
apply in the circumstances of your intended use. You are solely responsible for ensuring that you do not infringe someone
else's personality rights.

You might also like