Coding (프로그램)

     Microcontroller를 사용하기 위하여 기본적으로 C 언어를 사용한다. 여기에서 C 언어에 대하여 설명하기에는 부족하여 꼭 필요한 몇가지 사항에 대하여 정리하였다. 여기에서 보여주는 내용은 꼭 이해하고 사용할 수 있도록 연습해야 한다.

1. 주석 (Manual p28 참고)
     - // : 한 줄 주석 (주로 코드 설명시 사용, 프로그램에 영향 주지 않음)
     - /* ... */ : 여러 줄 주석

2. c 언어 구조 (Manual p30 참고)
     - main () 함수만 수행함

       void main (void)
       {
         .....
       }

     - 문장의 끝은 반드시 ';' 로 끝남
     - .c : c file
     - .h : header file
     - #include 를 사용하여 .c 또는 .h의 함수 또는 정의된 내용 사용가능

     - 12 : 10 진수, = 12
     - 0x12 : 16 진수, = 18
     - 0b010010 : 2 진수, = 18
     - 'A' : 문자
     - "abcdef" : 문자 열

3. 변수형태 (Manual p42 참고)
     - 변수를 사용하기전에 반드시 변수의 형태를 선언해야함
       int1 bitSw ; //* 변수 bitSw를 1 bit 정수형으로 선언 (0 ~ 1)
       int8 idx ; //* 변수 idx를 8 bit 정수형으로 선언 (0 ~ 255)
       signed int8 idx ; //* 변수 idx를 부호를 가지는 8 bit 정수형으로 선언 (-127 ~ 128)
       int16 value ; //* 변수 value를 16 bit 정수형으로 선언 (-32768 ~ 32767)
       signed int16 value ; //* 변수 value를 부호를 가지는 16 bit 정수형으로 선언 (-127 ~ 128)
       float value ; //* 변수 value를 실수형으로 선언
Type Size Unsigned Signed
int11 bit number0 to 1N/A
int88 bit number0 to 255-128 to 127
int1616 bit number0 to 65535-32768 to 32767
int3232 bit number0 to 4294967295-2147483648 to 2147483647
int4848 bit number0 to 281474976710655-140737488355328 to 140737488355327
int6464 bit numberN/A-9223372036854775808 to 9223372036854775807
float32 bit float-1.5x10^45 to 3.4x10^38-1.5x10^45 to 3.4x10^38

4. 사용 문 (Manual p33 참고)
     아래에 표현된 C-언어 구문은 가장 많이 사용하는 구문으로 매우 중요하기 떄문에 충분한 코딩을 통하여 익숙하게 사용할 수 있어야 한다.

     1) 조건문1: if (expr) ..... ; [else ..... ;]
       if (x == 25)
         x = 1 ;
       else
         x = x + 1 ;

     2) 조건문2: switch (expr) { case cexpr: ..... ; [default: ..... ] ... }
       switch (cmd) {
         case 0:
             printf ("cmd 0") ;
         break;
         case 1:
             printf ("cmd 1") ;
         break;
         default:
             printf ("cdm def") ;
       }

     3) 반복문1: while (expr) ..... ;
       while (i == 1000)
           i ++ ;

     4) 반복문2: for (expr1;expr2;expr3) ..... ;
       for (i = 1;i <= 10; ++i) {
           printf("%3d ", i) ;
       }

     5) return (): 함수 실행 결과 값을 되돌려줌
       int8 fnc1 (int8 a, int8 b)
       {
           int16 ret_value ;
           ret_value = a + b ;
           return (ret_value) ;
       }

     6) break: 반복문을 빠져 나올 때 사용
       while (1) {
           if (i == 1000) break ;
       }

5. 연산자 (Manual p39 참고)
     아래 표에 나오는 연산자는 많이 사용하는 것이므로 꼭 알아두어야 한다.
operatorfunction
+Addition Operator
-Subtraction operator
/Division operator
*Multiplication operator
!Logical negation operator
+=Addition assignment operator, x+=y, is the same as x=x+y
-=Subtraction assignment operator, x-=y, is the same as x=x- y
/=Division assignment operator, x/=y, is the same as x=x/y
*=Multiplication assignment operator, x*=y, is the same as x=x*y
%Modules operator
%=Modules assignment operator x%=y, is the same as x=x%y
++Increment
--Decrement
==Equality
!=Inequality
>Greater than operator
>=Greater than or equal to operator
<Less than operator
<=Less than or equal to operator
^Bitwise exclusive or operator
^=Bitwise exclusive or assignment operator, x^=y, is the same as x=x^y
&&Logical AND operator
&Bitwise and operator
&=Bitwise and assignment operator, x&=y, is the same as x=x&y
||Logical OR operator
|Bitwise inclusive or operator
|=Bitwise inclusive or assignment operator, xl=y, is the same as x=xly
>>Right shift operator
>>=Right shift assignment, x>>=y, is the same as x=x>>y
<<Left Shift operator
<<=Left shift assignment operator, x<<=y, is the same as x=x<
?:Conditional Expression operator
[ ]Array subscrip operator
~One's complement operator
.Member operator for structures and unions
&Address operator

참고:

     1. CCS C Compiler Manual (March 2019)
     2. C언어 간단문법 정리
     3. C 언어 문법 정리
     4. 처음 시작하는 기초 문법 알아 보기
     5. C언어 기초 내용 정리 1/2
     6. C언어 기초 내용 정리 2/2
     7. C Tutorial