Advertisement
igendel

PSoC 4 "IDAC Sounds" main.c

Jan 1st, 2016
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. #include <project.h>
  2.  
  3. const uint8_t sinTable[128] =
  4.     {255,255,255,255,254,254,254,253,253,252,251,250,250,249,248,
  5.      246,245,244,243,241,240,238,237,235,234,232,230,228,226,224,
  6.      222,220,218,215,213,211,208,206,203,201,198,196,193,190,188,
  7.      185,182,179,176,173,170,167,165,162,158,155,152,149,146,143,
  8.      140,137,134,131,128,124,121,118,115,112,109,106,103,100,97,
  9.      93,90,88,85,82,79,76,73,70,67,65,62,59,57,54,52,49,47,44,42,
  10.      40,37,35,33,31,29,27,25,23,21,20,18,17,15,14,12,11,10,9,7,6,
  11.      5,5,4,3,2,2,1,1,1,0,0,0};
  12.  
  13. volatile uint8_t timerTicked = 0;
  14. uint8_t buttonState = 0;
  15.    
  16. uint32_t sinus(const uint8_t index) {
  17.    
  18.     // Avoid timing difference between conditions
  19.     const uint8_t rIndex = 255 - index;
  20.    
  21.     if (index < 128) {
  22.         return sinTable[index];
  23.     } else {
  24.         return sinTable[rIndex];
  25.       } // else
  26.    
  27. } // sinus  
  28.    
  29. CY_ISR(timerTick_Handler) {
  30.     timerTicked = 1;
  31. } // ISR
  32.  
  33. int main() {
  34.    
  35.     uint32_t a;
  36.    
  37.     CyGlobalIntEnable; /* Enable global interrupts. */
  38.    
  39.     timerTick_StartEx(timerTick_Handler);
  40.     Timer_1_Start();
  41.    
  42.     for(;;)
  43.     {
  44.        
  45.         if (timerTicked) {
  46.            
  47.             if (Button_Pin_Read() != buttonState) {
  48.  
  49.                 // There are only two possibilities, so...
  50.                 buttonState = !buttonState;
  51.                
  52.                 if (buttonState) {
  53.                     IDAC_1_Stop();
  54.                 } else {
  55.                     IDAC_1_Start();
  56.                   } // else
  57.                 timerTicked = 0;
  58.                
  59.             } // if  
  60.            
  61.         } // if (timerTicked)  
  62.        
  63.         for (a = 0; a < 256; a++) {
  64.             IDAC_1_SetValue(sinus(a));
  65.             CyDelayUs(5);
  66.         } // for  
  67.     }
  68. } // main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement