#define DUR 500
3v0 i think i finally understand that Multitasking stuff a bit thanks to your PDF. But i noticed some error or something you forgot to edit..
For instance check out page 8 on the left side the line of code:
Where since KMAX is thought of as (per your example) to be 3. should that have not been a "2" like in the appendix code?Code:#define DUR 500
heh my help was small yours help is better heh thanks. I understand it more now.I am glad you are enjoying it.So it would be best to cut my code into sections like you did with the blink ...
You need to cut the code into sections/states. If you do not tasks will not run on time. That is the big pain with this sort of system. But it is not that hard to do when you understand it.Like when sending characters send about 2 to lcd then run back to main and then if free time then it will resume until all characters are sent. since this is fast most likey it shouldnt bother.
The LCD is not a fast device. LCD drivers generally either delay after each character or wait for the LCD to indicate it is ready for the next. In a multitasking system we NEVER wait or delay. Instead we yield to the next task. If your code needs to wait or delay end the state instead. If it is a delay set a kTimer to allow the the task to run after the time is up. If you are waiting setup some code to let the task run after the flag is set.Also instead of 2 characters maybe around 5 at a time before yielding and checking if another item needs time.
You need to write one char in the state then yield. That allows other tasks to run while the LCD is doing its job. That is where this type of multitasking shines. Instead of wasting the time waiting we are off doing other tasks.
As I said above write just the one character in the state.
#include <p18cxxx.h>
#include <timers.h>
#define NUMBER_OF_LEDS 8
long int counter = 0;
/* 6 */ void timer_isr (void);
/* 7 */
/* 8 */ static unsigned char s_count = 0;
/* 9 */
/* 10 */ #pragma code low_vector=0x18
/* 11 */ void low_interrupt (void)
/* 12 */ {
/* 13 */ _asm GOTO timer_isr _endasm
/* 14 */ }
/* 15 */
/* 16 */ #pragma code
/* 17 */
/* 18 */ #pragma interruptlow timer_isr save=PROD
/* 19 */ void
/* 20 */ timer_isr (void)
/* 21 */ {
/* 22 */ static unsigned char led_display = 0;
/* 23 */
/* 24 */ INTCONbits.TMR0IF = 0;
counter += 1;
/* 25 */
/* 26 */ s_count = s_count % (NUMBER_OF_LEDS + 1);
/* 27 */
/* 28 */ led_display = (1 << s_count++) - 1;
/* 29 */
/* 30 */ PORTB = led_display;
/* 31 */ }
/* 32 */
/* 33 */ void
/* 34 */ main (void)
/* 35 */ {
/* 36 */ TRISB = 0;
/* 37 */ PORTB = 0;
/* 38 */
/* 39 */ OpenTimer0 (TIMER_INT_ON & T0_SOURCE_INT & T0_16BIT);
/* 40 */ INTCONbits.GIE = 1;
/* 41 */
/* 42 */ while (1)
/* 43 */ {
/* 44 */ }
/* 45 */ }
/*
* Junebug Demo
* Purpose: Demonstrate cooporative multitasking
* using only the Junebug LED display
*
* File: DJ_coop.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 byte unsigned char
#define TRUE 1
#define FALSE 0
#define LED_PATTERN_MAX 4
#define INITIAL_PATTERN_ID 2
#define COUNTER_MAX 3
#define C_DEBOUNCE 0
#define C_DELAY0 1
#define setCounter(X,Y) counter[X] = Y
#define SET_LATA(val,mask) lata_=(lata_&~mask)|(val&mask)
#define SET_TRISA(val,mask) trisa_=(trisa_&~mask)|(val&mask)
//
// global vars
//
// counters triggered by timer0
unsigned int counter[COUNTER_MAX];
byte ledPatternChanged;
byte seenTimerInterrupt;
#include "LED.c"
//
// functions
//
// Update counters and set seenTimerInterrupt flag
void interrupt(void)
{
byte cNum;
intcon_.TMR0IF = 0; // always clear the IF
for (cNum=0; cNum<COUNTER_MAX; cNum++)
{
if (counter[cNum] != 0) // hold at 0
{
counter[cNum]--;
}
}
seenTimerInterrupt = TRUE;
}
// ---
// Read portb and determine switch settings
byte readSwitches(void)
{
byte readVal,switchVal;
readVal = portb_;
switchVal = 0;
if (!(readVal & 0x01)) switchVal += 0x01;
if (!(readVal & 0x04)) switchVal += 0x02;
if (!(readVal & 0x20)) switchVal += 0x04;
return switchVal;
}
// ---
// Determine led pattern base on switches
byte setPattern(byte switchVal)
{
static byte ledPatternId_ = INITIAL_PATTERN_ID;
ledPatternChanged = TRUE;
if (switchVal==1)
{
ledPatternId_=0;
}
else if ((switchVal==2) && (ledPatternId_<LED_PATTERN_MAX))
{
++ledPatternId_;
}
else if ((switchVal==4) && (ledPatternId_>0))
{
--ledPatternId_;
}
else
{
ledPatternChanged = FALSE;
}
return ledPatternId_;
}
//
// ---
//
void main (void)
{
byte switchVal = 4;
byte i, ledPatternId;
// sundry setup
{
// speed up the clock to 8MHz, 18F1320
osccon_.IRCF0=1;
osccon_.IRCF1=1;
osccon_.IRCF2=1;
// configure ports
adcon1_ = 0xFF; // all digital
intcon2_.RBPU = 0; // PORTB weak pullups on
trisb_ = 0xFF; // all inputs
lata_ = 0;
trisa_ = 0; // all outputs
// configure Timer0
t0con_ = 0xD0;
intcon_.TMR0IF = 0; // clear the IF
intcon_.TMR0IE = 1; // Enable Timer0 overflow interrupt
t0con__.TMR0ON = 1;
intcon_.GIE = 1;
}
// general init
{
ledScan = 1;
ledIdx = 0;
seenTimerInterrupt = FALSE;
ledBits = 0x3F;
ledPatternId = INITIAL_PATTERN_ID;
ledPatternChanged = TRUE;
// zero out the counters
for (i=0; i<COUNTER_MAX;i++) counter[i] = 0;
}
while (1)
{
ledPatternChanged = FALSE;
// task1 - turn on the correct LEDs
serviceLEDs();
// task2 - read the switches to select correct LED action
if ((!counter[C_DEBOUNCE]) && (switchVal=readSwitches()))
{
setCounter(C_DEBOUNCE,1000);
ledPatternId = setPattern(switchVal);
}
// task3 - animate the LED display
//for testing
ledPatternId = 4;
switch (ledPatternId)
{
case 0: // turn off the display
ledBits = 0;
break;
case 1: // 1st LED to blinks
blinkLed();
break;
case 2: // alternate 135 and 246
alternateLEDs();
break;
case 3: // 2 LEDs left to right
marchLEDs();
break;
case 4: // 2 LEDs left to right and back
marchBackLEDs();
break;
}
{ // option sychronization
#ifdef sync_loop
while(! seenTimerInterrupt); // wait for timer interrupt
seenTimerInterrupt = FALSE;
#endif
}
}
}
#pragma code low_vector=0x18
#pragma code low_vector=0x08
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?