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

switch restart

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
eng/ IBRAHIM



Joined: 14 Aug 2007
Posts: 31

View user's profile Send private message

switch restart
PostPosted: Tue Nov 20, 2007 8:14 am     Reply with quote

hi
i use 16f877 .how make that( when i push switch program is start .and when i push the same switch again program is restart)
mrpicing



Joined: 22 Oct 2005
Posts: 20

View user's profile Send private message

PostPosted: Wed Nov 21, 2007 2:33 am     Reply with quote

aoa.
Check datasheet of the part. place a pullup at vpp and a button from pin1 to ground.
If its not the solution for u then explain your problem.
You can try it as this
if (button pressed)
#asm
goto 00 //or starting address of programm. you can see listing file for ////your start address
#endasm
But it may cause some errors.
Look it carefully.
eng/ IBRAHIM



Joined: 14 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Wed Nov 21, 2007 8:23 am     Reply with quote

thanks
i have start/stop switch when press it the program is start. and when press it again at any step for program .program go reset.
i know can make that by interrupt but i don't know code
Ttelmah
Guest







PostPosted: Wed Nov 21, 2007 9:41 am     Reply with quote

Unfortunately, what you have asked, is a bit like a person posting on a driving forum "how do I start a car".
The answers will vary, from 'turn the key', throuogh descriptions of how to push start the vehicle, via descriptions of how to use the clutch/gearlever etc...
You really need to learn soe basic programming first.
If though, I am reading what you want correctly, then the answer is probably not to use interrupts to restart. Instead write your code, so it runs something like:
Code:

int1 toggle=false;

//Interrupt when button attached to pull RB0 to 0v is pressed
#int_ext
void key_interrupt(void) {
   //This ensures the key is still pressed, providing a tiny amount
   //of debounce
   if (input(PIN_RB0) ==0) toggle^=1;
}

void main(void) {
   //main program
   port_b_pullups(TRUE);
   ext_int_edge( H_TO_L ); 
   //setup interrupt to trigger on falling edge
   clear_interrupts(int_ext);
   enable interrupts(int_ext);
   enable_interrupts(global);
   //and enable
   while (TRUE) {
      //Now the main 'code' loop
      while (toggle==false)  //Here the key has not been pressed
          ;
      while (toggle) {
          //Now the code you want to execute once the key is pressed
      }
   }
}


This lacks the other initialisations, and fuses needed for your chip, but shows how to arrange this.
Beware, of the jump to 0 approach. This will probably cause a stack overflow.
This is the problem of this type of approach, it leaves things 'dirty', which is not the solution for good reliability.

Best Wishes
eng/ IBRAHIM



Joined: 14 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Tue Dec 04, 2007 6:02 am     Reply with quote

thanls all
i try this code but the compailler send error

#int_EXT
void EXT_isr(void)
{
goto loop;
}

void main()
{
loop:output_d(0x00);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
delay_ms(30);
while(1)
{
output_d(0xff);

}
}


and when try

#int_EXT
void EXT_isr(void)
{
reset_cpu();

}

void main()
{
loop:output_d(0x00);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
delay_ms(30);
while(1)
{
output_d(0xff);

}
}

the program no good play by proteus
Ttelmah
Guest







PostPosted: Tue Dec 04, 2007 9:44 am     Reply with quote

Don't do a goto, from inside an interrupt. This will cause a stack overflow.
Use the 'toggle' handler I have already posted.
Do your main with:
Code:

void main() {
   enable_interrupts(INT_EXT);
   enable_interrupts(GLOBAL);
   while (true) {
       output_d(0x00);
       delay_ms(30);
       toggle=TRUE;
       while(toggle) {
           output_d(0xff);
       }
   }
}


Best Wishes
eng/ IBRAHIM



Joined: 14 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Wed Dec 05, 2007 4:29 am     Reply with quote

Ver thanks for all
I try this code and it play very good

int1 toggle=false;
#int_ext
void key_interrupt(void) {
if (input(PIN_B0) ==0)
toggle^=1;
}

void main(void) {

l:output_d(0x00);
port_b_pullups(TRUE);
ext_int_edge( H_TO_L );
enable_interrupts(int_ext);
enable_interrupts(global);

while (TRUE) {
while (toggle==false)
goto l;
while (toggle) {
output_d(0xff);
}
}
}
Ttelmah
Guest







PostPosted: Wed Dec 05, 2007 5:05 am     Reply with quote

Just a 'comment', use a while, rather than a goto for the loop back to the start.
In general, 'goto' statements, are consider 'poor form'. There are places where they may need to be used, but they make it more likely that code will get to places it is not expected to go, and in many newer languages, are not even present in the language!....

Best Wishes
eng/ IBRAHIM



Joined: 14 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Mon Dec 10, 2007 6:20 am     Reply with quote

I found new proplem when applay this code. when press B0 the main program is run and when press it again the progran don't reset and wait the while function finished .
i'm search for code make that(when press some switch the main program is run and when press it again at any time or any step the program go reset )



int1 toggle=false;
#int_ext
void key_interrupt(void)
{

if (input(PIN_B0) ==0)
toggle^=1;
}
///////////////////////////////////////
void V()
{
while(!input(pin_B6))
{
output_high(pin_D3);
}
output_low(pin_D3);
}
///////////////////////////////////
void main(void) {
int M;
M=0;
l:output_d(0x00);

ext_int_edge( H_TO_L );

enable_interrupts(int_ext);
enable_interrupts(global);

while (TRUE)
{
while (toggle==false)
goto l;

while (toggle)
{
toggle=TRUE;
output_high(pin_D1);
delay_ms(5000);
while(input(pin_B7))
{

output_high(pin_D0);
}
output_low(pin_D0);
v();
}

}
}
timm02



Joined: 16 Jan 2008
Posts: 4
Location: Kuala Lumpur, Malaysia

View user's profile Send private message

set button
PostPosted: Sat Jan 19, 2008 2:49 am     Reply with quote

hi..im using 16f877a and im currently develop a current generator that can produce current at 4 different frequencies.
i've chosen to have 2 button which for single frequency=50kHz and the other button for multifreq=5kHz,100kHz and 1MHz.

im a newbies and i wanna check is it my code for the button right or wrong.
i haven't write the main function as i dont know how (im still learning).

Code:

#include <16f877a.h>
#include <stdlib.h>
#include <math.h>

#fuses HS, NOPROTECT, NOWDT, NOLVP
#use delay (clock=20000000)
#include <Button.c> // Function parameters:
// button(pin, DownState, Delay, Rate, BVar, Action)
int8 A4 = 0; // For the button on pin A4
int8 A5 = 0; // For the button on pin A5

#byte PORTA = 5
#byte PORTB = 6

int8 A4_pressed = FALSE;
int8 A5_pressed = FALSE;

int16 freq_value;
float freq_1, freq_2, freq_3, freq_4;

void main()
{
set_tris_a(0xff); // Set PORT A as inputs
set_tris_b(0x00); // Set PORT B as outputs
PORTB=0; // Initialize all PORT B output to 0 volt

while(TRUE)
{
if(A4_pressed == TRUE)
{
A4_pressed = FALSE;

}

if(A5_pressed == TRUE)
{
A5_pressed = FALSE;

}
}
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion 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