CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

printf problem (Solved)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Jerry I



Joined: 14 Sep 2003
Posts: 96
Location: Toronto, Ontario, Canada

View user's profile Send private message

printf problem (Solved)
PostPosted: Tue Mar 01, 2016 9:03 pm     Reply with quote

Thank you in advance for any help.

I got a new PIC16F1619 to use CLC Functions

I have used the PIC16F1509 with no problems. The code I ported is from the PIC16F1509 which
worked with no problems.

Compiler version PCM 5.052

The problem is the interupt #int_timer2 routine.

If I uncomment any code in the #int_timer2 routine the printf("Hello World\r\n")
in the main() loop code will get corrupted (chopped up).

I will post part of my program which will compile.

Code:

#device PIC16F1619

  #include <16F1619.h>
  #fuses intrc_io, nowdt, noprotect, nolvp, put
  #fuses nomclr, nodebug

  #device *=16
  #device adc=8
 
#byte TRISA = getenv("SFR:TRISA")
#byte TRISB = getenv("SFR:TRISB")
#byte TRISC = getenv("SFR:TRISC")


// #define CLOCK98MHZ
// #define CLOCK8MHZ
#define CLOCK16MHZ

#define TEST_BIT rc1
#define TEST_BIT2 rc2

#define PHASE_B   ra3
#define PHASE_A   rc7
// #define EL_PHASE_A   rd2
// #define EL_PHASE_B   rd3

#case

#ifdef CLOCK8MHZ
  #use delay(clock=8000000, internal)    //one instruction=0.2us
#endif

#ifdef CLOCK16MHZ
  #use delay(clock=16000000, internal)    //one instruction=0.2us
#endif

#ifdef CLOCK98MHZ
  #use delay(clock=9830400)   /////////// Check Crystal Value ////////////
#endif

#define BAUD1_SPEED 9600
#define BAUD2_SPEED 19200
#define BAUD3_SPEED 38400

#use rs232(baud=BAUD1_SPEED,xmit=PIN_B7,rcv=PIN_B5, ERRORS)

int32 tmr0cnt, tmr1cnt;

signed int16 encDelta;
signed int16 last;
signed int32 enc_cnt;
int1 last_A, last_B, new_A, new_B, A_error, B_error;
int16 error_A_cnt, error_B_cnt;


signed int16
DnCount,                             // Holds accumulated 'up' pulses
UpCount,                             // Holds accumulated 'down' pulses
prevDnCount,                         // Previous Holds accumulated 'up' pulses
prevUpCount,                         // Previous Holds accumulated 'down' pulses
mvelocity,                           // Measured motor velocity
prev_mvelocity                       // Holds prev UP/DOWN Counts
;

signed int32
travel_to,
position,                            // Commanded position.
mposition;                           // Actual measured position.


#int_timer2             // Quadrature Encoder read counters
void timer2_isr(void)
{
    signed int16 new, diff;

    mvelocity = DnCount;                 // UNCOMMENTING ANY OF THESE LINES printf gets corrupt
    mvelocity -= UpCount;                // UNCOMMENTING ANY OF THESE LINES printf gets corrupt
//!    prev_mvelocity = mvelocity;          // UNCOMMENTING ANY OF THESE LINES printf gets corrupt
//!
//!    UpCount = tmr0cnt + get_timer0();    // UNCOMMENTING ANY OF THESE LINES printf gets corrupt
//!    DnCount = get_timer1();              // UNCOMMENTING ANY OF THESE LINES printf gets corrupt
}

#int_timer0             // Quadrature Encoder read counters
void timer0_isr(void)
{
    tmr0cnt = tmr0cnt + 0x0100;
}

void main()
{
    signed int16 cnt = 0;

   
    setup_adc(ADC_OFF);

    TRISA = 0b00101011;
    port_a_pullups(0x04);

    TRISB = 0b11110111;   // All Inputs Lower nibble Interrupt on change

    TRISC = 0b11010000;

    setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);

    setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_EXTERNAL_SYNC);   

    tmr0cnt = 0L;
    tmr1cnt = 0L;

    set_timer0(0);
    set_timer1(0L);

    setup_timer_2(T2_DIV_BY_4, 64, 10);
   
    mvelocity = 0;
    prev_mvelocity = 0;
    DnCount = 0;
    UpCount = 0;
    prevUpCount = 0;
    prevDnCount = 0;
    mposition = 0;
       
    clear_interrupt(INT_TIMER0);    // Timer
    clear_interrupt(INT_TIMER2);    // Timer

    enable_interrupts(INT_TIMER0);     // TIMER2 Int
    enable_interrupts(INT_TIMER2);     // TIMER2 Int

    enable_interrupts(GLOBAL);
   
    printf("Hello World Test \r\n\r\n");
   
    while(TRUE)
    {
        printf("Hello World\r\n");
        delay_ms(2000);
    }
}


[img]http://postimg.org/image/5ewb01cid/[/img]

Any Ideas?? Question

Thanks

Jerry


Last edited by Jerry I on Wed Mar 02, 2016 7:58 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 01, 2016 9:30 pm     Reply with quote

Quote:
If I uncomment any code in the #int_timer2 routine the printf("Hello
World\r\n") in the main() loop code will get corrupted (chopped up).

That's the symptom of a software UART with its bit timing disrupted by
interrupts. You can see it's a software UART by looking at the .LST file.

To fix it you need to use a hardware UART. Just by playing around with it,
I was able to get the setup code that gives a hardware UART:
Code:

#pin_select U1TX = PIN_B7
#pin_select U1RX = PIN_B5

#use rs232(baud=BAUD1_SPEED, ERRORS, UART1)

This can be confirmed by looking at the .LST file. You will see it's using
the hardware UART registers.
Jerry I



Joined: 14 Sep 2003
Posts: 96
Location: Toronto, Ontario, Canada

View user's profile Send private message

PostPosted: Wed Mar 02, 2016 8:04 pm     Reply with quote

Thanks again PCM Programmer.

This is the first time I used a Pic where the use of pin_select for a peripheral.

Now I wondering if the timers that I am using also require to define external input pins as T0CKI and T1CKI with pin_select??.

Thanks again.

Jerry
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 03, 2016 1:48 am     Reply with quote

Quote:

Now I wondering if the timers that I am using also require to define
external input pins as T0CKI and T1CKI with pin_select ?

No, you don't have to. They have default pins. See this table in the
16F1619 data sheet: Table 13-1: PPS Input Register Reset Values
The default pin for Timer0 external clock is RA2.
The default pin for Timer1 external clock is RA5.
Jerry I



Joined: 14 Sep 2003
Posts: 96
Location: Toronto, Ontario, Canada

View user's profile Send private message

PostPosted: Thu Mar 03, 2016 2:06 pm     Reply with quote

Thanks for info,

What about the CLC outputs, I will have 2 each feeding the 2 external clock inputs. Do I have to set pin_select pins?.

Thanks again,

Jerry
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 03, 2016 2:49 pm     Reply with quote

This again is easy stuff... just read the datasheet....page 6, table 1 shows the available inputs/outputs including the CLC peripheral.
While I don't have or use this PIC it does seem fairly easy to use....
CCS may have an example of using the CLC, I haven't checked and my version of PCM is rather old, bound to be some 'how to use' stuff in the manual. Interesting PIC though I like the idea of the NCO.......

too many PICs... too little time

Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group