Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C18 Questions

Status
Not open for further replies.
Thanks for the tip.

How different is Hitech C? Im finding a lot of stuff on that.
 
Last edited:
Theres a program structure that I often see in small game programming, and I wonder if it would work well with pics, and how it could be done.

After all the setup and configuration a timer is started to call all the functions that need to repeat on a regularly timed interval. Since PICs are limited to only a few timers this would have to be modified to allow different frequencies of calling. I was thinking it could be done by having the timer call a function, withen it would be several entries like this, but for different frequencies of call:

(pseudo code)

called by timer, how often depends on whats a reasonable load

Count1000ms += 1 (or instead of 1, whatever represents the number of ms per call)
If Count1000ms >= 1000 then
Call All 1000msFuntions
Count1000ms = 0
end if

same thing for all the other frequencies needed

(end pseudo code)

Im thinking this isnt a practical way to manage program timing on a PIC since no one seems to do it this way. But I cant see where the problem would be. I'll try it out when i figure out how to set up the timer.
 
Last edited:
having trouble getting a response from this demo

I'm trying to use the multitasking demo from your site, so far all I've done is created a project, pasted the code, and programmed it on to the chip. For those who aren't familiar with the program it is below. This is the one called DJ_coop2.c. When the junebug is started up, it lights the leftmost led and that's it, it stays on. I have tried it in debug and release mode under build configuration, and the switches on the junebug are all on except for 6.

What part of this code starts the timer? is it part of multiCompiler.h?

