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

Thursday, December 11, 2014

Book Light

/*
 * File:   booklightmain.c
 * Author: Hans Mikelson
 *
 * Created on December 10, 2014, 9:13 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 */

#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 = 0b11001001;  //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 main()
{
    int i,j;
    int d[8]={1,2,4,8,16,32,64,127};
    init();
    GPIO = 0b00110001;
    i=0;
    while(1)
    {
       if (GPIObits.GP0 == 0)
       {
           i=(i+1)%8;
           vdelay(1000000);
       }
       GPIO = 0b11110000;
       vdelay(128-d[i]);
       GPIO = 0b11000000;
       vdelay(d[i]);
    }
}

Sunday, November 30, 2014

XMas Lights and Jingle Bells

This is the code used to generate fading Christmas lights and to play jingle bells.

/*
 * File:   main.c
 * Author: Hans Mikelson
 *
 * Created on November 30, 2014
 */
#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 */

#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 = 0b11101000;  //Set GP0, GP1, GP2 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(10);
    }
}

void tone(int n, int m)
{
    int i;
    for (i=0;i<=m;i++)
    {
     GPIO = 0b00000000;
     vdelay(n);
     GPIO = 0b00010000;
     vdelay(n);
    }
    vdelay(100);
}

void main()
{
    int i,j,k,l,ri,rv,gi,gv,bi,bv;
    signed char ra[7]={1,0,-1,0,0,0,0};

    signed char ga[7]={0,0,0,0,1,0,-1};
    signed char ba[7]={0,0,1,0,-1,0,0};
    rv=0;gv=0;bv=0;
    init();
    GPIO = 0b00000000;
    sGPIO= 0b00000000;
    vdelay(1000);
    tone(40,100);
    tone(40,100);
    tone(40,200);
    tone(40,100);
    tone(40,100);
    tone(40,200);
    tone(40,100);
    tone(34,118);
    tone(50,160);
    tone(45,45);
    tone(40,250);

    while(1)
    {
     for (j=0;j<7;j++)
      {
       for (k=0;k<63;k++)
        {
         ri=ra[j];gi=ga[j];bi=ba[j];
         for(l=0;l<16;l++)
         {
         for (i=0;i<63;i++)
          {
           if (i<rv)
            {
             sGPIO = 0b00000001 | sGPIO;
            }
           else
            {
             sGPIO = 0b11111110 & sGPIO;
            }
           if (i<gv)
            {
             sGPIO = 0b00000010 | sGPIO;
            }
           else
            {
             sGPIO = 0b11111101 & sGPIO;
            }
           if (i<bv)
            {
             sGPIO = 0b00000100 | sGPIO;
            }
           else
            {
             sGPIO = 0b11111011 & sGPIO;
            }
         GPIO = sGPIO;
       }
         }
      rv=rv+ri;gv=gv+gi;bv=bv+bi;
     }
   }
  }
}

Wednesday, July 9, 2014

Saving this from being erased:

Main Blinky . c

#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, GP2, GP4, GP5 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 vdelay(int n)
{
    int i;

    for (i=0;i<n;i++)
     {
      __delay_ms(1);
     }
}

void blink00()
// Blink 4 short pulses v
 {
  GPIO = 0b00000000;
  vdelay(2000);
  GPIO = 0b00000001;
  vdelay(50);
  GPIO = 0b00000000;
  vdelay(2000);
  GPIO = 0b00000010;
  vdelay(50);
  GPIO = 0b00000000;
  vdelay(2000);
  GPIO = 0b00000100;
  vdelay(50);
  GPIO = 0b00000000;
  vdelay(2000);
  GPIO = 0b00010000;
  vdelay(50);
 }

void blink01()
// Cycle through the lights from 0 to 3 v
{
    int i,s,n;
    s = ((rand() & 3)+1)*64;
    n = 512/(s+1);
    for (i=0;i<n;i++)
     {
      GPIO = 0b00000001;
      vdelay(s);
      GPIO = 0b00000010;
      vdelay(s);
      GPIO = 0b00000100;
      vdelay(s);
      GPIO = 0b00010000;
      vdelay(s);
      }
   GPIO = 0b00000000;
   vdelay(s);
}

void blink02()
// Blink light 0-3 from slow to fast v
{
    int i,j,k,n;
    k = 1;
    for (j=0;j<4;j++)
     {
      for (i=0;i<10;i++)
       {
        GPIO = 0b00000000;
        vdelay(200/(i+1));
        GPIO = k;
        vdelay(356/(i+1));
       }
      k=k<<1;
      if (k==8) k=k<<1;
     }
}

void blink03()
// Binary counter 0-32 (note pin 3 is input only) v
{
    int i,j,k,n;
    GPIO = 0b00000000;
    vdelay(100);
    for (i=0;i<10;i++)
    {
     for (j=0;j<32;j++)
      {
       k = j<8?j:j<<2;
       GPIO = j;
       vdelay(300/((i+1)*2));
      }
    }
   GPIO = 0b00000000;
   vdelay(100);

}

void blink04()
// Turn on lights 0-3 sequentially then faster v
{
    int i,j,n,k;
    GPIO = 0b00000000;
    vdelay(100);
    for (i=0;i<50;i++)
    {
     k=1;
     for (j=0;j<4;j++)
      {
       GPIO = k;
       vdelay(500/(i+1));
       k=k<<1;
       if (k==8) k=k<<1;
      }
    }
    GPIO = 0b00000000;
    vdelay(100);
}

