Jump to content

Works In Simulation But In Real World It Doesn't

Recommended Posts


my code works in simulation but in the real world it doesn't. my target PIC is PIC16f877a, 4Mhz Xtal Oscillator. before going specific here is my code:


#include <system.h>
#include <boostc.h>

//Target PIC16F877 configuration word

//Set clock frequency
#pragma CLOCK_FREQ	4000000

unsigned short R_peak1 = 0x0265;
unsigned short R_peak2 = 0x01ea;
bit flagR1 = 0;
bit flagR2 = 0;

unsigned int RRcounter = 0;

unsigned short Nullplus = 0x00f4;
unsigned short Nullminus = 0x0082;

/*	Fosc/2, AN0, right justified*/
void ADC_init()
trisa.0 = 1;
adcon0 = 00000000b;
adcon1 = 10001110b;
clear_bit(pir1, 6);		//clear ADIF
clear_bit(pie1, 6);		//ADC interrupt disabled
set_bit(intcon, 6);		//set PEIE
set_bit(intcon, 7);		//set GIE

void ADC()
unsigned short comb;
adcon0.0 = 1;
adcon0.2 = 1; 	//starts ADC
while(adcon0.2); // wait until conversion is done

void timer0_init()
/*---1:4 prescalar---*/
option_reg.2 = 0;
option_reg.1 = 0;
option_reg.0 = 0;
/* ------------------ */
option_reg.5 = 0;	// internal clk source
option_reg.3 = 0;	// prescalar assigned to timer0
//intcon.5 = 1;	// enables timer0 interrupt
tmr0 = 0x00;

void timer0_go(){
intcon.5 = 1;

void timer0_stop()
intcon.5 = 0;

void interrupt(void)
//Handle timer0 interrupt
if(intcon & (1<<T0IF))
	clear_bit( intcon, T0IF ); //clear timer 0 interrupt bit
	tmr0 = 0x00;

void main( void )
unsigned short ewan = 0;
unsigned long heart_rate = 0;
int RR_interval = 0;


trisc = 0x00;
portc = 0x00;
portc.3 = 1;
portc.4 = 1;

MAKESHORT(ewan, adresl, adresh);
//LOBYTE(portc, ewan);
//HIBYTE(portc, ewan);
	//clear_bit(pir1, ADIF);
	goto ADC;

//Detect peak only if flags are not set

	if(flagR1 == 1){
		flagR2 = 1;
		goto fin;																
	if(flagR1 == 0){
		flagR1 = 1;	
		goto ADC;

			//goto ADC;

		goto ADC;

		portc.0 = 1;
		portc.1 = 1;

		portc.7 = 1;
		portc.6 = 1;
		portc.7 = 0;
		portc.6 = 0;




this code is used to detect two peaks. R_peak1 and R_peak2. if R_peak1 was detected, it will set flagR1 then go to ADC again and wait for another peak which is R_peak2. after it has detected R_peak2, flagR2 will be set then it will go to fin to do the led blinking to check if it really works. in the simulation mode it really works as i wanted it. (portc.0 , 1, 3, and 4 should stay ON while portc.6 and 7 should blink).

Now heres the problem, portc.0 , 1 , 6 and 7 blinks together when R_peak is detected. this algo should only detect two R_peak but i think what it does in the real world is it keeps detecting R_peaks. i suspect that it doesn't go to the last while loop but why does portc.6 and 7 blinks?


I suspect it has something to do with the interrupt routine. i tried removing it in ny code but it was also the same so i though it does not have something to do with the interrupt routine.


Actually this algo will be used to detect heart rate that is why i ask from my previous question about integer to binary. well this is another problem but i'll ask it again after this one is done. thanks for reading my long post and i hope you had idea what was behind my problem.




Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...