Presentation is loading. Please wait.

Presentation is loading. Please wait.

Www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University C Programming 3/14/2013 Richard Kuo Assistant Professor.

Similar presentations


Presentation on theme: "Www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University C Programming 3/14/2013 Richard Kuo Assistant Professor."— Presentation transcript:

1 www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University C Programming 3/14/2013 Richard Kuo Assistant Professor

2 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Outline ► 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)

3 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Computer Architecture ► Von Neumann Architecture

4 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw C Compilation Stage C Compilation Model IDE Compiler Linker source code (c ) Libraries(lib) object code (obj) executable code (bin)

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

6 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw C Compilation Stage 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 -- standard library maths file. –#include -- 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

7 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Example of C codes #include main() { printf("Hello, world!\n"); return 0; } #include /* print a few numbers, to illustrate a simple loop */ main() { 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 2 nd Edition published in 1988 by Prentice-Hall, ISBN 0-13-110362-8

8 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw C/C++ Keywords ( _ are Microsoft-specific )

9 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Keil show C/C++ Keywords in blue

10 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Basic Data Types & Operators ► Types –char, int, long int, float, long float ► Constant ► Declarations (for variables) char c; int i; float f; int i1, i2; int j1=10, j2= 20; ► Operators + : addition - : subtraction * : multiplication / : division % : modulus (remainder) ~ : not & : and | : or ^ : exclusive or >> shift to right << shift to left

11 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw ANSI C/C++ Standard Data Types and Sizes

12 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Operators ► Bitwise operators &, (and), | (or), ^ (xor), ~ (one’s complement) ► Shift operators > (right shift) ► Increment/Decrement++, -- ► Arithmetic operators+, -, *, /, % (modulus) ► Assignment operators = ► Compound assignment +=, -=, *=, /= ► Relational operators ==, !=, >, <, <= ► Logical operators && (and), || (or), ! (not)

13 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw typedef in stdint.h /* 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;

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

15 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Operator Precedence Levels From Highest To Lowest

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

17 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Program Control : if, if-else ► If if ( input >4095) input = 0; if (keypad_number!=0) { GPA0=1; GPA1=1; GPA2=1; } ► If -Else if ( input >4095) input = 0; else input = input – 2048; if (keypad_number!=0) { GPA0=1; GPA1=1; } else { GPA0=0; GPA1=0; }

18 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Program Control : switch-case switch (number) { case 1 : GPA1=1; break; case 2 : GPA2=1; break; case 3 : GPA3=1; break; case 4 : GPA4=1; break; case 5 : GPA5=1; break; case 6 : GPA6=1; break; case 7 : GPA7=1; break; case 8 : GPA8=1; break; case 9 : GPA9=1; break; default: GPA0=1; } switch (number) { case ‘1’ : GPA1=1; break; case ‘2’ : GPA2=2; break; case ‘3’ : GPA3=3; break; case ‘4’ : GPA4=1; break; case ‘5’ : GPA5=1; break; case ‘6’ : GPA6=1; break; case ‘7’ : GPA7=1; break; case ‘8’ : GPA8=1; break; case ‘9’ : GPA9=1; break; default: GPA0=1; }

19 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Loop Controls  for for (i=0; i<max; i++) { DrvGPIO_SetBit (E_GPA, i); printf(“%d\n”, i); }  while while(1) { no = Scankey(); 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);

20 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Function int32_t DrvGPIO_GetBit(E_DRVGPIO_PORT port, int32_t i32Bit) { volatile uint32_t u32Reg; if ((i32Bit 16)) { return E_DRVGPIO_ARGUMENT; } u32Reg = (uint32_t)&GPIOA->PIN + (port*PORT_OFFSET); return ((inpw(u32Reg)>>i32Bit) & 0x1); }

21 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Function – pointer input 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'); }

22 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_GPIO_LED1 // // Smpl_GPIO_LED : GPC12 to control on-board LEDs // output low to enable red LEDs // #include #include "NUC1xx.h" #include "Driver\DrvGPIO.h" #include "Driver\DrvUART.h" #include "Driver\DrvSYS.h" // Initial GPIO pins (GPC 12,13,14,15) to Output mode void Init_LED() { // GPC12 pin set to output mode DrvGPIO_Open(E_GPC, 12, E_IO_OUTPUT); // GPC12 pin output Hi to turn off LED DrvGPIO_SetBit(E_GPC, 12);} int main (void) { UNLOCKREG(); // unlock register DrvSYS_Open(48000000); // running at 48MHz LOCKREG(); // lock register // Initialize LEDs DrvGPIO_Open(E_GPC, 12, E_IO_OUTPUT); DrvGPIO_SetBit(E_GPC, 12); // output Hi to turn off LED while (1) { DrvGPIO_ClrBit(E_GPC, 12); // output Low to turn on LED DrvSYS_Delay(1000000); // delay DrvGPIO_SetBit(E_GPC, 12); // output Hi to turn off LED DrvSYS_Delay(1000000); // delay }

23 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Function – DrvGPIO_Open int32_t DrvGPIO_Open(E_DRVGPIO_PORT port, int32_t i32Bit, E_DRVGPIO_IO mode) { volatile uint32_t u32Reg; if ((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; }

24 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw DrvGPIO macro using #define #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 }

25 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_GPIO_LED1_macro // 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 ))

26 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_GPIO_RGBled RGB LED drived by GPA12,13,14

27 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_GPIO_RGBled 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); }

28 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Buzzer schematic Note: R1 may need to be shorted if buzzer doesn’t have enough current to buzz

29 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_GPIO_Buzzer 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 }

30 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw 7-segment display

31 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw 7 segment LED schematic schematic has incorrect connection of SA~SG, DOT correct connection are shown in next slide

32 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Control Pins used for 7-segment Display 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

33 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw 7-Segment LED Driver 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 1 st /2 nd /3 rd /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);

34 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_7seg Counting 7 segment LEDs

35 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw seg_display routine // 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); }

36 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_7seg 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 }

37 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw General Disclaimer 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.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.peer reviewfeatured article 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);Creative Commons Attribution-Sharealike 3.0 Unported License GNU Free Documentation License

38 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw General Disclaimer 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.personality rights


Download ppt "Www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University C Programming 3/14/2013 Richard Kuo Assistant Professor."

Similar presentations


Ads by Google