Fogeaters, Light The World.

14

2016-Jan

AVR 초패스트 PWM

작성자: title: MoonBlonix IP ADRESS: *.148.87.98 조회 수: 1564

출처 : http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=614762#614762


주파수 최강자. 다만 하드웨어 pwm이 아니고 소프트웨어 pwm이라 정밀도는 좀 떨어짐.

사실 아주 특수한 케이스가 아니면 딱히 쓸 곳 없음. 진짜 엄청빠른 주파수가 필요한 특수한 경우가 아니라면..



What it shows is how to do PWM on an arbitrary pin by doing "soft PWM" but with timer interrupts. It works by starting a timer to have both overflow (PWM frequency) and compare (duty cycle) interrupts. In this case I wanted to vary a pulse width on PC0 using the fastest PWM frequency I could get (so timer 0 counting 256 with no prescaler) and OCR0 can be varied from 0x00 to 0xFF to vary the duty cycle:

// The following two ISRs are doing "poor man's PWM" 
//  but this allows it to be on a pin of my choice
ISR(TIMER0_COMP_vect) {
        // clear the output pin on OCR0 match
	PORTC &= ~(1<<PC0);
}
ISR(TIMER0_OVF_vect) {
        // set the output pin at timer overflow
	PORTC |= (1<<PC0);
}

int main(void) {

	// going to use PORTC.0 to PWM the contrast voltage
	DDRC = (1<<DDC0);
	TIMSK |= ((1<<OCIE0) | (1<<TOIE0)); // use both interrupts
	OCR0 = 10; // 10 out of 256 means very short on period (low voltage)
	TCCR0 = (1<<CS00); // timer on - nice high PWM frequency

	// Might later consider PWMing the backlight voltage too
	// so it would also be adjustable ...
	sei();

this is just a code "snippet" for GCC, not a complete program.

The application was actually to vary the pin 3 (contrast) voltage of an HD44780 LCD module. As the comments say it would be possible to vary the backlight voltage in a similar way.

The way this example works is that it runs an 8bit timer with no prescale so every 256 clock cycles it will overflow and cause an OVF interrupt. At this point the output is turned on. The counter then starts to count up 0, 1, 2, 3,... and I have set the OCR0 register to 10 so when it counts up to 10 and TCNT0==OCR0 it will trigger the COMP(are) interrupt. At this point I switch the output off. So in a complete period of 256 counts the output is on for 10 and off for 246. So the output is only "on" for about 4% of the time. As the output pin is switching between 0V and 5V then it'll be like a voltage that is just 4% of this will be produced by the output pin (after a bit of RC filtering). So it's 4% * 5V = 0.19V

In my example I don't vary OCR so the output voltage is always at this level. But say I now set OCR to 211 (out of 256) then the output signal will be on for 211 clocks and off for 256-211=45 clocks. Because of this the output will appear to be 211/256 * 5V = 4.12V (and so on).

profile
List of Articles
번호 제목 글쓴이 날짜 조회 수
공지 [Web] 클라우드 IDE + 2 title: MoonBlonix 2017-06-25 15126
32 [AVR] HC_SR04 초음파센서 사용 file title: MoonBlonix 2016-02-13 1668
31 [AVR] 피에조 부저 활용 file + 1 title: MoonBlonix 2016-02-12 1956
30 칼만필터(Kalman Filter) + 2 title: MoonBlonix 2016-02-11 4376
29 low pass filter, high pass filter (저역통과필터, 고역통과필터) file title: MoonBlonix 2016-02-11 1455
28 [AVR] 루프 실행시간 측정 (아두이노의 Millis(), Micros() 분석) title: MoonBlonix 2016-02-09 1455
27 상보필터(Complementary Filter) file title: MoonBlonix 2016-02-09 1738
26 가속도, 자이로 센서에 대해 title: MoonBlonix 2016-02-09 1571
25 [AVR] UART 통신 file title: MoonBlonix 2016-02-07 1608
24 C++ 과 C 를 같은 프로젝트에서 사용하기 title: MoonBlonix 2016-02-07 1505
23 라즈베리파이 운영체제에 관하여 title: MoonBlonix 2016-02-05 1458
22 [리눅스] 기본 명령어 title: MoonBlonix 2016-02-05 1631
21 [리눅스] C 언어 개발환경 구축 title: MoonBlonix 2016-02-05 1863
20 라즈베리파이 GPIO 핀 배열 file title: MoonBlonix 2016-02-05 2058
19 C++ 멤버 함수 포인터 title: MoonBlonix 2016-01-23 1774
18 AVR 직접만든 DC모터 라이브러리 (C++ Class) file title: MoonBlonix 2016-01-16 1516
17 [AVR] I/O 포트 메뉴얼 (ATMega128) file title: MoonBlonix 2016-01-16 1591
16 AVR delay 함수 (_delay_ms, _delay_us) title: MoonBlonix 2016-01-15 1640
15 AVR 멀티채널 PWM (타이머 하나로 여러 PWM 구동) title: MoonBlonix 2016-01-15 1650
» AVR 초패스트 PWM title: MoonBlonix 2016-01-14 1564
13 AVR 타이머 응용 여러 PWM 방식과 예제 file + 1 title: MoonBlonix 2016-01-14 1651