Sunday, February 22, 2015

Chiptune Noisemaker revised

/* 
 * File:   noisegenmain.c
 * Author: Hans Mikelson
 *
 * Created on February 13, 2015, 2:42 PM
 * Revised February 22, 2015
 * This program creates a random sequence of various types of noises
 * ranging from computer game like noises to white like noise.
 * These are all based on random or periodic on off pulses from the
 * microcontroller.  This program uses the 12f683 microcontroller but
 * should be adaptable to other brands of microcontroller.
 */

#if defined(__XC)
    #include <xc.h>         /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>        /* HiTech General Include File */
#endif

#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>       /* For true/false definition */
#include <stdlib.h>     /*rand()*/

#pragma config MCLRE=OFF,CP=OFF,WDTE=OFF,FOSC=INTOSCIO
#define _XTAL_FREQ 4000000
uint8_t sGPIO;

void init()
{
    //Configure GPIO Port
    ANSEL =  0b00000000;  //Configure all GPIO pins as digital
    TRISIO = 0b11000100;  //Set GP1-5 as outputs and the rest as inputs
    OPTION_REGbits.nGPPU = 0;
    WPU = 0b00000100;     //Enable weak pullups on GP2
    //Configuer AD Convertor
    ADCON0 = 0x00;        //AD disabled
    ADRESH = 0x00;        //Init the AD Register
    //Configure Comparator
    CMCON0 = 0xFF;   // Comparator is turned off
    CMCON1 = 0x00;   // Comparator is turned off
    //Interrupt configuration
    INTCON = 0x00;   //Disable all interrupts
}

void vdelay(int n)
{
    int i;
    for (i=0;i<=n;i++)
    {
     __delay_us(1);
    }
}

void noise1(uint8_t r3) // PWM sweep high f to low f
{
 int i,j,r1,r2;
 r1=rand() & (r3-1);
 r2=r3/r1;
 for (j=0;j<r1;j++)
  {
   for (i=0;i<j;i++)
    {
     GPIO = 0b11110111;
     vdelay(i*r2);
     GPIO = 0b11000000;
     vdelay((j-i)*r2);
    }
   for (i=0;i<j;i++)
    {
     GPIO = 0b11110111;
     vdelay((j-i)*r2);
     GPIO = 0b11000000;
     vdelay(i*r2);
    }
  }
}

void noise4(uint8_t r3) // PWM sweep low f to high f
{
 int i,j,r1,r2;
 r1=rand() & (r3-1);
 r2=r3/r1;
 for (j=r1;j>0;j--)
  {
   for (i=0;i<r1-j;i++)
    {
     GPIO = 0b11110111;
     vdelay(i*r2);
     GPIO = 0b11000000;
     vdelay((j-i)*r2);
    }
   for (i=0;i<r1-j;i++)
    {
     GPIO = 0b11110111;
     vdelay((j-i)*r2);
     GPIO = 0b11000000;
     vdelay(i*r2);
    }
  }
}

void noise2(uint8_t r3) //PWM sweep down, laser
{
 int i,j,r1,r2;
 r1=rand() & (r3-1);
 r2=r3/r1;
 for (j=r2;j<r1;j++)
  {
   for (i=0;i<j;i++)
    {
     GPIO = 0b11110111;
     vdelay(r2);
     GPIO = 0b11000000;
     vdelay((j-i)*r2);
    }
  }
}

void noise3(uint8_t r3) // Pulse noise with different frequencies
{
 int j,r1,r2;
 r1=rand() & 2047;
 r2 = 32*r3/r1;
 for (j=0;j<r2;j++)
  {
   GPIO = 0b11110111;
   vdelay(rand() & (r3-1));
   GPIO = 0b11000000;
   vdelay(rand() & (r3-1));
  }
}

void noise5(uint8_t r3) // Long random clicks
{
 int j,r1;
 r1=rand() & (r3-1);
 for (j=0;j<r1/4;j++)
  {
   GPIO = 0b11110111;
   vdelay(r3*8);
   GPIO = 0b11000000;
   vdelay(rand());
  }
}

void noise6(uint8_t r3) // Short random clicks
{
 int j,r1;
 r1=rand() & (r3-1);
 for (j=0;j<r1/2;j++)
  {
   GPIO = 0b11110111;
   vdelay(r3*2);
   GPIO = 0b11000000;
   vdelay(rand() & 8191);
  }
}

void noise7(uint8_t r3) // PWM sweep low f to high f
{
 int i,j,r1,r2;
 r1=(rand() & (r3-1))/4;
 r2=r3/r1;
 for (j=r1;j>0;j--)
  {
   for (i=0;i<r1-j;i++)
    {
     GPIO = 0b11110111;
     vdelay(i*r2);
     GPIO = 0b11000000;
     vdelay((j-i)*r2);
     vdelay(rand() & ((r3-1)/4));
    }
   for (i=0;i<r1-j;i++)
    {
     GPIO = 0b11110111;
     vdelay((j-i)*r2);
     vdelay(rand() & ((r3-1)/4));
     GPIO = 0b11000000;
     vdelay(i*r2);
    }
  }
}

