Shop OBEX P1 Docs P2 Docs Learn Events
Flip and propellerC language; RESOLVED - Page 2 — Parallax Forums

Flip and propellerC language; RESOLVED

245678

Comments

  • Ingenious, you guys should get together and write a book about Prop C.
    thank you
  • The "book" is available by just clicking on "Help" in SimpleIDE. Most of the information is in the various links.
  • bbrienbbrien Posts: 561
    edited 2020-02-04 03:49
    #include "simpletools.h"
              #include "propeller.h"
              #define clkset  (XTAL1*PLL1x,5000000)
                         
              int main()
               {
                 int mask = 0xff0000;
    
                 int  freq = CLKFREQ>>1;
    
                 DIRA = mask;
    
               while(1)
                {
                  int button1 = input(3);
                  if(button1 == 1)
                    {
                      high(17);
                      pause(200);
                      low(17);
                      pause(200);
                     }
                    else
                     {
                       low(17);
                     }
                  int button2 = input(4);
                  if(button2 == 1)
                    {
                      high(18);
                      pause(200);
                      low(18);
                      pause(200);
                    } 
                   else
                    {
                      low(18);
                    }
                  int button3 = input(5);
                  if(button3 ==1)
                    {
                      high(19);
                      pause(200);
                      low(19);
                      pause(200);
                    }     
                   else                                                                     
                    {
                      low(19);
                    }
                  int  button4 = input(6);
                  if(button4 == 1)
                    {
                      high(20);
                      pause(200);
                      low(20);
                      pause(200);
                    }
                   else
                    {
                      low(20);
                    }
             }
         }
    

  • Dave HeinDave Hein Posts: 6,347
    edited 2020-02-02 03:32
    You are not using clkset() correctly. You are using a #define to redefine it. Notice that the compiler is giving a warning about this. You should call clkset() at the beginning of your program by adding the line clkset(0x6b,5000000);" at the beginning of the main routine.

    You have a typo where you declare freq, and initialize it to "CLKFREQ>>!". You probably meant "CLKFREQ>>1".

    After fixing these problems the program compiles OK. I don't get an error about an "else" without a previous "if".
  • correction made , but still having the same problem.
  • Dave HeinDave Hein Posts: 6,347
    edited 2020-02-02 04:12
    Can you attach your *.c file instead of including it in a code block? Maybe you have some weird character in your file that isn't visible in the code block.
  • Dave HeinDave Hein Posts: 6,347
    edited 2020-02-02 04:24
    Make sure you don't have a statement or a semicolon immediately after the if statement. That would cause the error you're seeing. So instead of
                  if(button3 ==1)
                    {
                      high(19);
                      pause(200);
                      low(19);
                      pause(200);
                    }     
                   else                                                               
                    {
                      low(19);
                    }
    
    if you had
                  if(button3 ==1);
                    {
                      high(19);
                      pause(200);
                      low(19);
                      pause(200);
                    }     
                   else                                                               
                    {
                      low(19);
                    }
    
    you would see the error.

    I have accidentally created errors like that in the past when I added a debug print after the if statement, but before the "{".
  • Corrected the mistakes and it builds now and loads, however the outputs are not right. If I hold the button down the output pins should remain active flashing continuously until I release the button, but it doesn't. If I depress the button the corresponding led stays lit for approximately 2 seconds and then shuts off and doesn't reactivate for a second or two.
  • Dave HeinDave Hein Posts: 6,347
    edited 2020-02-04 13:43
    Please attach your code. I suspect it has something to do with the way you set the clock frequency.

    Try holding the button down for 20 seconds. I'm guessing that you'll see it toggle at a very low rate.
  • complete program is about 5 or 6 posts back.
  • That is clearly not the program you were running. That code doesn't even compile. If you'd like some help, please attach your current code. Otherwise, we're just guessing at what changes you made that may be causing the current problem.
  • bbrienbbrien Posts: 561
    edited 2020-02-06 02:43
    Sorry about that,wrong sheet. New sheet
     #include "simpletools.h"
     #include "propeller.h"
      
     int main()
      {
       clkset(0x6b,50000000);
    
       int mask = 0xff0000;
       int freq = CLKFREQ>>1;
       DIRA = mask;
    
     while(1)
      {
       int button1 = input(3);
       if(button1 == 1)
        {
         high(17);
         pause(200);
         low(17);
         pause(200);
        }
       else
        { 
         low(17);
        }
      int button2 = input(4);
      if(button2 == 1)
       {
        high(18);
        pause(200);
        low(18);
        pause(200);
       }
      else
       {
        low(18);
       }
      int button3 = input(19);
      if(button3 == 1)
       { 
        high(19);
        pause(200);
        low(19);
        pause(200);
       }
      else
       {
        low(19);
       }
      int button4 = input(20);
      if(button4 == 1)
       {
        high(20);
        pause(200);
        low(20);
        pause(200);
       }
      else
       { 
        low(20);
       }
      }
    }
    
  • I ran the program on my propeller development board, and it works OK for me. Of course, I had to fix the call to clkset first. It was missing the right parenthesis.

    What board are you using for the propeller? Did you build it yourself, or are you using a pre-built one? Have you run any other programs on this board?
  • bbrienbbrien Posts: 561
    edited 2020-02-06 19:48
    Flip from parallax running at 5MHz. I changed the pause amounts to a range of 20 to 70 and they flash much faster now. Now I want to generate a slow PWM to run my motors. I will include my next program. It doesn't produce the desired results, please check it out. It turns on but no PWM pulse.
     #include "simpletools.h"
     #include "propeller.h"
     int main()
      { 
       clkset(0x6b,5000000);
       int microsecond = CLKFREQ/1000000;
       int highClocks = microsec * 10; 
       int lowClocks = microsec * 90;
       
       int mask = 0xff0000;
       
       int freq = CLKFREQ>>1;
    
       DIRA = mask;
    
       while(1)
        {
         int button1 = input(3);
         if(button1 == 1)
           {
            OUTA |= (1<<17);
            waitcnt( CNT + highClocks) ;
            
            OUTA &= ~(1<<17);
            waitcnt( CNT + lowClocks );
          }
        int button2 = input(4);
        if(button2 ==1)
          {
           OUTA |=(1<<18);
           waitcnt( CNT + highClocks);
    
          OUTA &= ~(1<<18);
          waitcnt( CNT + lowClocks);
          }
       int  button3 = input(5);
       if(button3 == 1)
        { 
         OUTA |= (1<<19);
         waitcnt( CNT + highClocks);
    
         OUTA &= ~(1<<19);
         waitcnt( CNT + lowClocks);
         }
       int  button4 = input(6);
       if(button4 == 1)
        {
         OUTA |= (1<<20);
         waitcnt( CNT + highClocks);
    
         OUTA &= ~(1<<20);
         waitcnt( CNT + lowClocks);
         }
        }
    }
    
  • I looked at int pwm_start(unsigned int cycleMicroseconds ) and void pwm_set( int pin, int channel, int tHigh ), but I guess I don't understand
    how to setup and use them. can someone please show me an example
    with multiple outputs . all I got was a multitude of errors.
  • Here's an example taken from the Propeller C tutorials. This tutorial also shows how to use a remote joystick to control a robot using X-Bee radios to send the joystick data to the motor control. The tutorial starts here:

    learn.parallax.com/tutorials/language/propeller-c/dc-motor-control-project
    
    /*This application uses PWM to drive two DC motors. Maximum PMW is set at 1000 with command "pwm_start(1000)". This application program is entended 
    for use with a two H-bridges in this case the Pololu Motor Driver Carrier Board(28820). Also used in this application two DC gear motors(28819)
    P3 and P4 are used to drive the motors in a forward motion(includes turning). P2 and P5 are used to move in reverse. If you want to use only one
    motor comment out or remove pins 3 and 4 OR pins 2 and 5 from the code.
    */
    #include "simpletools.h"
    #include "fdserial.h"
    #include "abdrive.h"
    
    int main()                                 // Main function  
    {
      pause(250);
      // Keep pins low if unused.
      set_outputs(5, 2, 0b0000);
      set_directions(5, 2, 0b1111);
      
      fdserial *xbee;
                                                 
    {
      xbee = fdserial_open( 9, 8, 0, 9600 );   // Begin the serial connection ( this is why we
                                               // needed the jumper cables connected to pins 8 and 9 )
      char data; 
                                               // Create the variable that will be used to hold incoming data
      pwm_start(1000); 
                                                  
      while ( 1 )                              // Repeat this forever or until loss of power
      {
        data = fdserial_rxChar( xbee );        // Set data to the data received from the XBee board
     
        
        if ( data == 'f' )                     // If the data incoming is telling the robot to move forward
        {
        //(pin, pwm channel, 800/1000)         // Move forward 80 percent speed (800 of 1000)                                      
         pwm_set(3, 1,800);                    // left wheel command set
         pwm_set(4, 0,800);                    // right wheel command set
                                       
        }
        //(pin, pwm channel, 800/1000)
        else if ( data == 'b' )                // If the data incoming is telling the robot to move backward, move backwards
        {
                                               // Move backwards 80 percent speed (800 of 1000)
          pwm_set(2, 0,800);                   // left wheel command
          pwm_set(5, 1,800);                   // right wheel command                                       
        }
        
       
        else if ( data == 'l' )                // If the data incoming is telling the robot to turn left, turn left
        {                                      // Turn right motor faster than left motor to make a right turn
          pwm_set(4, 0,850);                   // right wheel command
          pwm_set(3, 1,550);                   // left wheel command                               
        }
        
        else if ( data == 'r' )                // If the data incoming is telling the robot to turn right, turn right
        {                                      // Turn left motor faster than right to make a right turn
          pwm_set(3, 0, 850);                   
          pwm_set(4, 1, 550);                           
        }
        
        else if ( data == 's' )                // If the data incoming is telling the robot to stop, then stop
        {                                      
          pwm_set(2, 0, 0);  
          pwm_set(5, 1, 0); 
          pwm_set(3, 0, 0);
          pwm_set(4, 1, 0);                           
        }  
        
    
    }
    }
    }
    
  • I use the statement "if (button1 == 1)
    {
    pwm_set(17, 0, 500);
    }
    else if( button2 == 1)
    { I get an error: :'else' without a previous 'if'. Why is the IDE unable to recognize the previous'if'.?
  • Do you get any other warnings or errors? It would really help if you attached your source file so that we could duplicate the problem.
  • bbrienbbrien Posts: 561
    edited 2020-02-08 20:00
    I will send the entire page,
     #include "simpletools.h"
     #include "propeller.h"
    
     int main()
      {
       clkset = 0x6b, 5000000;
       
       int mask = 0xff0000;
    
       int freq = CLKFREQ>>1;
    
       DIRA = mask;
    
       int button1 = input(3);
       int button2 = input(4);
    
       pause(250);
    
       pwm_start(3000);
    
     while(1)
      {
       int button1 = input(3);
       if (button1 ==1)
        {
          pwm_set(17, 0, 1000);
        }
        else
        {
          pwm_set(17, 0, 500);
        }
       int button2 = input(4);
       if(button2 == 1)
        {   
          pwm_set(18, 0, 500);
        }
        else
        {
          pwm_set(18, 0, 0);
        }
      
        {
        
      } 
     }
    
  • There were a number of errors in the program. Here is a corrected version showing the errors in comments.
    #include "simpletools.h"
     #include "propeller.h"
    
     int main()
      {
       clkset(0x6b, 5000000);    // **** error was "clkset = 0x6b, 5000000"
       
       int mask = 0xff0000;
    
       int freq = CLKFREQ>>1;
    
       DIRA = mask;               // ****  error was no ";" atend of statement
    
       int button1 = input(3);
       int button2 = input(4);
    
       pause(250);
    
       pwm_start(2000);
    
     while(1)
      {
       if (button1 ==1)
        {
          pwm_set(17, 0, 500);
        }
        else
        {
          pwm_set(17, 0, 0);
        }
       if(button2 == 1)
        {   
          pwm_set(18, 0, 500);
        }
        else
        {
          pwm_set(18, 0, 0);
        }
      if((button1 == 0) && (button2 == 0))   // ****  error was wrong syntax of if statement
        {
          pwm_set(17, 0, 100);              // ****  error was no ";" at end of statement
        }
      } 
     }
    

    Note, I don't know if the program does what you want since I don't have your hardware. But it compiles.
    When debugging a SimpleIDE program, look at the errors and start with the one on top. Look at the numbers in the error statement. The first number is the line number. Correct that error, rebuild, and if there are errors handle the next in order from the top down.
  • 'else' without a previous 'if' statement
    expected statement before ')' token
  • bbrien, something doesn't make sense here. Every time you paste code into your responses it is missing characters that would cause obvious compiler errors. However, you never mention those errors. You always mention other errors, such as an else without if. The errors that we get from your posted code are completely different from the errors that you get. I can only conclude that the code you are posting is not the same as the code you are compiling.

    PLEASE ATTACH YOUR SOURCE FILES INSTEAD OF PASTING THEM INTO YOUR POST!!!

    Use the "Attach a file" link to attach a file.
  • bbrien wrote: »
    'else' without a previous 'if' statement
    expected statement before ')' token

    Did you copy the code I put into my post and then just paste it into a blank Simple IDE screen? Or are you typing it in?

    In your previous code where you got 'else' without a previous 'if' statement error, the if statement had incorrect syntax so the following 'else' statement resulted in the 'else' without a previous 'if' statement.

    That is why it is important to scroll to the top of the error listing and correct errors from the top down.

    Be sure that you have ";" at the end of each statement

    Also check the line number for the error and look for the error on the same numbered line in the SimpleIDE listing.
  • dgatelydgately Posts: 1,620
    edited 2020-02-08 19:50
    bbrien,
    SimpleIDE lets you copy the error text so you can paste it in to the forum. Please use the Code 'C' (Ctrl-O) command within the forum post to include your text in a nice code-style format. I've added an error to the following code just to get the error text to display as an example:
    Screen%20Shot%202020-02-08%20at%2011.34.39%20AM.png

    Screen%20Shot%202020-02-08%20at%2011.36.55%20AM.png


    Error code example as copied from SimpleIDE's "Build Error Code" menu item:
    Project Directory: ./Documents/PropCCode/
    SimpleIDE Version 1.1.0
    ./Documents/SimpleIDE/Learn/Simple Libraries/
    ./Documents/SimpleIDE/ Updated on: 2019-10-22
    
    propeller-elf-gcc -v GCC 4.6.1 (propellergcc_v1_0_0-36-g4c46ecb)
    propeller-elf-gcc -I . -L . -I ./Documents/PropCCode/AB-IR-Remote-Wav/library/libsimpletools -L ./Documents/PropCCode/AB-IR-Remote-Wav/library/libsimpletools/cmm/ -I ./Documents/PropCCode/DS1302Demo/library/libsimpletext -L ./Documents/PropCCode/DS1302Demo/library/libsimpletext/cmm/ -I ./Documents/PropCCode/EdgeHeader/library/libsimplei2c -L ./Documents/PropCCode/EdgeHeader/library/libsimplei2c/cmm/ -o cmm/bbrien.elf -Os -mcmm -m32bit-doubles -fno-exceptions -std=c99 bbrien.c -lm -lsimpletools -lsimpletext -lsimplei2c -lm -lsimpletools -lsimpletext -lm -lsimpletools -lm
    bbrien.c: In function 'main':
    bbrien.c:27:7: error: expected '(' before '{' token
    Done. Build Failed!
    
    Click error or warning messages above to debug.
    

    This allows other forum posters to help you in solving your error.

    You can also create a .zip file of your whole project too attach to your posting (As I've done in this post). Look in the Project menu for the "Zip" command. See an attached .zip of a shortened (& repaired) version of your code, below...

    dgately
  • bbrienbbrien Posts: 561
    edited 2020-02-08 20:24
    Had a semicolon where it shouldn't have been and it took a long time before I saw it and it compiles now, now I have another problem. I get 13 rapid pulses and then no pulses for about 1 second + and then another group of pulses. What causes this and how do I correct. please refer to earlier post for revised program . thanks
  • New question regarding IIC communication, How hard is it. I have started reading the IIC library in simpleIDE.
  • kwinnkwinn Posts: 8,697
    IIC (aka I2C) is relatively simple at the hardware signalling level. It basically consists of 2 signals, a data signal (low or high) and a clock signal that indicates when a valid data bit is available. The communication protocols range from dead simple to fairly complex. See this page for a desription.
  • "This page" lost me. What I want to do is have the Flip read a signal from the Arduino which uses a single digit(4) as the address for I2C. What is the I2C address for the flip. My old sketch for the I2C read is as follows
       void receiveEvent (int  howMany)  //  received 4 bytes from hand controller
       { 
        if(howMany>=4)
        { 
         RmotorPinI2C = Wire.read();
         RdirPinI2C      = Wire.read();
         DmotorPinI2C = Wire.read();
         DdirPinI2C      = Wire.read();
         while(Wire.available()>0)
          {
            Wire.read ;
          }
         i2cFlag = true;
        }
      }
       void checkI2C()
        {
         if (i2cFlag ==true)
          {
           noInterrupts();
           analogWrite(rmotorPin,RmotorPinI2C);
           digitalWrite(rdirPin,RdirPinI2C);
           analogWrite(dmotorPin,DmotorPinI2C);
           digitalWrite(ddirPin,DdirPinI2C);
            interrupts();
           
            i2cFlag = false;
            }
          }
    
    How do I rewrite and what code lines do I use.
  • I am using an arduino as the master which uses an address of 4 and I want to use a propeller flip as a slave but I don't know what to use for the address to pass to the arduino, and what parts of simplei2c.h do I use to read the bytes from the above arduino code into the outputs of the 'flip'.
  • I'm going to have to give up on the idea of using I2C to communicate with the propeller Flip board. The Spin language has an assignment operator(:=) which copies the input directly to the output. Is the propeller C (==) the same or equivalent. I would prefer to use I2C so I wouldn't have to design a new board , but I cannot find any understandable examples. Is it possible to add spin code to the propeller C code.
Sign In or Register to comment.