Buy an Axon, Axon II, or Axon Mote and build a great robot, while helping to support SoR.
0 Members and 1 Guest are viewing this topic.
#include <pic.h>__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \ & UNPROTECT & BORDIS & IESODIS & FCMDIS);int x, y;delay(int xvalue,int yvalue){for(x = 0; x < xvalue; x++);for(y = 0; y < yvalue; y++);return; }main(){ PORTA = 0; CMCON0 = 7; ANSEL = 0; TRISA4 = 0; while(1 == 1) // Loop forever { RA4 = 1; // Pulse high delay(2,200); RA4 = 0; // Low delay(19,243); // 18 ms }}
#include <pic.h>__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \ & UNPROTECT & BORDIS & IESODIS & FCMDIS);int x, y;delay(int xvalue,int yvalue){for(x = 0; x < xvalue; x++);for(y = 0; y < yvalue; y++);return; } main(){ PORTA = 0; CMCON0 = 7; ANSEL = 0; TRISA4 = 0; TRISA3 = 1; while (1) { if (RA3 == 1) { RA4 = 1; // Pulse high delay(2,200); RA4 = 0; // Low delay(19,243); // 18 ms } else { RA4 = 1; // Pulse high delay(1,500); RA4 = 0; // Low delay(19,243); // 18 ms } }}
while (1 == 1) { RA4 = 1; // Pulse high delay(2,200); RA4 = 0; // Low delay(19,243); // 18 ms }
set pin low for 20msbring pin high for anywhere between 1ms and 2ms (1.5ms being center)repeat
// Sweep// by BARRAGAN <http://barraganstudio.com> #include <Servo.h> Servo myservo; // create servo object to control a servo int pos = 0; // variable to store the servo position void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } }
[quote author=Canabots link=topic=5967.msg45559#msg45559 date=1228072371]while (1 == 1) { RA4 = 1; // Pulse high delay(2,200); RA4 = 0; // Low delay(19,243); // 18 ms }
I can't help you with that, I'm programming my PIC with assembler. An tip those: Make sure the code you found was for your version of the "C" compiler! My guess would be that it's not for your compiler.
just an FYI, programming in assembler is going to progress very slow.Assembler is the most powerfull code possible, ever. But it's very hard to code complicated programs, therefor taking much more time.I strongly recommend using C.Very powerfull (second most powerfull of all languages), and allot faster and easier than asm.
Does anyone know other ways of creating delays? I heard there was a sleep function that could suspend and action timed in milliseconds. Any clues?
Oh! By "sleep", I didn't mean the microcontroller shuts down (if that's how you interpretted it, I should have explained it better. Sorry!). The Microcontroller would stay awake, it would just let the instruction pause for whatever amount of milliseconds.
I have looked into file linking, and how many people use that for their projects so they only have to state something in the program, and it will be interpreted and execute (I'm likely mistaken, so correct me f I'm wrong). Does anyone have experience with this?
To confuse you further I've noticed a similar FUNCTION used with the "C" or "PICBASIC" language that takes one parameter and works similar to what you call delay.
include <pic.h>__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & BORDIS & IESODIS & FCMDIS);int x, y;delay(int xvalue,int yvalue){for(x = 0; x < xvalue; x++);for(y = 0; y < yvalue; y++);return; }main(){ PORTA = 0; CMCON0 = 7; ANSEL = 0; TRISA2 = 0; while(1 == 1) // Loop forever { RA2 = 1; // Pulse high delay(0,300); RA2 = 0; // Low Pulse delay(12,222); }}
I'll get back with the code as soon as I get home (I'm at work now). I think there's a bit of time-zone difference between us, you might only see the answer on your tomorrow
delay(int xvalue,int yvalue){ for(x = 0; x < xvalue; x++); for(y = 0; y < yvalue; y++); return; }
#include "delay.h"
DelayMs(250); DelayMs(250); DelayMs(250); DelayMs(250);
DelayMs(1); DelayUs(250); DelayUs(250);
DelayMs(41);
#include <pic.h>#include "delay.h"__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & BORDIS & IESODIS & FCMDIS);main(){ TRISA = 0b11111011; // You need to make the TIRS bit "0" to make the bit an output; Using a binary constant is easy to read while(1 == 1) { RA2=1; // Make the bit high DelayMs(1); DelayUs(250); DelayUs(250); RA2=0; // The bit goes low DelayMs(41); }}
But I don't understand how to loop the servo pulse cycles and make them go in sequenced movements.
#include <pic.h>#include "delay.h"__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & BORDIS & IESODIS & FCMDIS);main(){ TRISA = 0b11111011; // You need to make the TIRS bit "0" to make the bit an output; Using a binary constant is easy to read while(1 == 1) { RA2=1; // Make the bit high DelayMs(1); DelayUs(250); DelayUs(250); RA2=0; // The bit goes low DelayMs(18); }}
#include <pic.h>#include "delay.h"__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & BORDIS & IESODIS & FCMDIS);// This routine takes two parameters, from 0 to 250, that will represent 250 possible positions// for every one of the two servos.void servopulse(unsigned char PulseRA2, unsigned char PulseRA1) { // First pulse RA2 RA2=1; DelayMs(1); Delay(PulseRA2); Delay(PulseRA2); RA2=0; // Now pulse RA1 RA1=1; DelayMs(1); Delay(PulseRA1); Delay(PulseRA1); RA1=0; // Wait for a while; The delay is shorter then the // original delay to account both 2ms pulses DelayMs(16); }// This is one possible first sequence for the servos. Both servos will move at the same time// to the same positions. I want the full swing from left to right to take about 10 seconds; Since I'll// have 250 steps for each servo, the servo will need to hold each of those 250 steps for// (20 * 1000) / 250 = 40 ms. Since the "servopulse" routine takes about 20 ms I'll call that routine// twice for each of the 250 positions.void sequence1(void) { unsigned char pos; // swing from left to right for(pos=0;pos<250;pos++) { servopulse(pos,pos); servopulse(pos,pos); } // swing from right to left for (pos=250;pos>0;pos--) { servopulse(pos,pos); servopulse(pos,pos); }}// This second sequence will make the servos move in oposite directions. This time I want the swing// to take about 2.5 seconds; If I'd stop at each of the 250 possible positions along the way// I'd need to make each position hold for only 10ms. This is not possible because the "servopulse"// routine takes about 20ms to run, so I'll simply skip steps.void sequence2(void) { unsigned char pos; unsigned char pos2; // First swing for (pos=0;pos<250;pos=pos+2) { pos2=250-pos; servopulse(pos,pos2); } // Reverse swing for (pos=0;pos<250;pos=pos+2) { pos2=250-pos; servopulse(pos2,pos); }}main(){ TRISA = 0b11111001; // Make RA2 and RA1 outputs // Do the first sequence once sequence1(); // Look pthe second sequence for ever while(1 == 1) sequence2(); }