Connect with us

Assistance with MPLAB C Code

Discussion in 'Microcontrollers, Programming and IoT' started by chopnhack, Sep 8, 2015.

Scroll to continue with content
  1. chopnhack

    chopnhack

    1,573
    352
    Apr 28, 2014
    I am not sure of what I am doing wrong. I tried to access one LED on port RA2 of a PIC10LF320. I was able to get it to flash in ASM, but I am trying to work this out in C. So far I have successfully (I think) created my header file. My program compiles and loads, but no light from the LED.
    Any ideas on how to address the LAT register more correctly? I also tried LATAbits.RA2 as well as PORTAbits.RA2 and variants of this containing TRISAbits.RA2. The compiler stated it did not recognize RA2. Am I missing an include file or ??

    First picture is of the header file and the second is the .c file.
    Thanks in advance!
    upload_2015-9-7_19-19-52.png

    upload_2015-9-7_19-20-59.png
     

    Attached Files:

  2. jayanthd

    jayanthd

    43
    10
    Jul 4, 2015
    It is

    PORTAbits.RA2
    LATAbits.LATA2
    TRISAbits.TRISA2

    If possible zip and post the complete MPLAB X XC8 project files. I will fix it and post it.
     
    chopnhack likes this.
  3. chopnhack

    chopnhack

    1,573
    352
    Apr 28, 2014
    That did it! Thank you so much jaya - it was the syntax!!!

    I was using LATAbits.RA2. Is there a reference to learn the proper syntax?
    Thanks again!
     
  4. jayanthd

    jayanthd

    43
    10
    Jul 4, 2015
    I am not sure where it is mentioned but see if it is mentioned in the device header file. The files are in Include folder of Compiler folder.
     
    Last edited: Sep 8, 2015
  5. chopnhack

    chopnhack

    1,573
    352
    Apr 28, 2014
    True, the compiler is XC8 IIRC.

    Thank you for that tip - that really does simplify the syntax a lot!
    I am trying to use one port to take in an analog signal. The datasheet refers to ADCON needing port config (I assume TRIS is used), channel select, adc conversion clock and interrupt.

    The direction setting, I believe I have correct with:
    TRISAbits.TRISA0=1 ; // setting the direction of AN0 to input.

    would this be the appropriate way to use the ADCON sfr?
    ANSELAbits.ANSA0 = 1; //setting analog input to this register
    ADCONbits.ADON = 1; //enabling ADC
    ADCONbits.CHS = 000; //selecting channel AN0
    ADCONbits.ADCS = 011; //using internal clock frequency
    delay_us(5); //delay for acquisition

    ADCONbits.GO_nDONE = 1; //start conversion
     
  6. jayanthd

    jayanthd

    43
    10
    Jul 4, 2015
    These are correct.

    Code:
    TRISAbits.TRISA0=1 ;
    ANSELAbits.ANSA0 = 1;
    ADCONbits.ADON = 1;
    
    but you could just write
    Code:
    TRISA = 0xC1; (RA6 and RA7 used for Crystal)
    ANSEL = 0x01;
    
    These I can't answer without looking at the datasheet and to do that I need to know which PIC you are using.

    Code:
    ADCONbits.CHS = 000; //selecting channel AN0
    ADCONbits.ADCS = 011; //using internal clock frequency
    
     
    chopnhack likes this.
  7. chopnhack

    chopnhack

    1,573
    352
    Apr 28, 2014
    I am having trouble understanding how we can convert the datasheet given values which are usually binary for ASM use to the hex values you showed. How did you derive those values? The chip I am working with does not need an external crystal nor does it have a6/a7. Its only a 8 pin (6 functional pins)

    I am using the PIC10LF320 - the link takes you to the datasheet if you have the time.
    Thank you so much! Progress!!
     
  8. jayanthd

    jayanthd

    43
    10
    Jul 4, 2015
    To use Hex values all you have to do is memorize the binary representation of the Hex values 0 (0x00) to Fh (0x0F).

    If you want to use 4 ADC and you have ANSEL register to do that then you would write

    Code:
    ANSEL = 0x0F
    In 0x0F, 0 represents for binary digits and F represents four binary digits.

    0x0F is equal to binary 0b00001111

    So, instead of writing

    Code:
    ANSELbits.ANSA0 = 1;
    ANSELbits.ANSA1 = 1;
    ANSELbits.ANSA2 = 1;
    ANSELbits.ANSA3 = 1;
    
    You could write it as

    Code:
    ANSEL = 0x0F
    
    This is correct
    Code:
    ADCONbits.ADON = 1;
    
    These are wrong.
    Code:
    ADCONbits.CHS = 000; //selecting channel AN0
    ADCONbits.ADCS = 011; //using internal clock frequency
    
    Look at the datasheet, page no. 96, REGISTER 15-1, CHS and ADCS are represented as CHS<2:0> and ADCS<2:0> It means they have bits 0, 1 and 2.

    So, there would be CHS0, CHS1, CHS2 and ADCS0, ADCS1 and ADCS2.

    You want to use channel 0, so you have to write 0 to all the three CHSx bits and you would do like this

    Code:
    ADCONbits.CHS0 = 0
    ADCONbits.CHS1 = 0
    ADCONbits.CHS2 = 0
    
    or
    Code:
    ADCON = 0b00000001;
    
    GO_DONE bit is set when ADC conversion is to be started.

    This is not the correct value yet.

    Code:
    ADCON = 0b00000001;
    
    because ADCSx bits have to be set properly and to do that I have to know what clock frequency you are using for the PIC10F.


    Regarding TRISA it should be 0x01 = 0b00000001.


    Edit

    I had made mistake in writing correct values to CHSx bits. I have edited the post and corrected it.
     
    Last edited: Sep 8, 2015
    chopnhack likes this.
  9. jayanthd

    jayanthd

    43
    10
    Jul 4, 2015
    @chopnhack
    Please use code tags to post the code.

    CODE with in square brackets [] before the code and /CODE within square brackets [] after the code.
     
  10. chopnhack

    chopnhack

    1,573
    352
    Apr 28, 2014
    I understand this better now, thank you! I found a chart as a reference and it seems that the hex method is much shorter to type.

    bit 0 indicates 'on', why does the datasheet list bits 7-5 '111' and '011' as both being FRC? FRC is the dedicated internal oscillator.

    I used binary value for OSCCON in my code, does binary value work for each SFR?
    Will do!

    My clock speed was 1MHz.
    Code:
    OSCCON = 00110000;
    So ADCON could change to:
    Code:
    ADCON = 0b11100001; or ' ' = 0b01100001;
     
  11. jayanthd

    jayanthd

    43
    10
    Jul 4, 2015
    ADCS<2:0> can have 8 values. These values are used to select the channel of the MUX which provide the conversion clock to the ADC circuit (ADC module). I have to read the datasheet ADC section to give you the correct value for ADCS<2:0> bits. I have never used FRC Oscillator and also I have never used PIC10F. I mainly use PIC12F, 16F and 18F devices.

    I will soon provide you the answer. I think as the datasheet says 9.5 TAD is required to do the adc conversion and based on the Oscillator (FOSC) value the TAD has to be calculated and then 9.5 TAD has to be calculated and and TAD value which is higher than the required TAD has to be selected for ADCS<2:0> bits.
     
    chopnhack likes this.
  12. chopnhack

    chopnhack

    1,573
    352
    Apr 28, 2014
    I appreciate that! Whenever you have time, I was a bit lost on that section so any explanation will be very welcome. Thanks again.
     
  13. Old Steve

    Old Steve

    734
    169
    Jul 23, 2015
    chopnhack, your calculator possibly has hex, binary, decimal, octal and maybe even pental. That's how I do my conversions. Otherwise the Windows calculator does it easily in 'scientific' mode. Much more convenient than using charts.

    Calculator hex dec bin oct.JPG
     
    chopnhack likes this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-