==========================================================================================
네트워크
==========================================================================================
<C>
=컴파일러(32bit)와 구조체 관계
(장시간 통신 - 구조체 사용시 size 계산 주의)
-보통 변수 선언 => 변수 선언 순서대로 ADDRESS 아래에서부터 할당
-배열/구조체 => ADDRESS 아래에서부터 통째로 할당 => 변수 선언순서대로 위에서부터 할당
-4BYTE > 2BYTE(1BYTE보다 클 경우) > 1BYTE
#include <stdio.h>
typedef struct _smart { int A; char B; short C; int D; char E; short F; char G; int H; char I; char J; short K; }smart;
int main() { printf("%d\n",sizeof(smart)); return 0; } |



=SIZE 최적화 1
#include <stdio.h>
typedef struct _smart { int A; char B; short C; int D; char E; short F; char G; int H; char I; char J; short K; }smart;
typedef struct _smart1 { int A; char B; short C; int D; char E; char G; short F; int H; char I; char J; short K; }smart1;
int main() { printf("smart size : %d\n",sizeof(smart)); printf("smart1 size : %d\n",sizeof(smart1)); return 0; } |



=SIZE 최적화 2
#pragma pack(1)
#include <stdio.h>
typedef struct _smart { int A; char B; short C; int D; char E; short F; char G; int H; char I; char J; short K; }smart;
//최적화 1 typedef struct _smart1 { int A; char B; short C; int D; char E; char G; short F; int H; char I; char J; short K; }smart1;
//최적화 2 #pragma pack(1) // pragma : compiler에 지시 => setting을 바꾸시오 typedef struct _smart2 { int A; char B; short C; int D; char E; short F; char G; int H; char I; char J; short K; }smart2; #pragma pack(4) // pack(1) : 임시로 1BYTE단위로 // pack(4) : 4BYTE단위로 다시 돌림
//최적화 3 typedef struct _smart3 { int A; char B; short C; int D; char E; char G; short F; int H; char I; char J; short K; }smart3;
int main() { printf("smart size : %d\n",sizeof(smart)); printf("smart1 size : %d\n",sizeof(smart1)); printf("smart2 size : %d\n",sizeof(smart2)); printf("smart3 size : %d\n",sizeof(smart3)); return 0; } |


=> 최적화 : 최적화 1 / 2를 적당히 섞어 사용함
=함수의 인자로 전달/ return문에의해 반환되는 구조체 변수
-Call by value(값 전달 방식) : 구조체 크기 커지면 copy하는데 시간 많이 걸림/ 메모리 차지많이 함
=>Call by reference(주소 전달 방식) 사용
#include <stdio.h>
typedef struct point { int xpos; int ypos; }Point;
void ShowPosition(Point pos) { printf("[%d, %d] \n", pos.xpos, pos.ypos); }
Point GetCurrentPosition(void) { Point cen; printf("Input current pos: "); scanf("%d %d", &cen.xpos, &cen.ypos); return cen; }
int main(void) { Point curPos = GetCurrentPosition(); ShowPosition(curPos); return 0; } |


=구조체 배열 멤버 전달
-값 전달 방식
#include <stdio.h>
typedef struct person { char name[20]; char phoneNum[20]; int age; }Person;
void ShowPersonInfo(Person man) { printf("name: %s \n", man.name); printf("phone: %s \n", man.phoneNum); printf("age: %d \n", man.age); }
Person ReadPersonInfo(void) { Person man;
printf("name? "); scanf("%s", man.name); printf("phone? "); scanf("%s", man.phoneNum); printf("age? "); scanf("%d", &man.age); return man; }
int main(void) { Person man = ReadPersonInfo(); ShowPersonInfo(man); return 0; } |
-주소 전달 방식
#include <stdio.h>
typedef struct person { char name[20]; char phoneNum[20]; int age; }Person;
void ShowPersonInfo(Person * man) { printf("name: %s \n", man->name); printf("phone: %s \n", man->phoneNum); printf("age: %d \n", man->age); }
void ReadPersonInfo(Person * man) {
printf("name? "); scanf("%s", man->name); printf("phone? "); scanf("%s", man->phoneNum); printf("age? "); scanf("%d", &man->age); return; }
int main(void) { Person man; ReadPersonInfo(&man); ShowPersonInfo(&man); return 0; } |


