Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.90 KB | None | 0 0
  1. #include <wiringPi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5.  
  6. char currentVolume;
  7. int digiPot;
  8.  
  9. void setLVolume(char Volume) {
  10.   int result;
  11.  
  12.   // Send the lower 5 bits (0-32) of Volume to the volume register for channel 0
  13.   result = wiringPiI2CWrite(digiPot, Volume & 0x1F);
  14.  
  15.   if(result == -1) {
  16.     printf("Error writing to digital potentiometer\n");
  17.   }
  18. }
  19.  
  20. void setRVolume(char Volume) {
  21.   int result;
  22.  
  23.   // Send the lower 5 bits (0-32) of Volume to the volume register for channel 1
  24.   result = wiringPiI2CWrite(digiPot, (Volume & 0x3F) | 0x40);
  25.  
  26.   if(result == -1) {
  27.     printf("Error writing to digital potentiometer\n");
  28.   }
  29. }
  30.  
  31. void volumeUp(void) {
  32.     setLVolume(++currentVolume);
  33.     setRVolume(currentVolume);
  34. }
  35.  
  36. void volumeDown(void) {
  37.     setLVolume(--currentVolume);
  38.     setRVolume(currentVolume);
  39. }
  40.  
  41. int main (void) {
  42.   // Start up WiringPi, using the GPIO pin numbering
  43.   if (wiringPiSetupGpio() == -1)
  44.     exit (1) ;
  45.  
  46.   // Setup I2C to talk to the digipot
  47.   digiPot = wiringPiI2CSetup(0x50);
  48.   if(digiPot == -1) {
  49.     printf("Couldn't connect to digital poteniometer\n");
  50.     return -1;
  51.   }
  52.  
  53.   // Setup the PWM to generate the -5V rail for the digipot
  54.   pinMode(18,PWM_OUTPUT);
  55.   pwmSetMode(PWM_MODE_MS);
  56.   pwmSetClock(512); // 512 = 17.5kHz, 256 = 35kHz, 128 = 70kHz, 64 = 140kHz
  57.   pwmSetRange(2);
  58.   pwmWrite (18, 1);
  59.  
  60.   // Enable the volume control
  61.   pinMode(27,OUTPUT);
  62.   digitalWrite(27,LOW);
  63.  
  64.   // Volume Up Button
  65.   pinMode(22,INPUT);
  66.   pullUpDnControl(22,PUD_UP);
  67.   wirintPiISR(22, INT_EDGE_FALLING, &volumeUp);
  68.  
  69.   // Volume Down Button
  70.   pinMode(23,INPUT);
  71.   pullUpDnControl(23,PUD_UP);
  72.   wirintPiISR(23, INT_EDGE_FALLING, &volumeDown);
  73.  
  74.   // Set the volume to about 30% on startup
  75.   currentVolume = 10;
  76.   setLVolume(currentVolume);
  77.   setRVolume(currentVolume);
  78.  
  79.   // Any extra init code can go here
  80.  
  81.   for (;;) delay(1000);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement