Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C18 Questions

Status
Not open for further replies.
I'm trying to implement a timer interrupt in the PWM capture code above, its purpose will be to generate a timed PWM signal to control a motor based on the captured servo signal.

Am I correct in thinking that any low interupt will call that ISR function? It seems like it would, is an if statement like the one I used there:
"if(PIR1bits.CCP1IF == 1)" which reads the interrupt flag the normal way to handle this?

Also, I don't have specific questions about this because I haven't really gotten into it yet, but if anyone can give me hints, or show me how to set up a timer interrupt in my PWM capture code it would be helpfull. I'm going to start trying to figure it out when I get home and I'll post my code so far.
 
Last edited:
Hi 3V0,here is the code.This is just a math test.

Code:
#include<p18f4620.h>
void main(void)
{    unsigned int result;
    unsigned int a=0x1018;
    unsigned int b=0x0018;
    unsigned int ceilingrd=0x1018;
    unsigned int ceilingr=0x0018;
    unsigned int diff,perc,castingl,castingh;
    castingl=(ceilingrd);
    castingh=(ceilingrd/256);
    diff=ceilingrd-ceilingr;
    result=b-a;
    perc=(diff/ceilingrd)*100;
    
    while(1);
If you see in MPLab Sim the variable perc is zero.


PS:3V0 can i PM you about a board that i made.Its basically a question(not technical) and may be useful to you.I would be really thankful.If you say yes the i will get back to you i 2 or 3 days..just wanted to ask since i have your attention.

A percentage is the (amount)/(max possible). Execpt for the case where they are equal it will always be less then 1 which is zero in integer math.
To overcome this we scale the number to get a percentage number instead of a fraction. We use (amount*100)/(possible). It works well.

The problem you are seeing is that you amount is 0x18 and the possible is 0x1018. This works out to about 0.0058. If we use our percent scaling we will still get a zero!

In short the math is correct. Zero is the right answer. You could choose to do rounding and then you would get 1%.

Please PM me. Sorry about the slow reply
 
Heres my servo receiver code if anyone is interested, I changed it just a bit. Now if used on the junebug it will change between the leds above button 1 if you go forward and reverse. You just need to connect the reciever pins that would go to a servo to the 3 pin port that's on the opposite side of the junebug from the USB port (Red to U5V, Black to GND and White to RB3)

Code:
//Basic configureation of chip periferials
 #pragma config OSC = INTIO2, WDT = OFF, LVP = OFF
 #include <p18f1320.h>

//setup pins for PWM input
#define ReceiverPin PORTBbits.RB3
#define ReceiverTris TRISBbits.TRISB3


//PWM capture variables
unsigned int PWM1RiseTime = 0; //timer value at rising edge capture
unsigned int PWM1Width = 0; //calculated width
char PWM1Edge = 1; //edge currently being monitored 1 = rising, 0 = falling 

//set up interrupt
void low_ISR(void);//prototype
#pragma code low_vector = 0x08
void low_interrupt (void){
_asm goto low_ISR _endasm
}
#pragma code
#pragma interrupt low_ISR

//debug stuff
char test1 = 0;
char test2 = 0;

void main(void)
{	
	unsigned int MotorCount = 1;
	unsigned char MotorOn;	

	OSCCON = 0x72; //8MHz clock
	while(!OSCCONbits.IOFS); //Wait for OSC to become stable 

	//configure timer1
	OpenTimer1(0b10010101);
	PIE1bits.TMR1IE = 1; //periferial interupt register, 1 = timer 1 interupt enabled
	PIR1bits.TMR1IF = 0; //clears the timer 1 interupt flag

	//configure CCP1
	CCP1CON = 0b0000101; //configure CCP1 for capture, rising edge
	INTCONbits.PEIE=1;		//enable peripheral interrupts
    PIE1bits.CCP1IE=1;		//enabled CCP1 interrupt
    INTCONbits.GIE=1;		//enable branching to interrupt
	
	ReceiverTris = 1; //set RB3 for input so the capture can work.
	
	//configure ports
	ADCON1 = 0xff; //all digital
	INTCON2bits.RBPU = 0; //port b weak pullups on
	
	//configure control bits for output
	TRISAbits.TRISA0 = 0;
	TRISAbits.TRISA6 = 0;
	TRISAbits.TRISA4 = 0;
	//enable bit
	LATAbits.LATA4 = 1; //Enable bit
	

	while(1)
	{
		

	}
}


void low_ISR(void)
{		

	test1 = 1;
	//ccp interrupt
	if(PIR1bits.CCP1IF == 1)
	{	
		PIR1bits.CCP1IF = 0; //clear the flag
		if(PWM1Edge == 1)//if detecting rising
		{	
			
			PWM1RiseTime = CCPR1;//save the low timer value for the rise time
			CCP1CON = 0b0000100;//switch to detect falling edge
			PWM1Edge = 0;//switch to indicate falling edge is next
		}
		else //detecting falling
		{

			PWM1Width = CCPR1 - PWM1RiseTime;
			CCP1CON = 0b0000101;//switch to detect rising edge
			PWM1Edge = 1;//switch to indicate rising edge is next
		}

		//This stuff is just for testing
		if(PWM1Width > 1800)
		{
			LATAbits.LATA6 = 0;
			LATAbits.LATA0 = 1;
		}
		
		if(PWM1Width < 1400)
		{
			LATAbits.LATA6 = 1;
			LATAbits.LATA0 = 0;
		}

		if(PWM1Width > 1400 && PWM1Width < 1800)
		{
			LATAbits.LATA6 = 0;
			LATAbits.LATA0 = 0;
		}
		
	}
}

now all i need is to put in a pwm output that changes with that number and reverses in the middle and im good to go.
 
A percentage is the (amount)/(max possible). Execpt for the case where they are equal it will always be less then 1 which is zero in integer math.
To overcome this we scale the number to get a percentage number instead of a fraction. We use (amount*100)/(possible). It works well.

The problem you are seeing is that you amount is 0x18 and the possible is 0x1018. This works out to about 0.0058. If we use our percent scaling we will still get a zero!

In short the math is correct. Zero is the right answer. You could choose to do rounding and then you would get 1%.

Please PM me. Sorry about the slow reply


Hi 3V0 Thanks for your reply. I already showed you the board in chat. It was my PICDEMZ clone. :)
 
Status
Not open for further replies.

Latest threads

Back
Top