void main()
{
uint8_t ra[8]={2,4,4,8,16,32,64,128};
int r,r4;

 init();
 r=0;
 while(1)
  {
   //r = rand()&7;
   if (GPIObits.GP2 == 0)
    {
     r=(r+1)%7;
     vdelay(1000000);
    }
   r4=rand()&7;
   switch (r)
    {
     case 1:
     noise1(ra[r4]);
     //vdelay(rand());
     break;

     case 2:
     noise2(ra[r4]);
     //vdelay(rand());
     break;

     case 3:
     noise3(ra[r4]);
     //vdelay(rand());
     break;

     case 4:
     noise1(ra[r4]);
     noise4(ra[r4]);
     //vdelay(rand());
     break;

     case 5:
     noise5(ra[r4]);
     //vdelay(rand());
     break;

     case 6:
     noise6(ra[r4]);
     //vdelay(rand());
     break;

     default:
     noise7(ra[r4]);
     //vdelay(rand());
    }
  }
}

Friday, February 20, 2015

Very Simple Drum Machine


// Drum Machine by Hans Mikelson
//
#if defined(__XC)
    #include <xc.h>         /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>        /* HiTech General Include File */
#endif

#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>       /* For true/false definition */
#include <stdlib.h>

//#fuses NOMCLR,NOPROTECT,NOWDT,INTRC
//#pragma config MCLRE=OFF,CP=OFF,WDTE=OFF,FOSC=INTOSCCLK
#pragma config MCLRE=OFF,CP=OFF,WDTE=OFF,FOSC=INTOSCIO

#define _XTAL_FREQ 4000000

void init()
{
    //Configure GPIO Port
    ANSEL =  0b11111001;  //Configure all GPIO pins as digital
    ANSELbits.ADCS=0b100;
    TRISIO = 0b11111001;  //Set GP1 as output and the rest as inputs
    WPU = 0x00;           //Disable weak pullups
    //Configuer AD Convertor
    ADCON0bits.ADON=1;
    ADCON0bits.CHS = 0b00;   // AD Ch0 selected
    ADCON0bits.ADFM = 0;     // Left justified 10 bits
    ADRESH = 0x00;           //Init the AD Register
    //Configure Comparator
    CMCON0 = 0xFF;   // Comparator is turned off
    CMCON1 = 0x00;   // Comparator is turned off
    //Interrupt configuration
    INTCON = 0x00;   //Disable all interrupts
}

void vdelay(uint8_t delrpt)
{
    uint8_t i;
    for (i=0;i<=delrpt;i++)
    {
        __delay_ms(10);
    }
}


void main()
{
    uint8_t rthm1,delrpt,i,j,cv;
    init();
    rthm1 = 0b10001010;
    while(1)
    {
     GPIO = rthm1 & 0b00000010;
     rthm1 = (rthm1 << 1)|(rthm1>>7);
     __delay_ms(1);
     GPIO = 0b00000000;
     ADCON0bits.GO = 1;
     while(ADCON0bits.nDONE);
     delrpt = ADRESH >> 3;
     cv=rand()>>13;
     //cv = 2;
     for (j=0;j<delrpt;j++)
     {
     for (i=0;i<16;i++)
      {
         if(i<cv)
        {
         GPIO = 0b00000100;
        }
         else
         {
          GPIO = 0b00000000;
         }
         __delay_ms(1);
     }
     }
     //vdelay(delrpt);
    }
}

Sunday, February 15, 2015

Chip Tune Noise


This program generates noise similar to that found in computer games.  There are seven different subroutines: 1. sweep up, 2. sweep down, 3. noise, 4. sweep up and down, 5. long random pulses, 6. short random clicks, 7. sweep up with random noise mixed in.  The each subroutine is selected randomly and plays for 1-2 seconds.  This was implemented on a Microchip 12f683 micro-controller.

/* 
 * File:   noisegenmain.c
 * Author: Hans Mikelson
 *
 * Created on February 13, 2015, 2:42 PM
 * This program creates a random sequence of various types of noises 
 * ranging from computer game like noises to white like noise.
 * These are all based on random or periodic on off pulses from the
 * microcontroller.  This program uses the 12f683 microcontroller but
 * should be adaptable to other brands of microcontroller.
 */

#if defined(__XC)
    #include <xc.h>         /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>        /* HiTech General Include File */
#endif

#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>       /* For true/false definition */
#include <stdlib.h>     /*rand()*/

#pragma config MCLRE=OFF,CP=OFF,WDTE=OFF,FOSC=INTOSCIO
#define _XTAL_FREQ 4000000
uint8_t sGPIO;

