Shop OBEX P1 Docs P2 Docs Learn Events
COG confusion — Parallax Forums

COG confusion

Beavis3215Beavis3215 Posts: 229
edited 2018-09-03 15:31 in Propeller 1
/* Fidget Spinner Motor
   Auto time version


  
  Pin 19 is on sensor input
  Pin 18 is off sensor input
  Pin 17 is magnet controller output
  Pin 16 is timing light output
  Pin 15 is timing advance input
  Pin 14 is timing retard input
  Pin 13-8 is binary timing output
  
*/
#include "simpletools.h"            // Include simple tools

void button_time();

void time_it();

void display_rpm();

static volatile int delay, on_sensor, off_sensor, rpm; //all global

int main()                          // Main function

{
 simpleterm_close();
 clkset(0b01101111, 80000000); 
 cog_run(button_time,128);          // Push button control
 cog_run(time_it,128);              // Times magnet on sensor to magnet off sensor
 cog_run(display_rpm,128);          // Displays RPM on 7 segment LEDs    
 
 on_sensor = 1 << 19;
 off_sensor = 1 << 18;
 int magnet = 1 << 17;
 int t_light = 1 << 16;

 
 
 
 
  DIRA &= ~(1 << 19);
  DIRA &= ~(1 << 18);
  DIRA |= (magnet);
  DIRA |= (t_light);
  
  
  while(1)
  {
   
    if ((INA & on_sensor) == on_sensor) // If on sensor high
    {
      OUTA |= (magnet);             // Turn magnet on
    }
    
    if ((INA & off_sensor) == off_sensor) // If off sensor high
    {
      pause(delay);                 // Delay is assigned in button_time
     
      OUTA &= ~(magnet);            // Turn magnet off             
      OUTA |= (t_light);            // Timing light on
      pause(1);             
      OUTA &= ~(t_light);           // Timing light off
    }            
  }  
}




void button_time()
{
  int lights = 0b111111 << 8;       //Timing delay LED's
  int advance = 1 << 15;
  int retard = 1 << 14;
  DIRA &= ~(advance);
  DIRA &= ~(retard);
  DIRA |= (lights);
  
  int t = 20;
  
  
  while(1)
  {
    
     if ((INA & retard) == retard)   // If retard button high
     {
       t++ ;
       
       if (t > 64)                   // Don't allow t to exceed 64
       {
         t = 64;
       }
                 
     }
     
     if ((INA & advance) == advance) // If advance button high
     {
       t-- ;
       
       if (t < 1)                    // Keep t 1 or larger
       {
         t = 1;
       }
               
     }
     
     delay = t;                      // Pass delay to main function
     
                 
        OUTA = (OUTA & ~(lights)) | (t << 8 ); // Put t value on output lights
        pause(300);               
    
  
  }
}      


 
void time_it()



{
  
unsigned int uS, t_initial, t_final;
int pass_number = 0;
int total_passes = 1;
int total = 0; 
uS = CLKFREQ/1000000;
int elapsed;
//int rpm;
const int travel_frac = 9;                      // How many on-off travels on 360 degrees
int multiplier = 60000000/travel_frac;
//simpleterm_open();



  while(1)
  {
    
    while(pass_number <= total_passes)
    {
      waitpeq(on_sensor, on_sensor);        // Wait for on sensor high
      
        t_initial = CNT;
      
      
      waitpeq(off_sensor, off_sensor);      // Wait for off sensor high 
                  
        t_final = CNT;
      
       
      total = total + (t_final - t_initial); // Accumulate time for total amount of passes(in ticks)
      pass_number++;
      
    }    
      elapsed = (total / uS)/total_passes;  // Calculate average pass time in uS
      
      rpm = multiplier / elapsed;
      //print("RPM = %d\n", rpm);
      pass_number = 0;
      total = 0; 
  }    

}  



