'2016/11/23'에 해당되는 글 2건

  1. 2016.11.23 Timer 3개 모두사용하는 예제
  2. 2016.11.23 USBASP V2.0 펌웨어 업그레이드
2016. 11. 23. 22:53

ATMega328P 에는 총 3개의 타이머가 있습니다.


타이머0 - 8비트

타이머1 - 16비트

타이머2 - 8비트


모드는 모두 CTC 모드로 작성 했습니다.


Clear Timer on Compare match


CTC모드는 TCNT 값이 0부터 증가 해서 OCR 에 정의된 값 까지 갑니다.

(여기서 8비트는 최대 255 까지 밖에 ㅜㅜ)


1초를 구하기 위해 16비트는 설정만으로 가능하지만, 8비트는 모두 세지 못합니다.


그래서 타이머 인터럽트 내에 몇번 반복 시켜서 1초를 구하게 됩니다.



8비트로 1초 구하는 방법...

CPU가 16 MHz 로 동작을 가정합니다.


1. 1초에 16000000 Hz 진동합니다.

2. prescale 256 으로 셋팅했으므로 256 Hz 당 1 타이머 count 가 증가합니다.

    즉, 16000000 / 256 = 0.000016 초에 1 Hz씩  ( 1 hz / 16 us)

3. OCR에 정의된 값까지 증가하고 0으로 되돌아 갑니다.

    125번 반복하면 걸리는 시간 = 0.000016 x 125 = 0.002  125번 반복에 0.002초 ( 2 ms) 

    125번 이지만 0 부터 시작하므로 -1을 하여 124를 설정합니다.

4. 타이머 인트럽트가 2ms 당 1번씩 호출되므로 500번 마다 작동하도록 설정하면 1초 마다 동작하도록 설정 됨.


복잡한 것 같지만... 차근차근 읽어보면 그냥 산수 입니다. ^__^


16비트 타이머의 경우 OCR 에 정의할 수 있는 값이 거대해서 인트럽트에서 별도 반복 없이도 1초 구하는 것이 가능합니다.



동작영상



타이머 3개 모두 동작시킨 풀 소스코드

예제는 16비트 1초, 8비트 1/2초, 8비트 1/4초 로 세팅되어 있습니다.

https://github.com/haebi/AVRExample/blob/master/atmega328p/timer2/timer2.cpp


#define F_CPU 16000000UL // Set CPU Frequency 16 MHz


#define setbit(PORTX, BitX) PORTX |= (1 << BitX) // set bit to 1

#define clrbit(PORTX, BitX) PORTX &= ~(1 << BitX) // clear bit


#include <avr/io.h>

#include <util/delay.h>

#include <avr/interrupt.h>


int count0 = 0;

int tick0  = 0;


int count1 = 0;


int count2 = 0;

int tick2  = 0;


void setTimer0()

{

// 1sec for 8-bit

// 1 / 16000000 = 0.0000000625 * 256 (prescale) = 0.000016 (usec)

// 0.000016 (usec) * 125 (multiplier) = 0.002 * 1000 = 2 (ms)

// 2 (ms) * 500 (count) = 1 (sec)


TCNT0   = 0;

// Set CTC compare value with a prescaler of 256

OCR0A   = 124; // 125 -1 


// 0 1 0 - Configure timer 0 for CTC mode [p.106]

clrbit(TCCR0B, WGM02); // 0

setbit(TCCR0A, WGM01); // 1

clrbit(TCCR0A, WGM00); // 0


// 1 0 0 - Start timer at Fcpu/256 [p. 108]

setbit(TCCR0B, CS02); // 1

clrbit(TCCR0B, CS01); // 0

clrbit(TCCR0B, CS00); // 0

// Enable CTC interrupt

setbit(TIMSK0, OCIE0A);

}


void setTimer1()

