How to make toggle switch using mikroc?

Status
Not open for further replies.

asking

Full Member level 5
Joined
Sep 21, 2010
Messages
279
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,298
Visit site
Activity points
3,377
Hello,
I tried to use default mikroc library to a toggle my one variable with 1 or 0. Means led = 0 led = 1 means I need to keep 1push button once clicked it will be either 0 or 1. Please provide me simple code how to do it.....
 

Code:
#define MX_PIC

//Defines for microcontroller
#define P18F4520
#define MX_EE
#define MX_EE_TYPE3
#define MX_EE_SIZE 256
#define MX_SPI
#define MX_SPI_C
#define MX_SPI_SDI 4
#define MX_SPI_SDO 5
#define MX_SPI_SCK 3
#define MX_UART
#define MX_UART_C
#define MX_UART_TX 6
#define MX_UART_RX 7
#define MX_I2C
#define MX_MI2C
#define MX_I2C_C
#define MX_I2C_SDA 4
#define MX_I2C_SCL 3
#define MX_PWM
#define MX_PWM_CNT 2
#define MX_PWM_TRIS1 trisc
#define MX_PWM_1 2
#define MX_PWM_TRIS2 trisc
#define MX_PWM_2 1
#define MX_PWM_TRIS2a trisb
#define MX_PWM_2a 3

//Functions
#define MX_CLK_SPEED 19660800
#ifdef _BOOSTC
#include <system.h>
#endif
#ifdef HI_TECH_C
#include <pic18.h>
#endif

//Configuration data
#ifdef _BOOSTC
#pragma DATA 0x300000, 0xff
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300001, 0x8
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300002, 0x1f
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300003, 0x1e
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300004, 0xff
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300005, 0x81
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300006, 0x81
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300007, 0xff
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300008, 0xf
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300009, 0xc0
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000a, 0xf
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000b, 0xe0
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000c, 0xf
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000d, 0x40
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif

//Internal functions
#include "C:\Program Files (x86)\Matrix Multimedia\Flowcode V4\FCD\internals.h"

//Macro function declarations


//Variable declarations
#define FCSZ_S 20
char FCV_I;
char FCV_J;
short FCV_N;
char FCV_A;
char FCV_S[FCSZ_S];



//Macro implementations

void main()
{
        
        //Initialisation
        adcon1 = 0x0F;


        //Interrupt initialisation code
        


        //Loop
        //Loop: While 1
        while (1)
        {
                //Input
                //Input: A0 -> N
                trisa = trisa | 0x01;
                FCV_N = ((porta & 0x01) == 0x01);


                //Decision
                //Decision: N==1?
                if (FCV_N==1)
                {
                        //Output
                        //Output: N -> B7
                        trisb = trisb & 0x7f;
                        if (FCV_N)
                                portb = (portb & 0x7f) | 0x80;
                        else
                                portb = portb & 0x7f;


                } else {
                        //Output
                        //Output: 0 -> B7
                        trisb = trisb & 0x7f;
                        if (0)
                                portb = (portb & 0x7f) | 0x80;
                        else
                                portb = portb & 0x7f;


                }


        }


       
}

try it ..using push button switch when ever your switch=1 port A0=1, at the same time Port B7=1,else 0.
 

HTML:
 sbit LED at RC0_bit;
 sbit Switch at RC1_bit;
 #define Switch_Pin 1
 #define Switch_Port PORTC
 #define Debounce_Time 20
 void main() {
 ANSEL = 0b00000000; //All I/O pins are configured as digital
 CMCON0 = 0x07 ; // Disbale comparators
 TRISC = 0b00000000; // PORTC All Outputs
 TRISA = 0b00001000; // PORTA All Outputs, Except RA3
 LED = 0;
 do {
  if (Button(&Switch_Port, Switch_Pin, Debounce_Time, 0)) {
    if (!Switch) {
    LED = ~LED;
    }
    while (!Switch); // Wait for release of the button
  }
 } while(1);  // Infinite Loop
}
 
Reactions: asking

    asking

    Points: 2
    Helpful Answer Positive Rating


can i use Do function again ? because i already have 1 do function afte void main in my program....but mikroc wont allow more than do function....if i remove do and compile it works fine but if i keep do function and then try to compile then it gives me error. so m little bit confused that its my programming error or its limitation of mikroc to use the Do function...only 1 time...or should i add the new code within that do function...?
 

Simple Code is
Code:
 [syntax=c]
void main() {
         TRISA.F0 = 0x01;
         PORTA = 0x00;
         TRISB.F0 = 0x00;
         PORTB = 0x00;

         while(1) {

                 if(PORTA.F0 == 1) PORTB.F0 = ~PORTB.F0;
         }
} [/syntax]
 
Reactions: asking

    asking

    Points: 2
    Helpful Answer Positive Rating

