Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
/*
* Junebug Demo
* Purpose: Demonstrate cooporative multitasking
* using 1 Junebug LED + AC coupled speaker on RB1
*
* File: DJ_coop2.c
* Software: BoostC or Microchip C18 compilers
* Hardware: Junebug (or other debugger + breadboarded circuit)
*
* by Daniel Johnson
* July 2008
*/
#include "multiCompiler.h"
//
// defines & macros
//
#define TRUE !0
#define FALSE 0
#define byte unsigned char
#define uInt unsigned int
#define state case
#define KMAX 3
#define BLINK 0
#define BEEP 1
#define DUR 2
#define TASK_BLOCKED 0xFFFF
#define LED_BIT_HI porta_=0b00000001
#define LED_BIT_LO porta_=0b00000000
#define SPEAKER_BIT_HI portb_=0b00000010
#define SPEAKER_BIT_LO portb_=0b00000000
// allocate KMAX kTimers
uInt kTimer[KMAX];
byte waitForTimer;
void interrupt(void)
{
byte k;
// clear the IF
intcon_.TMR0IF = 0;
for (k=0;k<KMAX;k++)
{
// hold at 0
if((kTimer[k]) && // stop at zero
(kTimer[k]!=TASK_BLOCKED)) // task is blocked
{
kTimer[k]--;
}
}
waitForTimer = FALSE;
}
void taskBlink(void)
{
static byte seq=0;
switch (seq)
{
state 0:
LED_BIT_HI;
seq=1;
break;
state 1:
LED_BIT_LO;
seq=0;
}
// set when to run again
kTimer[BLINK]=1953;
return;
}
void taskBeep(void)
{
static byte seq=0;
switch (seq)
{
state 0: // state INIT
kTimer[DUR]=750;
seq=1;
break;
state 1: // state ON
SPEAKER_BIT_HI;
kTimer[BEEP]=2; // 1000 Hz
seq=2;
break;
state 2: // state OFF
SPEAKER_BIT_LO;;
if(kTimer[DUR]) // another pulse
{
kTimer[BEEP]=2;
seq=1;
}
else // generate a rest
{
kTimer[BEEP]=750;
seq=0;
}
}
}
//
// --- main ---
//
void main (void)
{
byte i;
// setup
{
// speed up the clock to 8MHz, 18F1320
osccon_.IRCF0=1;
osccon_.IRCF1=1;
osccon_.IRCF2=1;
// configure ports
adcon1_ = 0xFF; // all digital
// Switches on RB0 RB2 and RB5
//intcon2_.RBPU = 0; // PORTB weak pullups for switches
trisb_ = 0xFD; // speaker on RB1
lata_ = 0;
trisa_ = 0xBE; // RA0 and RA6 ouputs 0b1011 1110;
// configure Timer0
t0con_ = 0xD0;
intcon_.TMR0IF = 0; // clear the IF
intcon_.TMR0IE = 1; // enable TMR0 overflow interrupt
t0con__.TMR0ON = 1; // turn on TMR0
intcon_.GIE = 1; // enable global interrupts
// zero out the counters
for (i=0; i<KMAX;i++)
{
kTimer[i] = 0;
}
}
// kTimer[BEEP] = TASK_BLOCKED; // disable taskBeep
while(1) // main loop
{
if (!kTimer[BLINK])
{
taskBlink();
}
if (!kTimer[BEEP])
{
taskBeep();
}
while(waitForTimer);
waitForTimer = TRUE;
}
}
/*
* Junebug Demo
* Purpose: Demonstrate cooporative multitasking
*
* File: multiCompiler.h
* Software: BoostC or Microchip C18 compilers
* Hardware: Junebug (or other debugger + breadboarded circuit)
*
* These defiles allow the use of more then one compiler.
* Register names ending with a single _ are byte referances.
* Register names ending with a dual __ are bit referances.
*
* by Daniel Johnson
* July 2008
*/
#ifdef _BOOSTC
#define FOUND_COMPILER
#define lata_ lata
#define trisa_ trisa
#define porta_ porta
#define latb_ latb
#define trisb_ trisb
#define portb_ portb
#define intcon_ intcon
#define intcon2_ intcon2
#define osccon_ osccon
#define t0con_ t0con
#define t0con__ t0con
#define adcon1_ adcon1
#include <system.h>
#pragma CLOCK_FREQ 8000000
#pragma DATA _CONFIG1H, _INTIO2_OSC_1H
#pragma DATA _CONFIG2H, _WDT_OFF_2H
#pragma DATA _CONFIG3H, _MCLRE_ON_3H
#pragma DATA _CONFIG4L, _LVP_OFF_4L
#endif
// MCC18
#ifdef __18CXX
#define FOUND_COMPILER
#define lata_ LATA
#define trisa_ TRISA
#define porta_ PORTA
#define latb_ LATB
#define trisb_ TRISB
#define portb_ PORTB
#define intcon_ INTCONbits
#define intcon2_ INTCON2bits
#define osccon_ OSCCONbits
#define t0con__ T0CONbits
#define t0con_ T0CON
#define adcon1_ ADCON1
#pragma config OSC = INTIO2, WDT = OFF, LVP = OFF
#include <p18f1320.h>
// code to make C18 interrupt look like BoostC's
void interrupt(void);
#pragma code low_vector=0x18
void low_interrupt (void)
{
_asm GOTO interrupt _endasm
}
#pragma code
#pragma interruptlow interrupt
#endif
#ifndef FOUND_COMPILER
error: unknown compiler
#endif
#define SPEAKER_BIT_HI portb_=0b00000010
#define SPEAKER_BIT_LO portb_=0b00000000
// configure Timer0
switch (seq)
I just setting here making this up as I go LOl mine did the same thing his didI would be glad to help you get it working if you are using the tutorial.
#ifdef _BOOSTC
#define FOUND_COMPILER
#define lata_ lata
#define trisa_ trisa
#define porta_ porta
#define latb_ latb
#define trisb_ trisb
#define portb_ portb
#define intcon_ intcon
#define intcon2_ intcon2
#define osccon_ osccon
#define t0con_ t0con
#define t0con__ t0con
#define adcon1_ adcon1
#include <system.h>
[COLOR="Red"]#pragma CLOCK_FREQ 8000000[/COLOR]
#pragma DATA _CONFIG1H, _INTIO2_OSC_1H
#pragma DATA _CONFIG2H, _WDT_OFF_2H
#pragma DATA _CONFIG3H, _MCLRE_ON_3H
#pragma DATA _CONFIG4L, _LVP_OFF_4L
#endif
#ifdef __18CXX
#define FOUND_COMPILER
#define lata_ LATA
#define trisa_ TRISA
#define porta_ PORTA
#define latb_ LATB
#define trisb_ TRISB
#define portb_ PORTB
#define intcon_ INTCONbits
#define intcon2_ INTCON2bits
#define osccon_ OSCCONbits
#define t0con__ T0CONbits
#define t0con_ T0CON
#define adcon1_ ADCON1
#pragma config OSC = INTIO2, FSCM = OFF, WDT = OFF, LVP = OFF
#include <p18f1320.h>
#define osccon_ OSCCONbits
OSCCON = 0x72;
// speed up the clock to 8MHz, 18F1320
osccon_.IRCF0=1;
osccon_.IRCF1=1;
osccon_.IRCF2=1;
#pragma config OSC = INTIO2, WDT = OFF, LVP = OFF
/*
* Junebug Demo
* Purpose: Demonstrate cooporative multitasking
*
* File: multiCompiler.h
* Software: BoostC or Microchip C18 compilers
* Hardware: Junebug (or other debugger + breadboarded circuit)
*
* These defiles allow the use of more then one compiler.
* Register names ending with a single _ are byte referances.
* Register names ending with a dual __ are bit referances.
*
* by Daniel Johnson
* July 2008
*/
#ifdef _BOOSTC
#define FOUND_COMPILER
#define lata_ lata
#define trisa_ trisa
#define porta_ porta
#define latb_ latb
#define trisb_ trisb
#define portb_ portb
#define intcon_ intcon
#define intcon2_ intcon2
#define osccon_ osccon
#define t0con_ t0con
#define t0con__ t0con
#define adcon1_ adcon1
#include <system.h>
#pragma CLOCK_FREQ 8000000
#pragma DATA _CONFIG1H, _INTIO2_OSC_1H
#pragma DATA _CONFIG2H, _WDT_OFF_2H
#pragma DATA _CONFIG3H, _MCLRE_ON_3H
#pragma DATA _CONFIG4L, _LVP_OFF_4L
#endif
// MCC18
#ifdef __18CXX
#define FOUND_COMPILER
#define lata_ LATA
#define trisa_ TRISA
#define porta_ PORTA
#define latb_ LATB
#define trisb_ TRISB
#define portb_ PORTB
#define intcon_ INTCONbits
#define intcon2_ INTCON2bits
#define osccon_ OSCCONbits
#define t0con__ T0CONbits
#define t0con_ T0CON
#define adcon1_ ADCON1
#pragma config OSC = INTIO2, WDT = OFF, LVP = OFF
#include <p18f1320.h>
// code to make C18 interrupt look like BoostC's
void interrupt(void);
#pragma code low_vector=0x18
void low_interrupt (void)
{
_asm GOTO interrupt _endasm
}
#pragma code
#pragma interruptlow interrupt
#endif
#ifndef FOUND_COMPILER
error: unknown compiler
#endif
/*
* Junebug Demo
* Purpose: Demonstrate cooporative multitasking
* using 1 Junebug LED + AC coupled speaker on RB1
*
* File: DJ_coop2.c
* Software: BoostC or Microchip C18 compilers
* Hardware: Junebug (or other debugger + breadboarded circuit)
*
* by Daniel Johnson
* July 2008
*/
#include "multiCompiler.h"
//
// defines & macros
//
#define TRUE !0
#define FALSE 0
#define byte unsigned char
#define uInt unsigned int
#define state case
#define KMAX 3
#define BLINK 0
#define BEEP 1
#define DUR 2
#define TASK_BLOCKED 0xFFFF
#define LED_BIT_HI porta_=0b00000001
#define LED_BIT_LO porta_=0b00000000
#define SPEAKER_BIT_HI portb_=0b00000010
#define SPEAKER_BIT_LO portb_=0b00000000
// allocate KMAX kTimers
uInt kTimer[KMAX];
byte waitForTimer;
void interrupt(void)
{
byte k;
// clear the IF
intcon_.TMR0IF = 0;
for (k=0;k<KMAX;k++)
{
// hold at 0
if((kTimer[k]) && // stop at zero
(kTimer[k]!=TASK_BLOCKED)) // task is blocked
{
kTimer[k]--;
}
}
waitForTimer = FALSE;
}
void taskBlink(void)
{
static byte seq=0;
switch (seq)
{
state 0:
LED_BIT_HI;
seq=1;
break;
state 1:
LED_BIT_LO;
seq=0;
}
// set when to run again
kTimer[BLINK]=1953;
return;
}
void taskBeep(void)
{
static byte seq=0;
switch (seq)
{
state 0: // state INIT
kTimer[DUR]=750;
seq=1;
break;
state 1: // state ON
SPEAKER_BIT_HI;
kTimer[BEEP]=2; // 1000 Hz
seq=2;
break;
state 2: // state OFF
SPEAKER_BIT_LO;;
if(kTimer[DUR]) // another pulse
{
kTimer[BEEP]=2;
seq=1;
}
else // generate a rest
{
kTimer[BEEP]=750;
seq=0;
}
}
}
//
// --- main ---
//
void main (void)
{
byte i;
// setup
{
// speed up the clock to 8MHz, 18F1320
osccon_.IRCF0=1;
osccon_.IRCF1=1;
osccon_.IRCF2=1;
// configure ports
adcon1_ = 0xFF; // all digital
// Switches on RB0 RB2 and RB5
//intcon2_.RBPU = 0; // PORTB weak pullups for switches
trisb_ = 0xFD; // speaker on RB1
lata_ = 0;
trisa_ = 0xBE; // RA0 and RA6 ouputs 0b1011 1110;
// configure Timer0
t0con_ = 0xD0;
intcon_.TMR0IF = 0; // clear the IF
intcon_.TMR0IE = 1; // enable TMR0 overflow interrupt
t0con__.TMR0ON = 1; // turn on TMR0
intcon_.GIE = 1; // enable global interrupts
// zero out the counters
for (i=0; i<KMAX;i++)
{
kTimer[i] = 0;
}
}
// kTimer[BEEP] = TASK_BLOCKED; // disable taskBeep
while(1) // main loop
{
if (!kTimer[BLINK])
{
taskBlink();
}
if (!kTimer[BEEP])
{
taskBeep();
}
while(waitForTimer);
waitForTimer = TRUE;
}
}
That may be it because it was saving back to the wrong file at first so i deleted it and put just one copy on C:\ the program is running at snail speed LOL got meMPLAB is not the most stable IDE in the world. Check to see if you are using the fuses from the program or from the IDE. It may not be saving or reading that as part of the project.
If using the fuses from the program fixes it we have found the problem.