Wednesday, May 28, 2014

Code

I'm just using this to store some PIC code so I can access it later.

// LFO (Note this does not work well as an LFO)
#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 <stdlib.h>

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

void init()
{
    //Configure GPIO Port
    ANSEL =  0b00000000;  //Configure all GPIO pins as digital
    TRISIO = 0b11001000;  //Set GP0, GP1, GP3, GP4, GP5 as outputs and the rest as inputs
    WPU = 0x00;           //Disable weak pullups
    //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 lfo_saw(int frq)
// Blink 4 short pulses v
 {
  int i, j, k;
  uint8_t sGPIO = 0b00000000;

  for (k=0;k<4;k++)
   {
    for (j=0;j<255;j=j+frq+1)
     {
      for (i=0;i<255;i++)
       {
        if (i<j)
         {
          sGPIO = GPIO;
          sGPIO = sGPIO | 0b00000001;
          sGPIO = sGPIO & 0b11111101;
          GPIO = sGPIO;
          //__delay_us(1);
         }
        if (i>j)
         {
          sGPIO = GPIO;
          sGPIO = sGPIO | 0b00000010;
          sGPIO = sGPIO & 0b11111110;
          GPIO = sGPIO;
          //__delay_us(1);
         }
        if (j<127)
         {
          sGPIO = GPIO;
          sGPIO = sGPIO | 0b00000100;
          sGPIO = sGPIO & 0b11101111;
          GPIO = sGPIO;
          //__delay_us(1);
         }
        if (j>127)
         {
          sGPIO = GPIO;
          sGPIO = sGPIO | 0b00010000;
          sGPIO = sGPIO & 0b11111011;
          GPIO = sGPIO;
          //__delay_us(1);
         }
        if (j<31)
         {
          sGPIO = GPIO;
          sGPIO = sGPIO | 0b00100000;
          GPIO = sGPIO;
          //__delay_us(1);
         }
        if (j>31)
         {
          sGPIO = GPIO;
          sGPIO = sGPIO & 0b11011111;
          GPIO = sGPIO;
          //__delay_us(1);
         }
       }
     }
   }
 }

void main()
{
    uint8_t db_cnt;
    int i, r;
    init();
    GPIO = 0b00000000;
    while(1)
    {
     r = rand() & 0b00001111;
     //r = 1;
     switch (r)
      {
       case 1:
        lfo_saw(r);
        break;
       default:
        lfo_saw(r);
      }
    }
}

//Motor Controller
#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 <stdlib.h>

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

void init()
{
    //Configure GPIO Port
    ANSEL =  0b00000001;  //Configure GPIO pins as digital GP0 set to analog
    TRISIO = 0b11001001;  //Set GP1,GP2,GP4,GP5 as outputs
    WPU = 0x00;           //Disable weak pullups
    //Configuer AD Convertor
    ADCON0 = 0b10110001;        //AD Set up
    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
}

int read_v()
// Read voltage from input 0
 {
    int val;
    ADCON0bits.GO=1;
    while(ADCON0bits.nDONE);
    val = (ADRESH<<2)+(ADRESL>>6);
    return val;
 }

void pwgen(int pw)
// Send pulse width
 {
  int i;
  uint8_t sGPIO = 0b00000001;

  for (i=0;i<255;i++)
   {
    if (i<pw)
     {
      GPIO = 0b00000010;
      __delay_us(200);
     }
    if (i>pw)
     {
      GPIO = 0b00000000;
      __delay_us(200);
     }
   }
 }

void main()
{
    uint8_t db_cnt;
    int v;
    init();
    GPIO = 0b00000010;
    while(1)
    {
     v = read_v();
     pwgen(v<<2);
    }

}

//PWM
#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 <stdlib.h>

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

void init()
{
    //Configure GPIO Port
    ANSEL =  0b00000000;  //Configure all GPIO pins as digital
    TRISIO = 0b11101100;  //Set GP0, GP1, GP2, GP4 as outputs (0) and the rest as inputs (1)
    WPU = 0x00;           //Disable weak pullups
    //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 main()
{
 uint8_t db_cnt, sPIR1,i,sCCP1CON; // ADC_result2;
 init();

 TRISIO = 0b11101100;  //Disable IO2
 PR2 = 0x00;
 CCP1CON = 0b00111100;
 CCPR1L =  1; // 1,4,16
 sPIR1 = PIR1 & 0b11111101; //Clear TMR2IF
 PIR1 = sPIR1;
 T2CON = 0b00000101;

 while(1)
  {
   for (i=0;i<4;i++)
    {
     if (PIR1bits.TMR2IF == 1)
      {
       TRISIO = 0b11101000;
       //sCCP1CON = CCP1CON;
      // CCP1CON = (sCCP1CON & 0b11001111) | (i<<4);
       //CCP1CON = 0b11001111;
       PR2 = i;
      }
     __delay_us(800000);
   }
  }

}

//Stop lights
#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 */

//#fuses NOMCLR,NOPROTECT,NOWDT,INTRC
#pragma config MCLRE=OFF,CP=OFF,WDTE=OFF,FOSC=INTOSCCLK
#define _XTAL_FREQ 4000000
uint8_t sGPIO;

void init()
{
    //Configure GPIO Port
    ANSEL =  0b00000000;  //Configure all GPIO pins as digital
    TRISIO = 0b11111000;  //Set GP0, GP1, GP3 as outputs and the rest as inputs
    WPU = 0x00;           //Disable weak pullups
    //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 main()
{
    uint8_t db_cnt;
    init();
    GPIO = 0b00100000;
    sGPIO= 0b00100000;
    while(1)
    {
     GPIO = 0b00100000;
     for (db_cnt = 0; db_cnt <= 5; db_cnt++)
     {
      GPIO = 0b00100001;
     __delay_ms(40);
     GPIO = 0b00100000;
     GPIO = 0b00100010;
     __delay_ms(40);
     GPIO = 0b00100000;
     GPIO = 0b00100100;
     __delay_ms(40);
     GPIO = 0b00100000;
     }
     __delay_ms(1000);
             for (db_cnt = 0; db_cnt <= 10; db_cnt++)
            {
               __delay_ms(10);
               if (GPIObits.GP5 == 1)
                db_cnt = 0;
            }
     GPIO = 0b00100001;
     __delay_ms(1000);
     GPIO = 0b00100000;
     GPIO = 0b00100010;
     __delay_ms(1000);
     GPIO = 0b00100000;
     GPIO = 0b00100100;
     __delay_ms(1000);
            for (db_cnt = 0; db_cnt <= 10; db_cnt++)
            {
               __delay_ms(1);
               if (GPIObits.GP5 == 1)
                db_cnt = 0;
            }

    }

}