void init()
{
    //Configure GPIO Port
    ANSEL =  0b00000000;  //Configure all GPIO pins as digital
    TRISIO = 0b11001000;  //Set GP1-5 as outputs and the rest as inputs
    OPTION_REGbits.nGPPU = 0;
    WPU = 0b00000001;     //Enable weak pullups on GP0
    //Configuer AD Convertor
    ADCON0 = 0x00;        //AD disabled
    ADRESH = 0x00;        //Init the AD Register
    //Configure Comparator
    CMCON0 = 0xFF;   // Comparator is turned off
    CMCON1 = 0x00;   // Comparator is turned off
    //Interrupt configuration
    INTCON = 0x00;   //Disable all interrupts
}

void vdelay(int n)
{
    int i;
    for (i=0;i<=n;i++)
    {
     __delay_us(1);
    }
}

void noise1(uint8_t r3) // PWM sweep high f to low f
{
 int i,j,r1,r2;
 r1=rand() & (r3-1);
 r2=r3/r1;
 for (j=0;j<r1;j++)
  {
   for (i=0;i<j;i++)
    {
     GPIO = 0b11110111;
     vdelay(i*r2);
     GPIO = 0b11000000;
     vdelay((j-i)*r2);
    }
   for (i=0;i<j;i++)
    {
     GPIO = 0b11110111;
     vdelay((j-i)*r2);
     GPIO = 0b11000000;
     vdelay(i*r2);
    }
  }
}

void noise4(uint8_t r3) // PWM sweep low f to high f
{
 int i,j,r1,r2;
 r1=rand() & (r3-1);
 r2=r3/r1;
 for (j=r1;j>0;j--)
  {
   for (i=0;i<r1-j;i++)
    {
     GPIO = 0b11110111;
     vdelay(i*r2);
     GPIO = 0b11000000;
     vdelay((j-i)*r2);
    }
   for (i=0;i<r1-j;i++)
    {
     GPIO = 0b11110111;
     vdelay((j-i)*r2);
     GPIO = 0b11000000;
     vdelay(i*r2);
    }
  }
}

void noise2(uint8_t r3) //PWM sweep down, laser
{
 int i,j,r1,r2;
 r1=rand() & (r3-1);
 r2=r3/r1;
 for (j=r2;j<r1;j++)
  {
   for (i=0;i<j;i++)
    {
     GPIO = 0b11110111;
     vdelay(r2);
     GPIO = 0b11000000;
     vdelay((j-i)*r2);
    }
  }
}

void noise3(uint8_t r3) // Pulse noise with different frequencies
{
 int j,r1,r2;
 r1=rand() & 2047;
 r2 = 32*r3/r1;
 for (j=0;j<r2;j++)
  {
   GPIO = 0b11110111;
   vdelay(rand() & (r3-1));
   GPIO = 0b11000000;
   vdelay(rand() & (r3-1));
  }
}

void noise5(uint8_t r3) // Long random pulses
{
 int j,r1;
 r1=rand() & (r3-1);
 for (j=0;j<r1/4;j++)
  {
   GPIO = 0b11110111;
   vdelay(r3*8);
   GPIO = 0b11000000;
   vdelay(rand());
  }
}

void noise6(uint8_t r3) // Short random clicks
{
 int j,r1;
 r1=rand() & (r3-1);
 for (j=0;j<r1/2;j++)
  {
   GPIO = 0b11110111;
   vdelay(r3*2);
   GPIO = 0b11000000;
   vdelay(rand() & 8191);
  }
}

void noise7(uint8_t r3) // PWM sweep low f to high f
{
 int i,j,r1,r2;
 r1=(rand() & (r3-1))/4;
 r2=r3/r1;
 for (j=r1;j>0;j--)
  {
   for (i=0;i<r1-j;i++)
    {
     GPIO = 0b11110111;
     vdelay(i*r2);
     GPIO = 0b11000000;
     vdelay((j-i)*r2);
     vdelay(rand() & ((r3-1)/4));
    }
   for (i=0;i<r1-j;i++)
    {
     GPIO = 0b11110111;
     vdelay((j-i)*r2);
     vdelay(rand() & ((r3-1)/4));
     GPIO = 0b11000000;
     vdelay(i*r2);
    }
  }
}

void main()
{
uint8_t ra[8]={2,4,4,8,16,32,64,128};
int r,r4;

 init();
 while(1)
  {
   r4=rand()&7;
   r = rand()&7;
   //r = 7;
   switch (r)
    {
     case 1:
     noise1(ra[r4]);
     //vdelay(rand());
     break;

     case 2:
     noise2(ra[r4]);
     //vdelay(rand());
     break;

     case 3:
     noise3(ra[r4]);
     //vdelay(rand());
     break;

     case 4:
     noise1(ra[r4]);
     noise4(ra[r4]);
     //vdelay(rand());
     break;

     case 5:
     noise5(ra[r4]);
     //vdelay(rand());
     break;

     case 6:
     noise6(ra[r4]);
     //vdelay(rand());
     break;

     default:
     noise7(ra[r4]);
     //vdelay(rand());
    }
  }
}