This code makes digital noise using a PIC microcontroller (12f683).
Three signals are generated: Random noise on GP0, random triggers GP1, and random gates on GP2.
/*
* File: Noise Generator 2 Routine.c
* Author: Hans Mikelson
*
* Created on March 17, 2016, 1 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;
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
OPTION_REGbits.nGPPU = 0;
WPU = 0b00000100; //Enable weak pullups=1
//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
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);
}
}
void scan_eyes(int n, int m, uint8_t led1, uint8_t led2) // 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<m;k++)
{
for (j=0;j<n;j++)
{
for (i=0;i<n;i++)
{
if (i>pw1)
{
sGPIO = sGPIO & ~led1;
}
else
{
sGPIO = sGPIO | led1;
}
if (i>pw2)
{
sGPIO = sGPIO & ~led2;
}
else
{
sGPIO = sGPIO | led2;
}
GPIO = sGPIO;
}
pw1 = pw1 + i1;
pw2 = pw2 + i2;
}
i3 = i1; i1 = i2, i2 = i3;
}
}
void noise0(uint8_t r3) // Pulse noise with different frequencies
{
int r1;
r1=rand() | 0b11111110;
sGPIO = GPIO;
//sGPIO = (sGPIO & 0b11111101);
sGPIO = (sGPIO | 0b00000001) & r1;
if (gr1<=0)
{
sGPIO = (sGPIO & 0b11101111);
}
if (gr2<=0)
{
gr0=(rand() & r3)+11;
gr1=(rand() & r3)+11;
gr2=gr1+gr0;
gr3=10;
gi3=-1;
sGPIO = (sGPIO | 0b00010010);
}
gr2--;
gr1--;
if (gr3==0)
{
gi3=0;
sGPIO = (sGPIO & 0b11111101);
}
gr3+=gi3;
GPIO = sGPIO;
}
void noise1(int r3) // Pulse noise
{
int r1;
r1=rand() | 0b11111110;
sGPIO = GPIO;
sGPIO = (sGPIO | 0b00000001) & r1;
if (gr1<=0)
{
sGPIO = (sGPIO & 0b11101111);
}
if (gr2<=0)
{
gr0=(rand() & gr4)+11;
gr1=(rand() & gr4)+11;
gr2=gr1+gr0;
gr3=10;
gi3=-1;
sGPIO = (sGPIO | 0b00010010);
}
gr2--;
gr1--;
if (gr3==0)
{
gi3=0;
sGPIO = (sGPIO & 0b11111101);
}
gr3+=gi3;
GPIO = sGPIO;
}
void main()
{
uint8_t r, d=20, rp=3;
init();
gi=0;
gi3=0; gr3=10;
GPIO = 0b00000000;
gr0=(rand() & 0b1111111111)+11;
gr1=(rand() & 0b1111111111)+11;
gr2=gr1+gr0;
scan_eyes(d,2,2,1);
while(1)
{
switch (gi)
{
case 0:
noise0(1);
break;
case 1:
noise1(rand());
break;
default:
scan_eyes(d,2,1,2);
}
}
}
void interrupt tc_int (void)
{
if (INTCONbits.INTF==1)
{
INTCONbits.INTF = 0;
gi=(gi+1)%3;
vdelay(100);
switch (gi)
{
case 0:
gi3=0; gr3=10;
GPIO = 0b00000000;
gr0=(rand() & 0b1111111111)+11;
gr1=(rand() & 0b1111111111)+11;
gr2=gr1+gr0;
break;
case 1:
gi3=0; gr3=10;
GPIO = 0b00000000;
gr4 = rand();
gr0=(rand() & gr4)+11;
gr1=(rand() & gr4)+11;
gr2=gr1+gr0;
break;
default:
GPIO = 0b00000000;
}
}
}
Showing posts with label microcontroller. Show all posts
Showing posts with label microcontroller. Show all posts
Sunday, April 3, 2016
Saturday, March 26, 2016
Interrupt Generator and Curve Tracer
Just putting this code up so I can easily access it when I want to. The curve tracer is an example of using PWM, the second file is an example of using an external interrupt. This is for a 12f683 device. I am working on putting this code into my mini-modular synthesizer. I have a module that is based on the PIC12f683 that generates noise and random events.
/*
* File: CurveTracerMain.c
* Author: Hans Mikelson
*
* Created on March 17, 2016, 12:34 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 8000000
uint8_t sGPIO;
int gi;
void init()
{
//Configure GPIO Port
ANSEL = 0b00000000; //Configure all GPIO pins as digital
TRISIO = 0b11001100; //Set GP# 1=inputs and 0=outputs
OPTION_REGbits.nGPPU = 0;
WPU = 0b00100100; //Enable weak pullups=1
//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
//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);
}
}
void main()
{
uint8_t r, d, rp=3, sPIR1, sCCP1CON, i=0, j=0, sCCPR1L, sGPIO; // ADC_result2;;
init();
gi=0;
GPIO = 0b00000000;
TRISIO = 0b11101100; //Disable IO2
PR2 = 0x0F;
CCP1CON = 0b00111100;
CCPR1L = 16; // 1,4,16
sPIR1 = PIR1 & 0b11111101; //Clear TMR2IF
PIR1 = sPIR1;
T2CON = 0b00000101;
while(1)
{
//Update PWM
if (PIR1bits.TMR2IF == 1)
{
i=(i+1)%0x0F;
TRISIO = 0b11101000;
CCPR1L = i;
}
if (i == 0x00)
{
j=(j+1)%4;
sGPIO = GPIO;
sGPIO = ((sGPIO & 0b11111100) | j);
GPIO = sGPIO;
}
}
}
/*
* File: CurveTracerMain.c
* Author: Hans Mikelson
*
* Created on March 17, 2016, 12:34 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 8000000
uint8_t sGPIO;
int gi;
void init()
{
//Configure GPIO Port
ANSEL = 0b00000000; //Configure all GPIO pins as digital
TRISIO = 0b11001100; //Set GP# 1=inputs and 0=outputs
OPTION_REGbits.nGPPU = 0;
WPU = 0b00100100; //Enable weak pullups=1
//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
//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);
}
}
void main()
{
uint8_t r, d, rp=3, sPIR1, sCCP1CON, i=0, j=0, sCCPR1L, sGPIO; // ADC_result2;;
init();
gi=0;
GPIO = 0b00000000;
TRISIO = 0b11101100; //Disable IO2
PR2 = 0x0F;
CCP1CON = 0b00111100;
CCPR1L = 16; // 1,4,16
sPIR1 = PIR1 & 0b11111101; //Clear TMR2IF
PIR1 = sPIR1;
T2CON = 0b00000101;
while(1)
{
//Update PWM
if (PIR1bits.TMR2IF == 1)
{
i=(i+1)%0x0F;
TRISIO = 0b11101000;
CCPR1L = i;
}
if (i == 0x00)
{
j=(j+1)%4;
sGPIO = GPIO;
sGPIO = ((sGPIO & 0b11111100) | j);
GPIO = sGPIO;
}
}
}
/*
* File: Interrupt Test Routine.c
* Author: Hans Mikelson
*
* Created on March 17, 2016, 1 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;
int gi;
void init()
{
//Configure GPIO Port
ANSEL = 0b00000000; //Configure all GPIO pins as digital
TRISIO = 0b11001100; //Set GP# 1=inputs and 0=outputs
OPTION_REGbits.nGPPU = 0;
WPU = 0b00100100; //Enable weak pullups=1
//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
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);
}
}
void scan_eyes(int n, int m, uint8_t led1, uint8_t led2) // 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<m;k++)
{
for (j=0;j<n;j++)
{
for (i=0;i<n;i++)
{
if (i>pw1)
{
sGPIO = sGPIO & ~led1;
}
else
{
sGPIO = sGPIO | led1;
}
if (i>pw2)
{
sGPIO = sGPIO & ~led2;
}
else
{
sGPIO = sGPIO | led2;
}
GPIO = sGPIO;
}
pw1 = pw1 + i1;
pw2 = pw2 + i2;
}
i3 = i1; i1 = i2, i2 = i3;
}
}
void main()
{
uint8_t r, d=200, rp=3;
init();
gi=0;
GPIO = 0b00000000;
while(1)
{
switch (gi)
{
case 0:
GPIO = 0b00000001;
break;
case 1:
GPIO = 0b00000010;
break;
case 2:
GPIO = 0b00000011;
break;
case 3:
GPIO = 0b00010000;
break;
case 4:
GPIO = 0b00010001;
break;
case 5:
GPIO = 0b00010010;
break;
case 6:
GPIO = 0b00010011;
break;
case 7:
GPIO = 0b00000000;
break;
default:
scan_eyes(d,2,1,2);
}
}
}
void interrupt tc_int (void)
{
if (INTCONbits.INTF==1)
{
INTCONbits.INTF = 0;
gi=(gi+1)%9;
vdelay(100);
}
}
Tuesday, March 31, 2015
Spider 2 w Noise
/*
* 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: 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();
}
}
}
Sunday, February 22, 2015
Chiptune Noisemaker revised
/*
* File: noisegenmain.c
* Author: Hans Mikelson
*
* Created on February 13, 2015, 2:42 PM
* Revised February 22, 2015
* 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 = 0b11000100; //Set GP1-5 as outputs and the rest as inputs
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 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 clicks
{
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();
r=0;
while(1)
{
//r = rand()&7;
if (GPIObits.GP2 == 0)
{
r=(r+1)%7;
vdelay(1000000);
}
r4=rand()&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());
}
}
}
* File: noisegenmain.c
* Author: Hans Mikelson
*
* Created on February 13, 2015, 2:42 PM
* Revised February 22, 2015
* 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 = 0b11000100; //Set GP1-5 as outputs and the rest as inputs
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 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 clicks
{
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();
r=0;
while(1)
{
//r = rand()&7;
if (GPIObits.GP2 == 0)
{
r=(r+1)%7;
vdelay(1000000);
}
r4=rand()&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());
}
}
}
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);
}
}
#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);
}
}
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;
}
}
}
// 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;
}
}
}
Subscribe to:
Posts (Atom)