# strange behavior of a pic 12f629

Status
Not open for further replies.

#### Kripton2035

hi,

I'm having troubles with a pic 12f629.
it's connected to 4 interruptors, and there is an output generating differents frequencies..
I use timer0 and timer1 to generate interrupts on overflow, to generate the frequencies.

a simulated schematic under proteus works fine as expected.
the real prototype doesnt work at all: no frequency on output ! seems stuck on any interrupt routine without exiting ...

if I program another thing without interrupts, the prototype works fine (with same special features as mclr disabled and internal clock), so there should be no bad wires on the real pic.

does anyone know where the problem may be ?????

the program is made with mikrobasic, but I tried with proton basic and the result is exactly the same ...

would be very happy if someone has the answer ...

regards,

#### betwixt

##### Super Moderator
Staff member
Difficult to predict without seeing the program - a few suggestions:

Are you clearing the interrupts when they occur?
Is the watchdog timer disabled or is it enabled but not being cleared?
Real hardware needs a decoupling capacitor across VSS and VDD.
TRIS register set correctly?
Are you trying to take output from GP3? (it's an input only pin!)

Brian.

#### Kripton2035

the real question is: what feature can be simulated correctly by proteus, and that is totaly not in real life of a pic 12f ...
I think it's around the interrupts, but I can't find exactly where ...
I will post the schematic and the code soon.
thanks.

#### betwixt

##### Super Moderator
Staff member
I have never seen Proteus so I can't help in that respect but I strongly suggest you download MPLAB from Microchip and try simulating with that. It's free, it comes direct from the IC manufacturer and with very few exceptions, none of which apply to the 12F629, it simulates exactly.

Post the schematic so we can see.

Brian.

#### xaccto

##### Full Member level 5
I learnt painfully, if you don't disable the comparator, the GP0,1 pins remain INPUTS,
regardless of how you set the TRIS register - an easily overlooked things that can get you stumped when it worked in the simulator!! in my case FED's pic environment.

I second using MPLAB, because it accurately simulated this, thats how I realized what was going on....and somehow missed it in reading the data sheet.

#### spiralbrain

good tip xaccto, I have been using Proteus and I feel its quite powerful. I am not really sure if you have tried it but you can a load a coff file in proteus and debug using your C source code. I sometimes find Proteus very strict with timings and constraints. MPLAB is really the best.

#### Kripton2035

hi,

I already saw the problem with not disabling the comparator ... and sorry to tell you that proteus simulated the correct thing ! gp0 and 1 not acting as input correctly ...
it took me a while for discovering this in the datasheet.but the problem is elsewhere
I will try your advise and simulate the thing using mplab to see if I can find something.
thanks.

here is the schematic:

and the code :
Code:
program pgrm2

'include "P12F629def.pbas"

Symbol min_frequency = 6
Symbol max_frequency = 248

symbol bobine = GP5
symbol lampe  = GP4
symbol inter0 = GP0
symbol inter1 = GP1
symbol inter2 = GP2
symbol inter3 = GP3

Dim savtmr As Byte
dim calclampe as byte
dim etatlampe as byte

sub procedure Interrupt
if INTCON.T0IF = 1 then
' interruption zero
GPIO.bobine = not GPIO.bobine  ' inverse letat de la bobine
INTCON.T0IF=0 ' set t0ie clear t0if
TMR0 = savtmr
end if
if PIR1.TMR1IF = 1 then
' interruption un, change la valeur de timer0 entre 6 et 156 (prescaler off)
' toutes les 0.5 seconde environ. donne une frequence entre 1 et 10 KHz
savtmr = savtmr+1
if TMR0>max_frequency then
savtmr=min_frequency
end if
TMR0 = savtmr
PIR1.TMR1IF=0
end if

end sub

sub procedure lectureinter
' lecture de letat des interrupteurs et allumage eventuel de la lampe
calclampe = testbit(GPIO, inter0)+testbit(GPIO, inter1)+testbit(GPIO, inter2)+testbit(GPIO, inter3)
if calclampe<>etatlampe then
GPIO.lampe = not GPIO.lampe        ' inverse letat de la lampe
etatlampe = calclampe    ' stocke letat des inters pour la suite
end if
end sub

main:
' calibrage de l'oscillateur
OSCCAL = 0x74 ' for the linker to know osccal !!!
'      asm
'      bsf STATUS,RP0
'      call 0x03ff
'      movwf OSCCAL
'      bcf STATUS, RP0
'      end asm

' disable pullups
setbit(option_reg,NOT_GPPU)
clearbit(WPU,0)
clearbit(WPU,1)
clearbit(WPU,2)
clearbit(WPU,3)

' disable comparator
setbit(CMCON,CM0)
setbit(CMCON,CM1)
setbit(CMCON,CM2)

' initialisation des entrees sorties

setbit ( TRISIO, inter0)  ' inter en entree
setbit ( TRISIO, inter1)
setbit ( TRISIO, inter2)
setbit ( TRISIO, inter3)
clearbit ( TRISIO, lampe)     ' lampe et bobine en sortie
clearbit ( TRISIO, bobine)

clearBit(GPIO, lampe)         ' lampe eteinte au debut
etatlampe = testbit(GPIO, inter0)+testbit(GPIO, inter1)+testbit(GPIO, inter2)+testbit(GPIO, inter3)

INTCON.GIE = 0          ' Disable global interrupts
While INTCON.GIE = 1    ' Make sure they are off
INTCON.GIE = 0      ' Continue to clear GIE
Wend                  ' Exit when GIE is clear

' initialisation des timers pour interruptions
' Timer0 Registers:
' Prescaler=1:4; TMR0 Preset=6; Freq=1 000,00Hz; Period=1 000,00 µs
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  = 0 ' bit 3 Prescaler Assignment bit: 0=Prescaler is assigned to the Timer0
OPTION_REG.PS2  = 0 ' bits 2-0  PS2:PS0: Prescaler Rate Select bits
OPTION_REG.PS1  = 0
OPTION_REG.PS0  = 0
OPTION_REG.NOT_GPPU = 1
TMR0 = min_frequency
savtmr = min_frequency
INTCON.T0IE = 1          ' Enable TMRO interrupt

' Timer1 Registers:
' Prescaler=1:8; TMR1 Preset=3036; Freq=2,00Hz; Period=500,00 ms
T1CON.T1CKPS1 = 1 ' bits 5-4  Prescaler Rate Select bits
T1CON.T1CKPS0 = 1
T1CON.T1OSCEN = 1 ' bit 3 Timer1 Oscillator Enable Control: bit 1=on
T1CON.NOT_T1SYNC  = 1 ' bit 2 Timer1 External Clock Input Synchronization Control bit: 1=Do not synchronize external clock input
T1CON.TMR1CS  = 0 ' bit 1 Timer1 Clock Source Select bit: 0=Internal clock (FOSC/4) / 1 = External clock from pin T1CKI (on the rising edge)
T1CON.TMR1ON  = 1 ' bit 0 enables timer
TMR1H = $0B ' preset for timer1 MSB register TMR1L =$DC        ' preset for timer1 LSB register
PIE1.TMR1IE = 1      ' enable interrupt on timer 1

INTCON.GIE = 1     ' autorise GIE les interrupts sur le pic

_loop:
lectureinter
goto _loop
end.

#### Kripton2035

some bad news from the bench ...
the hex file simulated with mplab sim is very well simulated ... the gp5 output is oscillating fine

the same hex file simulated with oshonsoft pic simulator is also oscillating, same as the proteus simulator in fact.

and so the same hex file programmed into the real 12f629 doesnt oscillate at all !!!!

anyone has an idea where my mistake can be ?????

regards,

I'm joining the hex file for anyone who wants to try simulating. the GP5 must oscillate between 1 khz and 12khz. does it in all simulators I tried, does not in real 12f629...

#### awmt102

##### Newbie level 4
Hi Kripton,

I have also found that the PIC Simulator in Proteus simulates well but when realised in hardware the same hex file does not work as expected. Initially all indications are that the hardware is at fault, but I soon realised that the hardware was absolutely fine (tested by writing a simpler program for the pic).

I eventually realised that, for some reason, Proteus does not seem to pay any attention to the __CONFIG bits in the hex files. E.g. I found that with a PIC18F2520 toggling RB.5 would simulate absolutely fine, but nothing would happen on real hardware. After re-reading the datasheet I found that RB5 can also be used for Low Voltage Prgramming. As soon as I turned this off with the appropriate config statement the hardware performed correctly.

I have found this to be true in a number of situations so my advice is to go through the datasheet and check if GP5 has any other purpose and disable that. Also try looking through all of the config options that your pic has - maybe one of them will jump out at you as being the problem.

As to why the simulator does not simulate this particular aspect of the program I do not know. Interestingly the reason I have come across your post is that I have a design that simulates perfectly but in hardware does not work. I have isolated the codes block that causes the problem but it is not clear why it simulates OK. For this reason I strongly going off the idea of PIC simulation.

Hope this helps.

Kripton2035

### Kripton2035

Points: 2

#### awmt102

##### Newbie level 4
I had a brainwave this morning, tried it out and it solved my problem!

I don't know if it is applicable to the 12f series but in the 18f series one of the config bits defines whether to use extended CPU instruction set or not. I had not specified whether to set or unset this bit. It appears that my compiler was setting it for me without my knowledge. This led to the stack overflowing when interrupts were called. I disabled the extended instruction set and everything worked as expected!

bukhari917

Points: 2