1
\$\begingroup\$

I'm new to embedded systems and I want to program a PIC12F675 for a project.

So, the idea is that there is a switch. If the switch is off, the microcontroller normally outputs a clock signal, which its frequency is controlled by a potentiometer. And if the switch is on, there is another button that I can push to output a pulse that lasts for 500ms and goes low again.

I wrote the code, but it seems to not be working correctly. Also I didn't code the potentiometer part because I don't know how, so it outputs a clock that it's frequency can't be controlled.

The problem:

When the switch is off (mode = 0), the clock doesn't work. However, when the switch is on (mode = 1), the clock works. It should be the other way around. Also, the pulse button doesn't work in either modes. Lastly, as I said before, I didn't code the potentiometer part because I don't know how.

So, could someone help me with this? Thank you.

enter image description here

Here's the config:

#pragma config FOSC = INTRCCLK  
#pragma config WDTE = OFF       
#pragma config PWRTE = OFF      
#pragma config MCLRE = OFF       
#pragma config BOREN = ON       
#pragma config CP = OFF         
#pragma config CPD = OFF 

Here's the code:

#include <xc.h>
#include "config.h"
#define _XTAL_FREQ 4000000

void dClock(int delay){
    GPIO2 = 1;
    __delay_ms(delay);
    GPIO2 = 0;
    __delay_ms(delay);
}

void main(void) {
    TRISIO2 = 0; //output
    TRISIO3 = 1; //mode
    TRISIO5 = 1; //pulse_button

    char pressed = 0;
    GPIO2 = 0;

    while(1){
        __delay_ms(50);

        if(GPIO3 == 0){
            int delay = ADC_Read(); //Supposed to read analog value here
            dClock(delay);
        }
        else{
            if(GPIO5 == 1 && pressed == 0){
                GPIO2 = 1;
                __delay_ms(500);
                GPIO2 = 0;
                pressed = 1;
            }
            else if(GPIO5 == 0 && pressed == 1){
                pressed = 0;
            }
        }
    }
    return;
}

Edit:

I edited the code and config, and everything is working fine. Now, I'm just still missing the analog input of the potentiometer.

\$\endgroup\$
  • 1
    \$\begingroup\$ Why are you enabling MCLR as reset? \$\endgroup\$ – Spehro Pefhany Jul 7 at 0:35
  • 1
    \$\begingroup\$ When your switch is off, your PIC is reset and your code is not running because you enabled MCLR with #pragma config MCLRE = ON. \$\endgroup\$ – brhans Jul 7 at 2:54
  • \$\begingroup\$ Thank you so much, guys. It works properly now. Just one thing is missing. How to get analog input from the potentiometer? \$\endgroup\$ – Ali Abo Shady Jul 7 at 11:04
  • \$\begingroup\$ Here you find the answer youtube.com/watch?v=LFpSlw2EJ9E Example code drive.google.com/file/d/0B8iMbc-iQqlUbzFxWURKV0swTHM/view \$\endgroup\$ – G36 Jul 7 at 11:13
  • 1
    \$\begingroup\$ I did all the steps in the video, but the ADC_Read(0) function didn't work. I got this error: "Implicit declaration of function ADC_Read is not valid in C99" \$\endgroup\$ – Ali Abo Shady Jul 7 at 11:36
0
\$\begingroup\$

The problem is here:

   int delay = ADC_Read(); //Supposed to read analog value here

ADC_Read() is not a standard function supported by the xc8 compiler. You had to write it on your own.
Hint: Maybe you like to use the library mcc

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.