Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <wiringPi.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- char currentVolume;
- int digiPot;
- void setLVolume(char Volume) {
- int result;
- // Send the lower 5 bits (0-32) of Volume to the volume register for channel 0
- result = wiringPiI2CWrite(digiPot, Volume & 0x1F);
- if(result == -1) {
- printf("Error writing to digital potentiometer\n");
- }
- }
- void setRVolume(char Volume) {
- int result;
- // Send the lower 5 bits (0-32) of Volume to the volume register for channel 1
- result = wiringPiI2CWrite(digiPot, (Volume & 0x3F) | 0x40);
- if(result == -1) {
- printf("Error writing to digital potentiometer\n");
- }
- }
- void volumeUp(void) {
- setLVolume(++currentVolume);
- setRVolume(currentVolume);
- }
- void volumeDown(void) {
- setLVolume(--currentVolume);
- setRVolume(currentVolume);
- }
- int main (void) {
- // Start up WiringPi, using the GPIO pin numbering
- if (wiringPiSetupGpio() == -1)
- exit (1) ;
- // Setup I2C to talk to the digipot
- digiPot = wiringPiI2CSetup(0x50);
- if(digiPot == -1) {
- printf("Couldn't connect to digital poteniometer\n");
- return -1;
- }
- // Setup the PWM to generate the -5V rail for the digipot
- pinMode(18,PWM_OUTPUT);
- pwmSetMode(PWM_MODE_MS);
- pwmSetClock(512); // 512 = 17.5kHz, 256 = 35kHz, 128 = 70kHz, 64 = 140kHz
- pwmSetRange(2);
- pwmWrite (18, 1);
- // Enable the volume control
- pinMode(27,OUTPUT);
- digitalWrite(27,LOW);
- // Volume Up Button
- pinMode(22,INPUT);
- pullUpDnControl(22,PUD_UP);
- wirintPiISR(22, INT_EDGE_FALLING, &volumeUp);
- // Volume Down Button
- pinMode(23,INPUT);
- pullUpDnControl(23,PUD_UP);
- wirintPiISR(23, INT_EDGE_FALLING, &volumeDown);
- // Set the volume to about 30% on startup
- currentVolume = 10;
- setLVolume(currentVolume);
- setRVolume(currentVolume);
- // Any extra init code can go here
- for (;;) delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement