Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 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 volumeUp(void) {
  10.     setLVolume(++currentVolume);
  11.     setRVolume(currentVolume);
  12. }
  13.  
  14. void volumeDown(void) {
  15.     setLVolume(--currentVolume);
  16.     setRVolume(currentVolume);
  17. }
  18.  
  19. void setLVolume(char Volume) {
  20.   wiringPiI2CWrite(digiPot, Volume & 0x3F);
  21. }
  22. void setRVolume(char Volume) {
  23.   wiringPiI2CWrite(digiPot, (Volume & 0x3F) | 0x40);
  24. }
  25.  
  26. void init(void) {
  27.   // Setup the PWM to generate the -5V rail for the digipot
  28.   pinMode(18,PWM_OUTPUT);
  29.   pwmSetMode(PWM_MODE_MS);
  30.   pwmSetClock(512); // 512 = 17.5kHz, 256 = 35kHz, 128 = 70kHz, 64 = 140kHz
  31.   pwmSetRange(2);
  32.   pwmWrite (18, 1);
  33.  
  34.   // Enable the volume control
  35.   pinMode(27,OUTPUT);
  36.   digitalWrite(27,LOW);
  37.  
  38.   // Setup the push buttons
  39.   // Up
  40.   pinMode(22,INPUT);
  41.   pullUpDnControl(22,PUD_UP);
  42.   wirintPiISR(22, INT_EDGE_FALLING, &volumeUp);
  43.  
  44.   // Down
  45.   pinMode(23,INPUT);
  46.   pullUpDnControl(23,PUD_UP);
  47.   wirintPiISR(23, INT_EDGE_FALLING, &volumeDown);
  48.  
  49.   // Setup I2C to talk to the digipot
  50.   digiPot = wiringPiI2CSetup(0x50);
  51.  
  52.   // Set the volume to about 30% on startup
  53.   currentVolume = 10;
  54.   setLVolume(currentVolume);
  55.   setRVolume(currentVolume);
  56.  
  57.  
  58.   // Any extra init code can go here
  59. }
  60.  
  61. void loop(void) {
  62.     delay(1000);
  63.    
  64.     // You can add code here if required, run once per second
  65. }
  66.    
  67. int main (void)
  68. {
  69.   if (wiringPiSetupGpio() == -1)
  70.     exit (1) ;
  71.  
  72.   init();
  73.  
  74.   for (;;) loop();
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement