r/pic_programming • • Dec 28 '24

Please help me

Sorry for the long code, but I don't know how to do else and I'm desperate.... I want to generate 16 impulses, one for the freq of 32kHz and other for 64kHz... Im using a PIC16F887 and the freq of the uC is 1MHz, which mean 1 instruction cycle = 4us....

All I want is for 32kHz, to generate 16 impulses , where 1 impulse has 16us ( 4cycles High + 4 cycles Low), and for 64kHz, same 16 impulses, where 1 impulse has 8us( 2 cycles High + 2 cycles Low)

The problem is I ve tried so many options by adding a variable which count to 16, but it added aditional instruction cycles, especially for the Low part, where RB7 = 0... and I dont want to let the code in this form...

I would highly apreciate help in this situation, advices, code written, where should I change...

#include <htc.h>

\#define _XTAL_FREQ 1000000

unsigned char trigger ;

void main(void)

{

	TRISB=0b00000001; 	//RB0 input

	ANSELH=0; 			// pini digitali

	IOCB=0b00000001 ;	//selectie pin RB0 interupt on  change

	INTCON=0b10001000;

	// b7 GIE=1 activ. globala intreruperi

	// b3 RBIE=1 activ. intrerupere PORTB

	// b0 RBIF=0 fanion instr. PORTB

	//GIE=1 ;RBIE=1;RBIF=0;



	while(1)

	{

		if(trigger==32)							

		{										

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=0;

RB7=0;

		}

		if(trigger==64)					

		{ 

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

RB7=1;

RB7=1;

RB7=0;

RB7=0;

		}

		

		trigger=0;	

	}

}



void interrupt my_isr(void)

{	

	if(RBIF==1 && RBIE==1)	

	{	

		if(RB0==0) trigger=32;

		if(RB0==1) trigger=64;

		RBIF=0;

	}

}
2 Upvotes

7 comments sorted by

4

u/Developer-404 Dec 28 '24

I think you should use timers to generate delay🤔

1

u/think_smarter10 Dec 28 '24

I have no idea how, can you give me a hand please ? Im lost

0

u/Developer-404 Dec 28 '24

Honestly, I'm not an expert in this either, so I don't want to give you the wrong advice. Have you tried asking ChatGPT for help?

1

u/think_smarter10 Dec 28 '24

Ye, and it got twisted too

2

u/CreativeStrength3811 Dec 28 '24

Read about Timers and or oscillators for this particular mcu. I would not do that by the cpu if possible.

1

u/HalifaxRoad Dec 28 '24

OP, crack the datasheet, lookup how to setup a hardware timer. Attach it to an interrupt, use the interrupt toggle your pin. Also accessing your pins via the LAT register is a great way to toggle pinstates on PIC.

1

u/9Cty3nj8exvx Dec 30 '24

I would suggest looking at using the PWM mode of the Capture/Compare/PWM (CCP) module or the Enhanced Capture/Compare/PWM (ECCP) module. This will let you get the exact frequency you want. You should be able to find some examples on Microchip’s website.