Code:
/* 
 * 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;
  }
}

multicompiler.h:
Code:
/* 
 * 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
 
Last edited:
That code is for both C18 and BoostC so in your main code did you define which your using I may be wrong but in your main.C or what you called it should have
#define __18CXX or the multicompiler.h wouldn't have ifdef _BOOSTC and
ifdef __18CXX
Ask 3v0 and see what he tells you i've never really done it with multicompiler but I have aways had to say hay I going to use A or B with a ifdef in a module
 
Last edited:
save thing happen for me i built it and just the left led came on
 
I run this program with switches 1-3 on as no others are required.

Start with either the C18 or BoostC project. The program compiles as expected with the target chip set to 18F1320. You do not need to modify the code,

This program runs two tasks at the same time. The first task blinks the LED nearest the USB connector. The second beeps a speaker connected as indicated in the tutorial.

This demo requires a small speaker like that used in a PC. This bit of code
Code:
#define SPEAKER_BIT_HI portb_=0b00000010
#define SPEAKER_BIT_LO portb_=0b00000000
tells you the speaker should be connected to PORTB bit 1 (2nd bit). The tutorial text tells you a 47uF capacitor should be used. The capacitor AC couples the speaker to the PIC.

gnd -----(speaker)----)|--------- RB1

RB1 is on the connector next to the USB.

I tried both C18 and BoostC. In both cases the LED blinked while the code was running.

The timer is configured in main by the code following
Code:
    // configure Timer0
The header file multiCompiler.h is used to create a set of common register names for both compilers. It also has some trivial code t0 unify interrupt handling.

Give it another try and if it does not blink the LED I can talk about what might be wrong.

3v0
 
Like i said I wasn't sure you had to set what compiler it builds fine but I don't think it set the chip. I no I loaded it with mplab and i got the one led on. What i did was open the pickit2 and load it with it and it said this hex doesn't have config bits set right for your chip take a look and see if this tells you any thing
 

Attachments

  • test1.PNG
    test1.PNG
    57.2 KB · Views: 129
  • test2.PNG
    test2.PNG
    42.7 KB · Views: 126
Last edited:
Thanks for all the help. My student version of MPLAB keeps locking up, even before I open a project, so I'm going to reinstall it. Who knows, that may fix the problem I'm having with the junebug too. Once I get home and get time to work on this, and apply your advice if the reinstal doesnt fix it, I'll let you know how it goes.
 
Triode:
The only thing odd that I could find is that the PICkit2 software said the HEX had LVP enabled when the C source code has it disabled.

The code is about a year old and this is the first time I have had anyone report this problem. I am not saying I write perfect code, the problem is where it is.

Regardless of the cause I would be happy to help you.

The code can be run under the Junebug debugger or with MPLAB SIM. To halt execution on each state change you can set a breakpoint inside each task on the switch statment

Code:
  switch (seq)
Burt:
We can also look at the fuse settings in MPLAB. There is not much point in running the code using the PICkit2 software because the code interaction is best seen with a debugger.

I would be glad to help you get it working if you are using the tutorial.

3v0
 
I would be glad to help you get it working if you are using the tutorial.
I just setting here making this up as I go LOl mine did the same thing his did
I tried the Hex from the down load and it works fine. But I'm done with this 3v0
I read your tutorial set it like you showed I no it worked for you and it will for me there some
reason it's not setting the chip up right it may be that I'm using mplab 8.33 now only reason I use the pickit2 is I like the way it works I no you can use mplab to I just like the pickit2 to look at the hex the one that came with the down load is not the same as whats being built.
How this for output
It no biggie I'll get it to work when I have more time thanks 3v0
 

Attachments

  • test3.PNG
    test3.PNG
    40.9 KB · Views: 140
Last edited:
Triode you have one led come on ans stay on that's whats happen with me I'm using 8.33 and I can tell you this for sure the osc is not set to run at full speed if you try the hex that was in the down load it works when I build it my self it doesn't set the osc now if you look at the red part it's set for boostC you can see it is set for 8 mhz
Code:
#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
here for C18 where is it set I tried it in the main code didn't work it not set here
it's pointing some where else
Code:
#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>
You can see the #define for it but I can't fine where it was set
Code:
#define osccon_   OSCCONbits
fine that part and it will blink. where the
Code:
 OSCCON = 0x72;
the code running at snail speed LOL
If you build with BoostC it works fine
 
Last edited:
haha, well I still haven't been able to try any of this since my first post about this code last night, I'm still at work. But I'm sure in a few hours once I give it a try we can get this working. I never have gotten timers figured out, so thats really what I'm working on here. The multitasking method makes perfect sense to me.
 
Burt,

The internal clock speed is set by these lines

Code:
    // speed up the clock to 8MHz, 18F1320
    osccon_.IRCF0=1;   
    osccon_.IRCF1=1;
    osccon_.IRCF2=1;
I have verified they are working by commenting them in and out to produce various clock speeds.

Perhaps you are building with the " Configuration bits set in code" box unchecked. It can be found under Configuration>Configuration_Bits
If that is the case you are over riding the fuse settings in the code.

Code:
 #pragma    config OSC = INTIO2, WDT = OFF, LVP = OFF
 
I didn't change any thing at all till the led didn't flash and I put that back like it was but maybe this is It also said my compiler is not the same as what was use for this project
 

Attachments

  • maybe.PNG
    maybe.PNG
    11.3 KB · Views: 130
  • maybe1.PNG
    maybe1.PNG
    11.8 KB · Views: 128
Last edited:
MPLAB 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.

EDIT: Be sure to do a build all.
 
3v0 look at this and see if it the same as you have it the multiCompiler.h this is what I'm using
Code:
/* 
 * 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
And here the
Code:
/* 
 * 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;
  }
}
 
MPLAB 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.
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 me
 
Last edited:
To test for this problem I downloaded the .zip file from my site, uncompressed it and loaded the project in MPLAB. We should have the same bits.

I replaced my files with the code you just posted and it works also.

At this point I am 90% sure the problem is not with the code. For me that is a fairly high degree of confidence. Given that the HEX bits I included work and the ones you build do not the problem has to be in your build environment.

Maybe one of the other members who use C18 and MPLAB could give it a go.

3v0
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top