ELI43-CR
User’s Manual
04/23/2018
Copyright ©2018, Future
Designs, Inc.
Page
12
of
16
#include<iostream>
#include<bcm2835.h>
#include<string>
using
namespace
std
;
#define LED RPI_GPIO_P1_12
// PWM pin number for
backlight control
#define RANGE 20
// Range for PWM steps
#define CLOCK 192
// Clock rate
int
main
(
int
argc
,
char
*
argv
[]){
int
data
=
0
;
// Brightness level
if
(
argc
!=
2
){
// Give user correct usage if
ran incorrectly
cout
<<
"Error: correct usage,
brightness <value>"
<<
endl
;
return
1
;
}
data
=
stoi
(
argv
[
1
]);
if
(
data
>
RANGE
||
data
<
0
){
cout
<<
"Error: brightness value must be
between 0 and "
<<
RANGE
<<
endl
;
return
1
;
}
if
(!
bcm2835_init
())
return
1
;
bcm2835_gpio_set_pad
(
BCM2835_PAD_GROUP_GPIO_0_27
,
BCM2835_PAD_DRIVE_2mA
);
// Sets the drive current
to 2mA
bcm2835_gpio_fsel
(
LED
,
BCM2835_GPIO_FSEL_ALT5
);
// Sets up pin 18 for
alt5 pwm mode
bcm2835_pwm_set_clock
(
CLOCK
);
// Sets pwm
clock to 19.2 MHz / CLOCK
bcm2835_pwm_set_mode
(
0
,
1
,
1
);
// Sets mode to
markspace
bcm2835_pwm_set_range
(
0
,
RANGE
);
// Sets range
bcm2835_pwm_set_data
(
0
,
data
);
// Sets data
rate to argument value
bcm2835_close
();
return
0
;
}