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.

Can't access registers of dsPIC by name

Status
Not open for further replies.

Cantafford

Member
I have a stupid question but I just can't figure this out. It's probably something really simple that I'm omitting.

I'm writing a simple program using a dsPIC30F and I'm trying to configure the Timer1 module.
When I try to write a value to a register(say 'TON=1' to start timer1) I get the following error:

main.c:16: error: 'TON' undeclared (first use in this function)


Basically my code does not recognize the registers by it's name. I tried T1CON.TON still nothing. And this goes for all registers not only the ones in T1CON.
I have in my header: "#include <p30Fxxxx.h>" that's what MPLAB put in there I'm assuming it belongs to the dsPIC30F family. So what is the problem why aren't my registers name recognized?
 
I have always assumed you were using C18 compiler..... XC8 has merged with C18 and Hi-tech...

XC16 however isn't a complete conversion of C30... There are many subtle differences..

Which compilers are you using?? Once I know this, I can help you better!!
 
Yes, indeed it was a compiler problem I think.
The dsPIC v3.20b that I used for the project did not recognize the '#include <xc.h>' and it did not recognize the registers by their name.
So I changed the compiler to XC16 and now it does recognize the library and the registers name(though I'm not sure if programming dsPIC's with that compiler is a good ideea, I hope I'm wrong).

But now I'm getting an error that I have no ideea what it is. This is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
#include <xc.h>

void main()
{
    TRISB = 0;
    T1CON.TON = 1;
    while(1)
    {}
}

When running this I get this error:
main.c:17:10: error: request for member 'TON' in something not a structure or union
 
In your "header.h" you will be duplicating stuff.. Just use their library..

If you use XC16 then you can use just TON otherwise use T1CONbits.TON

Do you mean I should just use the header file and not include the xc.h in the main.c?

Yes I'm using XC16 and I tried writting TON it threats it as a variable:
Code:
main.c:16:5: error: 'TON' undeclared (first use in this function)
 
(XC16) I normally use this ...
Code:
void main()
{
    TRISB = 0;
    T1CONbits.TON = 1;
    while(1)
    {}
}
 
Can you please give me an example on this code on what should I do to make it work. I don't understand what you mean by only using xc.h. Don't I also need stdlib and stdio.h?

Code:
#include <stdio.h
#include <stdlib.h>
#include "header.h"
#include <xc.h>


void main()
{
    TRISB = 0;
    T1CON.TON = 1;
    while(1)
    {}
}

This is the header.h:
Code:
// DSPIC30F2010 Configuration Bit Settings

// 'C' source line config statements

#include <p30Fxxxx.h>

 int FOSC __attribute__((space(prog), address(0xF80000))) = 0xFDF0 ;
//_FOSC(
//    XTL &              // Primary Oscillator Mode (XTL)
//    FRC &              // Oscillator Source (Internal Fast RC)
//    CSW_FSCM_OFF       // Clock Switching and Monitor (Sw Disabled, Mon Disabled)
//);
 int FWDT __attribute__((space(prog), address(0xF80002))) = 0x7FFF ;
//_FWDT(
//    WDTPSB_16 &        // WDT Prescaler B (1:16)
//    WDTPSA_512 &       // WDT Prescaler A (1:512)
//    WDT_OFF            // Watchdog Timer (Disabled)
//);
 int FBORPOR __attribute__((space(prog), address(0xF80004))) = 0x7F7C ;
//_FBORPOR(
//    PWRT_OFF &         // POR Timer Value (Timer Disabled)
//    NONE &             // Brown Out Voltage (Reserved)
//    PBOR_OFF &         // PBOR Enable (Disabled)
//    PWMxL_ACT_HI &     // Low-side PWM Output Polarity (Active High)
//    PWMxH_ACT_HI &     // High-side PWM Output Polarity (Active High)
//    RST_IOPIN &        // PWM Output Pin Reset (Control with PORT/TRIS regs)
//    MCLR_DIS           // Master Clear Enable (Disabled)
//);
 int FGS __attribute__((space(prog), address(0xF8000A))) = 0xFFFF ;
//_FGS(
//    GWRP_OFF &         // General Code Segment Write Protect (Disabled)
//    CODE_PROT_OFF      // General Segment Code Protection (Disabled)
//);
 int FICD __attribute__((space(prog), address(0xF8000C))) = 0xFFFF ;
//_FICD(
//    PGD                // Comm Channel Select (Use PGC/EMUC and PGD/EMUD)
//);

#define __XTAL_FREQ 8000000


 #ifndef _XC_H_
#define _XC_H_

#ifdef __XC8
#include <htc.h>
#endif

#endif        //_XC_H
 
Don't I also need stdlib and stdio.h?
Not unless you are using functions within them.... XC.h selects the correct header for the device you are using

XC.h will select the dspicxxxxx.h suitable for your chip... STDIO you will probably never use, as you aren't using standard IO... This is for standard output...ie screen ( the default on embedded micro's is RS232... Which still needs setting up... And Standard input... ditto... RS232 again for key stroke but a file system framework that also needs written!!

The only parts I use is printf / sprintf ..

STDLIB is conversions mainly atoi etc...
 
I tried everything no matter what I do it will still give that error when I try to use the name of the register.
I think it's a compiler problem because when I try to run a code for PIC18F family with XC8 compiler the registers are read fine but when I switch to dsPIC's and I have to use XC16 instead I get that error.
 
I tried everything no matter what I do it will still give that error when I try to use the name of the register.
I think it's a compiler problem because when I try to run a code for PIC18F family with XC8 compiler the registers are read fine but when I switch to dsPIC's and I have to use XC16 instead I get that error.

That's all my fault. T1CON.TON is not correct. You must use T1CONbits.TON (as granddad has shown in post #8).

As to the configurations from your header.h file, MPLAB has a tool which will generate config for you so that you just paste it then. If I remember correctly, it's under Windows->PIC Memory->Configuration bits
 
That's all my fault. T1CON.TON is not correct. You must use T1CONbits.TON (as granddad has shown in post #8).

As to the configurations from your header.h file, MPLAB has a tool which will generate config for you so that you just paste it then. If I remember correctly, it's under Windows->PIC Memory->Configuration bits
Yes I know about the configuration bits :D. The adding the 'bits' word solved the thing. Funny how the simplest things are the most time consuming. Cheers!
 
es I know about the configuration bits :D. The adding the 'bits' word solved the thing. Funny how the simplest things are the most time consuming. Cheers!
It was mentioned in post 5 8 and 12!!
 
Last edited:
Now I wrote this piece of code:
Code:
#include "header.h"
#include <xc.h>

int counter;


void main()
{
    TRISBbits.TRISB0 = 0;
    T1CONbits.TCKPS = 0x03; // prescaler is divide by 256
    T1CONbits.TCS = 0; // timer clock source is from TOSC

    IEC0bits.T1IE = 1; // enable timer1 interrupt
    // enable pher inter

    IPC0bits.T1IP2 = 0; // tmr1 inter priority = 0
    IPC0bits.T1IP1 = 0;
    IPC0bits.T1IP0 = 0;

    INTCON1 = INTCON1 | (1<<15); // enable global interrupts

    T1CONbits.TON = 1; // start the timer!

    TRISBbits.TRISB0 = 0; // b0 as output(led's here)

    while(1){}
}

void interrupt ISR()
{
   if(IEC0bits.T1IE && IFS0bits.T1IF)
   {
      counter++;  //Increment Over Flow Counter
      if(counter==30)
      {
         LATBbits.LATB0 = ~LATBbits.LATB0; // togle bit
         counter=0;  //Reset Counter
      }
      IFS0bits.T1IF = 0; // clear flag
   }
}

And when I compile I get this error:
Code:
error: expected '=', ',', ';', 'asm' or '__attribute__' before 'ISR'

on this line:
Code:
void interrupt ISR()

The syntax looks good to me. Any ideea what's wrong?
 
These chips have a separate interrupt vectors for different interrupts, so you need to do something of that sort:

C:
void __attribute__ ((interrupt)) _TMR1Interrupt(void)

You need to read the XC16 documentation on how to declare interrupts.
 
These chips have a separate interrupt vectors for different interrupts, so you need to do something of that sort:

C:
void __attribute__ ((interrupt)) _TMR1Interrupt(void)

You need to read the XC16 documentation on how to declare interrupts.
Will get right on it, thanks.
 
Ok I read the documentation and wrote this piece of code that is supposed to blink an LED every 1 second:

Code:
/* 
 * File:   main.c
 * Author: Paul
 *
 * Created on May 16, 2016, 11:22 AM
 */


#include "header.h"
#include <xc.h>

int counter;


void main()
{
    TRISBbits.TRISB0 = 0;
    T1CONbits.TCKPS = 0x03; // prescaler is divide by 256
    T1CONbits.TCS = 0; // timer clock source is from TOSC

    IEC0bits.T1IE = 1; // enable timer1 interrupt
    // enable pher inter

    IPC0bits.T1IP2 = 0; // tmr1 inter priority = 0
    IPC0bits.T1IP1 = 0;
    IPC0bits.T1IP0 = 0;

    INTCON1 = INTCON1 | (1<<15); // enable global interrupts

    T1CONbits.TON = 1; // start the timer!

    TRISBbits.TRISB0 = 0; // b0 as output(led's here)

    while(1){}
}

void _ISR _INT1Interrupt(void)
{
   if(IEC0bits.T1IE && IFS0bits.T1IF)
   {
      counter++;  //Increment Over Flow Counter
      if(counter==30)
      {
         LATBbits.LATB0 = ~LATBbits.LATB0; // togle bit
         counter=0;  //Reset Counter
      }
      IFS0bits.T1IF = 0; // clear flag
   }
}

The header file:
Code:
// DSPIC30F2010 Configuration Bit Settings

// 'C' source line config statements

#include <p30Fxxxx.h>


 int FOSC __attribute__((space(prog), address(0xF80000))) = 0xFDF0 ;
//_FOSC(
//    XTL &              // Primary Oscillator Mode (XTL)
//    FRC &              // Oscillator Source (Internal Fast RC)
//    CSW_FSCM_OFF       // Clock Switching and Monitor (Sw Disabled, Mon Disabled)
//);
 int FWDT __attribute__((space(prog), address(0xF80002))) = 0x7FFF ;
//_FWDT(
//    WDTPSB_16 &        // WDT Prescaler B (1:16)
//    WDTPSA_512 &       // WDT Prescaler A (1:512)
//    WDT_OFF            // Watchdog Timer (Disabled)
//);
 int FBORPOR __attribute__((space(prog), address(0xF80004))) = 0x7F7C ;
//_FBORPOR(
//    PWRT_OFF &         // POR Timer Value (Timer Disabled)
//    NONE &             // Brown Out Voltage (Reserved)
//    PBOR_OFF &         // PBOR Enable (Disabled)
//    PWMxL_ACT_HI &     // Low-side PWM Output Polarity (Active High)
//    PWMxH_ACT_HI &     // High-side PWM Output Polarity (Active High)
//    RST_IOPIN &        // PWM Output Pin Reset (Control with PORT/TRIS regs)
//    MCLR_DIS           // Master Clear Enable (Disabled)
//);
 int FGS __attribute__((space(prog), address(0xF8000A))) = 0xFFFF ;
//_FGS(
//    GWRP_OFF &         // General Code Segment Write Protect (Disabled)
//    CODE_PROT_OFF      // General Segment Code Protection (Disabled)
//);
 int FICD __attribute__((space(prog), address(0xF8000C))) = 0xFFFF ;
//_FICD(
//    PGD                // Comm Channel Select (Use PGC/EMUC and PGD/EMUD)
//);

#define __XTAL_FREQ 8000000


 #ifndef _XC_H_
#define _XC_H_

#ifdef __XC8
#include <htc.h>
#endif

#endif        //_XC_H

My led does not blink at all. Am I doing something wrong or am I forgetting to do something in the code?
 
Status
Not open for further replies.

Latest threads

Back
Top