• 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.

PIC12F675 code doesnt work

Status
Not open for further replies.

Flyback

Well-Known Member
Hello

Do you know why this C code for PIC12F675 doesn’t work?

Its using XC8 C

All it does is make GP0 = 0 if the ADC reads the ‘busvoltage’ variable as being greater than 0x19. If the ‘busvoltage’ variable is less than 0x19 then it should make GP0 = 1;


However, it does not work.

I think its the ADC function doADC(); ...but i am not sure.


When i don’t call the doADC function i have got it able to oscillate the GP0 output. (but i don’t want the code to do that, we want the code to read ADC and act accordingly.)


//Main for the crammed long PCB

//Each Buckboost has a PIC12F675 to control it.


// PIC12F675 Configuration Bit Settings
// 'C' source line config statements

// CONFIG
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdint.h>
#include <pic12f675.h>

#define _XTAL_FREQ 4000000
//#define driversOff GP0 = 1
//#define driversOn GP0 = 0
//#define udhigh GP2 = 1
//#define udlow GP2 = 0
//#define cshigh GP5 = 1
//#define cslow GP5 = 0


uint8_t count;
uint8_t count1;
uint8_t count2;
uint8_t busvoltage;

void zeropot(void);
void decrement(void);
void increment(void);
void doADC(void);
void up5steps (void);
void up35steps (void);
void up64steps (void);

void up5steps (void) {
for (count=5; count=0; count--) {
__delay_us(100);
GP2 = 0; //UD = low
__delay_us(100);
GP2 = 1; //UD = high
__delay_us(100);
}
return;
}

void up35steps (void) {
for (count=35; count=0; count--) {
__delay_us(100);
GP2 = 0; //UD = low
__delay_us(100);
GP2 = 1; //UD = high
__delay_us(100);
}
return;
}

void up64steps (void) {
for (count=64; count=0; count--) {
__delay_us(100);
GP2 = 0; //UD = low
__delay_us(100);
GP2 = 1; //UD = high
__delay_us(100);
}
return;
}

void doADC(void) {
//Initiate ADRESH, ADRESL
busvoltage = 0x00;
ADRESL = 0x00;
__delay_us(100);
ADON = 1; //Turn on ADC module
__delay_ms(1);
GO_DONE = 1; //start conversion
while (GO_DONE) {;} //Wait for conversion to finish
__delay_us(100);
busvoltage = ADRESL;
ADON = 0; //Turn off ADC module
__delay_us(100);
return;
}

void increment(void) { //GET DIGIPOT READY FOR INCREMENT
__delay_us(100);
GP5 = 1; //CS HIGH
__delay_us(100);
GP2 = 0; //UD low
__delay_us(100);
GP2 = 1; //UD high
__delay_us(100);
GP5 = 0; //CS LOW ....NOW inCREMENT
__delay_us(100);
return;
}

void decrement(void) { //GET DIGIPOT READY FOR DECREMENT
__delay_us(100);
GP5 = 1; //CS HIGH
__delay_us(100);
GP2 = 1; //UD high
__delay_us(100);
GP2 = 0; //UD low
__delay_us(100);
GP5 = 0; //CS LOW ....NOW DECREMENT
__delay_us(100);
return;
}

void zeropot(void) { //get wiper to bottom of resistor ladder, ie GND
//ZERO THE DIGI POT
__delay_us(100);
GP5 = 1; //CS HIGH
__delay_us(100);
GP2 = 1; //UD HIGH
__delay_us(100);
GP2 = 0; //UD LOW
__delay_us(100);
GP5 = 0; //CS LOW ....NOW DECREMENT
__delay_us(100);
//Now decrement 64 times
for (count = 65; count = 0; count--) {
__delay_us(100);
GP2 = 1;
__delay_us(100);
GP2 = 0;
__delay_us(100);
} //Now wiper is at the bottom.
return;
}



void main(void) {
//Setup ports
//GP0 = OP SHUTDOWN; High is shutdown
//GP1 = OP UNUSED OUTPUT
//GP2 = OP U/D MCP4013
//GP3 = IP MCLR
//GP4/AN3 = IP ADC input from input rail
//GP5 = OP CS MCP4013

//To increment CS LOW, AFTER UD high
//To decrement CS lOW AFTER UD LOW

//HW SETUP
GP0 = 1; //SHUTDOWN
GP1 = 0; //UNUSED
GP2 = 0; //UD LOW
//GP3 = INPUT
//GP4 = ADC INPUT
GP5 = 1; //CS HIGH
TRISIO = 0x18; //ADC IN
INTCON = 0x00;
OPTION_REG = 0xD7;
CMCON = 0x07;
ADCON0 = 0x0C; //0000-1100 ADCoff_noconv; ch AN3; VREF=VDD;ADON is OFF//L just
ANSEL = 0x68; //fosc/64, AN3,

//5 second delay
for (count=1;count<=10;count++) {
__delay_ms(1);
}

GP0 = 1; //Shutdown the led drivers
zeropot();
increment();
up64steps();

GP0 = 0; //Turn on LEDs

doADC();
if (busvoltage >= 0x19) {GP0 = 1;}


while(1) {;}


/* while(1) {
GP0 = 1; //driversOff;
for (count=1;count<=10;count++) {
__delay_ms(100); }
GP0 = 0; //driversOn;
for (count=1;count<=10;count++) {
__delay_ms(100); }

} */



return;

}
 

Pommie

Well-Known Member
Most Helpful Member
You seem to have the ADC set to left justify but only read the low byte of ADRES. This means you only read two bits.

Mike.
 

hexreader

Active Member
for (count=35; count=0; count--) {

This looks completely wrong to me. Try...

for (count = 35 ; count > 0; count--) {

And same change for the other similar functions.
 

Pommie

Well-Known Member
Most Helpful Member
You also need to check CS. In zeropot you leave it low and dont set it high in your various other routines.

Mike.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top