Loading

Test program

  1. //SAA1099 test program
  2.  
  3. #include <SPI.h>
  4.  
  5. #define address_saa  3  //Pin 3 SAA     <- pin 3 Arduino
  6. #define write_saa 4     //Pin 1 SAA     <- pin 4 Arduino (active low)
  7. #define copy_sr_saa 10  //Pin 12 595    <- pin 10 Arduino (rising edge)
  8. #define pwm_arduino 5   //Mono PWM mix  <- pin 5 Arduino
  9.  
  10. //LIST OF GLOBAL VARIABLES
  11.  
  12. volatile uint8_t spi_ready = 0;
  13. unsigned char note_data[12] = {33,60,85,109,132,153,173,192,210,227,243,5}; //C C# D D# E F F# G G# A A# B
  14.  
  15. void setup() {
  16.   // put your setup code here, to run once:
  17.  
  18.   // Interrupts off
  19.   cli();
  20.  
  21.   // Set some control pins as outputs
  22.   pinMode(address_saa, OUTPUT);
  23.   pinMode(write_saa, OUTPUT);
  24.   pinMode(copy_sr_saa, OUTPUT);
  25.   pinMode(pwm_arduino, OUTPUT);
  26.   pinMode(11, OUTPUT); //MOSI
  27.   pinMode(13, OUTPUT); //SCK
  28.   pinMode(pwm_arduino, OUTPUT);
  29.  
  30.   // Write high some control pins
  31.   digitalWrite(address_saa, HIGH);
  32.   digitalWrite(write_saa, HIGH);
  33.   digitalWrite(pwm_arduino, HIGH);
  34.  
  35.   SPI.beginTransaction(SPISettings(16000000, LSBFIRST, SPI_MODE0));
  36.   //SPCR |= (1<<SPIE); //Enable interrupt
  37.   SPI.attachInterrupt();
  38.  
  39.   //analogWrite (5,127);
  40.  
  41.   sei();
  42.   spi_ready = 1;
  43. }
  44.  
  45. // If data is clocked into shift register, copy to outputs, write to SAA1099P
  46. ISR(SPI_STC_vect)
  47. {
  48.   digitalWrite(copy_sr_saa, HIGH);  //Clock in data (PORTB |= _BV(copy_sr_saa);)
  49.   digitalWrite(copy_sr_saa, LOW);   //Clock in data (PORTB &= ~_BV(copy_sr_saa);)
  50.   digitalWrite(write_saa, LOW);     //PORTD &= ~_BV(write_saa); //Write data to SAA chip
  51.   digitalWrite(write_saa, HIGH);     //PORTD |= _BV(write_saa);
  52.   //PORTD |= _BV(select);
  53.   spi_ready = 1;
  54. }
  55.  
  56. //Write data to SAA1099 at a certain address
  57. void set_SAA(unsigned char add, unsigned char dat) //Writes address and data to SAA1099 chip
  58. {
  59.   while (spi_ready==0);
  60.   digitalWrite(address_saa, HIGH);    //PORTD |= _BV(address_saa);  //Writing address A0 = 1
  61.   SPDR = add;
  62.   spi_ready = 0;
  63.  
  64.   while (spi_ready==0);
  65.   digitalWrite(address_saa, LOW);    //PORTD &= ~_BV(address_saa); //Writing data A0 = 0
  66.   SPDR = dat; //SPI.transfer(dat);
  67.   spi_ready = 0;
  68. }
  69.  
  70. void loop() {
  71.   // put your main code here, to run repeatedly:
  72.   set_SAA(0x1C, 1);           //Sound enable
  73.   set_SAA(0x00, 255);       //Volume ch0
  74.   set_SAA(0x01, 0);         //Volume ch1
  75.   set_SAA(0x02, 0);         //Volume ch2
  76.   set_SAA(0x03, 0);         //Volume ch3
  77.   set_SAA(0x04, 0);         //Volume ch4
  78.   set_SAA(0x05, 0);         //Volume ch5
  79.   set_SAA(0x14, 1);
  80.   set_SAA(0x15, 0);
  81.  
  82.   //set_SAA(0x18,0b10101011);   //Envelope 0
  83.   //set_SAA(0x19,0b10101011);   //Envelope 1
  84.   set_SAA(0x10, 3);           //Octave ch0 = 3
  85.  
  86.   int a = 0;
  87.    
  88.   while(1){
  89.     /*for (uint8_t x=0; x<12; x++){
  90.       set_SAA(0x08, note_data[x]); //Let's play some notes!
  91.       delay(100);
  92.     }
  93.     */
  94.     set_SAA(0x08, note_data[random(12)]);
  95.     delay(100<<random(3));
  96.    
  97.     a++;
  98.     if (a>15) a=0;
  99.     set_SAA(0x00, a+((15-a)<<4));       //Volume ch0
  100.      
  101.   }
  102. }

Comments