본문 바로가기
코스웨어/16년 스마트컨트롤러

20160318-업무일지-허도경-타이머카운터 스톱,리셋

by 알 수 없는 사용자 2016. 3. 26.
728x90
반응형

.

 

main.c


#include"main.h"
#include"lcd.h"
#include"tc0.h"
int main(void)
{
    char caTime[]="00:00:00";
    unsigned int uiTime; //LCD 점등 
 
     LCD_Init();

 

// LCD_Data('B');           //문자 B 출력
// LCD_STR("STRING");         //문자열 출력

     TC0_Init();
     sei();
 
    //uiSec=0;
 
     uiTime=uiSec=0;
     while(1)
     {
          //TC0_MSDelay(1000);
          //++uiSec; 
          caTime[7]='0'+uiSec%60%10;
          caTime[6]='0'+uiSec%60/10;
          caTime[4]='0'+uiSec/60%60%10;
          caTime[3]='0'+uiSec/60%60/10;
          caTime[1]='0'+uiSec/60/60%10;

caTime[0]='0'+uiSec/60/60/10;

LCD_Inst(LCD_HOME);

LCD_STR(caTime);
while(uiTime==uiSec);

uiTime = uiSec;
//LCD_Data(0x18);
//TC0_MSDelay(1000); 

}
 //uiMsec = 100;
 return 0;

}

 

main.h

#ifndef __MAIN_H__
#define __MAIN_H__

#define PINA  (*((volatile unsigned char *)0x20))
#define DDRA  (*((volatile unsigned char *)0x21))
#define PORTA  (*((volatile unsigned char *)0x22))

#define PINC  (*((volatile unsigned char *)0x26))
#define DDRC  (*((volatile unsigned char *)0x27))
#define PORTC  (*((volatile unsigned char *)0x28))

#define EICRA   (*((volatile unsigned char *)0x69))
#define EICRB  (*((volatile unsigned char *)0x6A))
#define SREG  (*((volatile unsigned char *)0x5F))
#define EIMSK  (*((volatile unsigned char *)0x3D))
#define EIFR  (*((volatile unsigned char *)0x3C))

#define TCCR0A (*((volatile unsigned char *)0x44))
#define COM0A1 7
#define COM0A0 6
#define COM0B1 5 
#define COM0B0 4
#define WGM01  1
#define WGM00 0

#define TCCR0B (*((volatile unsigned char *)0x45))
#define FOC0A  7
#define FOC0B  6
#define WGM02 3
#define CS02  2
#define CS01  1
#define CS00  0

#define TCNT0  (*((volatile unsigned char *)0x46))
#define OCR0A (*((volatile unsigned char *)0x47))
#define OCR0B  (*((volatile unsigned char *)0x48))

#define TIMSK0 (*((volatile unsigned char *)0x6E))
#define OCIE0B 2
#define OCIE0A 1
#define TOIE0  0

#define TIFR0  (*((volatile unsigned char *)0x35))

#define INT7  7
#define INT6  6
#define INT5   5
#define INT4   4
#define INT3   3
#define INT2   2
#define INT1   1
#define INT0  0

#define ISC7  6
#define ISC6  4
#define ISC5  2
#define ISC4  0
#define ISC3  6
#define ISC2  4
#define ISC1  2
#define ISC0  0

 

#define sei() __asm__ __volatile__ ("sei" ::)
#define sleep() __asm__ __volatile__ ( "sleep" "\n\t" :: )

void Init(void);
void Pont_Init(void);
void INT_Init(void);
#endif //__MAIN_H__

void __vector_1 (void) __attribute__((signal, used, externally_visible));   // INT0
void __vector_2 (void) __attribute__((signal, used, externally_visible));
void __vector_3 (void) __attribute__((signal, used, externally_visible));   // INT1
void __vector_21 (void) __attribute__((signal, used, externally_visible));   // TIMER0 COMPA

 

tc0.c
#include"tc0.h"
static volatile unsigned int uiMsec;
volatile unsigned int uiSec;
volatile unsigned int uiSstate;

void TC0_Init()        //TC0함수 초기화
{

//TCCR0A = (0<<COM0A1) | (0<<COM0A0)|(0<<COM0B1)| (0<<COM0B0)|(0<< WGM01 )|(0<< WGM00 ); PWM모드에서 다시봅시다.
TCCR0B = (0<<FOC0A)|(0<<FOC0B)|(0<<WGM02)|(0<<CS02)|(1<<CS01)|(1<< CS00);   //64분주비로설정.
//TCNT0 =
OCR0A = 250;
//OCR0B =                     //A랑 비교.

TIMSK0 =(0<<OCIE0B)|(1<<OCIE0A)|(0<<TOIE0);
//TIFR0 =
EICRA = (3 << ISC1)|(3 << ISC0)|(3 << ISC2); 
EIMSK = (1 << INT1)|(1 << INT0)|(1 << INT2);
uiSstate = STOP;

}

void __vector_21 (void)
{
    ++uiMsec;
    if(uiMsec>=1000)
    {
        uiMsec = 0;
        ++uiSec;

}

}

void TC0_MSDelay(unsigned int uiDTime)
{
    if(uiDTime>1000)
    {
        uiDTime = 1000;
    

uiMsec = 0;
 
while(uiMsec<uiDTime); 

}
void __vector_1(void) //PORT D1(PJ0)로 제어
{  
    volatile unsigned int uiCnt;
    if(STOP==uiSstate)
    {
        TCCR0B = (0<<FOC0A)|(0<<FOC0B)|(0<<WGM02)|(0<<CS02)|(1<<CS01)|(1<<CS00);
        uiSstate = START;
    }
    else
    {
        TCCR0B = (0<<FOC0A)|(0<<FOC0B)|(0<<WGM02)|(0<<CS02)|(0<<CS01)|(0<<CS00); 
        uiSstate = STOP;
    }
    for(uiCnt = 0; 30000>uiCnt ; ++uiCnt);
    for(uiCnt = 0; 30000>uiCnt ; ++uiCnt);
  
    return;
}

void __vector_2(void) //리셋버튼 PORT D2(PJ1)
{  
    volatile unsigned int uiCnt;
    TCCR0B = (0<<FOC0A)|(0<<FOC0B)|(0<<WGM02)|(0<<CS02)|(0<<CS01)|(0<<CS00);
    uiSec = 0;
    uiSstate=STOP;
    for(uiCnt = 0; 30000>uiCnt ; ++uiCnt);
   
  
    return;
}
void __vector_3(void) //1분증가 PORT D3(PJ2)
{  
    volatile unsigned int uiCnt;
    uiSec=uiSec+60;
    for(uiCnt = 0; 30000>uiCnt ; ++uiCnt);
   
  
    return;
}

 

 

make를 한뒤 선3개를 DK128과 연결한다

DK128의 스위치부분을 연결


아무버튼과 연결하여도 무관하다

순서 대로 꽂은 뒤 실행 시키면 PJ0와 연결된 버튼은 스톱, PJ1과 연결된 버튼은 리셋, PJ2와 연결된 버튼은 1분 증가함을 알수있다.

728x90