User Tools

Site Tools


user:jcavalu3:portfolio:hpcexperiment8

Speaker Played through Raspberry Pi

Shawn and I worked together on operating a PC speaker using gpio pins. It was interesting the way that it worked. We had to figure out the amount of time it needed to sleep between turning the speaker on and off. We figured out the right amount of time by dividing the usleep time (1000000 microseconds, or 1 second) by the frequency of the specific note. Also, I made a countdown program for this as well! :D

The code:

  1 #include "rpi.h"
  2 #include <stdlib.h>
  3 
  4 int main()
  5 {
  6 
  7     if(map_peripheral(&gpio) == -1)
  8     {
  9         printf("Failed to map the physical GPIO registers into the virtual memory space.\n");
 10         return -1;
 11     }
 12 
 13     int read = 0;
 14 
 15     INP_GPIO(18);
 16     OUT_GPIO(18);
 17 
 18     int countdown = 0;
 19 
 20     printf("Welcome to THE COUNTDOWN.\nHow long would you like to COUNT DOWN: ");
 21     scanf("%d", &countdown);
 22 
 23     printf("PREPARE FOR THE COUNTDOWN.\nStarting at: %u seconds\n\n", countdown);
 24 
 25     for(countdown; countdown >= 0; countdown--)
 26     {
 27         printf("%u\n", countdown);
 28         usleep(1000000);
 29     }
 30 
 31     printf("GAME OVER, BITCH.\n");
 32 
 33     while(1)
 34     {
 35 
 36         GPIO_SET = 1 <<  18;
 37         usleep(1136);
 38         read = GPIO_READ(18);
 39         read = read >> 18;
 40         printf("\nPIN: %u", read);
 41         GPIO_CLR = 1 <<  18;
 42         usleep(1136);
 43         read = GPIO_READ(18);
 44         read = read >> 18;
 45         printf("\nPIN: %u", read);
 46     }
 47 
 48     return(0);
 49 }
user/jcavalu3/portfolio/hpcexperiment8.txt · Last modified: 2013/12/13 18:18 by jcavalu3