Menu

ReadAD10 to ATTiny13 does not work correctly

2018-03-19
2018-03-20
  • Agustin Hernandez

    In the manual it states that AdRefSource = AD_REF_AVCC should be set but due to AVR library failure it does not work. It is also necessary to edit the configuration file tyny13.dat and add the bit REFS1, ADMUX, 7 of the AVR family although this micro is not implemented. If it is not added, during compile time it gives a 'REFS1 not defined' error.

    ;Chip Settings
    #chip tiny13,4,8
    
    ;Constantes
    ; --- Definiciones necesarias para el correcto funcionamient de la coversion AD
    #define AD_REF_SOURCE ADRefSource
    #define AD_VREF_DELAY 5 ms
    
    ; --- Entradas / Salidas
    #define LED_CALOR     PORTB.0
    #define LED_FRIO      PORTB.1
    #define LED_PARPADEO  PORTB.2
    ; Entradas Analogicas
    #define Mando_In  AN2
    #define Sonda_In  AN3
    
    ; --- Otras Constantes del Programa
    #define Histeresis  1024*10/100 ; El 10%
    #define ZONA_MUERTA 0
    #define CALOR       1
    #define FRIO        3
    
    ;Variables
    Dim Sonda  As word  ; Resistencia NTC como elemento sensor de temperatura
    Dim Mando  As word  ; Potenciometro para fijar la temperatura de consigna
    Dim Estado As byte
    Dim Anterior As byte
    
    ;INICIO DE PROGRAMA
    Dir LED_CALOR    Out
    Dir LED_FRIO     Out
    Dir LED_PARPADEO Out
    
    Do Forever
    
    ;=================== TO CORRECTLY COMPILE THE CODE =================
      REFS0 = On
     ; Referencia de conversion = VCC / Si se pone a 0 se selecciona la   
     ; referencia interna de 1.1 voltios.
     ; En el manual pone que habria que poner AdRefSource = AD_REF_AVCC 
     ; pero debido a fallo en libreria AVR no funciona.
     ; Tambien se hace necesario editar el fichero de configuracion
     ; tyny13.dat y añadir el bit REFS1,ADMUX,7 de la familia
     ; AVR aunque en este micro no esta implementado. Si no se añade,
     ; durante tiempo de compilacion da fallo "REFS1 no definido"
    
     ;  AdRefSource = AD_REF_AVCC  ---> NOT WORKING
    ; ===================================================================
    
      Sonda = ReadAD10(AN3)
      Mando = ReadAD10(AN2)
    
      Estado = ZONA_MUERTA
      If ((Sonda+Histeresis)<Mando) Then Estado = CALOR
      If (Sonda>(Mando+Histeresis)) Then Estado = FRIO
      Select Case Estado
        Case CALOR
          Set LED_CALOR On
          Set LED_FRIO  Off
        Case FRIO
          Set LED_CALOR Off
          Set LED_FRIO  On
        Case ZONA_MUERTA
          Set LED_CALOR Off
          Set LED_FRIO  Off
      End Select
    
      Wait 500 ms
      LED_PARPADEO = Not LED_PARPADEO
    
    Loop
    

     
  • Theo

    Theo - 2018-03-19
    Augustin,
    
    1. bring the tiny13.dat file back to original 
    2. delete REFS0 = On in your code
    
    3. adapt you code with this line as workaround:
    
    #define REFS1 REFS0
    
    until the tiny13.dat(and/or the a-d.h) is adapted by the developers.
    
    4. I don't know what the effect is on compiling but change the Chip setting into:
    
    #chip tiny13,4.8
    or 
    #chip tiny13,9.6
    
    there must be a dot between the 4 and the 8.
    
    Then the code will compile without errors, if it will run correct I can't check!
    
    Success
    
    Theo.
    
     

    Last edit: Theo 2018-03-19
  • Anobium

    Anobium - 2018-03-19
     
    • Agustin Hernandez

      Ok, thank
      1. I have reinstall last version GCBasic (0.98.01 2017-10-27)
      2. copy and replace a-d.h
      3. delete "REFS0 = On" in my code

      New code:

      ;Chip Settings
      #chip tiny13,4.8
      ;#Option Explicit
      
      ;Constantes
      ; --- Definiciones necesarias para el correcto funcionamient de la coversion AD
      #define AD_REF_SOURCE ADRefSource
      #define AD_VREF_DELAY 5 ms
      
      ; --- Entradas / Salidas
      #define LED_CALOR     PORTB.0
      #define LED_FRIO      PORTB.1
      #define LED_PARPADEO  PORTB.2
      ; Entradas Analogicas
      #define Mando_In  AN2
      #define Sonda_In  AN3
      
      ; --- Otras Constantes del Programa
      #define Histeresis  1024*10/100 ; El 10%
      #define ZONA_MUERTA 0
      #define CALOR       1
      #define FRIO        2
      
      ;Variables
      Dim Sonda  As word  ; Resistencia NTC como elemento sensor de temperatura
      Dim Mando  As word  ; Potenciometro para fijar la temperatura de consigna
      Dim Estado As byte
      
      ;INICIO DE PROGRAMA
      Dir LED_CALOR    Out
      Dir LED_FRIO     Out
      Dir LED_PARPADEO Out
      
      Do Forever
        AdRefSource = AD_REF_AVCC
        Sonda = ReadAD10(AN3)
        Mando = ReadAD10(AN2)
      
        Estado = ZONA_MUERTA
        If ((Sonda+Histeresis)<Mando) Then Estado = CALOR
        If (Sonda>(Mando+Histeresis)) Then Estado = FRIO
        Select Case Estado
          Case CALOR
            Set LED_CALOR On
            Set LED_FRIO  Off
          Case FRIO
            Set LED_CALOR Off
            Set LED_FRIO  On
          Case ZONA_MUERTA
            Set LED_CALOR Off
            Set LED_FRIO  Off
        End Select
      
        Wait 500 ms
        LED_PARPADEO = Not LED_PARPADEO
      
      Loop
      

      Compiler:

        4.3  Sec.    Compiler Version: 0.98.01 2017-10-27   Program Memory: 388  bytes    RAM: 10/64 bytes (15,62%)   Chip: TINY13
      

      OK, but ERROR if #Option Explicit

        4.8  Sec.    <<<   WARNINGs / ERRORs while compiling!
      Doubleclick on errormessage below to go to sourcecode-line:
      ATTiny13_Termostato.gcb (40): Error: Variable ADREFSOURCE was not explicitly declared
      
       

      Last edit: Agustin Hernandez 2018-03-19
  • Anobium

    Anobium - 2018-03-19

    Lots of undefined variables. Hunt through this code to spot where I DIM the variables.

    ;Chip Settings
    #chip tiny13,4.8
    #Option Explicit
    
    ;Constantes
    ; --- Definiciones necesarias para el correcto funcionamient de la coversion AD #define AD_REF_SOURCE ADRefSource #define AD_VREF_DELAY 5 ms
    
    ; --- Entradas / Salidas
    #define LED_CALOR     PORTB.0
    #define LED_FRIO      PORTB.1
    #define LED_PARPADEO  PORTB.2
    ; Entradas Analogicas
    dim Mando as word
    dim Sonda as word
    dim ZONA_MUERTA, Estado as long
    
    ; --- Otras Constantes del Programa
    #define Histeresis  1024*10/100 ; El 10% #define ZONA_MUERTA 0
    #define CALOR       1
    #define FRIO        2
    
    ;Variables
    Dim Sonda  As word  ; Resistencia NTC como elemento sensor de temperatura Dim Mando  As word  ; Potenciometro para fijar la temperatura de consigna Dim Estado As byte
    
    ;INICIO DE PROGRAMA
    Dir LED_CALOR    Out
    Dir LED_FRIO     Out
    Dir LED_PARPADEO Out
    
    Do Forever
      'AdRefSource = AD_REF_AVCC
      Sonda = ReadAD10(AN3)
      Mando = ReadAD10(AN2)
    
      Estado = ZONA_MUERTA
      If ((Sonda+Histeresis) < Mando ) Then Estado = CALOR
      If (Sonda>(Mando+Histeresis)) Then Estado = FRIO
      Select Case Estado
        Case CALOR
          Set LED_CALOR On
          Set LED_FRIO  Off
        Case FRIO
          Set LED_CALOR Off
          Set LED_FRIO  On
        Case ZONA_MUERTA
          Set LED_CALOR Off
          Set LED_FRIO  Off
      End Select
    
      Wait 500 ms
      LED_PARPADEO = Not LED_PARPADEO
    
    Loop
    
     
    • Agustin Hernandez

      Excuseme I had a error in download library a-d.h. Please see the last reply I edited.

       
      • Agustin Hernandez

        Ok, now if delete line: AdRefSource = AD_REF_AVCC
        Code Working correct and in similator ISIS Proteus AD ref is VCC.
        All Ok, but not as the GCBasic help file manual says.

        I will continue testing CGBasic with the small AVR Tiny.
        Thank you
        greetings

         
  • Agustin Hernandez

    Definitive Code working:
    1. I have reinstall last version GCBasic (0.98.01 2017-10-27)
    2. copy new lib and replace a-d.h

    Code:

    ;Chip Settings
    #chip tiny13,4.8
    #Option Explicit
    
    ;Constantes
    ; --- Entradas / Salidas
    #define LED_CALOR     PORTB.0
    #define LED_FRIO      PORTB.1
    #define LED_PARPADEO  PORTB.2
    ; --- Entradas Analogicas
    #define Mando_In  AN2
    #define Sonda_In  AN3
    ; --- Otras Constantes del Programa
    #define Histeresis  1024*10/100 ; El 10%
    #define ZONA_MUERTA 0
    #define CALOR       1
    #define FRIO        2
    
    ;Variables
    Dim Sonda  As word  ; Resistencia NTC como elemento sensor de temperatura
    Dim Mando  As word  ; Potenciometro para fijar la temperatura de consigna
    Dim Estado As byte
    
    ;INICIO DE PROGRAMA
    Dir LED_CALOR    Out
    Dir LED_FRIO     Out
    Dir LED_PARPADEO Out
    
    Do Forever
      Sonda = ReadAD10(Sonda_In)
      Mando = ReadAD10(Mando_In)
    
      Estado = ZONA_MUERTA
      If ((Sonda+Histeresis)<Mando) Then Estado = CALOR
      If (Sonda>(Mando+Histeresis)) Then Estado = FRIO
      Select Case Estado
        Case CALOR
          Set LED_CALOR On
          Set LED_FRIO  Off
        Case FRIO
          Set LED_CALOR Off
          Set LED_FRIO  On
        Case ZONA_MUERTA
          Set LED_CALOR Off
          Set LED_FRIO  Off
      End Select
    
      Wait 500 ms
      LED_PARPADEO = Not LED_PARPADEO
    
    Loop
    

    6.5 Sec. Compiler Version: 0.98.01 2017-10-27
    Program Memory: 344 bytes RAM: 8/64 bytes (12,5%) Chip: TINY13

    And working in ISIS Proteus AD REF = VCC (not 1.1 v. Internal Ref.)

     

    Last edit: Agustin Hernandez 2018-03-19
  • kent_twt4

    kent_twt4 - 2018-03-19

    Just as a side note the tiny13A is a very popular AVR, and is the basis for a large proportion of inexpensive Chinese made flashlights/drivers. Large support base (tiny13A, tiny85) and huge C software depository for reflashing these chips insitu (Pomona SOIC8 chip clip) over at BLF (BugetLightForum).

     
    • Agustin Hernandez

      Yes, that's right, it's very popular. Code I'm trying is to revive some electronic thermostats for air conditioning. The thermostat is priced at around 50 € and they are basically the circuit I have shown above. They can be completely repaired by replacing or reprogramming the ATTiny13A, which only costs about 0.3 €. :-)

       
      • Agustin Hernandez

        With a bit of electronic reverse engineering and being able to reprogram your ATTiny13A, I can fully customize the operation of these thermostats ... Pure Hacking!

         

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.