void display_rpm()

 
 {
   int seven_seg = 0b1111111;
   int driver = 0b1111 << 20;

   DIRA |= (seven_seg);
   DIRA |= (driver);

   
   int d[] = {0b0111111, 0b0000110, 0b1011011, 0b1001111, 0b1100110, 
              0b1101101, 0b1111101, 0b0000111, 0b1111111, 0b1100111};
               
   
   while(1);
     {
      int thousands = (int)(rpm / 1000);
      int hundreds  = (int)((rpm - (thousands * 1000)) / 100);
      int tens      = (int)((rpm - (thousands * 1000) - (hundreds * 100)) / 10);
      int ones      = (int)(rpm - (thousands * 1000) - (hundreds * 100) - (tens * 10));
      
      OUTA = (OUTA & ~ (seven_seg)) | d[thousands];
      OUTA = (OUTA & ~ (driver))    | (0b1000 << 20);
      pause(8);
      OUTA = (OUTA & ~ (driver));
      
      
      OUTA = (OUTA & ~ (seven_seg)) | d[hundreds];
      OUTA = (OUTA & ~ (driver))    | (0b0100 << 20);
      pause(8);
      OUTA = (OUTA & ~ (driver));
      
      
      OUTA = (OUTA & ~ (seven_seg)) | d[tens];
      OUTA = (OUTA & ~ (driver))    | (0b0010 << 20);
      pause(8);
      OUTA = (OUTA & ~ (driver));
      
      
      
      OUTA = (OUTA & ~ (seven_seg)) | d[ones];
      OUTA = (OUTA & ~ (driver))    | (0b0001 << 20);
      pause(8);
      OUTA = (OUTA & ~ (driver));
    }      
      
          
   
   
   
   
 }
    
 
    
Desired output: self turning fidget spinner with RPM reading on 4, 7 segment LEDs. Spinner timing controlled by 2 pushbuttons.

In a previous version, display_rpm was incorporated into time_it. This worked but had problems with display blinking. LED display was dependent on spinner travel, but otherwise worked and gave correct output. In an attempt to fix this problem, I put display_rpm into its own COG. There seems to be no output now and I can't figure out why. When I say no output, the LED display is not on. I increased the pauses from 8 to 100 just to make sure that the output was not to fast to see.

Comments

  • #include "simpletools.h"
    void main()
    
     
     {
       int seven_seg = 0b1111111;
       int driver = 0b1111 << 20;
    
       DIRA |= (seven_seg);
       DIRA |= (driver);
    
       int d[] = {0b0111111, 0b0000110, 0b1011011, 0b1001111, 0b1100110, 
                  0b1101101, 0b1111101, 0b0000111, 0b1111111, 0b1100111};
                   
       int rpm = 1234;
       
       while(1);
         {
          int thousands = (int)(rpm / 1000);
          int hundreds  = (int)((rpm - (thousands * 1000)) / 100);
          int tens      = (int)((rpm - (thousands * 1000) - (hundreds * 100)) / 10);
          int ones      = (int)(rpm - (thousands * 1000) - (hundreds * 100) - (tens * 10));
          
          OUTA = (OUTA & ~ (seven_seg)) | d[thousands];
          OUTA = (OUTA & ~ (driver))    | (0b1000 << 20);
          pause(100);
          OUTA = (OUTA & ~ (driver));
          
          
          OUTA = (OUTA & ~ (seven_seg)) | d[hundreds];
          OUTA = (OUTA & ~ (driver))    | (0b0100 << 20);
          pause(100);
          OUTA = (OUTA & ~ (driver));
          
          
          OUTA = (OUTA & ~ (seven_seg)) | d[tens];
          OUTA = (OUTA & ~ (driver))    | (0b0010 << 20);
          pause(100);
          OUTA = (OUTA & ~ (driver));
          
          
          
          OUTA = (OUTA & ~ (seven_seg)) | d[ones];
          OUTA = (OUTA & ~ (driver))    | (0b0001 << 20);
          pause(100);
          OUTA = (OUTA & ~ (driver));
          
          print("RPM = %d\n", rpm);
        }      
          
              
       
       
       
       
     }
    
    

    I don't see why this doesn't work, doesn't even print RPM = on terminal.
  • display_rpm has a misplaced semicolon:
    while (1);
      {
         .. some stuff ..
      }
    
    The "some stuff" code that you think will be repeated will never be executed, because "while (1);" is an endless loop that does nothing. Removing the semicolon will help.
  • aha.... thanks
Sign In or Register to comment.