Menu

adc example needed

Help
2017-07-27
2017-07-29
  • stan cartwright

    stan cartwright - 2017-07-27

    Sorry to ask, I have tried to setup adc in free running mode with mega328p and use on interrupt ADCReady to store data in an array that looped.
    Using #define ADReadPreReadCommand I tried to set up ADCSRB Bits 2:0 – ADTSn: ADC Auto Trigger Source [n = 2:0] 000 Free Running mode from data sheet.
    Point is I couldn't sort it. To save time an example for 328p, oninterrupt adcread and any info on setting adc register bits would be useful to me.I can do more with a working example than figureing things out for myself and making errors. We're all busy so I appreciate any help. I do try to sort things myself first but sometimes get nowhere.
    I have looked at the online help for latest info. In the mean time I'll start again and test the interrupt with a scope and build up logically. moi,logical,ha

     
  • Anobium

    Anobium - 2017-07-27

    What do you mean by a free running mode?

     
    • Anobium

      Anobium - 2017-07-28

      Untested but the basis of free running ADC for AVR Mega328p - will require debugging and validating.

      Anobium

        '
        #chip mega328p
      
        ' THIS CONFIG OF THE SERIAL PORT WORKS WITH A  MAX232 (or equiv) THEN TO PC
        ' Ports are defined in datasheet or by PPS
        ' USART settings
        #define USART_BAUD_RATE 9600
        #define USART_BLOCKING
        #define USART_BLOCKING
        ' You change the usart delay value. Uncomment next line
        ' #define USART_DELAY 5 ms
        ' Let USART settle down
        wait 500 ms
      
        Dim LLADResult As Word Alias ADCH, ADCL   'this will contain the ADC result
      
        readFlag = 0
      
      
        ADATE = 1   'enable auto-triggering.
        ADTS0 = 0   'Clear ADTS0 in ADCSRB to set trigger mode to free running.
        ADTS1 = 0   'Clear ADTS1 
        ADTS2 = 0   'Clear ADTS2
        on Interrupt ADCReady call HandleADC
      
        'Use the GCB method  set up the ADC
        ADVal  = ReadAD10( An0 )
      
      
        Do
      
          if readFlag = 1 then
      
            HSerPrint   LLADResult
            HSerPrintCRLF
      
            readFlag = 0
      
          end if
      
        Loop
      
      
        sub HandleADC
      
          ' Done reading
          readFlag = 1
      
        End Sub
      
       
  • stan cartwright

    stan cartwright - 2017-07-27

    From the data sheet and this link..and others https://www.instructables.com/id/Girino-Fast-Arduino-Oscilloscope/
    I did mention it to you. I tried implementing it in GCB but it's worth clarifying..for me anyway.
    ;
    data sheet says-
    Bits 2:0 – ADTSn: ADC Auto Trigger Source [n = 2:0]
    If ADATE in ADCSRA is written to one, the value of these bits selects which source will trigger an ADC
    conversion. If ADATE is cleared, the ADTS[2:0] settings will have no effect. A conversion will be triggered
    by the rising edge of the selected Interrupt Flag. Note that switching from a trigger source that is cleared
    to a trigger source that is set, will generate a positive edge on the trigger signal. If ADEN in ADCSRA is
    set, this will start a conversion. Switching to Free Running mode (ADTS[2:0]=0) will not cause a trigger
    event, even if the ADC Interrupt Flag is set.
    Table 28-6. ADC Auto Trigger Source Selection
    ADTS[2:0] Trigger Source
    000 Free Running mode
    001 Analog Comparator
    010 External Interrupt Request 0
    011 Timer/Counter0 Compare Match A
    100 Timer/Counter0 Overflow
    101 Timer/Counter1 Compare Match B
    110 Timer/Counter1 Overflow
    111 Timer/Counter1 Capture Event
    PS It's supposed to be a faster way of reading adc so I thought, 1st sample 25us then subsequent 13us...at certain clock prescale...doh

     

    Last edit: stan cartwright 2017-07-27
  • Anobium

    Anobium - 2017-07-27

    What did you use for ADReadPreReadCommand?

     
  • stan cartwright

    stan cartwright - 2017-07-27

    Does not matter,it didn't work so logically irrellevant and want working code example as first post because no point in me posting my failed efforts..I reckon. Scratch pad ideas gone anyway.
    @William posted a frequency counter program which was very useful. Helped me a lot. ps cheers.
    Often on this forum a "try this code" solves everything.
    If there was an example in help I wouldn't impose on others time but not everything is in help..yet.
    Give me an example and I'll try it and report results.
    I don't think this is not normal use of GCB for using uchips,as the link,seems it's what people try.
    Someone else might try this in GCB and have the same problems..or not

     
    • Anobium

      Anobium - 2017-07-28

      OK

       
  • stan cartwright

    stan cartwright - 2017-07-28

    I searched for my attempt thinking it was commented out code but can't find it.
    It's like interrupt on pinchange...you need to add PCINT0 = 1
    On Interrupt PinChange0 Call portb0change
    It is not shown in help and not untill @William posted code did I get to use it.I wouldn't have figured PCINT0 = 1 out myself.
    On Interrupt PinChange0 Call portb0change
    Same with oninterrupt adcread finished. Does it check all ad channels?
    I don't like asking for advice but for GCB, the forum is the only source of info for GCB specific keywords, etc.and seems more mega328p data sheet type words have been added.
    A this is how to do it example would be like a picture says...etc.

     
  • Anobium

    Anobium - 2017-07-28

    I will share a good strategy to understand the Great Cow BASIC keywords the IDE contains a list of the keywords, the attachment is part of the IDE.

    However, I think you mean the microcontroller registers and registers bits. These are not under our control as these come from the external sources including datasheets and only microcontroller specific source files. A strategy. Look at the microcontroller specific data file, for the mega328p it is called the mega328p.dat (all the chip files are in the chipdata folder. In this file you will see the registers and the register bits. If you then look at the datasheet you will see a direct correlation. The Great Cow BASIC dat file is our way specifing the database of the microcontroller.

    • An example. PCINT0. We call that PinChange0. This is shown in the Help. PinChange0 is the Logic level of PCINT0 pin has changed and is specific to the AVR range of microcontrollers.

    There may be errors or omissions in these dat files. We correct when folks report errors but we mostly find the source information provided to be at fault. We do have very robust methods to manage the .dat files including auto correction of known issues and very good parsing routines - but, these dat files can be incorrect.

    But, if this does not answer your question - let me know.

     
  • stan cartwright

    stan cartwright - 2017-07-29

    Thank you for replying, an example is what I need though.
    Here's another example of me checking data sheet for GCB not obvious method use.
    ;Inittimer1 (EXT, PRE0_1) ; THIS IS NOT EXPLAINED IN HELP
    ;TCCR1B = TCCR1B or 0B00000111 ;set CS12, CS11 and CS10 to "1" port d.5 input
    cs12=1:cs11=1:cs10=1
    On interrupt Timer1Overflow Call CountOverflows
    3 ways to do the same thing. It was interesting...but I didn't want to refer to data to use a GCB method.
    It's just all new to me, a seasoned user would just be implementing ideas using GCB rather than learning pics using GCB.
    It's only an experiment anyway. I know adc is slow and using a fast adc chip needs different hardware.
    The Due board I just got off ebay is much faster. Got to use arduino ide and amtel studio for first time :(

     
  • Anobium

    Anobium - 2017-07-29

    Ok

     
  • stan cartwright

    stan cartwright - 2017-07-30

    I tried with GLCD ili9341 and it stops the display as I posted before using hardware spi and interrupts but problem not acknowledged.
    Here is a working test rig display until I add the interrupt. A timeroverflow interrupt stops display also.
    Is GLCD_RESET necessary and cause of problem? I tie it hi and normally ok

    #chip mega328p,16
    #option explicit
    #include <glcd.h>
    ;
        #define GLCD_TYPE GLCD_TYPE_ILI9341
        'Pin mappings for SPI - this GLCD driver supports Hardware SPI and Software SPI
        #define GLCD_DC   portb.2 ;    DIGITAL_10         'HW Data command line DC
        #define GLCD_CS   portd.7 ;    DIGITAL_7          ' Chip select line CS
        #define GLCD_RESET   portd.4 ;      DIGITAL_4         ' Reset line Tie high..not needed ...or is it
        #define GLCD_DO   portb.3 ;   DIGITAL 11          'HW Data out | MOSI SDI
        #define GLCD_SCK   portb.5 ;   DIGITAL_13          'HW Clock Line SCK
    ;
        #define ILI9341_HardwareSPI    ' remove/comment out if you want to use software SPI.
        'GLCD selected extension font set. ASCII characters 31-254, the extended font uses 1358 bytes of program memory
        #define GLCD_EXTENDEDFONTSET1
        GLCDfntDefaultsize = 1
    
    dir portc.3 out
    dir portc.0 in
    dim Array_ptr as word
    dim sampleArray1 (1024) as byte
    
    ;********** This as is stops display ************
    Dim LLADResult As Word Alias ADCH, ADCL   'this will contain the ADC result
      ADATE = 1   'enable auto-triggering.
      ADTS0 = 0   'Clear ADTS0 in ADCSRB to set trigger mode to free running.
      ADTS1 = 0   'Clear ADTS1
      ADTS2 = 0   'Clear ADTS2
    ;  on Interrupt ADCReady call HandleADC
    ;*************************************************  
      wait 10 ms
    ;
    dim volt as byte
    dim ptr1 as word
    dim ptr2 as word
    dim buffer1 (60)
    dim buffer2 (60)
    dim xpos_new as word
    dim ypos_new as byte
    dim xpos_old as word
    dim ypos_old as byte
    dim xpos1_new as word
    dim ypos1_new as byte
    dim xpos1_old as word
    dim ypos1_old as byte
    ;
    volt=readad(an0)
    GLCDRotate ( Portrait_Rev )
    GLCDfntDefaultsize = 3
    GLCDCLS ILI9341_BLUE
    filledbox  0,0,239,257,ILI9341_BLACK
    box  0,0,239,257,ILI9341_YELLOW
    ;
    for ptr1=1 to 60 ;read 60 samples
      volt = sampleArray1(ptr1)
      buffer1 (ptr1)=255-volt
      buffer2 (ptr1)=buffer1 (ptr1)
    next ptr1
    ;
    do ; start of main loop
      ptr1=1
      ptr2=1
      do until ptr1=60 ;erase old screen data and redraw new
        ypos_old=buffer2 (ptr1) ;old data
        ypos_new=buffer2 (ptr1+1)
        xpos_new=ptr2+4
    ;
        ypos1_old=buffer1 (ptr1) ;new data
        ypos1_new=buffer1 (ptr1+1)
        xpos1_new=ptr2+4
    ;
        line ptr2,ypos_old+1,xpos_new,ypos_new+1,ILI9341_BLACK ;erase last data
        line ptr2,ypos1_old+1,xpos1_new,ypos1_new+1,ILI9341_cyan ;draw new data
    ;
        ptr1=ptr1+1 ;data pointer
        ptr2=ptr2+4 ;x position for start and end line x positions
      loop
    ;ploting done now redraw cursor
    line 1,127,239,127,ILI9341_GREEN ; Draw cursor
    line 119,1,119,254,ILI9341_GREEN ;after ploting
    ;
    for ptr1=1 to 60 ;get new samples
      volt = sampleArray1(ptr1)
      buffer2 (ptr1)=buffer1 (ptr1) ;new data to old data
      buffer1 (ptr1)=255-volt ;set 0,0 top left screen and update new data
    next ptr1
    ;
    loop ;end of main
    ;
    sub HandleADC ;test 
    portc.3=!portc.3
    '  sampleArray1 (array_ptr)=ADCH
    '  array_ptr = Array_ptr + 1
    '  if Array_ptr = 1023 then
    '    Array_ptr = 1
    '  end if
    End Sub
    
     
  • stan cartwright

    stan cartwright - 2017-07-30

    Please have a look,best way to explain it https://youtu.be/JxhCXlDKgiA
    this line alone stops display init.
    ADTS0 = 0 'Clear ADTS0 in ADCSRB to set trigger mode to free running

     

    Last edit: stan cartwright 2017-07-30

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.