Continue to Site

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.

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

12f617

Status
Not open for further replies.

prestonee

New Member
I want to start off saying I havent used a PIC since college gdays but am being thrown back into it after switching jobs from ic design to pcb design.

I ordered this pic part because it is small footprint, small power, and has 'self writing flash'. I am assuming this self writing flash means i can have my program write to the flash in normal operation mode ( save data in non volatile memory).

My issue is I cant find any example code for this pic. and I am unaware of any built in functions. I need general guidance on programming this pic or if this is a bad pic what a good alternative would be. I am currently using MPLAB SIM with HI-Tech C compiler for PIC 10-12-16...

Im currently stuck on getting the CCPIF special interupt to trigger the ADC GO bit.
Also do these sim tools simulate the PIC ADC? or do they just use stimulus to insert what the ADC Predicted Result will be into the ADC result register?

Please reply, as you can see I am a bit lost...
 
Can you give us some more info on what you mean by getting the CCPIF to trigger the ADC GO bit? The CCP and ADC are two different modules, so I assume what you mean is that you are entering an ISR based on a CCP module interrupt and that you can't set the ADC go bit from inside your ISR.

Is this what you are saying?

If so, are you sure that you have the ADC module turned on by setting the appropriate bit in the ADCON0 register?

I'm not sure about certain stimulae like ADC conversions in MPLAB, but the hang up could be that you are doing everything right, but that your code polls the GO/DONE bit for a 0 and MPLAB won't show it without doing some tricky things with the stimulus settings.
 
I am going based of this information from the spec sheet: When Special Event Trigger mode is chosen
(CCP1M<3:0> = 1011), the CCP1 module does the
following:
• Resets Timer1
• Starts an ADC conversion if ADC is enabled

I believe this is a special interupt with automatic cause/effect that exists soley for starting the adc. The register settings are set up to use the running timer to compare with a programmed value , when the compare equals the adc takes a sample ( a time delayed adc sample).
// Interupts
INTCON = 0b010000000;
PIE1 = 0b00100000;

//Port settings
TRISIO = 0b00111001;
ANSEL = 0b01101000;
//ADC settings
ADCON0 = 0b10001101;
// Timer1 settings
T1CON = 0b10111101;
//Comparitor Settings
CCP1CON = 0b00001011;
// Compare N timer setting
CCPR1H = 0b00000000 ;
CCPR1L = 0b00001001;
 
I don't know C, so take what I'm saying with a grain of salt.

I've noticed that there are 9 bits listed in your declaration of the value of the INTCON register. I'd imagine that can throw things off.

It's hard to tell what you are trying to do from the 9 bits you are showing for INTCON, but remember that you have to set both the global interrupt enable and peripheral interrupt enable bits.
 
Last edited:
It was off by 1, but the fix did not change anything, i tried enabling the global but it had no impact.

Just cant get the Go bit to toggle unless i write my own code to change it manually.
 
I understand that, but i cant get the program to start the go bit.
Also... is there any place to go to for already made functions, like I2C?
 
If you enable interrupts without an ISR then it will crash. Can you post a compileable file so we can see what might be the problem?

Mike.
 
#include <htc.h>
#include <stdio.h>


//__CONFIG(OSC_INTRC_NOCLKOUT | WDT_OFF | PWRTE_ON | MCLRE_OFF | CP_OFF | IOSCFS_4MHZ | BOREN_OFF | WRT_OFF );
__CONFIG(11110001000100);

long maxvalue = 11110000;
int x=0;

// pupose: sleep mode on, slow count, end of count= adc sample, add sample to total, subtract total from max, output to LED remaining.

// look into: activating adc from interupt, writing to flash, outputting in i2c


int monitor_function(){

//retrieve adc value
long z = (ADRESH<<8) | ADRESL; //need to concatonate? C = (A<<4) | B ;
long total = total + z; //largest value?
int count;
count ++;
if (count == 10){
//write total to flash
count =0;
}

long remaining = maxvalue - total;
return remaining;
}

int main(){

// Interupts
INTCON = 0b11000000;
PIE1 = 0b00100000;

//Port settings
TRISIO = 0b00111001;
ANSEL = 0b01101000;
//ADC settings
ADCON0 = 0b10001101;
// Timer1 settings
T1CON = 0b10111101;
//Comparitor Settings
CCP1CON = 0b00001011;
// Compare N timer setting
CCPR1H = 0b00000000 ;
CCPR1L = 0b00001001;

GP2 = 0;
TMR1 = 0;
int y;


while(x<10000000){
x++;
if(CCPIF == 1){
GP2= 1;
//implement a go ==0 check before continueing
y= monitor_function();
CCPIF = 0;
}
else
GP2=0;

}

return 0;
}
 
It compiles and runs but im not seeing the go bit toggle.
Also i see codes that use syntax like ADCON0.GODONE or just using the word GODONE, but neither work for me. is there an include thats needed for this?
 
I've not had a chance to play with it but saw some mistakes, mainly enableing interrupts without an ISR.
Code:
#include <htc.h>
#include <stdio.h>


[COLOR="red"]__CONFIG(OSC_INTRC_NOCLKOUT & WDT_OFF & PWRTE_ON & MCLRE_OFF & CP_OFF & IOSCFS_4MHZ & BOREN_OFF & WRT_OFF );[/COLOR]

long maxvalue = 11110000;
int x=0;

// pupose: sleep mode on, slow count, end of count= adc sample, add sample to total, subtract total from max, output to LED remaining.

// look into: activating adc from interupt, writing to flash, outputting in i2c


int monitor_function(){
[COLOR="red"]    long z;
    long total;
    int count;
    long remaining;[/COLOR]
    //retrieve adc value
    z = (ADRESH<<8) | ADRESL; //need to concatonate? C = (A<<4) | B ;
    total = total + z; //largest value?
    count;
    count ++;
    if (count == 10){
        //write total to flash
        count =0;
    }
    
    remaining = maxvalue - total;
    return remaining;
}

int main(){
    // Interupts
    INTCON = 0b[COLOR="red"]00[/COLOR]000000;
    PIE1 = 0b00100000;
    
    //Port settings
    TRISIO = 0b00111001; 
    ANSEL = 0b01101000;
    //ADC settings
    ADCON0 = 0b10001101;
    // Timer1 settings
    T1CON = 0b10111101;
    //Comparitor Settings
    CCP1CON = 0b00001011;
    // Compare N timer setting
    CCPR1H = 0b00000000 ;
    CCPR1L = 0b00001001;
    
    GP2 = 0;
    TMR1 = 0;
    int y;
    
    
    while(x<10000000){
        x++; 
        if(CCPIF == 1){
            GP2= 1;
            //implement a go ==0 check before continueing
            y= monitor_function();
            CCPIF = 0;
        }
        else
        GP2=0;    
    }
    
    return 0;
}
I also removed the inline declarations as I believe initialisation has to be constant values.

Note, GP2 will only go high for less than a mS with the above code.

Mike.
 
Status
Not open for further replies.

Latest threads

Back
Top