# Programming of ADC in PIC12F675: Not Working Properly

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.

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){
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.

• Why are you enabling MCLR as reset? – Spehro Pefhany Jul 7 at 0:35
• When your switch is off, your PIC is reset and your code is not running because you enabled MCLR with #pragma config MCLRE = ON. – brhans Jul 7 at 2:54
• Thank you so much, guys. It works properly now. Just one thing is missing. How to get analog input from the potentiometer? – Ali Abo Shady Jul 7 at 11:04
   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.