Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC12F675

vijaya_narayana

Full Member level 3
Joined
Jun 12, 2007
Messages
175
Helped
11
Reputation
22
Reaction score
1
Trophy points
1,298
Location
India
Activity points
2,170
Dear All
This is my First Code in PIC12F675 , I am Building a Timer using value set from the POT. We are Using External POT to set the timing value here is the code for the same each button press the time for Relay on Should be calculated based on POT Posistioning my ADC is not Working i am posting the code, Do help in Identifying the problem
Code:
#include <stdio.h>
#include <stdlib.h>

#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)

#include <xc.h>
#define _XTAL_FREQ 4000000
int Count,Timer_ON,eight,fourteen,twel,ten,result;
int sec,min;
void main()
{
    ANSEL  = 0x14;
    ADCON0 = 0x09;         // Turn on the A/D Converter with AD2 As channel Select
    CMCON  = 0x07;         // Shut off the Comparator, so that pins are available for ADC
    VRCON  = 0x00;         // Shut off the Voltage Reference for Comparator
    TRISIO = 0xEF;
    while(1)
    {
      
       
       if(GP5==0)
       {
        while(GP5==0);
        GO_nDONE=1;
       __delay_ms(10);
      while(GO_nDONE==1);//wait for conversion completion
       __delay_ms(100);
       result = ((ADRESH<<8)+ADRESL);
        GP4=1;
        for(sec=0;sec<=result;sec++);
       {
        __delay_ms(1000);
       }
        GP4=0;
      }
    }
}

[edited by moderator to add code tags]
 
Last edited by a moderator:

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
15,205
Helped
4,957
Reputation
9,936
Reaction score
4,781
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
129,126
You don't need the 10mS and 100mS delays, you can safely remove them.
I think your problem may be the calculation of the result, try changing the line:
result = ((ADRESH<<8)+ADRESL);
to
result = ((int)(ADRESH<<8)+ADRESL);

The reason being that ADRESH is an 8-bit register so if you shift its value 8 times you get zero as the result. Promoting it to an 'int' makes the compiler treat it as though it was 16 bits wide so the shift doesn't lose it's bits.

Brian.
 

vijaya_narayana

Full Member level 3
Joined
Jun 12, 2007
Messages
175
Helped
11
Reputation
22
Reaction score
1
Trophy points
1,298
Location
India
Activity points
2,170
i have changed the code as advised
C:
#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)

#include <xc.h>
#define _XTAL_FREQ 4000000
int Count,Timer_ON,eight,fourteen,twel,ten,result;
int sec,min;
void main()
{
    ANSEL  = 0x14;
    ADCON0 = 0x09;         // Turn on the A/D Converter with AD2 As channel Select
    CMCON  = 0x07;         // Shut off the Comparator, so that pins are available for ADC
    VRCON  = 0x00;         // Shut off the Voltage Reference for Comparator
    TRISIO = 0xEF;
    while(1)
    {
    if(GP5==0)
       {
        while(GP5==0);
        GO_nDONE=1;
       __delay_ms(10);
      while(GO_nDONE==1);//wait for conversion completion
       __delay_ms(100);
       result = ((int)(ADRESH<<8)+ADRESL);
        GP4=1;
        for(sec=0;sec<=result;sec++);
       {
        __delay_ms(1000);
       }
        GP4=0;
      }
    }
}


Still no change
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
20,793
Helped
4,495
Reputation
9,002
Reaction score
4,559
Trophy points
1,393
Activity points
137,285
Hi,

I agree with Brian.

My recommendation:
* get into the habit of adding comments to your code. It will not only help us to understand what you try to do, but it also will help you to do the same when you read(modify) the code after years...
* please mind to give useful error descriptions. Like forum rules say "is not working" is almost useless..

Just to give you an idea of the bandwith of possible issues
* compiling or downloading problems
* missing pullup in GP5
* missing/wrong GP4 and GP5 initialisation
* variable mismatch problems like Brian suggested
* right/left aligning problems of ADC value
* your expectation

So you should at least tell
* schematic (to see how you connected the button and maybe it´s pullup/pulldown resistor )
* what you do
* what you expect
* what is working as expected
* what is not working as expected

Example:
* compiling and downloading works
* when button is not pressed: GP4 is LOW
* when button is pressed: GP4 goes HIGH for too long time

Klaus
 

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top