CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Several LED Blinkers including Cylon eye

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
TimC



Joined: 12 Nov 2005
Posts: 7

View user's profile Send private message

Several LED Blinkers including Cylon eye
PostPosted: Tue Oct 08, 2024 10:20 pm     Reply with quote

Hi All,
Here is a blink utility that can be useful for:
Testing a new uP, xtal speed tests, fuse config errors, pins incorrectly tied to ground or power and most importantly fun!

To add the Blink utility:
Code:

#include <led_blink.c>
     .
     .
Blink_Pin(Pin_A1);  // Blink for 10 Sec
Up_Seq(Pin_D0, Pin_D7);  // test a port
Cylon_Seq(Pin_D0);  // Knight Rider 


Here is the led_blink.c include source:
Code:

#ifndef _LED_BLINK_C_
#define _LED_BLINK_C_

// led_blink.c  This is a collection of routines for blinking leds
// Starting from the single pin blink to a variable length Cylon Eye
// Details are within each routine.
// Using CCS v5.118 October 8, 2024 v1.0 written by TimC

// This is to match your hardware design.
#ifndef LED_OFF_STATE
#define LED_OFF_STATE   0   // 0=High (V+) will turn leds on.
//#define LED_ON_STATE   !LED_OFF_STATE
#endif
#ifndef LED_ON_STATE   // only needed for the Cylon blinker
#define LED_ON_STATE   !LED_OFF_STATE
#endif


void Blink_Pin(Int16 Single_Pin,int cycles=20) {
// Led Blink just using a single pin.
// Useful for testing hardware, Testing new uP, xtal speed, debugging,
//      Fuse config, i/0 ports, pin tied to ground or Power
// Example:
//      #include <led_util.c>   // first include this source file
//      Blink_Seq(Pin_B5);      // later add Blink_seq() with correct pin
//
// 1/2 sec on, 1/2 sec off makes it easy to determine
//      if clock is running a correct speed.
// Cycle count of 20 = 10 seconds for a fast check of osc fuses

// If state of led is +Voltage to turn on then 0, or GND to turn on then 1.
// This is to match your hardware design. So you can set it globally
//#define LED_OFF_STATE   0
   int i;
   // Return if wildly wrong Start_Pin, End_Pin values
   if(Single_Pin < PIN_A0)   // error Start_Pin value too low
      return;

   // LED_OFF_STATE define globally or place 0 or 1 here.
   output_bit(Single_pin, LED_OFF_STATE);

   if(cycles & 1)   
      cycles += 1;   // bump up cycle counter if odd #

   // cycle count of 20 = 10 seconds for a fast check of osc fuses
   for(i = 0; i < cycles; i++) {
      output_toggle(Single_Pin);
      delay_ms(500);
   }
}

void Ramp_Seq(Int16 Start_Pin, int16 End_Pin, int16 all_led_Pause=1000) {
// Led Ramp going from Start_Pin to the End_Pin.
// Changing all_led_pause to 0 will stop the slow Ramp return back
//      to the original led state.
// Useful for checking hardware, Testing new uP, xtal speed, debugging,
//      Fuse config, i/0 ports, pins tied to ground or Power
// Example:
//      #include <led_util.c>   // first include source file
//      Ramp_Seq(Pin_A0, Pin_B5,300); // later add Ramp_seq() with correct pins
//
// This routine could also stress the uP and power supply because it can leave
//      many Pins in a ON state. So the current Max is set at 30 Leds   

   int16 i;
   int16 next_led_delay=500;   // change as needed

   // Return if wildly wrong Start_Pin, End_Pin values
   if(Start_Pin < PIN_A0)   // error Start_Pin value too low
      return;
   if(Start_Pin > End_Pin) // Start_Pin must be less then End_Pin
      return;
   if(End_Pin - Start_Pin > 30) // Prevent a accident with End_Pin way to large
      return;

   for(i = Start_Pin; i <= End_Pin; i++)
      output_bit(i, LED_OFF_STATE);

   // Turn on all the Led's one by one till all are on
   for(i = Start_Pin; i <= End_Pin; i++) {
      output_toggle(i);
      delay_ms(next_led_delay);
   }

   delay_ms(all_led_Pause);
   if (all_led_pause < 20)
      next_led_delay=0;

   // Now turn Off all the Led's one by one
   for(i = End_Pin; i >= Start_Pin; --i) {
      delay_ms(next_led_delay);
      output_toggle(i);   // restore pin status
   }
}