{

// OCR1A =  OCR1AH + OCR1AL = (8 + 8 = 16 bit)

// Dec 15624 : repeat 0 ~ 15624 (15625 = 0)

OCR1A = 0x3D08; 


// Page 171-172

// Waveform Generation Mode : (TCCR1B:4,  TCCR1B:3, TCCR1A:1, TCCR1A:0)

// 0 1 0 0 - Set to TOP OCR1A

setbit(TCCR1B, WGM12); // Configure timer 1 for CTC mode


// Page 173

// Clock Select Bit ( CS12 CS11 CS10 )

// 0 1 0 - clock I/O / 1024 (From prescaler)

setbit(TCCR1B, CS12);

setbit(TCCR1B, CS10);


// Page 135

// TIMSK1 – Timer/Counter1 Interrupt Mask Register

setbit(TIMSK1, OCIE1A); // Enable CTC interrupt

}


void setTimer2()

{

// 1sec for 8-bit

// 1 / 16000000 = 0.0000000625 * 256 (prescale) = 0.000016 (usec)

// 0.000016 (usec) * 125 (multiplier) = 0.002 * 1000 = 2 (ms)

// 2 (ms) * 500 (count) = 1 (sec)


TCNT2   = 0;

// Set CTC compare value with a prescaler of 256 

OCR2A   = 124; // 125 -1 


// 0 1 0 - Configure timer 2 for CTC mode [p.155]

clrbit(TCCR2B, WGM22); // 0

setbit(TCCR2A, WGM21); // 1

clrbit(TCCR2A, WGM20); // 0


// 1 1 0 - Start timer at Fcpu/256 [p.156-157]

setbit(TCCR2B, CS22); // 1

setbit(TCCR2B, CS21); // 1

clrbit(TCCR2B, CS20); // 0


// Enable CTC interrupt

TIMSK2 |= (1 << OCIE2A);

}


// Main Function

int main(void)

{

setTimer0(); //  8 bit timer (TCNT0)

setTimer1(); // 16 bit timer (TCNT1)

setTimer2(); //  8 bit timer (TCNT2)


sei(); // Enable global interrupts


setbit(DDRD, DDD6); // Set output PD6

setbit(DDRD, DDD7); // Set output PD7

setbit(DDRB, DDB0); // Set output PB0


while (1)

{


}

}


// Timer0 interrupt

ISR (TIMER0_COMPA_vect)

{

tick0++;


// if (!(tick0 == 500)) // 1 sec

if (!(tick0 == 125)) // 1/4 sec

return;


tick0 = 0;


if (count0 == 0)

{

count0++;

clrbit(PORTB, PORTB0);

}

else

{

setbit(PORTB, PORTB0);

count0 = 0;

}

}


// Timer1 interrupt

ISR (TIMER1_COMPA_vect)

{

// already 1 sec


if (count1 == 0)

{

count1++;

clrbit(PORTD, PORTD6);

}

else

{

setbit(PORTD, PORTD6);

count1 = 0;

}

}


// Timer2 interrupt

ISR (TIMER2_COMPA_vect) 

{

tick2++;


// if (!(tick2 == 500)) // 1 sec

if (!(tick2 == 250)) // 1/2 sec

return;


tick2 = 0;


if (count2 == 0)

{

count2++;

clrbit(PORTD, PORTD7);

}

else

{

setbit(PORTD, PORTD7);

count2 = 0;

}

}




Posted by 해비
2016. 11. 23. 22:20

그간 미뤄오던 USBASP의 펌웨어 업그레이드를 진행 했다.


달라진건... warning 이 안뜬다는거랑 프로그램 속도가 좀 더 빨라진것 같은 느낌...?


까먹기전에 역시 메모...


준비물 : USBASP 2개 (한쪽은 주는쪽 한쪽은 당하는(?)... 쪽)


(서로 번갈아 가며 2번 하면 둘다 펌업이가능 합니다만... 1개만 있으면 아쉽게도.. 불가.;;)


타겟에 JP2 를 쇼트 내도록 합니다.

(이거 몰라서 왜 안되징...?? 하며 1시간 헤메었네요 ㅜㅜ)




소스코드 다운로드

