Shop OBEX P1 Docs P2 Docs Learn Events
PropC confusion - Page 2 — Parallax Forums

PropC confusion

2

Comments

  • Beavis3215 wrote: »
    That was my typo, thanks Andy for the help.
    That is the point of the first statement? The value of t is set by the third statement and the value established by the first one is never used?

  • Rookie me, about six months ago I made this mistake on my spin program (which I never tested). I carried it into my C program. It has been such a long time since I have worked on my project I forgot and didn't think twice about it.
  • Beavis3215Beavis3215 Posts: 229
    edited 2016-11-07 00:14
    Why doesn't
    DIRA |= (0b111111 << 9);
    OUTA |= (0b111111 << 9);


    set bits 9..4 high?
  • Dave HeinDave Hein Posts: 6,347
    edited 2016-11-07 01:12
    You should shift by 4 instead of 9.
  • Thanks Dave
  • Beavis3215Beavis3215 Posts: 229
    edited 2016-11-15 00:50
    Is there a way in C to read data like in the BASIC language similar to this

    for (int i = 1; i <= 5; 1 ++)
    {
    read Data (i);
    }



    Data 1,5,7,9,10
  • Dave HeinDave Hein Posts: 6,347
    edited 2016-11-15 01:22
    You could just declare Data as an array, and initialize it with the values 1, 5, 7, 9 and 10. This program will loop through the array and print it out. Note that the first value of the array is located at Data[0].
    #include <stdio.h>
    
    char Data[] = {1,5,7,9,10};
    
    int main()
    {
        for (int i = 0; i < 5; i++)
        {
            printf("%d\n", Data[i]);
        }
        return 0;
    }
    
  • Thanks again Dave
  • What is the reason/function of "return 0" ?
  • By default the main function returns an int value. The convention is to return a zero on a successful return, and a non-zero value when a failure occurs. The return value really doesn't matter on the Prop.

    I could have used a void function for the example, but I wanted to show a program that could compile and run on the Prop or any other platform. I tested it under Cygwin just to make sure there weren't any errors.
  • I am attempting to convert the first program to the second program using what Dave showed me last night, but apparently im doing something wrong. The first works properly. When button pushed, 6 leds brighten in ten increments. After when pushed again leds dim in 10 increments.
    // PWM program 3
    
    #include "simpletools.h"
    
    void del(int time)
      {
        waitcnt(time + CNT);
      }
      
      
    int main()
    {
      
      
      int time_off = 40000;
      int debounce_time = 5000000;
      int sw_mask = 21;
      int out_mask = 9;
      
      DIRA |= (0b111111 << out_mask - 5);
      DIRA &= ~(1 << sw_mask);
      
      
      while(1)
      {
        waitpeq(1 << sw_mask, 1 << sw_mask);
        del(debounce_time);
        
        for (int time = 500; time <= 5000; time = time + 500)
        
        {
          for (int i = 1; i <= 500; i = i + 1)
          {
            OUTA |= (0b111111 << out_mask - 5);
            del(time);
            OUTA &= ~(0b111111 << out_mask - 5);
            del(time_off);
          }
                  
        }
        OUTA |= (0b111111 << out_mask - 5);
        waitpeq(1 << sw_mask, 1 << sw_mask);
        del(debounce_time);
        
        for (int time = 5000; time >= 500; time = time - 500)
        
        {
          for (int i = 1; i <= 500; i = i + 1)
          {
           OUTA |= (0b111111 << out_mask - 5);
           del(time);
           OUTA &= ~(0b111111 << out_mask - 5);
           del(time_off);
          }       
          
        }      
        
              
      }    
      
    }
    




    // PWM program 3
    // Makes 6 Leds brighten in 10 increments when button pushed.
    // Dims in 10 increments when button pushed.
    
    #include "simpletools.h"
    
    void del(int time)
      {
        waitcnt(time + CNT);
      }
      
    char Data[] = {500,1000,1500,2000,2500,3000,3500,4000,4500,5000};  
    int main()
    {
      
      
      int time_off = 40000;
      int debounce_time = 5000000;
      int sw_mask = 21;
      int out_mask = 9;
      
      DIRA |= (0b111111 << out_mask - 5);
      DIRA &= ~(1 << sw_mask);
      
      
      
      while(1)
      {
        waitpeq(1 << sw_mask, 1 << sw_mask);
        del(debounce_time);
        
        for (int time = 1; time <= 10; time++)   //instead of time generated by loop,
                                                 //time read from list using loop.
        {   
          for (int i = 1; i <= 500; i = i + 1)
          {
            OUTA |= (0b111111 << out_mask - 5);
            del(Data[time]);
            OUTA &= ~(0b111111 << out_mask - 5);
            del(time_off);
          }
                  
        }
        OUTA |= (0b111111 << out_mask - 5);
        waitpeq(1 << sw_mask, 1 << sw_mask);
        del(debounce_time);
        
        for (int time = 10; time >= 1; time--)
        
        {
          for (int i = 1; i <= 500; i = i + 1)
          {
           OUTA |= (0b111111 << out_mask - 5);
           del(Data[time]);
           OUTA &= ~(0b111111 << out_mask - 5);
           del(time_off);
          }       
          
        }      
        
              
      }    
      
    }
    
  • AribaAriba Posts: 2,690
    edited 2016-11-16 04:28
    The Data array index goes from 0 to 9 and not from 1 to 10 and for values up to 5000 a char is not the right size.
    In your code you read index 10 which likely returns zero as a result, so the del() waits for a counter overflow (54 seconds) between fading up and down.

    Some hints:
    All these (1 << out_mask - 5) makes no sense, you define this out_mask variable at begin so that you can change it at one place and don't need to do it at several places in the code:
    int out_mask = 4; //instead of 9

    And if you call it "mask" then you should use a mask and not a pinnumber, these spares all the '1 <<' in the code.

    Here is a modified version that might work, but I have not testet it:
    // PWM program 3
    // Makes 6 Leds brighten in 10 increments when button pushed.
    // Dims in 10 increments when button pushed.
    
    #include "simpletools.h"
    
    void del(int time)
      {
        waitcnt(time + CNT);
      }
      
    int Data[] = {500,1000,1500,2000,2500,3000,3500,4000,4500,5000};
    
    int main()
    {
      
      int time_off = 40000;
      int debounce_time = 5000000;
      int sw_mask = 1<<21;           //bitmask for pin 21
      int out_mask = 0b111111<<4;    //bitmask for pins 9..4
      
      DIRA |= out_mask;
      DIRA &= ~sw_mask;
      
      
      
      while(1)
      {
        waitpeq(sw_mask, sw_mask);
        del(debounce_time);
        
        for (int time = 0; time <= 9; time++)    //instead of time generated by loop,
                                                 //time read from list using loop.
        {   
          for (int i = 1; i <= 500; i++)
          {
            OUTA |= out_mask;
            del(Data[time]);
            OUTA &= ~out_mask;
            del(time_off);
          }
                  
        }
        OUTA |= out_mask;
        waitpeq(sw_mask, sw_mask);
        del(debounce_time);
        
        for (int time = 9; time >= 0; time--)
        
        {
          for (int i = 1; i <= 500; i++)
          {
           OUTA |= out_mask;
           del(Data[time]);
           OUTA &= ~out_mask;
           del(time_off);
          }       
          
        }      
        
              
      }    
      
    }
    

    Andy
  • Beavis3215Beavis3215 Posts: 229
    edited 2016-11-16 23:49
    That did work. I appreciate your help. There are a lot of pieces to the C puzzle. Thanks for your pointers. Kind of funny how people with lots of experience just know these things. With me, sometimes I don't even know the wording of how to look it up. I guess I am kind of an expert in my area of expertise to others outside the circle.
  • Beavis3215Beavis3215 Posts: 229
    edited 2016-11-21 16:28
    For some reason I cant figure this out. How would you use clkset to configure the propeller clock to run at 80mhz using a 5mhz crystal?
    #include "simpletools.h" 
    clkset(xtal1 + pll16x, 5000000)
    


    This is what I gather from prop.h library, but it wont compile.
    I can never figure out the syntax properly from the propeller.h library, what am I doing wrong?
    Another thing I just thought of is that will I need to change the compiler settings? From what I have read, the prop speed is selected by the board type selected.
  • Just a guess (and I'd like to see the correct answer)

    From the propeller data sheet

    mode for xtal1 and pll16x is 0b01101111
    frequency is clock frequency = 80000000 // 80 Mhz

    So the lines should be
    #include "simpletools.h" 
    clkset(0b01101111, 80000000);
    
  • EFI.c:9:1: error: expected identifier or '(' before 'do'
    EFI.c:9:1: error: expected identifier or '(' before 'while'
    Done. Build Failed!

    Thanks for helping, I tried what you suggested and the above is what happened. I believe that I got the same errors before
  • How about if instead of using the macro "clkset" you try just the instructions that are in the clkset macro? such as:
      _CLKFREQ = (frequency);   // put in value 80000000
      _CLKMODE = (mode);         // put in value 0b01101111
      __builtin_propeller_clkset(mode);   // put in value 0b01101111
    
    

    tom
  • Beavis, I get the same errors when I compile a file containing only the two lines that twm posted. The clkset line must be within a function, such as the main function. This code compiles fine:
    #include "simpletools.h" 
    
    int main()
    {
      clkset(0b01101111, 80000000);
      return 0;
    }
    
  • Thanks again Dave. :)
  • /*
      EFI Project Code
      The flywhhel has 35 steel targets mounted to it.There is a large gap between target 35 and target 1.
      Inputs: Crankshaft position sensor connected to pin number 15.
      Outputs: Fire ignition coil connected to pin number 16. Binary target indicator connected to pins 23 - 18.
       
    */
    #include "simpletools.h" 
                        
    
    int main()                                    
    {
      clkset(0b01101111, 80000000);
      
      int crank = 1 << 15;                      //crank sensor input
      int coil =  1 << 16;                      //not used yet
      int display = 0b111111 << 18;
      int t = 0;
      int a = 0;
      int b = 0;
      int target = 0;
      
      DIRA &= ~(crank);                         //set pin 15 as input for crank sensor
      DIRA |= (coil);
      DIRA |= (display);                        //set pins 23 - 18 as outputs for target indicator
      
    
      
     
      
      
        
        // Routine to find target 1 on flywheel
        for (int n = 1; n <= 350; n = n + 1)
           {
             waitpeq(crank, crank);
             waitpne(crank, crank);            //wait for 350 targets to pass for rpm to stabilize
           }
           
        while(a > 5 * b)                       //repeat only if a is huge(big gap)compared to b
                                               //wait for a to be small
                                               
          {         
           t = -CNT;
           waitpeq(crank, crank);              //time low to high
           t += CNT - 368;
           a = t;
           
           waitpne(crank, crank);
           
           t = -CNT;
           waitpeq(crank, crank);              //time low to high
           t += CNT - 368;
           b = t;                             
         }                                     //upon leaving loop,a is the low to high time between 2 close targets 
         
           waitpne(crank, crank);
         
        while(b < 5 * a)                       //repeat until b is huge meaning target gap
        
          {
            t = -CNT;
            waitpeq(crank, crank);             //time low to high
            t += CNT - 368;
            b = t;
          }
        //upon leaving loop, crank is high on target number 1
        print("close = %d",a);
        print("far = %d",b);
        print("time = %d",t);
        
        while(1)
        
        {
           waitpne(crank, crank);
           
           OUTA &= ~(target << 18);            //reset display when crank signal is low
           
           waitpeq(crank, crank);
           target ++;
           if (target = 36)      
             {
               target = 1;
             }
             
           OUTA |= (target << 18);            //display target number when crank signal high   
           
        }           
      }  
    
    


    I am once again playing with my EFI project. I am attempting implementing it in C as many people have recommended.
    I have a motorized wheel with steel targets and a hall sensor.There are 35 close equally spaced targets and a gap area that is about 10 targets wide. This program section is intended to identify the gap, and then to keep account of the target number on LED's. For now what I would like to be able to do is to run the wheel on the electric motor a while, stop the motor, and manually move the wheel by hand to see if the program can really keep track of what target is aligned with the sensor. As expected it does not work right. I inserted some print statements in the middle and ran it on terminal. All outputs are zero. Do I have a problem with my logic, or my coding? The wheel spins at about 500 rpm. The program does seem to get past the first 350 targets, but gives faulty output afterwards. One other question is, in terminal, how do you execute a carriage return between prints
  • I can't help with your main question, but here's how to get a line feed after your print. Just embed a \n in the print string. For example:
     print("time = %d\n",t);
    

    tom
  • Thanks Tom
  • You initialize a, b and t to 0. You never enter your while loops since (a > 5 * b) and (b < 5 *a) are never true. So the values of a, b and t remain at 0.
  • Beavis3215Beavis3215 Posts: 229
    edited 2016-12-31 20:37
    .
  • I get it.
  • Beavis3215Beavis3215 Posts: 229
    edited 2016-12-31 20:36
    .
  • I am going to try initializing a to one,which I think will get the ball rolling.
  • Beavis3215Beavis3215 Posts: 229
    edited 2016-12-31 20:40
    I tried setting a to one, which gets me into the loops. The output is still irregular. I seem to be getting -64 as some of my measured times. Is this some kind of overflow?
  • When timing an event that may be too short, or perhaps the crank sensor skips a beat, is there some significance to the reoccurance of the number -64 in clock ticks.
  • Dave HeinDave Hein Posts: 6,347
    edited 2017-01-02 15:59
    I suspect -64 is what you get when you execute the following code, and the crank pin is high.
            t = -CNT;
            waitpeq(crank, crank);             //time low to high
            t += CNT - 368;
    
    You should probably subtract 304 instead of 368 so that you get 0.
Sign In or Register to comment.