ex1)
// cpp
#include <iostream>
using namespace std;
int main()
{
int **pArray;
int i, j;
int cnt = 0;
// int형 5*7 배열 동적 할당
pArray = new int*[5];
for(i = 0 ; i < 5 ; i++)
{
pArray[i] = new int[7];
}
// 각 배열에 차례되로 숫자 입력
for(i = 0 ; i < 5 ; i++)
{
for(j = 0 ; j < 7 ; j++)
{
pArray[i][j] = cnt;
++cnt;
cout.width(2);
cout << pArray[i][j] << endl;
}
}
// int형 5*7 배열 해제
for(i = 0 ; i < 5 ; i++)
{
delete[] pArray[i];
}
delete[] pArray;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int (*p)[7];
int i, j;
int cnt = 0;
// 5 * 7 int
형 배열 동적 할당
p = (int (*)[7])new int[7 * 5];
for(i = 0 ; i < 5 ; i++)
{
for(j = 0 ; j < 7 ; j++)
{
p[i][j] = cnt;
++cnt;
cout << p[i][j] << endl;
}
}
delete[] p; // 동
적 할당 해제
return 0;
}
// c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int (*p)[7];
int i, j;
int cnt = 0;
// 5 * 7 int형 배열 동적 할당
p = (int (*)[7])malloc(5 * 7 * sizeof(int));
for(i = 0 ; i < 5 ; i++)
{
for(j = 0 ; j < 7 ; j++)
{
p[i][j] = cnt;
++cnt;
printf("p[%d][%d] = %d\n", i, j, p[i][j]);
}
}
// 동적 할당 해제
free(p);
return 0;
}
ex2)
#include <iostream>
using namespace std;
int main()
{
int *pArray;
int num;
cout << "동적 배열크기 입력: ";
cin >> num;
// 동
적 배열 할당
pArray = new int[num];
for(int i = 0 ; i < num ; i++)
{
pArray[i] = i;
cout << "[" << i << "] = " << pArray[i] << endl;
}
// 동적 배열 해제
delete[] pArray;
return 0;
}
ex3)
#include <iostream>
using namespace std;
int main()
{
int num;
int min, sec;
cout << "Input Seconds: ";
cin >> num;
min = num / 60; // 분
sec = num % 60; // 초
cout << min << " min " << sec << " sec" << endl;
return 0;
}
ex4)
#include <iostream>
using namespace std;
int main()
{
int num;
int com;
cout << "Input Number: ";
cin >> num;
com = (~num) + 1; // 2의 보수
cout << "2's complement(10진수): " << com << endl;
cout << "2's complement(16진
수): " << hex << com << endl;
return 0;
}
LED 예제
// port.h
#define PINA (*(volatile unsigned int *)0x39)
#define DDRA (*(volatile unsigned int *)0x3A)
#define PORTA (*(volatile unsigned int *)0x3B)
#define PINB (*(volatile unsigned int *)0x36)
#define DDRB (*(volatile unsigned int *)0x37)
#define PORTB (*(volatile unsigned int *)0x38)
#define PINC (*(volatile unsigned int *)0x33)
#define DDRC (*(volatile unsigned int *)0x34)
#define PORTC (*(volatile unsigned int *)0x35)
#define PIND (*(volatile unsigned int *)0x30)
#define DDRD (*(volatile unsigned int *)0x31)
#define PORTD (*(volatile unsigned int *)0x32)
#define PINE (*(volatile unsigned int *)0x21)
#define DDRE (*(volatile unsigned int *)0x22)
#define PORTE (*(volatile unsigned int *)0x23)
#define PINF (*(volatile unsigned int *)0x20)
#define DDRF (*(volatile unsigned int *)0x61)
#define PORTF (*(volatile unsigned int *)0x62)
void delay(unsigned int ms);
void delay(unsigned int ms)
{
unsigned int u = 0;
unsigned int m = 0;
while(1)
{
u++;
if(u >= 1000)
{
m++;
u=0;
}
if(m >= ms)
{
break;
}
}
}
ex1)
#include "delay_m.h"
#include "port.h"
int main(void)
{
DDRF = 0xff;
PORTF = 0xff;
unsigned int flag = 1;
while(1)
{
if(flag)
{
PORTF = 0x00;
flag = 0;
}
else
{
PORTF = 0xff;
flag = 1;
}
delay(500);
}
}
ex2)
#include "port.h"
#include "delay_m.h"
int main(void)
{
DDRF = 0xff;
PORTF = 0xff;
unsigned char cnt = 0x00;
while(1)
{
PORTF = ~cnt;
delay(700);
if(0xff == cnt)
{
cnt = 0x00;
PORTF = 0xff;
}
cnt++;
}
}
ex3)
#include "port.h"
#include "delay_m.h"
int main(void)
{
DDRF = 0xff;
PORTF = 0xff;
unsigned char num = 0x01;
while(1)
{
PORTF = ~num;
num <<= 1;
if(num == 0x00)
{
num = 0x01;
}
delay(700);
}
return 0;
}
ex4)
#include "port.h"
#include "delay_m.h"
int main(void)
{
DDRF = 0xff;
PORTF = 0xff;
unsigned char data = 0xff;
unsigned int flag = 1;
while(1)
{
PORTF = data;
while((PINC & 0x01) == 0x00) // 1번 스위치가 눌러지면
{
if(flag)
{
if(0x00 == data)
{
data = 0xff;
}
else
{
data <<= 1;
}
flag = 0;
}
}
flag = 1;
}
return 0;
}