ATMEGA128

ATMEGA128, CodeVisonAVR, TIMER, pwm_led

CoyoteUgly 2018. 8. 30. 19:44

ATMEGA128, CodeVisonAVR, TIMER, pwm_led



출력 파형 주파수 foc = fclk / (scaler * 256)


0. 참고 자료


1. 모듈

2. HW 구성

3. Timers > Timer1

Timer0 pwm을 사용하면 정확하게 1ms 등의 주기 설정이 되지 않아
Timer1을 사용하였습니다.

  • Clock Source : System Clock
  • Clock Value : 250.000 kHz
  • Mode : Fast PWM top=ICR1
  • Out A : Inverted
    • connect with PORTB.5 Gpio
  • Inp. Capture : f9
    • f9 = 249
    • 250000 Hz / 250 = 1kHz = 1ms
  • Comp A : 7c
    • 7c = 124
    • 0.5ms, 50% duty


4.


5. SW 구성

  • TCCR1A : 0xC2
    • COM1A1 / COM1A0 / COM1B1 / COM1B0 / COM1C1 / COM1C0 / WGM11 / WGM10

  • TCCR1B : 0x1B
    • ICNC1 / ICES1 / – / WGM13 / WGM12 / CS12 / CS11 / CS10

  • ICR1L : 0xF9
    • max value 249
  • OCR1AL : 0x7C
    • center value 124



#include <mega128.h>
#include <delay.h>

// Declare your global variables here

void main(void)
{
    // Declare your local variables here
    unsigned char period = 0xF9;
 
    // Port B initialization
    // Func7=In Func6=In Func5=Out Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=T State6=T State5=0 State4=T State3=T State2=T State1=T State0=T
    PORTB=0x00;
    DDRB=0x20;

    // Timer/Counter 1 initialization
    // Clock source: System Clock
    // Clock value: 250.000 kHz
    // Mode: Fast PWM top=ICR1
    // OC1A output: Inverted
    // OC1B output: Discon.
    // OC1C output: Discon.
    // Noise Canceler: Off
    // Input Capture on Falling Edge
    // Timer1 Overflow Interrupt: Off
    // Input Capture Interrupt: Off
    // Compare A Match Interrupt: Off
    // Compare B Match Interrupt: Off
    // Compare C Match Interrupt: Off
    TCCR1A=0xC2; // 1100 0010
    TCCR1B=0x1B; // 0001 1011
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0xF9; // 249
    OCR1AH=0x00;
    OCR1AL=0x7C; // 124
    OCR1BH=0x00;
    OCR1BL=0x00;
    OCR1CH=0x00;
    OCR1CL=0x00;

    while (1)
    {
        // Place your code here
        period--;
        if (period <= 0)
            period = 0xF9;
            
        OCR1AL = period;
        
        delay_ms(50);
    }
}


6. 소스


7. 결과

pwm 1ms 주기에 50% duty입니다.


  • pwm 주파수

https://youtu.be/hNvUKlGsanQ



  • pwm led

https://youtu.be/dtNwq5BVWRM