Wednesday, April 5, 2017

Note On Note Off

This code uses the PIC12f1822 UART (EUSART) to send a note on and off message.  I tested this and it seems to work OK. 220 ohm resistor on the output pin and 220 ohm resistor to +5 V.

/*
 * File:   T1822MIDIMain.c
 * Author: hmikelson
 *MIDI Test Program
 *
 * Created on April 5, 2017, 10:49 AM
 */

#if defined(__XC)
    #include <xc.h>
#endif

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

__CONFIG(MCLRE_ON & CP_ON & WDTE_OFF & BOREN_OFF & FOSC_INTOSC);

#define _XTAL_FREQ 16000000

void init()
{
    TRISA  = 0b00000000; // all bits output
    OSCCON = 0b01111010;  // set internal osc to 16 MHz
}

char UART_Init(const long int baudrate)
{
    unsigned int x;
    x = (_XTAL_FREQ - baudrate*64)/(baudrate*64);
    if(x>255)
    {
        x = (_XTAL_FREQ - baudrate*16)/(baudrate*16);
        BRGH = 1;
    }
    if(x<256)
    {
        SPBRG = x;
        TXEN = 1;
        SYNC = 0;
        SPEN = 1;
        CREN = 1;
        return 1;
    }
    return 0; //Fail
}

void UART_Write(char data)
{
    while(!TRMT);
    TXREG = data;
}

char UART_TX_Empty()
{
    return TRMT;
}

//Note On
void NoteOn(char ch, char note, char vel)
{
    char chan;
    ch = ch - 1;
    chan = (0b10010000 | ch);
    UART_Write(chan);
    UART_Write(note);
    UART_Write(vel);
}

//Note Off
void NoteOff(char ch, char note, char vel)
{
    char chan;
    ch = ch - 1;
    chan = (0b10000000 | ch);
    UART_Write(chan);
    UART_Write(note);
    UART_Write(vel);
}

void main()
{
    init();
    UART_Init(31250);
    while(1)
    {
        NoteOn(1,64,64);
        __delay_us(1000000);
        NoteOff(1,64,0);
        __delay_us(1000000);
    }
}

Sunday, April 2, 2017

Drum Multi Trigger

This program was written for a pic 12f683.  It attempts to read a voltage from input GPIO 4 and then repeats on GPIO 0-2.  The intention is for a drum trigger repeater.  One output could be used to trigger an analog drum shell sound, another could trigger a noise for a snare, etc.  This code is still buggy so use it with caution.  I am just putting it here so I can access it remotely.

 /*
 * File:   Drum Trigger Repeater
 * Author: Hans Mikelson
 *
 * Created on April 2, 2017, 1 PM
 * This program will read an input trigger voltage from for example
 * a piezo device and repeat it onto the outputs 0-2
 */

#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;
int gi,gi1,gr1,gr2,gr3,gi3,gr0,gr4;

void init()
{
    //Configure GPIO Port
    //ANSEL =  0b00000000;  //Configure all GPIO pins as digital
    //TRISIO = 0b11001100;  //Set GP# 1=inputs and 0=outputs
    ANSEL =  0b00010000;  //Configure GPIO pins as digital, GP4 Analog
    TRISIO = 0b11011000;  //Set GP0,GP1,GP2,GP5 as outputs
    //Configuer AD Convertor
    ADCON0 = 0b10001101;  //AD Set up
    ADRESH = 0x00;        //Init the AD Register
    ADRESL = 0x00;
    OPTION_REGbits.nGPPU = 0;

    WPU = 0b00001000;     //Enable weak pullups=1
    //Configure Comparator
    CMCON0 = 0xFF;   // Comparator is turned off
    CMCON1 = 0x00;   // Comparator is turned off
    //Interrupt configuration
    INTCON = 0x00;   //Disable all interrupts
    //INTCONbits.INTF = 0;       // External interrupt on GP2 only
    //OPTION_REGbits.INTEDG = 0; // 1=rising edge, 0=falling
    //INTCONbits.INTE = 1;       // Enable external interrupt
    //INTCONbits.GIE = 1;        // Global interrupt enable
}

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

int read_v()
// Read voltage from input
 {
    int val,v1,v2;
    ADCON0bits.GO=1;
    while(ADCON0bits.nDONE);
    v1 = ADRESH;
    v2 = ADRESL;
    val = (v1<<8) + v2;
    return val;
 }

uint8_t gtrig;

void main()
{
    //uint8_t i;
    int v1,v2,v3,v4,v5,va,i;

    init();
    while(1)
    {
        v1=read_v();
        v2=read_v();
        v3=read_v();
        v4=read_v();
        v5=read_v();
        va = (v1+v2+v3+v4+v5)/5;
        if (va>10)
        {
          GPIO = 0b00000111;
          for (i=0; i<va; i++);
        }
        GPIO = 0b00000000;
    }
}