• .
  • Willkommen im Forum!
  • Alles beim Alten...
  • Du hast kaum etwas verpasst ;-)
  • Jetzt noch sicherer mit HTTPS
Hallo, Gast! Anmelden Registrieren


Tiny Tone
#13
(16.01.2019, 02:12 AM)alfsch schrieb: >Damit wäre Stromsparen im sleep modus also nicht möglich.... 

hä?  sleep hat < 1uA....was willst da genau noch "sparen" ??

Nö, es ging ja darum die PLL zu nutzen - und die wird aber schon im standby Modus deaktiviert.
Den progagierten Stromverbrauch erreiche ich ja darüber dass der uP die meiste Zeit im standby-Mode verweilt.
Echter power-down nach WDT-TimeOut ist eine andere Baustelle.

Code:
/* A Very Basic USB/LiIon-Battery-Powered Audio Sine-Wave Test Generator
created: 2014-01-21
update: 2019-01-07a
author: VoltWide
target                                                       ATTINY25-20
clock frequency           ...............                    8Mhz/1 internal
fuses                                                        uncheck CLKDIV8
                                                             check untouch EEPROM
hfuse readout, bitEna=0                                       11010111b
WDTON = bit4                                                  disabled=safety level1

prototype data
output function                                              pwm sine
max output level, peak-peak                                  >90% of Vsupply
pwm sample rate                                              8MHz/512 = 16khz
output frequencies/THD                                       15Hz/0.48%
                                                             30Hz/0.33%
                                                             60Hz/0.31%
                                                             125Hz/0.52%
                                                             250Hz/0.6%
                                                             500Hz/0.92%
                                                             1000Hz/0.63%
power supply                                                 2.7~5.5V: USB, 3V-LiBatt, 2~3xAA..
power consumption active, no load, 2*ADC/17+17ms, LED        5V*2.68mA, 4V*2.05mA, 3V*1.41mA
power consumption sleep-idle, cli, max prr, PORTB=0          3V*0.7mA
Atmel specs page 166
power consumption idle @8MHz                                 5V*1.2mA
power consumption ref1.1.....................................2.7V*15uA
cpu load irq  on/off                                         7.4us/56.4us
eof battery charge threshold = watchdog disable              4V
UVLO shut down threshold                                     disabled
shut down current consumption                                <1uA

DIP8/SOIC8 multilevel-pinning:
pin1 PB5 PCINT5 /RESET ADC0 dW
pin2 PB3 PCINT3 XTAL1 CLKI /OC1B ADC3
pin3 PB4 PCINT4 XTAL2 CLKO OC1B ADC2
pin4 GND
pin5 PB0 MOSI DI SDA AIN0 OC0A /OC1A AREF PCINT0
pin6 PB1 MISO DO AIN1 OC0B OC1A PCINT1
pin7 PB2 SCK USCK SCL ADC1 T0 INT0 PCINT2
pin8 VCC
*/

#include <stdlib.h>
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/eeprom.h>
#include <avr/wdt.h>


const __flash char SineTab[] = {                        // 8bit sinewave lookup table
0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,0x98,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,
0xb0,0xb3,0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,
0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xed,0xef,0xf0,0xf2,0xf3,0xf5,
0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfc,0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfd,0xfc,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,
0xf6,0xf5,0xf3,0xf2,0xf0,0xef,0xed,0xec,0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc,
0xda,0xd8,0xd5,0xd3,0xd1,0xce,0xcc,0xc9,0xc7,0xc4,0xc1,0xbf,0xbc,0xb9,0xb6,0xb3,
0xb0,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x98,0x95,0x92,0x8f,0x8c,0x89,0x86,0x83,
0x80,0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a,0x67,0x63,0x60,0x5d,0x5a,0x57,0x54,0x51,
0x4f,0x4c,0x49,0x46,0x43,0x40,0x3e,0x3b,0x38,0x36,0x33,0x31,0x2e,0x2c,0x2a,0x27,
0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,0x15,0x13,0x12,0x10,0x0f,0x0d,0x0c,0x0a,
0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x0a,0x0c,0x0d,0x0f,0x10,0x12,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,
0x25,0x27,0x2a,0x2c,0x2e,0x31,0x33,0x36,0x38,0x3b,0x3e,0x40,0x43,0x46,0x49,0x4c,
0x4f,0x51,0x54,0x57,0x5a,0x5d,0x60,0x63,0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c
};
const __flash uint16_t FqTab[] = {
//64,128,256,512,1024,2048,4096,8192,16384,32768
63,126,250,526,1047,2089,4188                                   //2018-12-27 customized!!
};
const int8_t FrequIdxMax = 6;                                   //2018-12-27