http://www.fischl.de/usbasp/



압축해제 후 bin 폴더안에 hex 파일을 찾는다.


그리고 아래 명령어로 플래싱을 한다.

(-p m8 옵션은 ATMega8A MCU 이다. 혹시 다른 MCU 라면 해당 모델로 바꾸도록 한다)


$ avrdude -c usbasp -p m8 -F -e -U flash:w:usbasp.atmega8.2011-05-28.hex


avrdude: AVR device initialized and ready to accept instructions


Reading | ################################################## | 100% 0.00s


avrdude: Device signature = 0x1e9307 (probably m8)

avrdude: erasing chip

avrdude: reading input file "usbasp.atmega8.2011-05-28.hex"

avrdude: input file usbasp.atmega8.2011-05-28.hex auto detected as Intel Hex

avrdude: writing flash (4700 bytes):


Writing | ################################################## | 100% 1.80s


avrdude: 4700 bytes of flash written

avrdude: verifying flash memory against usbasp.atmega8.2011-05-28.hex:

avrdude: load data flash data from input file usbasp.atmega8.2011-05-28.hex:

avrdude: input file usbasp.atmega8.2011-05-28.hex auto detected as Intel Hex

avrdude: input file usbasp.atmega8.2011-05-28.hex contains 4700 bytes

avrdude: reading on-chip flash data:


Reading | ################################################## | 100% 1.11s


avrdude: verifying ...

avrdude: 4700 bytes of flash verified


avrdude: safemode: Fuses OK (E:FF, H:D9, L:FF)


avrdude done.  Thank you. 


끝... (이래 간단한 것을...;;)




펌웨어 업글 이전 / 이후 비교로그

haebi@haebi-W330AU ~/workspace/avr/atmega328p/timer2 $ ./program.sh 


avrdude: warning: cannot set sck period. please check for usbasp firmware update.

avrdude: AVR device initialized and ready to accept instructions


Reading | ################################################## | 100% 0.00s


avrdude: Device signature = 0x1e950f (probably m328p)

avrdude: erasing chip

avrdude: warning: cannot set sck period. please check for usbasp firmware update.

avrdude: reading input file "timer2.hex"

avrdude: input file timer2.hex auto detected as Intel Hex

avrdude: writing flash (602 bytes):


Writing | ################################################## | 100% 0.41s


avrdude: 602 bytes of flash written

avrdude: verifying flash memory against timer2.hex:

avrdude: load data flash data from input file timer2.hex:

avrdude: input file timer2.hex auto detected as Intel Hex

avrdude: input file timer2.hex contains 602 bytes

avrdude: reading on-chip flash data:


Reading | ################################################## | 100% 0.30s


avrdude: verifying ...

avrdude: 602 bytes of flash verified


avrdude: safemode: Fuses OK (E:FF, H:DE, L:FF)


avrdude done.  Thank you. 

업글 이전 warning 이 표시되었다.



그리고 업글 후...

haebi@haebi-W330AU ~/workspace/avr/atmega328p/timer2 $ ./program.sh 


avrdude: AVR device initialized and ready to accept instructions


Reading | ################################################## | 100% 0.00s


avrdude: Device signature = 0x1e950f (probably m328p)

avrdude: erasing chip

avrdude: reading input file "timer2.hex"

avrdude: input file timer2.hex auto detected as Intel Hex

avrdude: writing flash (602 bytes):


Writing | ################################################## | 100% 0.23s


avrdude: 602 bytes of flash written

avrdude: verifying flash memory against timer2.hex:

avrdude: load data flash data from input file timer2.hex:

avrdude: input file timer2.hex auto detected as Intel Hex

avrdude: input file timer2.hex contains 602 bytes

avrdude: reading on-chip flash data:


Reading | ################################################## | 100% 0.15s


avrdude: verifying ...

avrdude: 602 bytes of flash verified


avrdude: safemode: Fuses OK (E:FF, H:DE, L:FF)


avrdude done.  Thank you.


warning 이 사라졌다!!







Posted by 해비