Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with strange PIC behavior

Status
Not open for further replies.

mrcube_ns

Advanced Member level 1
Joined
Apr 10, 2002
Messages
452
Helped
48
Reputation
96
Reaction score
34
Trophy points
1,308
Location
Europe
Activity points
3,813
Strange PIC behavior

Here some small test in Hi te ch Pi cc mcu is 16f628.

Here is code:

#include <pic.h>
//#include <pic1662x.h>
#define PORTBIT(adr, bit) ((unsigned)(&adr)*8+(bit))
//__CONFIG(CP1|CP0|CPD|FOSC2|FOSC0|WDTE);
static bit test @ PORTBIT(PORTB, 1);
unsigned int i;


void main (void)
{
TRISA = 0;
TRISB = 0;
PCON = 0xFF;
OPTION=0x40;
INTCON=0x90;

PORTA = 255;
PORTB = 255;

test=1;

while(1){
if(test == 0)
test = 1;
else
test = 0;

for(i=0;i<10000;i++){
}//for
}//while
}//main



Here is what I see on oscilloscope on test pin non simetrical square signal :


___| |________________________| |_________________________



Any ideas?

Is it something wrong with code, am I doing something wrong???


Mr.Cube
 

Re: Strange PIC behavior

It probably has to do with what instructions the compiler generates for the while loop, there probably is a GOTO involved (which takes 2 cycles).
If you need perfect timing better do it in MPASM and count the clock cycles for each instruction.
 

Re: Strange PIC behavior

You can get strange things with the 628 if you don't disable the comparator functions when you are not using them!



ASM

movlw b'00000111' ; disable comparators
movwf CMCON
 

Re: Strange PIC behavior

Hi mrcube_ns,

1)

//__CONFIG(CP1|CP0|CPD|FOSC2|FOSC0|WDTE);

Not HiTe user but it seems that you have the WDT enable.


2)
unsigned int i; // This is an 8 bit integer whose max val = 255

Then in the for loop, the conditional test is expecting something imposible.

for(i=0;i<10000;i++) // i < 10000 ??????



Hope this help you.

Regards,


Humber555
 

Re: Strange PIC behavior

mrcube_ns,

as you do not use PORTA you can leave the comperators enabled without problems.

Your problem is that you have enabled the watchdog and do not reset it.

Put this:

asm("clrwdt");

in you for loop and you should get wat you want :)

best regards
 

Re: Strange PIC behavior

You can get strange things with the 628 if you don't disable the comparator functions when you are not using them!

Good to know that comparators can make trouble sometimes.

Code:
2) 
unsigned int i; // This is an 8 bit integer whose max val = 255 

Then in the for loop, the conditional test is expecting something imposible. 

for(i=0;i<10000;i++) // i < 10000 ??????

Humber555
integer is number between 0-65535 if it's defined as unsigned int value, so 10000 is very good int value (signed or unsigned no mater).


C-man tnx for answer, good point. But I deactivate WD option in programmer software (SW:ic prog, HW: propic2 programmer). Maybe software dont disable WD ?!!? All try to reset WD in while loop and see result.

Mr.Cube
 

Strange PIC behavior

Hi Humber555

unsigned int i;
for(i=0;i<10000;i++)


These statments are vaild becuase

int i takes 2 bytes from -32768 to +32768
unsigned int i removes the signed and add them to the positive (i will value between 0 : 65535)

But

char i take 1 byte ( -127 to 128)

and so

unsigned char ( 0 -255)

SphinX
 

Re: Strange PIC behavior

Sorry, my mistake because I had been working with NO ANSI compliance
C Compiler, where
unsigned int size is 8 bit (255)

humber555
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top