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){
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.
Best Answer
The problem is 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