// i/o mapping
#define pin1 (1<<PB5)
#define pin2 (1<<PB3)
#define pin3 (1<<PB4)
#define pin5 (1<<PB0)
#define pin6 (1<<PB1)
#define pin7 (1<<PB2)

#define OutSine pin6
#define OutChargeReq pin5
#define OutPoti pin7
#define InVcc pin2
#define InPoti pin3
//input divider Rhi/Rlo=10k/2k2 Cin=0 Vref=1V1 VBattMin=3V VBattMax=4V:
//Vin(max,min) = 0.72/0.54V
//ADCin(maxs,min) =167/125
#define VBattMax 167
#define VBattHyst 5
#define EmptyBattCountDown 200
//!!! 2count/sec:
#define WDTirqCountDown 3600

//++++++++ global vars

volatile uint16_t Phase16,PhaseOfs16,WDTirqCount;               // 2019-01-06
uint8_t PotiResult,VccResult,EmptyBattcount,FrequIdx,EnaGetVcc,LoopCount;    //2018-12-27

//++++++++ Initialisation

void Boot(void){
   //Timer
   GTCCR = (1<<TSM) | (1<<PSR0);                               // timer0: reset pre-scaler
   TCCR0A = (1<<COM0B1) | (1<<COM0B0) | (1<<WGM00);            // 2018-12-27 output pin6 only
   TCCR0B = ( 1<<CS00 );                                       // clk=cpuclk/1
   TCNT0 = 0;                                                  // clear counter
   OCR0A = 0;                                                  // clear pwm output compare register A
   OCR0B = 0;                                                  // clear pwm output compare register B
   TIMSK = (1<<TOIE0);                                         // timer0-irq on counter overflow
   GTCCR = 0;                                                  // start counter(s)
   //Ports
   MCUCR |= (1<<PUD);                                          // 2018-12-26 pullup,sleep=off, sleepmode=idle
   DDRB = OutSine | OutChargeReq | OutPoti;                    // 2018-12-26 output ports
   PINB = -1;                                                  // 2018-12-26 power reduction of dig input buffers
   DIDR0 = (1<<ADC0D) | (1<<ADC1D) | (1<<ADC2D) | (1<<ADC3D);  // 2018-12-26 dig inputs=off
   //ADC
   ADMUX = (1<<ADLAR) | (1<<REFS1) | (1<<MUX0) | (1<<MUX1);    // 2018-12-27 adcref=1V1, result left adj, in=ADC3/PB3/pin2
   ACSR = (1<<ACD);                                            // 2018-12-26 comp=off
   ADCSRA = 0;                                                 // 2018-12-26 ADCen,ADCstart,ADCtrig,ADCirq=off, adcclk=cpuclk/2
   ADCSRB = 0;                                                 // 2018-12-26 unipolar, polarity=tru,free running
   //Misc
   PRR = (1<<PRTIM1) | (1<<PRUSI) | (1<<PRADC);                // power timer1,usi,adc=off
   PhaseOfs16 = FqTab[FrequIdx];
   Phase16 = 0;
   LoopCount=-1;                                               // 2018-12-27!!
   WDTirqCount = WDTirqCountDown;                              // 2019-01-06
   EnaGetVcc=1;                                                // 2019-01-06
   sei();
  }
