PIC12F683 TMR0 Interrupt issue. SOFT PWM not as Desired.

Status
Not open for further replies.

asking

Full Member level 5
Joined
Sep 21, 2010
Messages
279
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,298
Visit site
Activity points
3,377
Hello,

I have 12F683 @ 8MHZ (internal), and i want to have Soft PWM using interrupt. TMR0

I m using PIC Calculator

Here's my code, Please help... when i connect on PIN GP2 it shows 7.14KHZ Signal on Oscilloscope....it should show 38.4KHZ....please help...i tried...but not getting it..

Code:
void interrupt()
{
  // Timer0 Interrupt - Freq = 38461.54 Hz - Period = 0.000026 seconds
  if (INTCON.TMR0IF ==1) // timer 0 interrupt flag
  {
    GPIO.F2 = ~GPIO.F2;      // Toggle PORTB bit0 LED
    INTCON.TMR0IF = 0;         // clear the flag
    INTCON.TMR0IE = 1;         // reenable the interrupt
    TMR0 = 204;           // reset the timer preset count
  }

}



// code starts here...
void main()
{
  ANSEL = 0;
  // setup portb to show the interrupts by blibking LEDs
  TRISIO = 0;    // PORT is all output...to show the interrupts
  GPIO.F2 = 0;       // start with all outputs low
//Timer0 Registers Prescaler= 1 - TMR0 Preset = 204 - Freq = 38461.54 Hz - Period = 0.000026 seconds
OPTION_REG.T0CS = 0;  // bit 5  TMR0 Clock Source Select bit...0 = Internal Clock (CLKO) 1 = Transition on T0CKI pin
OPTION_REG.T0SE = 0;  // bit 4 TMR0 Source Edge Select bit 0 = low/high 1 = high/low
OPTION_REG.PSA = 1;   // bit 3  Prescaler Assignment bit...0 = Prescaler is assigned to the WDT
OPTION_REG.PS2 = 0;   // bits 2-0  PS2:PS0: Prescaler Rate Select bits
OPTION_REG.PS1 = 0;
OPTION_REG.PS0 = 0;
TMR0 = 204;             // preset for timer register


// Interrupt Registers
 INTCON = 0;           // clear the interrpt control register
 INTCON.TMR0IE = 1;        // bit5 TMR0 Overflow Interrupt Enable bit...1 = Enables the TMR0 interrupt
INTCON.TMR0IF = 0;        // bit2 clear timer 0 interrupt flag
  INTCON.GIE = 1;           // bit7 global interrupt enable


  while(1)  //endless loop
  {
  }
}
 

have you solved this?
youre setting PSA to 1, i would have thought you wanted to assign the prescaler to the tmr0. also TOCS gives you Fosc / 4.. if you're expecting Fosc you out by a factor of 4 which is close to what you got... also is your WDT disabled? you're not refreshing it in the code
 

I tested the above code and it was giving 7.14khz... i did not addedd code for 8MHZ internal osciallator. i just set in mikroc project settings 8mhz and intosc.

PSA = 1; mean i want to assign tmr0 to prescaler. i think its fine. TOCS give Fosc/4. Thats right. I need 38500Hz ~ 38.5Khz frequency. so time comes to be around 26us i think... I want to use internal 8MHZ osciallator. So do i need to add code for it or just select 8MHZ in mikroc and intosc in mikro c ? i am confused with that part..

Confused... :d:
 

Finally by trying some how i managed to get 38.3 KHZ with TMR2, but its hit n trial method..i am still not able to answer why i cannot get the same with TMR0 & TMR1.

Here's my code. If you can help please show me how its 38.3KHZ with 8MHZ internal osciallator....some calculations if..

Code:
// Interrupt Handler
void interrupt()
{

  // Timer2 Interrupt- Freq = 38461.54 Hz - Period = 0.000026 seconds
  if (PIR1.TMR2IF == 1)          // timer 2 interrupt flag
  {
    GPIO = ~GPIO;
    PIR1.TMR2IF = 0;     // clears TMR2IF       bit 1 TMR2IF: TMR2 to PR2 Match Interrupt Flag bit
  }
}



// code starts here...
void main()
{
  ANSEL = 0;
  // setup portb to show the interrupts by blibking LEDs
  TRISIO = 0;    // PORT is all output...to show the interrupts
  GPIO = 0;       // start with all outputs low


//Timer2 Registers Prescaler= 1 - TMR2 PostScaler = 1 - PR2 = 52 - Freq = 38461.54 Hz - Period = 0.000026 seconds
T2CON |= 0;        // bits 6-3 Post scaler 1:1 thru 1:16
T2CON.TMR2ON = 1;  // bit 2 turn timer2 on;
T2CON.T2CKPS1 = 0; // bits 1-0  Prescaler Rate Select bits
T2CON.T2CKPS0 = 0;
PR2 = 25;         // PR2 (Timer2 Match value)

// Interrupt Registers
  INTCON = 0;           // clear the interrpt control register
  INTCON.TMR0IE = 0;        // bit5 TMR0 Overflow Interrupt Enable bit...0 = Disables the TMR0 interrupt
  PIR1.TMR2IF = 0;            // clear timer1 interupt flag TMR1IF
  PIE1.TMR2IE = 1;         // enable Timer2 interrupts
  INTCON.TMR0IF = 0;        // bit2 clear timer 0 interrupt flag
  INTCON.GIE = 1;           // bit7 global interrupt enable
  INTCON.PEIE = 1;          // bit6 Peripheral Interrupt Enable bit...1 = Enables all unmasked peripheral interrupts
  OSCCON.IRCF0 = 1;
  OSCCON.IRCF1 = 1;
  OSCCON.IRCF2 = 1;
  OSCCON.HTS = 1;

  while(1)  //endless loop
  {
  }
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…