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.

creating PWM using PIC

Status
Not open for further replies.
From you older posts, I understand, you have a continuous rotation servo. And that you need to rotate the shaft 180 degrees to the right and to the left, in a loop.
try this -
1) Write a subroutine to turn the motor to the left - say, goLeft();
2) Write another to turn to right - say goRight();
Find the time for the shaft to reach your 180 degrees. Write a subroutine for the time delay

call goLEft and goRight with the delay subroutine in between, and put the entire thing in a loop.

This is, assuming you know how to turn your motor to the left and to the right. If you have variations in the load at shaft, the time will change. If the positioning is critical, put a sensor at the 180 degree point. Then wait for the shaft to reach that point before you call either of the goLeft or goRight subroutines.
im not too good at programming , because that i ask on this forum

hi allbits
i dont understand what you are saying
i just need the asm/C file :)
 
Last edited:
hi allbits
i dont understand what you are saying
i just need the asm/C file

Just giving the asm file may not work, because I got no clue about your motor, and the delays will be definitely different.
 
No, servos only have a fairly limited movement - which the specs don't seem to mention?.

It certainly couldn't be 270 degrees in each direction, that would require 540 degrees total movement, and the pot can only move a possible maximum of 270 degrees.

I think I was pretty clear in my post. Going from -90° to +90° and back again is going 180° in each direction. Going (nearly) 270° in each direction requires a total movement of 270°, not 540°. Did you really not understand such a simple concept?:D

Mike.
 
Last edited:
Do you want a simple circuit that tests a servo? See Talking Electronics: "50 - 555 Circuits" for more details
**broken link removed**

**broken link removed**
 
Last edited:
Here you go, BoostC source code for a 16F886.
Code:
#include <system.h>

#pragma DATA _CONFIG, _DEBUG_ON & _MCLRE_ON
                    & _CP_OFF & _PWRTE_OFF
                    & _WDT_OFF & _INTOSCIO
                    & _LVP_OFF
#pragma CLOCK_FREQ 8000000

#define Servo portb.0
#define ServoTris trisb.0

void WaitInt();
unsigned int Position;
unsigned char PK2_reserved@0x70;
unsigned int ccp1 @CCPR1L;

void main(void){
    while(!osccon.HTS);
    osccon=0x71;                //8 meg
    ansel=0;
    anselh=0;
    adcon1=0x0f;
    ServoTris=0;                //make servo pin output
    Servo=0;                    //Servo output off
    ccp1con=0b00001011;         //Special event trigger
    t1con=0b10010001;           //Timer 1 on with Pre=2
    Position=1500;              //set servo to mid position
    pie1.CCP1IE=1;
    intcon.GIE=1;
    intcon.PEIE=1;
    while(1){
        while(Position<2000){
            WaitInt();
            Position+=10;
        }
        while(Position>1000){
            WaitInt();
            Position-=10;
        }

    }
}

void WaitInt(){
    while(Servo);       //wait for low
    while(!Servo);      //wait for high
}

void interrupt(void){
    if(Servo){
        Servo=0;
        ccp1=20000-Position;
    }else{
        Servo=1;
        ccp1=Position;
    }
    pir1.CCP1IF=0;
}
The above uses the internal oscillator and outputs the pulse on B0. It takes 2 seconds to go from one extreme to the other.

Mike.
 
Last edited:
Here you go, BoostC source code for a 16F886.
Code:
#include <system.h>

#pragma DATA _CONFIG, _DEBUG_ON & _MCLRE_ON
                    & _CP_OFF & _PWRTE_OFF
                    & _WDT_OFF & _INTOSCIO
                    & _LVP_OFF
#pragma CLOCK_FREQ 8000000

#define Servo portb.0
#define ServoTris trisb.0

void WaitInt();
unsigned int Position;
unsigned char PK2_reserved@0x70;
unsigned int ccp1 @CCPR1L;

void main(void){
    while(!osccon.HTS);
    osccon=0x71;                //8 meg
    ansel=0;
    anselh=0;
    adcon1=0x0f;
    ServoTris=0;                //make servo pin output
    Servo=0;                    //Servo output off
    ccp1con=0b00001011;         //Special event trigger
    t1con=0b10010001;           //Timer 1 on with Pre=2
    Position=1500;              //set servo to mid position
    pie1.CCP1IE=1;
    intcon.GIE=1;
    intcon.PEIE=1;
    while(1){
        while(Position<2000){
            WaitInt();
            Position+=10;
        }
        while(Position>1000){
            WaitInt();
            Position-=10;
        }

    }
}

void WaitInt(){
    while(Servo);       //wait for low
    while(!Servo);      //wait for high
}

void interrupt(void){
    if(Servo){
        Servo=0;
        ccp1=20000-Position;
    }else{
        Servo=1;
        ccp1=Position;
    }
    pir1.CCP1IF=0;
}
The above uses the internal oscillator and outputs the pulse on B0. It takes 2 seconds to go from one extreme to the other.

Mike.
hi thanks pommie
i want to try it but i loss my 16F886 while experimenting with my other pic
uh , i cant remember it :(
gotta need to find it !
 
Status
Not open for further replies.

Latest threads

Back
Top