=구조체 변수 대상 Call-by-reference(주소 전달)
#include <stdio.h>
typedef struct point { int xpos; int ypos; }Point;
void OrgSymTrans(Point *ptr) //원점대칭 { ptr->xpos = (ptr->xpos) * -1; ptr->ypos = (ptr->ypos) * -1; }
void ShowPosition(Point pos) { printf("[%d, %d] \n", pos.xpos, pos.ypos); }
int main(void) { Point pos = {7,-5}; OrgSymTrans(&pos); ShowPosition(pos); OrgSymTrans(&pos); ShowPosition(pos); return 0; } |


=구조체 덧셈, 뺄셈
-Call by value
#include <stdio.h>
typedef struct point { int xpos; int ypos; }Point;
Point AddPoint(Point pos1, Point pos2) { Point pos = {pos1.xpos + pos2.xpos, pos1.ypos + pos2.ypos}; return pos; }
Point MinPoint(Point pos1, Point pos2) { Point pos = {pos1.xpos - pos2.xpos, pos1.ypos - pos2.ypos}; return pos; }
int main(void) { Point pos1 = {5, 6}; Point pos2 = {2, 9}; Point result;
result = AddPoint(pos1, pos2); printf("[%d, %d] \n", result.xpos, result.ypos); result = MinPoint(pos1, pos2); printf("[%d, %d] \n", result.xpos, result.ypos); return 0; } |
-Call by reference
#include <stdio.h>
typedef struct point { int xpos; int ypos; }Point;
void AddPoint(Point * result, Point * pos1, Point * pos2) { Point pos = {pos1->xpos + pos2->xpos, pos1->ypos + pos2->ypos}; *result = pos; //result->xpos = pos1->xpos + pos2->xpos; //result->ypos = pos1->ypos + pos2->ypos; return; }
void MinPoint(Point * result, Point * pos1, Point * pos2) { Point pos = {pos1->xpos - pos2->xpos, pos1->ypos - pos2->ypos}; *result = pos; return; }
int main(void) { Point pos1 = {5, 6}; Point pos2 = {2, 9}; Point result;
AddPoint(&result, &pos1, &pos2); printf("[%d, %d] \n", result.xpos, result.ypos); //printf("[%d, %d] \n", pos1.xpos + pos2.xpos, pos1.ypos + pos2.ypos); MinPoint(&result, &pos1, &pos2); printf("[%d, %d] \n", result.xpos, result.ypos); return 0; } |


=구조체를 구조체의 멤버로 선언
#include <stdio.h>
typedef struct point { int xpos; int ypos; }Point;
typedef struct circle { Point cen; double rad; }Circle;
void ShowCircleInfo(Circle * cptr) { printf("[%d, %d] \n", (cptr->cen).xpos, (cptr->cen).ypos); printf("radius: %g \n\n", cptr->rad); }
int main(void) { Circle c1 = {{1, 2}, 3.5}; Circle c2 = {{2, 4}, 3.9}; ShowCircleInfo(&c1); ShowCircleInfo(&c2); return 0; } |



=공용체
*---
short 0xABCD => (1111111111111111)1010..... => 음수(앞 1로 채워짐)
unsigned short 0xABCD => 양수 (0으로 채워짐)
---*

#include <stdio.h> /* typedef struct _t { unsigned int a; unsigned short b; unsigned char c; }T; */
typedef union _t { unsigned int a; unsigned short b; unsigned char c; }T;
int main(void) { T obj1; obj1.a = 0x12345678; obj1.b = 0xABCD; obj1.c = 0xEE; printf("%08X \n",obj1.a); printf("%08X \n",obj1.b); printf("%08X \n",obj1.c); printf("%d\n",sizeof(T)); return 0; } |


=union memory alloc
#include <stdio.h>
typedef struct sbox { int mem1; int mem2; double mem3; }SBox;
typedef union ubox { int mem1; int mem2; double mem3; }UBox;
int main(void) { SBox sbx; UBox ubx;
printf("%p %p %p \n", &sbx.mem1, &sbx.mem2, &sbx.mem3); printf("%p %p %p \n", &ubx.mem1, &ubx.mem2, &ubx.mem3); printf("%d %d \n", sizeof(SBox), sizeof(UBox)); return 0; } |


=
#include <stdio.h>
typedef union ubox // 공용체 union 정의 { int mem1; int mem2; double mem3; }UBox;
int main(void) { UBox ubx; // 8byte 메모리 할당 ubx.mem1 = 20; printf("%d \n", ubx.mem2);
ubx.mem3 = 7.15; printf("%d \n", ubx.mem1); printf("%d \n", ubx.mem2); printf("%g \n", ubx.mem3); return 0; } |


=공용체의 유용함 : 다양한 접근방식
#include <stdio.h>
typedef struct dbshort { unsigned short upper; unsigned short lower; }DBShort;
typedef union rdbuf { int iBuf; char bBuf[4]; DBShort sBuf; }RDBuf;
int main(void) { RDBuf buf; printf("정수 입력: "); scanf("%d", &(buf.iBuf));
printf("상위 2바이트: %u \n", buf.sBuf.upper); printf("하위 2바이트: %u \n", buf.sBuf.lower); printf("상위 1바이트 아스키 코드: %c \n", buf.bBuf[0]); printf("하위 1바이트 아스키 코드: %c \n", buf.bBuf[3]); return 0; } |



=열거형의 정의 변수선언
-enum
#include <stdio.h>
typedef enum syllable { Do = 1, Re = 2, Mi = 3, Fa = 4, So = 5, La = 6, Ti=7 }Syllable;
void Sound(Syllable sy) { switch(sy) { case Do: puts("도는 하얀 도라지 "); return; case Re: puts("레는 둥근 레코드 "); return; case Mi: puts("미는 파란 미나리 "); return; case Fa: puts("파는 예쁜 파랑새 "); return; case So: puts("솔은 작은 솔방울 "); return; case La: puts("라는 라디오고요~ "); return; case Ti: puts("시는 졸졸 시냇물 "); return; } puts("다 함께 부르세~ 도레미파 솔라시도 솔 도~ 짠~"); }
int main(void) { Syllable tone; for(tone = Do; tone <= Ti; tone+=1) Sound(tone); return 0; } |


=예제
-자동 숫자 할당(int + #define)
#include <stdio.h>
typedef enum _smart { ZERO, ONE, TWO, THREE, FOUR }smart;
int main() { smart e1; e1 = TWO; printf("%d \n", e1); return 0; } |


#include <stdio.h>
typedef enum _smart { ZERO = 100, ONE, TWO, THREE, FOUR }smart;
int main() { smart e1; e1 = TWO; printf("%d \n", e1); return 0; } |


#include <stdio.h>
typedef enum _smart { ZERO = 100, ONE, TWO=502, THREE, FOUR }smart;
int main() { smart e1; e1 = FOUR; printf("%d \n", e1); return 0; } |


#include <stdio.h>
typedef enum _smart { ZERO = -100, ONE, TWO=502, THREE, FOUR }smart;
int main() { int e1; e1 = ONE; printf("%d \n", e1); printf("%d \n", THREE); return 0; } |


==========================================================================================
AVR
==========================================================================================
=AVR2560
=INTERRUPT
=BUTTON ON->OFF (FALLING EDGE) INTERRUPT INT0 => LED 8EA (SYNC TYPE) LEFT SHIFT ON
=BUTTON ON->OFF (FALLING EDGE) INTERRUPT INT1 => LED 8EA (SYNC TYPE) RIGHT SHIFT ON
-헤더파일(AVR제공)
<AVR/INTERRUPT.H>

<INTERRUPT.H>

<INTERRUPT.C>

-헤더파일(MY)
<INTERRUPT.H>

<INTERRUPT.C>

<MAIN.C>

=결과
=AVR128


<AVR2560.H> : AVR128용으로 REGISTER ADDRESS 수정

<INTERRUPT.H>

<INTERRUPT.C>


=결과
1. COUNT UP/DOWN
2. LED SHIFT(LED 2개로 테스트)