void ShutDown(void) {
   cli();                                                      // 2018-12-26
   ADCSRA = 0;                                                 // 2018-12-26 dis ADC???
   TCCR0A = 0;                                                 // 2018-12-27 disconn sine port from timer, PRIOR! to powersave
   MCUSR = 0;                                                  // 2019-01-06 clear WDRF
   WDTCR |= (1<<WDCE) | (1<<WDE);                              // 2019-01-06
   WDTCR = 0;                                                  // 2019-01-06 clear WDE
   ADMUX = 0;                                                  // 2019-01-06 disable ref1V1
   PRR = (1<<PRTIM0) | (1<<PRTIM1) | (1<<PRUSI) | (1<<PRADC);  // 2018-12-26 power timer0+1,usi,adc=off
   PORTB = 0;                                                  // 2018-12-26 LED=off
   set_sleep_mode(SLEEP_MODE_PWR_DOWN);                        // 2018-12-26
   sleep_enable();                                             // 2018-12-26
   sleep_cpu();                                                // shut down
}
ISR(WDT_vect){                                                  // ca 2x/s: autoclears WDIE, must be set again to avoid wdt-reset
   sleep_disable();                                            // wakeup!
   WDTirqCount--;                                              // 2019-01-07 shutdown not here, but in main loop
   WDTCR |= (1<<WDIE);                                         // continue wdt irq, not wdt reset
}
void WDTon(void) {                                              // 2019-01-06 p.45, safety level 1
   WDTCR = (1<<WDIE) | (1<<WDE) | (1<<WDP2) | (1<<WDP0);       // Watchdog timer 0.5sec ticker
   MCUSR |= (1<<WDRF);                                          // 2019-01-07 overrides WDE
}
void WDToff(void) {                                             // 2019-01-06 p.45, safety level 1, timed sequence:
   MCUSR = 0;                                                  // c.code example 8.4.2: clear WDRF
   WDTCR |= (1<<WDCE) | (1<<WDE);
   WDTCR = 0;                                                  // clear WDE
}
ISR(TIM0_OVF_vect) {                                            // DDS-irq: output next sound sample
   uint8_t Voltage,Phase8;                                     //2019-01-06
   Phase16 += PhaseOfs16;                                      //16bit phase pointer
   Phase8 = Phase16 >> 8;                                      // 8bit table index
   Voltage = (SineTab[Phase8]);                                // drive h-bridge:
   OCR0B = Voltage;                                            // 2018-12-27 sinewave true output
}
void GetPoti (void) {
   ADCSRA |= (1<<ADEN) | (1<<ADSC);                            // 2018-12-26 start ad-conversion fsample=clk/2 adcirq=off
   while (0 == (ADCSRA & (1<<ADIF)));                          // A/D-conversion
   ADCSRA |= (1<<ADIF);                                         // 2019-01-07
   PotiResult = ADCH;                                          // bit9..2 , 8bit result
   ADMUX = (1<<ADLAR) | (1<<REFS1) | (1<<MUX0) | (1<<MUX1);    // 2018-12-27 ref-1V1 early turn-on, ADC3=PB3
}
void GetVcc (void) {
   ADCSRA |= (1<<ADEN) | (1<<ADSC);                            //2019-01-06 start adc fsample=clk/2 adcirq=off
   while (0 == (ADCSRA & (1<<ADIF)));                          //
   ADCSRA |= (1<<ADIF);                                        // 2019-01-07
   VccResult = ADCH;                                           // bit9..2 , 8bit result
   ADMUX = (1<<ADLAR) | (1<<MUX1);                             // 2019-01-06 ref-1V1 early turn-off!!!
   if (VccResult>=VBattMax){
       WDToff();                                               // 2019-01-06 WDT=off
       WDTirqCount = WDTirqCountDown;                          // 2019-01-06
   }
   if (VccResult<=(VBattMax-VBattHyst)) {
       WDTon();                                                // 2019-01-06 hysteresis!! WDT=on
   }
}
void GetFrequ (void) {                                          // 2018-12-26
   FrequIdx = PotiResult>>5;                                   // 2018-12-26
   if (FrequIdx>FrequIdxMax)  FrequIdx=FrequIdxMax;            // 2018-12-27
   PhaseOfs16 = FqTab[FrequIdx];                               // 2018-12-26
}
//++++++++ Main Program

int main(void) {
   Boot();
   for (;;) {
       while (LoopCount!=0) {                                  // 2019-01-06 skip main loops
           LoopCount--;
           sleep_cpu();                                        // 2019-01-06 power down
       };                                                      // skip main program
       PRR &= ~(1<<PRADC);                                     // 2018-12-27 ADC-power-reduction=off
       PORTB |= OutPoti;                                       // 2019-01-06 tpulse=0
       if ( EnaGetVcc) {                                       // 1.pass: battery monitor first for max ref1V1 setup time
           GetVcc();
           EnaGetVcc=0;
       }
       else {                                                  // 2.pass
           GetPoti();
           GetFrequ();
           LoopCount=-1;                                       // 2019-01-06 done, skip main loops
           EnaGetVcc=1;                                        // 2019-01-06
           PORTB &= ~OutPoti;                                  // 2019-01-06 disconnect Vcc from poti
       }                                                       // 2019-01-06 tpulse=21.6us/16.2ms
   if (WDTirqCount==0) ShutDown();                             // 2019-01-06
       PRR = (1<<PRTIM1) | (1<<PRUSI) | (1<<PRADC);            // 2019-01-06 power timer1,usi,adc=off
       set_sleep_mode(SLEEP_MODE_IDLE);                        // 2019-01-06 cpu & i/o clocks active
       sleep_enable();
       sleep_cpu();                                            // 2019-01-06 power down
   }
}
...mit der Lizenz zum Löten!
 
