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);
    }
}

No comments:

Post a Comment