USING TIMER0 AS A COUNTER

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

USING TIMER0 AS A COUNTER

Postby mohdismiaswaly » Tue Jan 21, 2014 11:43 pm

Hello,

Please check my schematic simulation using Proteus.
Switch is toggle ON 3 times so that the LED will ON. After that, the PIC will start again to count 3 times of the switch.

The code is as below, any comment? The simulation is not working
CODE: SELECT_ALL_CODE
#include <htc.h>


#pragma   config FOSC = HS         // HS oscillator
#pragma   config FCMEM = OFF         // Fail-Safe Clock Monitor disabled
#pragma   config IESO = OFF         // Oscillator Switchover mode disabled
#pragma   config PWRT = OFF         // PWRT disabled
#pragma   config BOREN = OFF         // Brown-out Reset disabled in hardware and software
#pragma   config WDT = OFF         // WDT disabled (control is placed on the SWDTEN bit)
#pragma   config MCLRE = ON         // MCLR pin enabled; RE3 input pin disabled
#pragma   config PBADEN = OFF         // PORTB<4:0> pins are configured as digital I/O on Reset
#pragma   config CCP2MX = ON          // CCP2 input/output is multiplexed with RC1
#pragma   config LVP = OFF         // Single-Supply ICSP disabled
#pragma   config XINST = OFF         // Extended

delay_ms(unsigned int x);                 // function prototype for delay

   
void main()
{
    char TEST = 3;                  // Constant TEST = 3
    enum outputs {LED = 3};   // Constant RELAY = 3
   
    ADCON1= 0b00001111;  // Make all AN0 - AN12 digital inputs
    PORTA = 0;                      // Reset port A
    TRISA = 0xFF;                  // All portA pins are configured as inputs
    PORTD = 0;                        // Reset port D
    TRISD = 0b11110111;       // Pin RD3 is configured as an output, while the rest are
                                          // configured as inputs
   

   
    T0CON=0b11111000;        // ON Timer 0,
    TMR0 = 0;                               // Reset timer/counter TMR0
   
    do {
        if (TMR0 == TEST)              // Does the number in timer match constant TEST?
        {PORTD.LED = 1;                // Numbers match. Set the RD3 bit (output LED
         delay_ms(1500);
        }
    }
    while (1);                              // Remain in endless loop
}


delay_ms(unsigned int x)      // delay 1 mS   
{
   for(;x>0;x--);
   {
   T0CON=0b10010000;    //Fosc 4MHz, Timer 0,16-bit mode, prescaler 1:2, use internal clock, increment on positive-edge
   TMR0H=0xF6;
   TMR0L=0X3C;
   T0CONbits.TMR0ON=1;
   while(INTCONbits.TMR0IF==0);
   T0CONbits.TMR0ON=0;
   INTCONbits.TMR0IF=0;
   }      
}
Attachments
Untitled.png
schematic proteus
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm

Re: USING TIMER0 AS A COUNTER

Postby mohdismiaswaly » Tue Jan 28, 2014 11:14 pm

Any thoughts?
Mr.Ober?
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm

Re: USING TIMER0 AS A COUNTER

Postby Idris » Wed Jan 29, 2014 2:32 pm

Hi mohdismiaswaly,

Looking at your schematic, you need to set external clock for Timer 0 because you use switch to trigger. But from your code, I see the comment here "use internal clock".
CODE: SELECT_ALL_CODE
T0CON=0b10010000;    //Fosc 4MHz, Timer 0,16-bit mode, prescaler 1:2, use internal clock, increment on positive-edge

So maybe you need to change that to external clock. Correct me if I'm wrong.
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 409
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: USING TIMER0 AS A COUNTER

Postby mohdismiaswaly » Thu Jan 30, 2014 11:37 pm

Idris WROTE:Hi mohdismiaswaly,

Looking at your schematic, you need to set external clock for Timer 0 because you use switch to trigger. But from your code, I see the comment here "use internal clock".
CODE: SELECT_ALL_CODE
T0CON=0b10010000;    //Fosc 4MHz, Timer 0,16-bit mode, prescaler 1:2, use internal clock, increment on positive-edge

So maybe you need to change that to external clock. Correct me if I'm wrong.



Hello Idris,

You mean the configuration bit in T0CON<5>, yes, I think that's maybe too. Because I connect the switch to pin T0CK1, so I need to set bit 5 in T0CON register, right?
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm

Re: USING TIMER0 AS A COUNTER

Postby mohdismiaswaly » Thu Jan 30, 2014 11:46 pm

Hello Idris,

I forgot, the T0CON configuration that you mentioned just as above is for creating a timer for my delay function in the do-while statement. Therefore bit 5 in T0CON register is cleared to enable a timer function, not a counter.

But,the other T0CON configuration in the upper code area I set 1 for bit 5 to enable counter.
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 1 guest

cron