Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

code pic12F683

Status
Not open for further replies.

ken1989

New Member
Hi

I 've build an electric circuit with 2 magnet sensors to control an engine.
When 1 sensor gets a signal de engine should start, then when the other one gets a signal the engine has to stop. I need to control al this with a microcontroller (PIC12F683). But something is wrong. Don't know if I've build the circuit wrong, or the code of my chip. I've used next code:
( GPIO.F0 = sensor 1, GPIO.F1 = sensor 2, GPIO.F4 = engine)


Code:
void init() {

  // Configure GPIO Port
  ANSEL = 0b00000001;      // Configure all GPIO pins as DIGITAL
  TRISIO = 0b00000011;     // GP0 and GP1 are input, rest is output
  GPIO = 0x00;             // Set all output pins to low
  WPU = 0x00;              // Weak-pull-up disabled

  // Configure AD-onvertor
  ADCON0 = 0x00;          // AD disabled
  ADRESH = 0x00;          // Init the AD-register

  // Configure Comparator
  CMCON0 = 0xFF;           // Comparator is turned OFF
  CMCON1 = 0x00;           // Comparator is turned OFF

  // Interrupt Configuration
  INTCON = 0x00;        // Disable all interrupts
}

void main() {
  init();
  while(1)
  {
     if (GPIO.F0){
     GPIO.F4 = 1 ;
     }
     if (GPIO.F1){
     GPIO.F4 = 0;
     }

  }
}

I hope someone can tell me this is correct or not.

Thanks
Kenny
 
Last edited:
What compiler are you using? I have never come across any compiler that uses F4 etc.

Also, when posting code if you put
Code:
 before it and [/c[COLOR="Black"]od[/COLOR]e] after it then it will keep it's formatting.

Mike.
 
The code looks like it is written in MikroC .. as for the code itself it does compile..

First, if you are really using MikroC then I suggest you to use Button function
Code:
if( Button(GPIO, 0, 3, 1) ){ //3ms debounce period
  GPIO.F4=0;
}

in order to eliminate debouncing. If you are not using the MikroC write the debuoncing function yourself as sensors have bin known to "flicker"

also check the logic inside your while loop. what if both bit 0 and bit1 of GPIO are 1 ? you will on/off the engine, I'm sure your intent was not to flick it, so, you have to decide what your "default" value in all four cases is (0,0; 0,1; 1,0; 1,1) as you cannot expect the input to always be only (0,1; 1,0).

the while loop might look like:
Code:
unsigned char default_o;
while(1){
  default_o = 0;
  if (Button(GPIO, 0, 3, 1) ){
     default_o = 1;
  } 
  GPIO.F4 = default_o;
}

or something like:
Code:
unsigned char default_o;
while(1){
  default_o = 1;
  if (Button(GPIO, 1, 3, 1) ){
     default_o = 0;
  } 
  GPIO.F4 = default_o;
}

or you can do something more complex like
Code:
while(1){
  if (Button(GPIO, 0, 3, 1) ){
     if (Button(GPIO, 1, 3, 1) ){
        GPIO.F4 = 1;
     }else{
        GPIO.F4 = 0;
     }
  } else {
        GPIO.F4 = 0;
  }
}
 
The first code example will work... but there's a small mistake in ANSEL setting. It has to be
Code:
ANSEL = 0;
The first example left GPIO.0 as analog hence why it never see GPIO.F0 as high.

If the config fuses are properly set, it will work.
 
thx for all the fast replies.
It was build in microC.

Gonna try all this out...
Let y'all know if it worked

grtzzz
 
Status
Not open for further replies.

Latest threads

Back
Top