![]() | ![]() | ![]() |
| | #151 |
|
Debug runs the chip at it's real speed when in RUN mode. But when you want to single step it's very slow at 31kHz (the default 18F1320 speed). OSCCON = $72 will speed things up to 8MHz. Note: The more variables you're watching the slower single step / animate will run. | |
| |
| | #152 |
|
I usually have been using run mode, even though I wasnt sure that would capture pwm properly I was sure that step animate wouldent get it. Thanks for the speed control pointer.
__________________ -Paul | |
| |
| | #153 |
|
I had the PWM measuring code working pretty well inside 3V0's program, but now that I moved it on its own I cant get the interrupt to go. I must be missing something. The standalone program I have so far is fairly short. I wouldent be surprised if the problem is in the way I configured the interrupt as thats something I dont understand very well yet. Does each type of interrupt use its own function, or do they all call the interrupt function, then you sort out what to do by checking the flags? I've seen various interrupt code, and I cant really tell what each part means, so its hard to know what I copied over wrong. I've tried this line "#pragma code low_vector = 0x18" with 0x08 and 0x18 since I've seen both in examples, I cant see to get that interrupt to trigger. Code: #pragma config OSC = INTIO2, WDT = OFF, LVP = OFF
#include <p18f1320.h>
//set up interrupt
void low_ISR(void);//prototype
#pragma code low_vector = 0x18
void low_interrupt (void){
_asm goto low_ISR _endasm
}
#pragma code
#pragma interrupt low_ISR
//setup pins for PWM input
#define ReceiverPin PORTBbits.RB3
#define ReceiverTris TRISBbits.TRISB3
//PWM capture variables
unsigned int PWM1RiseTime = 0;
unsigned int PWM1Width = 0;
char PWM1Edge = 1;
char test1 = 0;
// Interrupts
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
}
}
}
void main(void)
{
//set clock to 8 Mhz
OSCCONbits.IRCF0=1;
OSCCONbits.IRCF1=1;
OSCCONbits.IRCF2=1;
//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
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
while(1)
{
}
}
__________________ -Paul Last edited by Triode; 21st September 2009 at 02:52 AM. | |
| |
| | #154 |
|
You aren't enabling the interrupts, you need to add these lines, Code: INTCONbits.PEIE=1; //enable peripheral ints
PIE1bits.CCP1IE=1; //namely the CCP1 int
INTCONbits.GIE=1; //finally turn them on
Code: PIE1bits.TMR1IE = 1; //periferial interupt register, 1 = timer 1 interupt enabled PIR1bits.TMR1IF = 0; //clears the timer 1 interupt flag Mike. | |
| |
| | #155 |
|
Thanks, what was missing from all my variations so far in trying to get it to work was turning on GIE. For anyone interested, heres the working code I have so far for Capturing the width of a PWM signal. In this case from a servo receiver plugged into RB3 of the junebug: 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;
unsigned int PWM1Width = 0;
char PWM1Edge = 1;
//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)
{
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
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
}
}
}
__________________ -Paul | |
| |
| | #156 |
|
Hi! me again...i was doing a program on the 18LF4620 that requires me to calculate the percent difference between two integers.I was surprised to find out that although C18 allows multiplication and division of 'int' but when i was subtracting one from the other, it was only subtracting the Lower 8 bits.Weird huh?Is there any way with out using the 32 bit libraries(huge code size)?Except the manual way(8 bit operations)
__________________ Syed | |
| |
| | #157 |
|
Post the code. You have done something wrong. Sixteen bit maths works fine in C18. Mike. | |
| |
| | #158 |
|
Just worked it out.The problem was with the result that i was expecting.I cant calculate percentages with out using floating point math since the answer is going to be in decimals(after divisions) which cant be used with out teh math library so even after multiplying it with 100 i got zero.So what i have done is just calculated the difference and then did the other stuff in my labview vi(constituting the other part of the 'project').Thanks for you reply Mike.
__________________ Syed | |
| |
| | #159 |
|
I would still like to see the code that did not work. I think we could have fixed it for you. 3v0
__________________ Please post questions to the forums. PM's are for personal communication. BCHS/3v0's Tutorials Junebug USB PIC programmer kit., USB Bit Whacker, The 15 Minute Printed Circuit Board! (+drill time) | |
| |
| | #160 |
|
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);
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.
__________________ Syed | |
| |
| | #161 |
|
Try, perc=(diff*100/ceilingrd); Mike. | |
| |
| | #162 |
|
Thanks Mike it works.
__________________ Syed | |
| |
|
| Tags |
| c18, questions |
| Thread Tools | |
| Display Modes | |
| |
Similar | ||||
| Title | Starter | Forum | Replies | Latest |
| A few questions | erosennin | Feedback/Comments | 24 | 29th November 2007 12:08 AM |
| 2 questions | juan123 | Electronic Projects Design/Ideas/Reviews | 5 | 27th September 2007 03:46 AM |
| A few questions. | Marks256 | General Electronics Chat | 55 | 5th August 2006 11:49 PM |
| few questions | Victor Frankenstein | General Electronics Chat | 13 | 5th July 2005 07:29 PM |
| Questions? | Philipc | Electronic Projects Design/Ideas/Reviews | 4 | 7th August 2003 07:18 PM |