Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 15th June 2009, 09:27 PM   #31
Default

I'll have to give this a try with the junebug Looks good Mike. futz It's good to see you been a long time haven't seen you glad to see your still with us.I just tried it with a cheapo parallax servo works great Thanks Mike. good work.

Last edited by be80be; 16th June 2009 at 01:39 AM.
be80be is offline  
Old 16th June 2009, 03:28 AM   #32
Default

Quote:
Originally Posted by Triode View Post
Thanks, I see a lot of the variables you used, but with "bits" at the end, what does that do?
It's the way C18 accesses the individual bits in a variable. Other compilers use a syntax like adcon0.GO=1 (BoostC) but Microchip decided that typing practice was good for people and added bits into the middle of it and so in C18 it's ADCON0bits.GO=1. Another way to write it is ADCON0|=2; as the GO bit is bit 1 (I think).

Mike.
Pommie is online now  
Old 16th June 2009, 03:31 AM   #33
Default

Quote:
Originally Posted by be80be View Post
futz It's good to see you been a long time haven't seen you glad to see your still with us.
Haven't seen Futz for a long time, he seems to have stopped visiting. I guess that you didn't notice that this thread is over a year old.

Mike.
Pommie is online now  
Old 16th June 2009, 03:52 AM   #34
Default

That why I didn't see it till today Triode stated it back up
Quote:
I guess that you didn't notice that this thread is over a year old.
dope till I posted two times lol I looked at futz site to day he got into bigger toys

Last edited by be80be; 16th June 2009 at 03:54 AM.
be80be is offline  
Old 16th June 2009, 02:04 PM   #35
Default

Well, like I said when I replied to it, I googled for exactly what this thread was about and ended up finding it here, where I already post, so why not bring it back.

Thanks for clairifying that pommie, I had found the list of what each bit did in the variables, but I had never seen "bits" written in like that.
Triode is offline  
Old 10th October 2009, 03:45 AM   #36
Default

If anyone is having problems with the interrupt driven version it is because I used the wrong interrupt vector.

The line,
Code:
#pragma code low_vector=0x18    //setup the ISR vector
Should be,
Code:
#pragma code low_vector=0x08    //setup the ISR vector
The old version of the C18 compiler put NOPs between the vectors and so the code worked. The new version of the compiler puts code there and so it crashes.

I noticed this as I needed the code to align a servo that had stripped its gears.

Mike.

Last edited by Pommie; 10th October 2009 at 03:46 AM.
Pommie is online now  
Old 24th October 2009, 08:15 AM   #37
Default

Quote:
Originally Posted by Pommie View Post
Here is a version that will go fully left if button 1 pressed, center if button 2 pressed and fully right if button 3 pressed. If you press all 3 buttons together it will take up the position defined by the pot.

I also changed it to use interrupts because, as Mike said, it isn't as intuitive as one would imagine.

Mike.
Code:
#include <p18f1320.h>
#pragma config WDT = OFF, LVP = OFF, OSC = INTIO2

#define ServoPin LATBbits.LATB3
#define ServoTris TRISBbits.TRISB3

void ccp1_isr();                    

volatile int ServoPos;          //used to hold the servo position

#pragma code low_vector=0x18    //setup the ISR vector
void low_interrupt (){
  _asm GOTO ccp1_isr _endasm    //jump to interrupt handler
}
#pragma code

#pragma interruptlow ccp1_isr   //the ISR
void ccp1_isr(){
    if(ServoPin==1){            //will be 1 if we are at end of pulse
        ServoPin=0;             //turn off servo output
        CCPR1=20000-ServoPos;   //Off time = 20mS - Servo Time
    }
    else{
        ServoPin=1;             //turn on servo output
        CCPR1=ServoPos;         //On time 
    }
    PIR1bits.CCP1IF=0;          //clear int flag
}
#pragma code

void main(){
    OSCCON=0x70;                //Osc=8MHz
    ADCON0=0b00000101;          //A2D on and select AN1
    ADCON1=0x7d;                //A1 = analogue
    ADCON2=0b10110101;          //Right justify - Fosc/16
    ServoTris=0;                //make bit 0 output
    ServoPin=0;                 //Servo output off
    CCP1CON=0b00001011;         //Special event trigger
    T1CON=0b10010001;           //Timer 1 on with Pre=2
    ServoPos=1500;              //set servo to mid position
    CCPR1=ServoPos;             //set CCP initial value   
    PIE1bits.CCP1IE=1;          //enable CCP1 interrupt
    INTCONbits.PEIE=1;          //enable peripheral interrupts
    INTCONbits.GIE=1;           //enable glogal interrupts
    INTCON2bits.RBPU=0;         //enable port b week pullups
    while(1){                   //loop forever
    static char Mode=0;             //0 = use ADC, 1=fixed pos
    static char Keys;               //holds previous key values
    char OldKeys,Edges;             //local variables
        while(!ServoPin);           //Wait for start of servo pulse
        while(ServoPin);            //wait for end - makes for good debounce
        OldKeys=Keys;               //make a copy of keys
        Keys=PORTB&0b00100101;      //get switch state
        Keys^=0b00100101;           //make pressed keys = 1
        if(Keys==0b00100101)        //all 3 pressed?
            Mode=0;                 //yes, set to use ADC input
        Edges=(Keys^OldKeys);       //keep only keys that have changed
        Edges&=Keys;                //keep only new key presses - not key releases
        if(Mode==0){                //are we using the pot?
            ADCON0bits.GO=1;        //yes, start conversion
            while(ADCON0bits.GO);   //Wait for it to complete
            ServoPos=ADRES+1000;    //Pos will be 1mS to 2.023mS
        }
        if(Edges!=0)                //any key pressed
            Mode=1;                 //switch to fixed mode
        if(Edges==0b00000001)       //Key 1 pressed
            ServoPos=1000;          //set servo fully left
        if(Edges==0b00000100)       //key 2?
            ServoPos=1500;          //set center
        if(Edges==0b00100000)       //key 3?
            ServoPos=2000;          //set fully right
    }
}
hi mike,

