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 support@ccsinfo.com

Relay controller - 8 ports with led status

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



Joined: 08 Jun 2019
Posts: 31

View user's profile Send private message Visit poster's website

Relay controller - 8 ports with led status
PostPosted: Fri Jun 21, 2019 9:28 am     Reply with quote

Code:

*/
// ************************************************ Array version ************************************************ //
   
   Project:    Demo Controller Board - 8 Channels Relay with status LEDs and push buttons for an ON/OFF toggle
   Compiler:   CCS-C-PIC - PCWHD Version 5.076
   Author:     MassaM, June 2019
   Chip:       PIC16F877A@20Mhz
   Copyright:  No copy-rights nor copy-wrongs! :)
   Request:    Make it better and post it back on this thread, so we all learn please! And always give credits where due, as what goes around, comes around! ;)
   Forum Link: http://www.ccsinfo.com/forum/viewtopic.php?t=58066
   Credits:    Ttelmah -> http://www.ccsinfo.com/forum/profile.php?mode=viewprofile&u=14537&sid=42ae004153892ee2adcd40879d100ea4
               temtronic -> http://www.ccsinfo.com/forum/profile.php?mode=viewprofile&u=15187
               dluu13 -> http://www.ccsinfo.com/forum/profile.php?mode=viewprofile&u=24556&sid=42ae004153892ee2adcd40879d100ea4
               PCM programmer -> http://www.ccsinfo.com/forum/profile.php?mode=viewprofile&u=4
               
*/

#include <16F877A.h>

//Note: Edited 1 & 2 on 17th July 2019
#fuses HS, BROWNOUT, PUT, NOWDT // Edited 1: HS instead of XT
#use delay(crystal=16M)  // Edited 2: was (clock=20M)
#use rs232(stream=SERIAL, baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8)  // settings for Serial/Terminal outputs

#define FOSC getenv("CLOCK")  // Get PIC oscillator frequency
#define TIMER0_PRELOAD (256 - (FOSC/4/256/100))

/* ****************************************************************************************************************** */
// Array version variables
int16 switches[] = {PIN_B0,PIN_B1,PIN_B2,PIN_B3,PIN_B4,PIN_B5,PIN_B6,PIN_B7};
int16 leds[] = {PIN_D0,PIN_D1,PIN_D2,PIN_D3,PIN_D4,PIN_D5,PIN_D6,PIN_D7};
int8 switch_is_down[] = {FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE};
int8 active_state[] = {0,0,0,0,0,0,0,0};
int8 previous_state[] = {0,0,0,0,0,0,0,0};
int8 portb_states;

/* ****************************************************************************************************************** */

/* ****************************************************************************************************************** */
// Array version functions
/* ****************************************************************************************************************** */

void display_binary(char c) // Used for printing binary out to terminal as debugging (Credit to: PCM Programmer)
{
   char i;
   
   putc('0');
   putc('b');
   
   for(i = 0; i < 8; i++)
      {
       if(c & 0x80)
          putc('1');
       else
          putc('0');   
   
       c <<= 1;
      }
}

void clearSwitches_Array()
{
   int8 i;
   for(i=0; i < 8; i++)
   {
      switch_is_down[i] = FALSE;  // Clear the switch is pushed flag
   }
}


void pollSwitches_Array()
{
   int8 i;
   for(i=0; i < 8; i++)
   {
      if(switch_is_down[i] == TRUE)
      {
         switch_is_down[i] = FALSE;  // Clear the switch is pushed flag
         output_toggle(leds[i]);
      }
   }   
}


void checkStates_Array()

   int8 i;
   for(i=0; i < 8; i++)
   {
      active_state[i] = input(switches[i]);  // Read the button

      if((previous_state[i] == 1) && (active_state[i] == 0))
      {
         switch_is_down[i] = TRUE;
      }
     
      previous_state[i] = active_state[i];  // Save current value for next time

      pollSwitches_Array();                 // <- Moved this function call from the main's while(TRUE) to here and THE Compiler is HAPPY NOW! :)
                                            // Credit: dluu13 -> For the awesome clue! Thanks! :)
   }
   
}

void timer0_init(void)
{
   setup_timer_0(T0_INTERNAL | T0_DIV_256 | T0_8_BIT);
   set_timer0(TIMER0_PRELOAD);
   clear_interrupt(INT_TIMER0);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);     
}

#int_timer0
void timer0_isr(void)
{   
   set_rtcc(TIMER0_PRELOAD);  // Reload Timer0 for 10ms rate
   
   checkStates_Array();       // All working now! :)

}

void initAll(){
   
   timer0_init();
   
   clearSwitches_Array();
   
   portb_states = input_b();                 // read the state of all switches as full port

   // Uncomment the block below for port B states debug to terminal output
   /*
   printf("Initial PORT B States\r\n");      // print the initial port state
   display_binary(portb_states);             // print the port state to terminal in binary
   printf("\r\n---------------------\r\n");  // print separation lines
   */
}

void main()
{

   initAll();
     
   while(TRUE)
   { 
      portb_states = input_b();                 // read the state of all switches as full port

    // Uncomment the block below for port B states debug to terminal output
    /*
      printf("Updated PORT B States\r\n");      // print the updated port state
      display_binary(portb_states);             // print the port state to terminal in binary
      printf("\r\n---------------------\r\n");  // print separation lines
   */

   } // while true
   
} // main



Youtube links for:

Simulation:

https://www.youtube.com/watch?v=gqhuIhm1n2k

Demonstration on breadboard:

https://www.youtube.com/watch?v=R96LOQxJpZk


Hope this helps.

Cheers!
_________________
while(!dead)
{
keepLearning();
}
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