A/D Conversion on PIC12CE674 with C

Discussion in 'Embedded Systems and Microcontrollers' started by GammaRay86, Dec 18, 2009.

  1. GammaRay86

    Thread Starter Member

    Dec 9, 2007
    14
    0
    Hi guys,

    I'm very new to programming PICs. I'm trying to do an A/D conversion of a voltage given obtained from a sensor. I'm using the PIC12CE674 for now and programming in C.

    Here is my code:

    Code ( (Unknown Language)):
    1. #pragma chip PIC12CE674
    2. #include "delay.h"
    3.  
    4. void main(void){
    5.  
    6. int adval;       // adval is the ADC value
    7. TRIS.0 = 1;          // Set GP0/AN0 as input
    8. TRIS.4 = 0;      // Set GP4/AN3 as output
    9. ADCON1 = 0b.0000.0010;   // Set GP4 as Digital and GP0 as Analog, Vref = Vdd
    10.  
    11. while(1){
    12. //Do the sensor conversion
    13. delay_us(50);           // Set delay of 50 us for sampling (TAD)
    14. ADCON0 = 0b.1100.0101;      // Set oscillator to FRC, Analog Channel 0 (GP0/AN0) selected, Go/Done=1, ADON=1
    15. while(ADCON0.2 == 1){}      // A/D conversion started, continue to check Go/Done, when 0 it is completed
    16.                 // Bit 1 of ADCON0 is cleared when finished
    17. adval = ADRES;          // Read ADRES into adval
    18.  
    19. if(adval >100){         // LED on at  GP4/AN3 (pin 3) when AN0 > 1.8V, off < 1.8V
    20.     GPIO = 0b.0001.0000;
    21. }
    22. else {
    23. GPIO = 0b.0000.0000;
    24. }
    25. }
    26. }
    Here's a link to the datasheet: http://pdfdata.datasheetsite.com/pdf1/Microchip/PIC12LCE674-04-P.pdf

    It compiles fine in MPLAB but when I go to program it into the PIC with PICSTART PLUS, it gives an error of "device NOT blank: Program Memory". What does this mean? Why would the program memory not be blank considering the PIC is brand new?

    I was reading on some things and I came across MCLR. What exactly does this do and do I need to use this somehow? I would appreciate the help.Thanks a lot. :)
     
    Last edited: Dec 18, 2009
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    It might be something to do with the OSCAL value which is preprogrammed. See page 56. Maybe you are supposed to read the value, UV erase the part, and then reprogram.
    I'd strongly recommend getting a 12F675 instead which doesen't require the UV erase. The 674 seems to be a bit of a dinosaur.
     
  3. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
  4. GammaRay86

    Thread Starter Member

    Dec 9, 2007
    14
    0
    Thanks a lot. I didn't know the PIC was one-time programmable. I'm just using it now until I get my 16F684.

    For the code I have, do I require that delay command if the oscillator was set to internal RC? I'm really confused about that part.

    On another note, here's my code for the PIC16F684. I have the delay here as well and I'm not sure if its needed. If anyone can look at it and let me know if it's fine. Thanks!


    Code ( (Unknown Language)):
    1.  
    2. #pragma chip PIC16F684
    3. #include "delay.h"
    4.  
    5. void main(void){
    6.  
    7. int adval, a, b;    // adval is the ADC value
    8. TRISA.0 = 1;        // Set RA0/AN0 (pin 13) as I/P
    9. TRISC.2 = 0;        // Set RC2/AN6 (pin 8) as a dig O/P
    10. ANSEL.0 = 1;        // Set RA0/AN0 (pin 13) as analog I/P
    11. ADCON1 = 0b.0011.0000;  // Clock set to internal Frc
    12.  
    13. while(1){
    14. //Do the sensor conversion
    15. delay_us(50);           // Set delay of 50 us for sampling (TAD)
    16. ADCON0 = 0b.1000.0011;      // Right justified, Vref = Vdd,
    17.                 // Read from AN0 (pin 13), Go/Done = 1, ADC Enabled
    18. while(ADCON0.1 == 1){}      // A/D conversion started, continue to check Go/Done, when 0 it is completed
    19.                 // Bit 1 of ADCON0 is cleared when finished
    20. a = ADRESH.1;           // Read bit 1 of ADRESH into variable a
    21. a = a * 512;            // Multiply "a" by the position of that bit (2^9)
    22. b = ADRESH.0;           // Read bit 0 of ADRESH into variable b
    23. b = b * 256;            // Multiply "b" by the position of that bit (2^8)
    24. adval = ADRESL + a + b;     // ADC value is the sum of ADRESL, a, and b
    25.  
    26. if(adval <369){         // LED on at  RC2 (pin 8) when AN0 < 1.8V, off > 1.8V
    27.     PORTC = 0b.0000.0100;
    28. }
    29. else {
    30. PORTC = 0b.0000.0000;
    31. }
    32. }
    33. }
     
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    You don't need a delay before setting the go/done bit. TAD is the clock that the A/D converter runs from. You need to set bits 2:0 of ADCON1 depending on table 9.1.
    The FRC clock is separate from the main system clock.
    The while loop checking the go/done bit is correct.
     
  6. Harrington

    New Member

    Dec 19, 2009
    86
    3
    Don’t forget this peace of code for the PIC 12F675if you do forget this and erase the device during a reprogram of code you will land up with an un calibrated oscall value and this will cause you no end of problems as i found out not so long ago when designing something else for someone !! You must include this in your main method

    Here is the snippet of code

    asm{
    bsf _status,RP0
    call 0x3FF
    movwf _osccal
    bcf _status,RP0
    } // end asm


    Check the datasheets for this before starting with that device they can be recalibrated but
    I’ve not had much success with this as yet perhaps someone else can throw some light on this subject
     
Loading...