Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Hi 3V0,here is the code.This is just a math test.
If you see in MPLab Sim the variable perc is zero.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);
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.
//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;
}
}
}
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