|
|
View previous topic :: View next topic |
Author |
Message |
cindyw
Joined: 21 Oct 2004 Posts: 9
|
MPLAB SIM & ICD2 get different result |
Posted: Thu Mar 03, 2005 7:19 pm |
|
|
Posted: Thu Mar 03, 2005 2:31 am
Here is a testing code. I found the PORTA is correct by running MPLAB SIM , however, the actual test result by run MPLAB ICD2, PinPA5 always on(PORTA=0x20)
compiler version 3.188
Anyboday can help on this?
-----------code start----------
#include <16F676.h>
#DEVICE adc=8 //only use 8 bit ??
#fuses INTRC_IO,NOWDT,NOPROTECT//NOBROWNOUT //MCLR
#use delay (clock=4000000) //4M hz internal osc
//#endif
void init();
/************************************************************
Main program
*************************************************************/
void main() { int temp;
init(); // init for ADC, I/O, Timer
CHANNEL_1: output_high(PIN_A4 );
temp=read_adc(); //read noise
output_low(PIN_A4 ); //RA4--Status_LED
delay_us(100);
delay_us(100);
goto CHANNEL_1;
}
void init()
{
//----I/O initialization---
#asm
bcf 0x03,0x5; //bank 0
clrf 0x05; //;Init PORTA
clrf 0x07; //;Init PORTC
bsf 0x03,0x5; //bank 1
movlw 0x08;
movwf 0x85 ;//set TRISA RA0-2,RA4,5 as output, system not recognize TRISA, use address instead
movlw 0x0f;
movwf 0x87 ;//set TRISC Rc4,5 as output,other as input
#endasm
//----ADC initialization---
SETUP_ADC_PORTS(sAN4|sAN5|sAN6|sAN7|VSS_VDD); // AN4:7 A/D. others digital
setup_adc( ADC_CLOCK_INTERNAL ); //internal oscillator 2-6us
set_adc_channel(5);
SETUP_COMPARATOR(NC_NC_NC_NC); //
//----Timer initialization---
SETUP_TIMER_1(T1_INTERNAL|T1_DISABLED|T1_DIV_BY_8); //prescale=1:8,
}
---code end
_________________ CC |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Mar 03, 2005 10:33 pm |
|
|
Quote: |
I found the PORTA is correct by running MPLAB SIM , however, the actual
test result by run MPLAB ICD2, PinPA5 always on(PORTA=0x20) |
You said pin "PA5". I assume you mean RA5.
Questions:
1. Pin RA5 is on pin 2 of the chip. Are you checking pin 2 ?
2. Do you have anything connected to pin 2, such as a LED
or other component ? If so, explain the circuit.
3. What are you using to check the voltage level ? A volt-meter
or an oscilloscope ?
4. Do you know for certain that your program is running ?
How do you know ?
5. What power supply voltage are you using for the PIC ?
Is it +5 volts ?
6. You are using the ICD2 in debug mode. Have you tried it
as a programmer ? Does that make your PIC work OK ?
I think you should try a small test program that only tests the RA5
problem. Copy the following program into MPLAB, compile it, and
program it into the 16F676. (By using the ICD2 as a programmer).
Then remove the ICD2 cable and flip the power off and on to reset
the PIC. Check pin 2 on the PIC with a oscilloscope. Does it change
state every 1/2 second ?
If this program doesn't work, then edit the two output lines in it and
change them to use "PIN_C4". This is pin 6 on the chip. Recompile,
program, and check pin 6. Is it changing every 1/2 second ?
Do all these tests and tell me what you see.
Code: | #include <16F676.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, NOMCLR
#use delay (clock=4000000)
void main()
{
while(1)
{
output_high(PIN_A5);
delay_ms(500);
output_low(PIN_A5);
delay_ms(500);
}
} |
|
|
|
cindyw
Joined: 21 Oct 2004 Posts: 9
|
I/O intialization problem ?? |
Posted: Fri Mar 04, 2005 1:54 am |
|
|
PCM programmer,
You are so kind to help on this.
let's me answer your questions first.
1, there is LED connect to RA5 (pin 2) , I can see its on/off.
2, there is also LED connect to RA4 (pin 3) ,
3, LED circirt (same): MicroP pin-1k Resistor-LED-GND
4, I use tektronix scope to check voltage
5, I run my program step-by-step. It is running because I can see RA4 is blinking.
6, +5V supply
7, your sample code is working great.
Here is my new findings
1, my code is working well in continues run mode, RA5 turn on only when I Halt runing or when program run in step by step mode. Based on this I suppose it will run properly in programmer mode.
2, I remove I/O initialization portion in init{}, then it works well
So, does this means I/O not need initialization? output_high() is enough? or how to do correct initilization?
_________________ CC |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Mar 04, 2005 2:05 am |
|
|
Because the simple program that I posted is working, I suggest that
you use that program as a base, and then add new code to it.
What feature do you want to add to your program next ?
If you tell me, then I'll describe the most simple way to do it.
If everything is kept very simple, there is a better chance that
it will work. |
|
|
Guest
|
|
Posted: Fri Mar 04, 2005 2:32 am |
|
|
actually I have some more working code. This testing code is just for verify the timing to run a read_adc() function.
My testing can be done, only accidently found above mentioned problem. I am wondering whether same problem happened somewhere while I am not aware .
I need to do 3 contiues ADC reading at interval 32us by using internal 4 MHz oscillator. Each reading compare with threshold. My test result shows it needs 44us to do ADC only in debugging mode(including on-off port RA4). I did not do testing in programmer mode yet. It sounds timing very tight. The code is here.
Code: |
//-------noise checking -------optional
for(i=0;i<3;i++)
{
output_high(status_LED);
temp=read_adc(); //read noise
output_low(status_LED); //check timing purpose
delay_us(5); //?
if (temp>Vn)
{ //if >noise level
recheck=1;
output_low(CUR);
LED_Blink(1,2,CH); //indication of noise fail 1 times, pattern 2
return recheck,done;
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Mar 04, 2005 1:05 pm |
|
|
If you need to do three A/D readings quickly, then get rid of the for(;;)
loop. The for(;;) loop takes several instructions and at 4 MHz, each
instruction is 1 or 2 microseconds. Just repeat the A/D code 3 times.
It takes more ROM, but it's faster without the loop. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Fri Mar 04, 2005 3:23 pm |
|
|
This may be the issue
Looking at your code
CHANNEL_1: output_high(PIN_A4 );
temp=read_adc(); //read noise
output_low(PIN_A4 ); //RA4--Status_LED
delay_us(100);
delay_us(100);
goto CHANNEL_1;
Notice there is no delay between the high and the low on pin_a4 except for the read_adc() action time.
Now when you step through there is time to see the transition from high to low when its running it will go low but the high time is very very short so it will not be seen with the human eye. |
|
|
cindyw
Joined: 21 Oct 2004 Posts: 9
|
|
Posted: Mon Mar 07, 2005 12:22 am |
|
|
Hi, Thanks for all of your advise. _________________ CC |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|