Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ATtiny13
- // Battery Protection from discharge
- // © Grigoriev Alexander , 2016
- // ADC Module
- #include <avr/io.h>
- #define F_CPU 8000000UL
- #include <compat/deprecated.h>
- #define GATE PORTB0
- #define VOLT PORTB4
- #define WARN PORTB3
- int ReadADC();
- /*
- R1=100K;
- R2=20K;
- Vout=(R2/(R1+R2)); ~0.166667
- 11.1*0.16667 =
- 1024*1.85/5=378.88;
- 1024*2.03/5=415.7;
- c=34.13
- 11.1*34.13=378.8
- */
- uint16_t LTHR = 379; //11.1
- uint16_t HTHR = 415; //12.2;
- #define SRAM __attribute__((section(".noinit")))
- SRAM int temp ;
- void initadc()
- {
- ADMUX |= (0 << REFS0); // VCC as Reference
- //
- // MUX[1:0] Single Ended Input
- // 00 ADC0 (PB5)
- // 01 ADC1 (PB2)
- // 10 ADC2 (PB4)
- // 11 ADC3 (PB3)
- ADMUX |= (1 << MUX1) | (0 << MUX0); // ADC2 PB.4
- }
- int __attribute__((naked))
- main (void)
- {
- sbi(DDRB,GATE); // DDRB |= (1 << GATE); // output
- sbi(PORTB,GATE); //null
- cbi(DDRB,VOLT); // DDRB &= ~(1 << VOLT);
- cbi(PORTB,VOLT); //PORTB &= ~(1 << VOLT);
- // Bit 7 – ADEN: ADC Enable
- // Writing this bit to one enables the ADC. By writing it to zero, the ADC is turned off. Turning the
- // ADC off while a conversion is in progress, will terminate this conversion.
- initadc();
- ADCSRA |= (1 << ADEN); // Enable ADC
- while (1)
- {
- temp=ReadADC();
- if(temp<=LTHR)
- {cbi(PORTB,GATE);
- cbi(PORTB,WARN); }
- else
- if (temp>=HTHR){
- sbi(PORTB,GATE);
- cbi(PORTB,WARN);
- }
- else
- sbi(PORTB,WARN);
- }
- }
- int ReadADC()
- {
- initadc();
- ADCSRA |= (1 << ADSC); // Start Converstion
- while((ADCSRA & 0x40) !=0){}; //wait for conv complete
- return ADC;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement