C Programming: Department of Electrical Engineering
C Programming: Department of Electrical Engineering
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
source code (c )
IDE
Compiler
object code (obj)
Libraries(lib)
Linker
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
#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
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;
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
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
}
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
…
► 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);
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.