void Up_Seq(Int16 Start_Pin, int16 End_Pin, int16 next_led_delay=100) {
// Single Led Blink going from Start_PIN to the End_Pin.
// Example: Up_Seq(Pin_B0, Pin_B7,300);

   int16 i;

   // Return if wildly wrong Start_Pin, End_Pin values
   if(Start_Pin < PIN_A0)   // error Start_Pin value too low
      return;
   if(Start_Pin > End_Pin) // Start_Pin must be less then End_Pin
      return;
   if(End_Pin - Start_Pin > 80) // Prevent a accident with End_Pin way to large
      return;

   for(i = Start_Pin; i <= End_Pin; i++)
      output_bit(i, LED_OFF_STATE);

   // Turn on and off all the Led's one by one
   for(i = Start_Pin; i <= End_Pin; i++) {
      output_toggle(i);
      delay_ms(next_led_delay);
      output_toggle(i);
   }
   delay_ms(next_led_delay*5);      // Totally optional pause here
}


void Up_and_Back_Seq(Int16 Start_Pin, int16 End_Pin, int16 next_led_delay=200) {
// Single Led Blink going from Start_PIN to the End_Pin.
// Example: Up_and_Back_Seq(Pin_B0, Pin_B7,300);
// Cylon_seq() is a step up from this routine

   int16 i;

   // Return if wildly wrong Start_Pin, End_Pin values
   if(Start_Pin < PIN_A0)   // error Start_Pin value too low
      return;
   if(Start_Pin > End_Pin) // Start_Pin must be less then End_Pin
      return;
   if(End_Pin - Start_Pin > 80) // Prevent a accident with End_Pin way to large
      return;

   for(i = Start_Pin; i <= End_Pin; i++)
      output_bit(i, LED_OFF_STATE);


   // Turn on and off all the Led's one by one
   for(i = Start_Pin; i <= End_Pin; i++) {
      //output_high(i);
      output_toggle(i);
      delay_ms(next_led_delay);
      output_toggle(i);
   }
   // Now change direction one by one
   for(i = End_Pin-1; i >= Start_Pin; --i) {
      output_toggle(i);
      delay_ms(next_led_delay);
      output_toggle(i);   // restore pin status
   }
}


void Cylon_Seq(Int16 Start_Pin, int16 Cylon_delay=50) {
// Cylon will look good with as little as 3 Led's
// Example: Cylon_Seq(Pin_D0,100);
// Similar to the Up_and_Back_Seq() but 2 leds go up and back

   int16 End_Pin,i,j;
   int8 NumofLeds=7;   // zero based
   int8 repeat=0;      // # of cycles, 0=Just do once

   End_Pin = Start_Pin+NumofLeds;   

   for(i = Start_Pin; i <= End_Pin; i++)
      output_bit(i, LED_OFF_STATE);

   for(j = 0; j <= repeat; j++) {
      i = Start_Pin;
      // Start sequence
      output_bit(i, LED_ON_STATE);
      delay_ms(Cylon_delay);
      output_bit(i+1, LED_ON_STATE);
      delay_ms(Cylon_delay);
      // across one direction
      for(i = Start_Pin; i <= (End_Pin-2); i++) {
         output_bit(i, LED_OFF_STATE);
         delay_ms(Cylon_delay);
         output_bit(i+2, LED_ON_STATE);
         delay_ms(Cylon_delay);
      }
      // ending sequence.
      output_bit(i+NumofLeds, LED_OFF_STATE);
      delay_ms(Cylon_delay);
      output_bit(i+NumofLeds, LED_ON_STATE);
      delay_ms(Cylon_delay);
      // across return direction
      for(i = End_Pin; i >= Start_Pin; --i) {
         output_bit(i, LED_OFF_STATE);
         delay_ms(Cylon_delay);
         output_bit(i-2, LED_ON_STATE);
         delay_ms(Cylon_delay);
      }
      // turn off last led and delay before next cycle
      output_bit(i+1, LED_OFF_STATE);
      delay_ms(Cylon_delay);
   }
}

#endif   // _LED_BLINK_C_ define


Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group