News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

12F675 & Timer1

Started by hitronics, May 02, 2022, 01:34 AM

Previous topic - Next topic

hitronics

I am doing some tests with 12F675 using timer1 then I can not use GPIO4 & GPIO5 then if disable timer1 then all things works as expected
so is it normal?

top204

#1
Because Timer1 can use an internal or external oscillator, it must be setup so that it does not require the crystal on its OSC pins (GPIO.4 and GPIO.5 with a PIC12F675 device), and uses the internal oscillator instead. Otherwise, the Timer1 OSC pins are dedicated to that function and are not I/O lines. See section 5.0 in its datasheet. The same when choosing the internal oscillator for the microcontroller using the config fuses. The CLK pins must be set so they are I/O lines and not CLK lines.

Below is a simple test program that sets up Timer1 and a Timer1 overflow interrupt. It flashes LEDs connected to GPIO.4 and GPIO.5 in the foreground, and flashes an LED connected to GPIO.0 in the background using the Timer1 overflow interrupt. This shows that GPIO.4 and GPIO.5 are operating as I/O lines and that Timer1 is running correctly:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' A test to make sure the OSC pins GPIO.4 and GPIO.5 are free to use as I/O pins when using Timer1
' It flashes LEDs connected to GPIO.4 and GPIO.5 in the foreground
' And flashes an LED connected to GPIO.0 within a Timer1 overflow interrupt, in the background
'
' Written by Les Johnson for the Positron8 BASIC compiler.
'
    Device = 12F675                           ' Tell the compiler what device it will be compiling for
    Declare Xtal = 4                          ' Tell the compiler what speed the microcontroller will be operating at
    On_Hardware_Interrupt GoTo ISR_Handler    ' Point to the Interrupt Handler routine
'
' Create variables here
'
    Dim wTimer1 As TMR1L.Word                 ' Create a 16-bit SFR (Special Function Register) from SFRs: TMR1L and TMR1H
   
'----------------------------------------------------------------------------
' The main program starts here
' Flash two LEDs in the foreground and an LED in the background using a Timer1 overflow interrupt
'
Main:
    PinLow GPIO.0                   ' Make the interrupt LED toggle pin an output low
  
    T1CON = $3D                     ' Setup Timer1     
    wTimer1 = 0                     ' Reset the Timer1 count SFRs TMR1L\H
    T1CONbits_TMR1ON = 1            ' Enable Timer1
    PIR1bits_T1IF = 1               ' Clear the Timer1 interrupt flag
    PIE1bits_T1IE = 1               ' Enable a Timer1 interrupt
    INTCONbits_PEIE = 1             ' Enable peripheral interrupts
    INTCONbits_GIE = 1              ' Enable global interrupts
'
' Flash the LEDs connected to GPIO.4 and GPIO.5
'   
    Do                              ' Create a loop
        PinHigh GPIO.4              ' \
        PinHigh GPIO.5              ' / Bring the 2 pins high
        DelayMS 300                 ' Wait for 300 milliseconds
        PinLow GPIO.4               ' \
        PinLow GPIO.5               ' / Pull the 2 pins low
        DelayMS 300                 ' Wait for 300 milliseconds
    Loop                            ' Do it forever

'---------------------------------------------------------------------------- 
' Interrupt handler
' Flashes an LED every Timer1 overflow

ISR_Handler:
    Context Save                    ' Save important SFRs and any compiler system variables
   
    If PIR1bits_T1IF = 1 Then       ' Is it a Timer1 overflow that has triggered the interrupt?
        Toggle GPIO.0               ' Yes. So toggle the LED connected to GPIO.0
        PIR1bits_T1IF = 0           ' Clear the Timer1 interrupt flag
    EndIf

    Context Restore                 ' Restore important SFRs and any compiler system variables, and exit the interrupt
       
'----------------------------------------------------------------------------
' Setup the fuses for internal oscillator, and GPIO.4 and GPIO.5 as I/O pins on a PIC12F675 device
'
    Config INTRC_OSC_NOCLKOUT, WDT_OFF, PWRTE_ON, CP_OFF, MCLRE_ON

With the Positron compilers, it doesn't get much simpler to create a program that has an interrupt and a timer within it. :-) Even after all of these years working with electronics and computers and microcontrollers of all types, you can never beat an LED flashing to show things are working as standard. :-)

Below is a screenshot of the above program working within a simulator. When running, all three LEDs are flashing.

PIC12F675_Timer1.jpg

hitronics

@top204
thanks for your answer I will tested it
another strange issue with 12F675 MCLR OFF configuration but all times 12F675 reset

top204

Make sure you have disabled the WDT (Watchdog Timer) in the config fuses (WDT_OFF), otherwise the microcontroller will continuously reset if it is not cleared using the ClrWdt mnemonic in your program.

hitronics

@top204
I am sure WDT_OFF
I tried also to to make MCLR_ON and connected to VDD via 1K resistor also same issue when I touch any pin with my hand it reset
12F675 I have it is written on back side CHINA
maybe it is copy

RGV250

 Are you using a battery or mains power, I recall that I had a similar issue when I used a cheap phone charger as a power supply but when on battery it was OK.

Bob

hitronics

@RGV250
I am using switching power supply
then tested with battery same isuue