Reply
  


Nachrichten in diesem Thema
Tiny Tone - von voltwide - 14.01.2019, 09:02 PM
RE: Tiny Tone - von voltwide - 14.01.2019, 09:17 PM
RE: Tiny Tone - von voltwide - 14.01.2019, 10:23 PM
RE: Tiny Tone - von christianw. - 15.01.2019, 01:55 PM
RE: Tiny Tone - von voltwide - 15.01.2019, 06:41 PM
RE: Tiny Tone - von voltwide - 15.01.2019, 11:49 PM
RE: Tiny Tone - von voltwide - 16.01.2019, 01:25 AM
RE: Tiny Tone - von alfsch - 15.01.2019, 06:19 PM
RE: Tiny Tone - von christianw. - 15.01.2019, 07:01 PM
RE: Tiny Tone - von christianw. - 16.01.2019, 12:28 AM
RE: Tiny Tone - von voltwide - 16.01.2019, 12:53 AM
RE: Tiny Tone - von alfsch - 16.01.2019, 02:12 AM
RE: Tiny Tone - von voltwide - 16.01.2019, 02:21 AM
RE: Tiny Tone - von christianw. - 16.01.2019, 02:34 PM
RE: Tiny Tone - von voltwide - 16.01.2019, 06:48 PM
RE: Tiny Tone - von christianw. - 16.01.2019, 10:33 PM
RE: Tiny Tone - von christianw. - 16.01.2019, 10:43 PM
RE: Tiny Tone - von voltwide - 18.01.2019, 05:45 PM
RE: Tiny Tone - von christianw. - 18.01.2019, 08:51 PM
RE: Tiny Tone - von voltwide - 18.01.2019, 11:01 PM
RE: Tiny Tone - von voltwide - 19.01.2019, 12:03 AM
RE: Tiny Tone - von alfsch - 19.01.2019, 01:12 AM
RE: Tiny Tone - von voltwide - 19.01.2019, 09:34 AM
RE: Tiny Tone - von alfsch - 19.01.2019, 10:47 AM
RE: Tiny Tone - von christianw. - 19.01.2019, 09:29 AM
RE: Tiny Tone - von voltwide - 19.01.2019, 11:49 AM
RE: Tiny Tone - von alfsch - 19.01.2019, 12:24 PM
RE: Tiny Tone - von alfsch - 19.01.2019, 01:24 PM
RE: Tiny Tone - von voltwide - 19.01.2019, 03:23 PM
RE: Tiny Tone - von alfsch - 20.01.2019, 12:39 AM
RE: Tiny Tone - von voltwide - 23.01.2019, 11:01 PM
RE: Tiny Tone - von christianw. - 23.01.2019, 11:55 PM
RE: Tiny Tone - von voltwide - 24.01.2019, 01:56 AM
RE: Tiny Tone - von voltwide - 24.01.2019, 12:48 PM
RE: Tiny Tone - von kahlo - 24.01.2019, 04:17 PM
RE: Tiny Tone - von christianw. - 24.01.2019, 04:18 PM
RE: Tiny Tone - von voltwide - 24.01.2019, 04:36 PM
RE: Tiny Tone - von voltwide - 24.01.2019, 04:42 PM
RE: Tiny Tone - von christianw. - 24.01.2019, 04:40 PM
RE: Tiny Tone - von alfsch - 24.01.2019, 08:23 PM
RE: Tiny Tone - von voltwide - 24.01.2019, 09:06 PM
RE: Tiny Tone - von voltwide - 24.01.2019, 11:16 PM
RE: Tiny Tone - von voltwide - 24.01.2019, 11:51 PM
RE: Tiny Tone - von alfsch - 25.01.2019, 01:38 PM
RE: Tiny Tone - von voltwide - 25.01.2019, 07:39 PM
RE: Tiny Tone - von voltwide - 25.01.2019, 08:57 PM
RE: Tiny Tone - von voltwide - 26.01.2019, 01:23 AM
RE: Tiny Tone - von alfsch - 26.01.2019, 12:46 PM
RE: Tiny Tone - von voltwide - 26.01.2019, 02:20 PM
RE: Tiny Tone - von voltwide - 26.01.2019, 02:32 PM
RE: Tiny Tone - von voltwide - 26.01.2019, 11:35 PM
RE: Tiny Tone - von voltwide - 27.01.2019, 12:28 AM
RE: Tiny Tone - von alfsch - 27.01.2019, 02:38 PM
RE: Tiny Tone - von voltwide - 27.01.2019, 02:56 PM
RE: Tiny Tone - von voltwide - 27.01.2019, 09:06 PM
RE: Tiny Tone - von voltwide - 27.01.2019, 10:29 PM
RE: Tiny Tone - von voltwide - 28.01.2019, 04:27 PM
RE: Tiny Tone - von voltwide - 29.01.2019, 12:42 AM
RE: Tiny Tone - von Bandre - 29.01.2019, 08:43 AM
RE: Tiny Tone - von voltwide - 29.01.2019, 01:50 PM
RE: Tiny Tone - von alfsch - 30.01.2019, 09:59 AM
RE: Tiny Tone - von voltwide - 30.01.2019, 11:08 AM
RE: Tiny Tone - von alfsch - 30.01.2019, 02:38 PM
RE: Tiny Tone - von voltwide - 30.01.2019, 04:40 PM
RE: Tiny Tone - von Bandre - 29.01.2019, 01:58 PM
RE: Tiny Tone - von christianw. - 30.01.2019, 01:00 PM
RE: Tiny Tone - von alfsch - 16.02.2019, 11:04 PM
RE: Tiny Tone - von voltwide - 17.02.2019, 12:03 AM
RE: Tiny Tone - von E_Tobi - 17.02.2019, 10:09 AM
RE: Tiny Tone - von alfsch - 17.02.2019, 01:37 PM
RE: Tiny Tone - von alfsch - 17.02.2019, 12:47 AM
RE: Tiny Tone - von voltwide - 17.02.2019, 09:38 AM
RE: Tiny Tone - von kahlo - 17.02.2019, 03:17 PM
RE: Tiny Tone - von voltwide - 17.02.2019, 03:49 PM
RE: Tiny Tone - von alfsch - 17.02.2019, 04:05 PM
RE: Tiny Tone - von E_Tobi - 17.02.2019, 05:01 PM
RE: Tiny Tone - von alfsch - 17.02.2019, 05:52 PM
RE: Tiny Tone - von E_Tobi - 17.02.2019, 06:09 PM
RE: Tiny Tone - von alfsch - 17.02.2019, 06:45 PM
RE: Tiny Tone - von alfsch - 17.02.2019, 07:04 PM
RE: Tiny Tone - von voltwide - 18.02.2019, 03:55 PM
RE: Tiny Tone - von E_Tobi - 17.02.2019, 07:19 PM
RE: Tiny Tone - von alfsch - 17.02.2019, 07:30 PM
RE: Tiny Tone - von E_Tobi - 17.02.2019, 07:56 PM
RE: Tiny Tone - von alfsch - 18.02.2019, 05:10 PM
RE: Tiny Tone - von E_Tobi - 18.02.2019, 08:24 PM
RE: Tiny Tone - von alfsch - 18.02.2019, 09:36 PM
RE: Tiny Tone - von voltwide - 19.02.2019, 01:10 AM
RE: Tiny Tone - von alfsch - 19.02.2019, 11:39 AM
RE: Tiny Tone - von christianw. - 19.02.2019, 12:19 PM
RE: Tiny Tone - von alfsch - 19.02.2019, 12:42 PM
RE: Tiny Tone - von christianw. - 19.02.2019, 01:53 PM
RE: Tiny Tone - von voltwide - 19.02.2019, 02:37 PM
RE: Tiny Tone - von E_Tobi - 19.02.2019, 09:37 PM
RE: Tiny Tone - von alfsch - 19.02.2019, 10:49 PM
RE: Tiny Tone - von voltwide - 19.02.2019, 11:36 PM
RE: Tiny Tone - von alfsch - 20.02.2019, 12:19 AM
RE: Tiny Tone - von christianw. - 19.02.2019, 11:39 PM
RE: Tiny Tone - von voltwide - 20.02.2019, 12:02 AM
RE: Tiny Tone - von christianw. - 20.02.2019, 08:55 AM
RE: Tiny Tone - von voltwide - 21.02.2019, 01:29 AM