728x90
반응형
string 클래스
문자열의 처리를 목적으로 정의된 클래스
string 클래스의 분석
String 클래스는 이런 방식으로 동작된다.
#include <iostream>
#include <cstring>
using namespace std;
class String
{
private:
int len;
char * str;
public:
String();
String( const char * s);
String( const String& s);
~String();
String& operator=(const String& s);
String& operator+=(const String& s);
bool operator ==(const String& s);
String operator+(const String& s);
friend ostream& operator <<(ostream& os, const String& s);
friend istream& operator >>(istream& is, String& s);
};
String::String() //디폴트 생성자
{
len=0;
str=NULL;
}
String::String(const char* s) //문자열 입력 -> "hi"
{
len=strlen(s)+1;
str= new char [len];
strcpy(str,s);
}
String::String(const String& s) //복사생성자
{
len=s.len;
str= new char [len];
strcpy(str,s.str);
}
String::~String() //소멸자
{
if(str!=NULL)
{
delete []str;
}
}
String& String:: operator=(const String& s) // 대입연산자
{
if(str!=NULL)
{
delete []str;
len=s.len;
str= new char [len];
strcpy(str, s.str);
return *this ; //A=B -> A반환되어야 하기 때문에
}
}
String& String:: operator+=(const String& s)
{
len+=(s.len-1);
char* tempstr=new char[len];
strcpy(tempstr,str);
strcat(tempstr,s.str);
if(str!=NULL)
{
delete []str;
}
str=tempstr;
return *this ; //A=A+B => A가 반환되어야 하기 때문에
}
bool String::operator ==(const String& s)
{
return strcmp(str,s.str) ? false :true;
}
String String::operator+( const String& s)
{
char* tempstr=new char[len+s.len-1];
strcpy(tempstr,str);
strcat(tempstr,s.str);
String temp(tempstr);
delete []tempstr;
return temp; // A+B =>A+B를 더한 값이 A와 B에 포함되지 않기 때문에 임시객체에 저장
}
ostream& operator<<(ostream& os, const String& s) //cout<<문자열
{
os<<s.str;
return os;
}
istream& operator>>(istream& is, String& s) //cin>>문자열
{
char str[100];
is>>str;
s=String(str);
return is;
}
int main()
{
String str1= "I like ";
String str2= "string class";
String str3=str1+str2;
cout<<str1<<endl;
cout<<str2<<endl;
cout<<str3<<endl;
str1=str1+str2;
if(str1==str3)
{
cout<< "동일I 문자U열!" <<endl;
}
else
{
cout<< "동일I하I지o 않E은 문자U열!" <<endl;
}
String str4;
cout<< "문자U열 입O력A: " ;
cin>>str4;
cout<< "입O력A한N 문자U열: " <<str4<<endl;
return 0;
}
템플릿
같은 함수에서 다양한 자료형의 함수를 만들어 낼 수 있다.
<>을 생략하여 사용할 수 있다.
함수 템플릿의 특수화
위의 설명처럼 문자열을 대상으로 호출하면 어떤 의미로 대소비교하는지를 알 수가 없다.
그래서 문자열을 사용할 때에는 명확히 따로 설정을 해주어야 한다.
위의 템플릿 함수는 각각 문자열 길이와 사전적문자비교를 따로 템플릿 함수로 표현해주고 있다.
클래스 템플릿
클래스도 템플릿으로 정의할 수 있다.
단 템플릿 클래스의 객체생성시 자료형의 정보는 생략이 불가능하다.
ARM
- os 스케줄러 인터럽트 사용
- 20bit 카운터, 12bit adder
- MCK = 48MHZ / 16 => 3MHZ
PIT 디이어그램
PIT 타이밍도
PIT 레지스터 종류
* PIT장치 System Controller에 존재하기 때문에 PIT 인터럽트 사용시 Peripheral Identifier표에 SYSC(sys)를 사용한다. Peripheral Identifier표를 보면 PID번호에 PIT장치가 없는 것을 알 수 있다.
PIT 소스코드
led.h 파일
#ifndef _LED_H_
#define _LED_H_
#include "AT91SAM7S256.h"
#define LED_PIN AT91C_PIO_PA0 //LED_PIN을 PA0사용
#define __ON__ 1
#define __OFF__ 0
void LED_Init(void);
void LED_On(void);
void LED_Off(void);
unsigned int LED_Status(void);
void LED_Toggle(void);
#endif
#define _LED_H_
#include "AT91SAM7S256.h"
#define LED_PIN AT91C_PIO_PA0 //LED_PIN을 PA0사용
#define __ON__ 1
#define __OFF__ 0
void LED_Init(void);
void LED_On(void);
void LED_Off(void);
unsigned int LED_Status(void);
void LED_Toggle(void);
#endif
led.c 파일
#include "led.h"
static volatile unsigned int uiStatus; //전역에서 static사용함으로써 접근 제한
void LED_Init(void)
{
*AT91C_PMC_PCER = AT91C_ID_PIOA; // PIN 전원 활성화
*AT91C_PIOA_PER = LED_PIN; //LED_PIN활성화
*AT91C_PIOA_OER = LED_PIN; //LED_PIN 출력 활성화
*AT91C_PIOA_PPUDR = LED_PIN; //Pull-up 비활성화
*AT91C_PIOA_SODR = LED_PIN; //LED_PIN 종료
uiStatus = __OFF__;
}
void LED_On(void)
{
*AT91C_PIOA_CODR = LED_PIN; //LED_PIN ON
uiStatus = __ON__;
}
void LED_Off(void)
{
*AT91C_PIOA_SODR = LED_PIN; //LED_PIN OFF
uiStatus = __OFF__;
}
unsigned int LED_Status(void)
{
return uiStatus;
}
void LED_Toggle(void)
{
volatile unsigned int i;
if(__OFF__ == uiStatus)
{
LED_On();
for(i=0;i<60000;i++);
for(i=0;i<60000;i++);
for(i=0;i<60000;i++);
}
else
{
LED_Off();
for(i=0;i<60000;i++);
for(i=0;i<60000;i++);
for(i=0;i<60000;i++);
}
}
static volatile unsigned int uiStatus; //전역에서 static사용함으로써 접근 제한
void LED_Init(void)
{
*AT91C_PMC_PCER = AT91C_ID_PIOA; // PIN 전원 활성화
*AT91C_PIOA_PER = LED_PIN; //LED_PIN활성화
*AT91C_PIOA_OER = LED_PIN; //LED_PIN 출력 활성화
*AT91C_PIOA_PPUDR = LED_PIN; //Pull-up 비활성화
*AT91C_PIOA_SODR = LED_PIN; //LED_PIN 종료
uiStatus = __OFF__;
}
void LED_On(void)
{
*AT91C_PIOA_CODR = LED_PIN; //LED_PIN ON
uiStatus = __ON__;
}
void LED_Off(void)
{
*AT91C_PIOA_SODR = LED_PIN; //LED_PIN OFF
uiStatus = __OFF__;
}
unsigned int LED_Status(void)
{
return uiStatus;
}
void LED_Toggle(void)
{
volatile unsigned int i;
if(__OFF__ == uiStatus)
{
LED_On();
for(i=0;i<60000;i++);
for(i=0;i<60000;i++);
for(i=0;i<60000;i++);
}
else
{
LED_Off();
for(i=0;i<60000;i++);
for(i=0;i<60000;i++);
for(i=0;i<60000;i++);
}
}
main.c 파일
#include "AT91SAM7S256.h"
#include "LED.h"
#define PICNT 20 //PIT_PIVR 레지스터 20bit 위치에 있음
void PIT_Init(void);
void ISR_System(void);
volatile unsigned int iCnt;
int main(void)
{
LED_Init();
PIT_Init();
while (1);
return 0;
}
void PIT_Init(void)
{
AT91C_AIC_SVR[AT91C_ID_SYS] =((volatile unsigned int)ISR_System);
#include "LED.h"
#define PICNT 20 //PIT_PIVR 레지스터 20bit 위치에 있음
void PIT_Init(void);
void ISR_System(void);
volatile unsigned int iCnt;
int main(void)
{
LED_Init();
PIT_Init();
while (1);
return 0;
}
void PIT_Init(void)
{
AT91C_AIC_SVR[AT91C_ID_SYS] =((volatile unsigned int)ISR_System);
//SYS 인터럽트 설정
*AT91C_AIC_IECR = (1<<AT91C_ID_SYS); //인터럽터 활성화 *AT91C_PITC_PIMR = AT91C_PITC_PITIEN |AT91C_PITC_PITEN | 0xF4240;
//PIT 활성화 | PIT 인터럽트 활성화 | CPIV 1/3초 = 1000000 설정
}
void ISR_System(void) //PIT인터럽트 함수
{
if(0 != *AT91C_PITC_PISR )
{
if(1==(*AT91C_PITC_PIVR >>PICNT)) //PICNT값 == 1 ?
{
++iCnt;
}
if(0==(iCnt%3)) // 3/3초 => 즉 1초를 나타냄
{
LED_Toggle();
}
}
}
}
void ISR_System(void) //PIT인터럽트 함수
{
if(0 != *AT91C_PITC_PISR )
{
if(1==(*AT91C_PITC_PIVR >>PICNT)) //PICNT값 == 1 ?
{
++iCnt;
}
if(0==(iCnt%3)) // 3/3초 => 즉 1초를 나타냄
{
LED_Toggle();
}
}
}
728x90
'코스웨어 > 14년 스마트컨트롤러' 카테고리의 다른 글
10.31 이경진 (9) | 2014.11.03 |
---|---|
ARM_PIT_LED 손병규 (0) | 2014.10.31 |
ARM Interval Timer LED on, off 김해성 (1) | 2014.10.31 |
ADS LED ON/OFF(PIT) 김진철 (0) | 2014.10.31 |
10.30 이경진 led (0) | 2014.10.31 |
인터벌 타이머 김재성 (0) | 2014.10.31 |
ADS LED On/Off 양태영 (1) | 2014.10.31 |
ADS LED ON/OFF(PIT) 김화린 (0) | 2014.10.31 |