thanks for putting this code up. my application basically uses a PIC18 with a 10Mhx crystal connected to it. Will i have to make any modifications to this code, especially with regards to the delays?or is it the same regardless of the clock speed?
yohanevindra is offline  
Old 24th October 2009, 10:00 AM   #38
Default

With a 10MHz clock a 1mS pulse will be 1250 cycles. So multiply all time by 1.25 to get the new times. The line ServoPos=ADRES+1000; will need to be ServoPos=ADRES*5/4+1250;

Note also the interrupt vector is wrong. See the post above yours.

Mike.
Pommie is online now  
Old 24th October 2009, 03:47 PM   #39
Default

how did you do the maths for it?im a bit confused :S

no mike, ur interrupt vector is correct..low priority interrupts are a 0x0018 and high priority is at 0x0008...
yohanevindra is offline  
Old 25th October 2009, 04:42 AM   #40
Default

The maths is simple, at 8MHz the timer would be clocked at 2MHz if the prescaler wasn't used. I set the prescaler to 2 and so the timer is clocked once every microsecond. With a 10MHz clock it is 10/8 (=1.25) times faster.

The interrupt vector would be correct if I used interrupt priorities but I chose to use compatibility mode and so all interrupts go to the vector at 0x08.

Mike.
Pommie is online now  
Old 25th October 2009, 07:19 AM   #41
Default

Code:
#include <p18f452.h>

#define ServoPin PORTAbits.RA2

int ServoPosition;

void CCP2_ISR();

#pragma code high_pri_vec = 0x0008
void high_pri_vec()
{
	_asm
	GOTO CCP2_ISR
	_endasm
}

#pragma interrupt CCP2_ISR
void CCP2_ISR()
{	
	if(ServoPin==1)
	{
		ServoPin = 0;
		CCPR2 = 25000 - ServoPosition;
	}
	else
	{
		ServoPin = 1;
		CCPR2 = ServoPosition;
	}
	PIR2bits.CCP2IF = 0;
	
}

#pragma code
void main()
{
	ServoPin = 0;
	TRISAbits.TRISA2 = 0; //Set Pin A2 as an output for the Servo
	CCP2CON = 0b00001011; //Setup CCP2 to generate special event
	CCPR2 = ServoPosition;
	T3CON = 0b10011001; //Setup timer3, prescaler=2
	ServoPosition = 1875; //Mid point position for Servo
	TMR3H = 0;
	TMR3L = 0;
	PIE2bits.CCP2IE = 1; //Enable CCP2 interrupt
	INTCONbits.PEIE = 1; //Enable peripheral interrupts
	INTCONbits.GIE = 1; //Enable all interrupts
	while(1);
}
i modified the code, since i needed a program which enabled the servo position to be controlled outside of the main servo control program. basically this is a servo control program....i was testing it in the MPLAB SIM, and it seems like it is stuck in an endless loop. the timer goes up to a value above 200, but never above 300, and then simply restarts. as a result, the compare never occurs. what's wrong here?

ok.so it seems like timer3 isnt accessing the high byte and counting up only to 256. what am i doing wrong?

Last edited by yohanevindra; 25th October 2009 at 07:50 AM.
yohanevindra is offline  
Old 25th October 2009, 08:04 AM   #42
Default

You are still using Timer1 for the CCP module. Try T3CON = 0b11011001;

Mike.
Pommie is online now  
Old 25th October 2009, 08:14 AM   #43
Default

i changed tht...its still the same..now it seems to be giving me random values...

any other registers i need to configure?
yohanevindra is offline  
Old 25th October 2009, 08:19 AM   #44
Default

You need to make A2 digital with ADCON1=0x07;

Analogue pins always read back as zero.

Mike.
Pommie is online now  
Old 25th October 2009, 10:19 AM   #45
Default

same problem...for some reason the timer is not counting into the high byte, so hence its maximum value is only 256.

im looking at the timer through the watch window, and it doesnt seem to go upto the value specified in the CCPR register, but then when i looked at the simulator logic analyser, and measured the pulses, it seems like its working. to measure the frequency of pulses in the logic analyser, do i just need to line the cursor with the respective point and then multiply the no of cycles by (Fosc/4)?

Last edited by yohanevindra; 25th October 2009 at 10:23 AM. Reason: Seems like it works now
yohanevindra is offline  
Reply

Tags
c18, code, junebug, servo

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
MP Lab Program Help bamafan54 Micro Controllers 5 7th January 2009 03:16 PM
servo motor program code basf_12 Electronic Projects Design/Ideas/Reviews 8 31st October 2006 03:57 AM
Tough assembly program for the PIC16F84 asmpic Micro Controllers 34 3rd December 2004 07:50 PM
how to understand this code!!! indie Electronic Projects Design/Ideas/Reviews 3 11th September 2004 08:39 PM
An error in pic16f84a, why? Zener_Diode Micro Controllers 6 11th April 2004 03:55 AM



All times are GMT. The time now is 06:37 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker