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

PIC16F1936 timer enable problem

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



Joined: 18 Jun 2009
Posts: 4

View user's profile Send private message

PIC16F1936 timer enable problem
PostPosted: Fri Aug 28, 2009 7:41 am     Reply with quote

Hello,

I have changed from the PIC 16f886 to the new micro watt 16F1936. The parts are pin compatible. The code ported over very easily thanks to CCS abstractions. :-) I am currently having a problem making the timers work. I have tried timer0, timer2 and timer4 and none of them will actually tic or start resulting in the interrupt not firing either.

any help would be great.

I am running v4.093 of PCW IDE

Code:
#include <16F1936.h> 
#include <stdio.h>
#include <stdlib.h>


#fuses noDEBUG   //  debug - in circuit debugger enabled on RB6 and RB7 - DISABLED
#fuses noLVP     //  LVP - low voltage enable - DISABLED
#fuses noFCMEN   //  Fail safe clock monitor - DISABLED
#fuses noIESO    //  IESO - internal / external switch over DISABLED
#fuses BROWNOUT  //  Brown out reset selection bits - 11 BOR  ENABLED
#fuses noCPD     //  CPD - date code protect DISABLED
#fuses noPROTECT //  CP - code protect DISABLED
#fuses MCLR      //  MCLRE - RE3 is MCLR
#fuses noSTVREN
#fuses noPUT     //  PWRTE - power up timer enable bit DISABLED
#fuses INTRC_IO, noWDT

// preprocessor directive that defines fuses for the chip 
//application will use the internal oscillator at 8MHz.
//compiler will set INTOSC_IO config bit, and set the internal
//oscillator to 8MHz.
#use delay(internal=4M)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)

unsigned int16 _restart_reason;

#define INTS_PER_SECOND 122
int _new_sec;      // A running seconds counter
int secs;         // A running seconds counter
int int_count;    // Number of interrupts before a second has elapsed

//****************
#INT_TIMER0
void timer0_isr(void)
{
    putc('+');
    if ( --int_count == 0 )       
    {           
      secs++;
      _new_sec++;
      int_count = INTS_PER_SECOND;

      if ( secs >= 60 ) {
        secs=0;
      }

    }
}



//*******************
//*******************
//**
//**
void timer_init()
{
 
   _new_sec  = 0;
   secs      = 0;
   int_count = INTS_PER_SECOND;

   set_timer0(5);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);

   /*
You see the 'Timer 0 (AKA RTCC)" in the first comments line? Looking at your program you
should get a toggle on the test pin every 1.6 seconds right? You are using a 4Mhz crystal
so your basic timer0 tick (the time it takes the timer to go up one count) is 1us. You are
using it in 8 bit mode and a prescaler of 64. So timer0 overflows in 1us*2^8*64 = 0.0164 seconds.
 Everytime it overflows you enter the ISR and increase the overflow count by one.
Once counts hits 100 the pin is toggled. So 0.0164*100 = 1.64 seconds.

8Mhz so timer tick is 0.5us
8 bit mode and a prescaler of 64
each overflow happens at :
0.5us*2^8*64 = 0.008192 secs
1/0.008192 = 122 overflows/INTS per second
   */
}


//*******************
//*******************
//**
//**
//#separate
#zero_ram
void main()
{                   
    int b;
    int16 x;

    setup_oscillator( OSC_4MHZ | OSC_PLL_OFF );


    //** get the reason for last reboot for later saving to EEPROM
    _restart_reason = restart_cause();
    switch(_restart_reason)
      {
      case MCLR_FROM_RUN:   //** on reset or WD time out this reason gets set
        printf("mclr\n\r");
        break;
      case WDT_TIMEOUT:
        printf("wdt\n\r");
        break;
      case NORMAL_POWER_UP:
        printf("norm\n\r");
        break;
      case BROWNOUT_RESTART:
        printf("brown\n\r");
        break;
      default:
        printf("other\n\r");
    };

    // Set PORT to all output
    set_tris_a(0);
    output_a(0);  //all port A off
       
    //** all PORTB leds OFF
    set_tris_b(0);
    output_b(0);

    printf("hello !!\n\r");
    delay_ms(2000);
    timer_init();

    output_b(1);
    b=0;
    while(1)
    {
      delay_ms(1000);

      //** flash status led every second
#if 1
      if ( b == 0 ) {
        output_high(PIN_B0);
        b = 1;
      }
      else {
        output_low(PIN_B0);
        b = 0;
      }
#endif
     

      printf("+ %d", get_timer0());

      if ( _new_sec == 0 )
        continue;

      _new_sec = 0;

      printf(".");
    }


}



_________________
gpacer68
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 28, 2009 11:40 am     Reply with quote

Try a simple program which toggles an LED that is connected to pin C0.
Connect a 470 ohm resistor between pin C0 and the anode of the LED.
The connect the cathode of the LED to ground. This assumes the PIC
is running at +5v. This program should toggle the LED on/off at about
a 1 Hz rate. I don't have this PIC to test in hardware, so I don't
guarantee that this will work.
Code:

#include <16F1936.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)

#define LED_PIN  PIN_C0

#int_timer1
void timer1_isr(void)
{
output_toggle(LED_PIN);
}

//======================================
void main()
{
output_low(LED_PIN);

setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
set_timer1(0);

clear_interrupt(INT_TIMER1);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);

while(1);
}
gpacer68



Joined: 18 Jun 2009
Posts: 4

View user's profile Send private message

PostPosted: Sat Aug 29, 2009 3:04 pm     Reply with quote

I tried your program and it worked, however when I changed it to use timer0 and I could not get that timer to work. I obviously changed the interrupt handler to account for the new frequency of interrupts with timer0.

In reading the list of changes to the compiler (recall I was at 4.093) it appears that more support for the new midrange PICs were added in newer versions. So I upgraded to 4.098 and the timer0 code worked as is.

So, the summary is, it appears the 4.093 did not fully support the PIC16F1936 timer0 and timer 2/4/6 features of this chip. When I upgraded to 4.098 these timers worked.

Thanks for the help.
_________________
gpacer68
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