Thursday, July 6, 2017

Code Dump

This is just another code dump:

/*
 * File:   ls5main.c
 * Author: hans
 *
 * Created on December 17, 2014, 8:59 AM
 */

#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>        /* includes 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 = 0b110001001;  //Set GP0, GP1, GP2 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(100);
    }
}

void main()
{
    int i=3,j,k,r1=0,r2=0;
    init();
    GPIO = 0b00000000;

    while(1)
    {
     if (GPIObits.GP0 == 0)
      {
       i=(i+1) % 4;
       GPIO = 0b00000000;
       vdelay(10000);
      }
     //i=3;
     switch (i)
      {
       case 1:
        GPIO = rand() & 63;
        vdelay(4000);
        break;
       case 2:
        GPIO = rand() & 63;
        vdelay(2000);
        break;
       case 3:
        for (j=0;j<255;j++)
         {
          for (k=0;k<255;k++)
           {
            if (j<k)
             {
              GPIO=r2;
              __delay_us(4);
             }
            else
             {
              GPIO=r1;
              __delay_us(4);
             }
           }
         }
        r2 = r1;
        r1 = rand() % 64;
        break;
      default:
       GPIO = rand() & 63;
       vdelay(8000);
       break;
      }
    }
}


/*
 * File:   spider2main.c
 * Author: hmikelson
 *
 * Created on March 24, 2015, 4:30 PM
 */

#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 GP3 as input and the rest as outputs
                    // GP0 = eye 1, GP2 = eye 2, GP5 = speaker, GP4 = piezo
    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 fade_eyes(int n) // PWM sweep high f to low f
{
 int i;
  {
   for (i=0;i<n;i++)
    {
     GPIO = 0b11000101;
     vdelay(i);
     GPIO = 0b11000000;
     vdelay(n-i);
    }
   for (i=0;i<n;i++)
    {
     GPIO = 0b11000101;
     vdelay(n-i);
     GPIO = 0b11000000;
     vdelay(i);
    }
  }
}

void scan_eyes(int n) // PWM sweep high f to low f
{
 int i,j,k;
 int i1,i2,i3,pw1,pw2;
 uint8_t sGPIO;

 i1=1; i2=-1; pw1=0; pw2=n;
 sGPIO = GPIO;
 for (k=0;k<6;k++)
 {
  for (j=0;j<n;j++)
   {
    for (i=0;i<n;i++)
     {
      if (i>pw1)
       {
        sGPIO = sGPIO & 0b11111110;
       }
      else
       {
        sGPIO = sGPIO | 0b00000001;
       }
      if (i>pw2)
       {
        sGPIO = sGPIO & 0b11111011;
       }
      else
       {
        sGPIO = sGPIO | 0b00000100;
       }
      GPIO = sGPIO;
     }
    pw1 = pw1 + i1;
    pw2 = pw2 + i2;
   }
  i3 = i1; i1 = i2, i2 = i3;
 }
}

void dark_eyes(void) // PWM sweep high f to low f
{
 GPIO = 0b11000000;
 __delay_ms(2000);
}

void scream(int n) // PWM sweep high f to low f
{
 int i,r,j;
  {
    for (j=0;j<n;j++)
    {
     for (i=0;i<j;i++)
      {
       r=rand()%8;
       GPIO = 0b11100101;
       vdelay(i+r);
       GPIO = 0b11000000;
       vdelay(n-i+r);
       i = i+r;
      }
    }
  }
}

void scream2(int n) // PWM sweep high f to low f
{
 int i,r,j;
  {
    for (j=0;j<n;j++)
    {
     for (i=0;i<j;i++)
      {
       r=rand()%8;
       GPIO = 0b11010101;
       vdelay(i+r);
       GPIO = 0b11000000;
       vdelay(n-i+r);
       i = i+r;
      }
    }
  }
}

void scream3(int n) // PWM sweep high f to low f
{
 int i,j,k,r;
 int i1,i2,i3,pw1,pw2;
 uint8_t sGPIO;

 i1=1; i2=-1; pw1=0; pw2=n;
 sGPIO = GPIO;
 for (k=0;k<2;k++)
 {
  for (j=0;j<n;j++)
   {
    for (i=0;i<n;i++)
     {
      if (i>pw1)
       {
        sGPIO = sGPIO & 0b11011110;
       }
      else
       {
        sGPIO = sGPIO | 0b00100001;
       }
      if (i>pw2)
       {
        sGPIO = sGPIO & 0b11101011;
       }
      else
       {
        sGPIO = sGPIO | 0b00010100;
       }
      GPIO = sGPIO;
      r=rand()%6;
      vdelay(r);
     }
    pw1 = pw1 + i1;
    pw2 = pw2 + i2;
   }
  i3 = i1; i1 = i2, i2 = i3;
 }
}

void main()
{
uint8_t r,r2;

 init();

 while(1)
  {
   r=rand()%32;
   //r=4;
   r2 = rand()%8+1;
   switch (r)
    {
     case 1:
     fade_eyes(100*r2);
     break;

     case 2:
     fade_eyes(20*r2);
     break;

     case 3:
     scan_eyes(20*r2);
     break;

     case 4:
     scream(16*r2);
     break;

     case 5:
     scream2(8*r2);
     break;

     case 6:
     scream3(8*r2);
     break;

     default:
     dark_eyes();
    }
  }
}


/*
 * 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  = 0b00000010; // input/output
    OSCCON = 0b01111010;  // set internal osc to 16 MHz
    ANSELA = 0b00000000;
}

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;
        CREN = 1;
        SYNC = 0;
        SPEN = 1;
        CREN = 1;
        TXIE = 0;
        //TX9 = 0;
        //RX9 = 0;
        //RCIE = 1;
        //PEIE = 1;
        //GIE = 1;
        return 1;
    }
    return 0; //Fail
}

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

char UART_TX_Empty()
{
    return TRMT;
}

//UART Data Ready
char UART_Data_Ready()
{
    return RCIF;
}

char UART_Read()
{
    while(!RCIF);
    return RCREG;
}

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

//Timing Clock
void TCLK()
{
    UART_Write(0xF8);
}

void main()
{
    int r;
    char c, cr;
    init();
    UART_Init(31250);
    c=0b00000000;
    while(1)
    {
        //TCLK();
        //r=(rand()%36)+36;
        //NoteOn(1,r,127);
        //__delay_us(10000);
        //NoteOff(1,r,0);
        //__delay_us(10000);
        //cr = rand();
        //UART_Write(cr);
        if (UART_Data_Ready())
        {
            //PORTA = 0b00000100;
            c = UART_Read();
            //__delay_us(1000);
            UART_Write(c);
        }
            PORTA=c;
            //__delay_us(100000);
            //PORTA=0b00000000;
            //__delay_us(400000);
    }
}

 

No comments:

Post a Comment