ok great...what if i want to replace PORTB.F0 with some variable unsigned short LED. it will be
Code:
if(PORTA.F0 == 1) LED = ~LED;
?

Means when i click on switch, Variable LED = 1, and if i click switch again it will be LED = 0; ? because i dont want to use push to on and push to off switch i just want to use push to on switch to toggle the output 0 or 1....

- - - Updated - - -

post your full code.

Code:
void main()
{
  void Soft_I2C_Init();
  ADCON1 = 0xFF;
  TRISB = 0x00; // Set PORTB7 input all other direction to be output
  PORTB = 0xff;    // Turn OFF LEDs on PORTB
  TRISA = 0b110000; // RA5-6-7 is input only
  TRISC = 0b00001100; // RA5-6-7 is input only
  write_ds1307(0,0x00);


  do {

  set = 0;
     if(PORTC.F2 == 0)
     {
         delay_ms(70);
         if(PORTC.F2 == 0)
         {
             set_count++;
             if(set_count >= 3)
             {
                set_count = 0;
             }
         }
     }
     if(set_count)
     {
        if(PORTC.F3 == 0)
        {

          if(PORTC.F3 == 0)
              set = 1;
         }
          if(set_count && set)
        {
          switch(set_count)
          {
            case 1:
                    hour = BCD2Binary(hour);
                    hour = hour + set;
                    if(hour>=24)
                    hour=0;
                    hour = Binary2BCD(hour);

                    write_ds1307(2, hour); //write hour
                    break;
            case 2:
                     minute = BCD2Binary(minute);
                     minute = minute + set;
                     if(minute >= 60)
                        minute = 0;
                     if(minute < 0)
                        minute = 59;
                     minute = Binary2BCD(minute);
                     write_ds1307(1, minute); //write min
                     break;
          }
        }
     }

i just want this condition to be true...by pressing button, and it should be false if i press button again. Means just toggle the condition true or false.

if(d1==0)d1+=12;
else if(d1>=13)d1-=12;

i tried but no change so i need for this. My other switch works fine as shown above in my code.
 

Code:
 [syntax=c]
#define SW PORTA.F0
#define LED PORTB.F0

void main() {
         TRISA.F0 = 0x01;
         PORTA = 0x00;
         TRISB.F0 = 0x00;
         PORTB = 0x00;
 
         while(1) {
 
                 if(SW == 1) {
			Delay_ms(100)
			if(SW == 1) {
				LED = ~LED
			}
		 } 
         }
}
[/syntax]
 
Reactions: asking

    asking

    Points: 2
    Helpful Answer Positive Rating
Code:
if(PORTC.F7 == 0)
      {
            Delay_ms(30);
            if(PORTC.F7 == 0) {
                LED = ~LED;
           }
         }

This worked like charmed....thanks here my LED is flag which is turned 0 and 1. It was not turning because i kept it LED = 0; it should be LED == 0;
 

LED == 0 is not right. PORTC.F7 == 0 is right. LED is connected to output pin, so, LED = 0 makes sense.

Jayanth,

i already told, LED is variable (FLAG) i just need to turn it 1 or 0 for proving my condition true or false. Its not connected to output pin dear....its just Variable in program.
 

Guys, I want if i pressed the button in RA0 to make the led on PortB.F7 to light up till next press . so even if i released the button the led should be on till next press any hep on this please .
 

Code:
 [syntax=c] #define SW PORTA.F0
#define LED PORTB.F7
 
void main() {
         TRISA.F0 = 0x01;
         PORTA = 0x00;
         TRISB.F7 = 0x00;
         PORTB = 0x00;
 
         while(1) {
 
            if(SW == 1) {
                 Delay_ms(100)
                 if(SW == 1) {
                      LED = ~LED
                 }
            } 
         }
} [/syntax]
 

Hi jayanth,
I did the same but the Led will be On for 100 ms only and will be OFF again , i think you did not get my point and i will be more clear . when i power the Circuit the LED should be Off then if i pressed the Push button the LED should be ON and keeps ON even if i released the button and when i pressed the button again it should return OFF and be OFF till next press .
 

@kail123

Try this code. It is for PIC16F887

Code:
 [syntax=c]
#define SW PORTA.F0
#define LED PORTB.F7

void main() {
         TRISA.F0 = 0x01;
         PORTA = 0x00;
         TRISB.F7 = 0x00;
         PORTB = 0x00;
         ANSEL = 0x00;
         ANSELH = 0x00;
         CM1CON0.C1ON = 0;
         CM2CON0.C2ON = 0;
         
         while(1) {

            if(SW == 1) {
                 Delay_ms(150);
                 if(SW == 1) {
                      LED = ~LED;
                 }
            }
         }
}
[/syntax]

See the simulation video
 

Attachments

  • LED SW.rar
    41.9 KB · Views: 127
  • led_switch.rar
    150.6 KB · Views: 143

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…