Processor

【라즈베리파이】 wiringPi C 언어 : PWM LED 제어

작성자 임베디드코리아 작성일26-02-17 17:08 조회137회 댓글0건
● PWM이란?
  - Pulse Width Modulation(펄스 폭 변조)
  - 디지털 출력으로 아날로그 회로를 제어하는 기법
      (GPIO핀은 디지털이기때문에 PWM을 이용하여 아날로그 신호를 제어한다.)

PWM 번호 GPIO Function       Alt             dtoverlay
  PWM 0           12             4             Alt 0     dtoverlay=pwm,pin=12,func=4
  PWM 0           18             2             Alt 5     dtoverlay=pwm,pin=18,func=2
  PWM 1          13             4              Alt 0     dtoverlay=pwm,pin=12,func=4
  PWM 1         18              2              Alt 5     dtoverlay=pwm,pin=18,func=2


>>> 예제  pwm_led.c  : duty cycle = 50%이면 2.5V의 효과를 낼 수 있다. <<<<

#include<stdio.h>
#include<wiringPi.h>
#include<softPwm.h>

#define LED 1

int main()
{
pinMode(LED , OUTPUT);        //핀 초기화
if(wiringPiSetup()==-1)
return 1;

softPwmCreate(LED,0,100);    //PWM 범위 설정
while(1)
{
softPwmWrite(LED,HIGH);    //핀 HIGH 설정
delay(150);
softPwmWrite(LED,LOW);
delay(150);
}
return 0;
}
-------------------------------------------------------------------------
$ gcc  -o pwm_led  pwm_led.c  -lwiringPi
$ sudo ./led


--->>> 예제 pwm_led01.c  PWM Ports C 사용하기  <<<-----------------------
// PWM 신호는 듀티를 변경하며 사용합니다. LED의 밝기를 조절한다.

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>

const int PWM_pin = 1;  /* GPIO 1 as per WiringPi, GPIO18 as per BCM */

int main (void)
{
  int intensity ;           

  if (wiringPiSetup () == -1)
    exit (1) ;

  pinMode (PWM_pin, PWM_OUTPUT) ; /* set PWM pin as output */

  while (1)
  {
    for (intensity = 0 ; intensity < 1024 ; ++intensity)
    {
      pwmWrite (PWM_pin, intensity) ; /* provide PWM value for duty cycle */
      delay (1) ;
    }
    delay(1);

    for (intensity = 1023 ; intensity >= 0 ; --intensity)
    {
      pwmWrite (PWM_pin, intensity) ;
      delay (1) ;
    }
    delay(1);
  }
}
----------------------------------------------------------------------------------------------------
$ gcc  -o pwm_led01  pwm_led01.c  -lwiringPi
$ sudo  pwm_led01

--->>> 예제  PWM_LED.cpp  :  PWM Ports 사용하기  C++  <<<-----
using System;
using System.Device.Pwm;

namespace TestTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello PWM!");
            var pwm = PwmChannel.Create(0, 0, 400, 0.5);
            pwm.Start();
            Console.ReadKey();
        }
    }
}