void blink05()
// Chaser lights from 0 to 3 then 3 to 0 v
{
    int i,s,n;
    s = ((rand() & 3)+1)<<5;
    n = 1024/(s+1);
    for (i=0;i<n;i++)
     {
      GPIO = 0b00000001;
      vdelay(s);
      GPIO = 0b00000010;
      vdelay(s);
      GPIO = 0b00000100;
      vdelay(s);
      GPIO = 0b00010000;
      vdelay(s);
      GPIO = 0b00000100;
      vdelay(s);
      GPIO = 0b00000010;
      vdelay(s);
      }
   GPIO = 0b00000000;
   vdelay(s);
}

void blink06()
// Random lights constant time
{
 int i,s,n,r;
 s = ((rand() & 7)+1)<<6;
 n = 1024/(s+1)*2;
 for (i=0;i<n;i++)
  {
   r = rand() & 7;
   switch (r)
    {
     case 1:
      GPIO = 0b00000001;
      vdelay(s);
      break;
     case 2:
      GPIO = 0b00000010;
      vdelay(s);
      break;
     case 3:
      GPIO = 0b00000100;
      vdelay(s);
      break;
      case 4:
      GPIO = 0b00010000;
      vdelay(s);
      break;
     case 5:
      GPIO = 0b00010001;
      vdelay(s);
      break;
     case 6:
      GPIO = 0b00000110;
      vdelay(s);
      break;
     default:
      GPIO = 0b00010111;
      vdelay(s);
    }
  }
 GPIO = 0b00000000;
 vdelay(s);
}

void blink07()
// Smooth panning lights
 {
  int i,s,n,j,k,m,p;
  uint8_t sGPIO;
  uint8_t aGPIO[7]={0b00000001,0b00000010,0b00000100,0b00010000,0b00000100,0b00000010,0b00000001};
  s = ((rand() & 7)+1)<<3;
  n = 1023/(s+1);
  sGPIO = 0b00000000;
  for (p=0;p<2;p++)
  {
  for (m=0;m<6;m++)
   {
    for (j=0;j<n;j++)
     {
      k = n-j;
      for (i=0;i<n;i++)
       {
        if (i<k)
         {
          sGPIO = aGPIO[m] | sGPIO;
          GPIO = sGPIO;
          __delay_us(1);
         }
        if (i>k)
         {
          sGPIO = !aGPIO[m] & sGPIO;
          GPIO = sGPIO;
          __delay_us(1);
         }
        if (i<j)
         {
          sGPIO = aGPIO[m+1] | sGPIO;
          GPIO = sGPIO;
          __delay_us(1);
         }
        if (i>j)
         {
          sGPIO = !aGPIO[m+1] & sGPIO;
          GPIO = sGPIO;
          __delay_us(1);
         }
       }
     }
   }
  }
 }

void main()
{
    uint8_t db_cnt;
    int i, r;
    init();
    GPIO = 0b00000000;
    while(1)
    {
    r = rand() & 0b00000111;
     switch (r)
      {
       case 1:
        blink01();
        break;
       case 2:
        blink02();
        break;
       case 3:
        blink03();
        break;
       case 4:
        blink04();
        break;
       case 5:
        blink05();
        break;
       case 6:
        blink06();
        break;
       case 7:
        blink07();
        break;
       default:
        blink00();
      }
    }
}

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
    ANSEL =  0b01111000;  //Configure GPIO pins as digital, GP4 Analog

    TRISIO = 0b11011000;  //Set GP1,GP2,GP4,GP5 as outputs
    WPU = 0x00;           //Disable weak pullups
    //Configuer AD Convertor
    ADCON0 = 0b10001101;        //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

    OPTION_REGbits.nGPPU = 0;
    WPU = 1<<1;     //Enable weak pullups on GP1
    //Configuer AD Convertor
    //ADCON0 = 0x00;        //AD disabled
    //ADRESH = 0x00;        //Init the AD Register
        //Configuer AD Convertor

}

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

void vdelay(int n)
 {
  int i;

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

void pwgen(int pw)
// Send pulse width
 {
  int i, n=4;

  if (pw>56)
  {
      pw = 255;
      n=1;
  }
  if (pw<16)
   {
      n = 10;
   }
  for (i=0;i<255;i++)
   {
    if (i<pw)
     {
      GPIO = 0b00000010;
      vdelay(n);
     }
    if (i>pw)
     {
      GPIO = 0b00000000;
      vdelay(n);
     }
   }
 }

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


}

PWMC

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

    }

}

Wednesday, June 4, 2014

Motor Controller Fix

//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
    ANSEL =  0b01111000;  //Configure GPIO pins as digital, GP4 Analog

    TRISIO = 0b11011000;  //Set GP1,GP2,GP4,GP5 as outputs
    WPU = 0x00;           //Disable weak pullups
    //Configuer AD Convertor
    ADCON0 = 0b10001101;        //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

    OPTION_REGbits.nGPPU = 0;
    WPU = 1<<1;     //Enable weak pullups on GP1
    //Configuer AD Convertor
    //ADCON0 = 0x00;        //AD disabled
    //ADRESH = 0x00;        //Init the AD Register
        //Configuer AD Convertor

}

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(400);
     }
    if (i>pw)
     {
      GPIO = 0b00000000;
      __delay_us(400);
     }
   }
 }

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

}