#include <iotiny13.h>
#include <intrinsics.h>
#include <avr_macros.h>
#include <stdio.h>
// ATtiny13 pins:
// Port B bit0 (pin 5) - LED as a test
// Port B bit5 (pin 1) - ADC0 analog input
// Port B
#define LED 0x01
// outputs
#define LED_ON PORTB |= LED
#define LED_OFF PORTB &= ~LED
void
delay_10ms(unsigned int val)
{
unsigned int i;
for (i=0; i<val; i++)
{
__delay_cycles(20000);
}
}
int main( void )
{
unsigned char lsbyte, msbyte;
unsigned int anaval;
DDRB=0x01; // just let LED pin be an output
ADMUX=0x00; // select channel 0
DIDR0=0x20; // set channel 0 to be analog, not digital pin
ADCSRB=0x00; // free-run mode for ADC
ADCSRA=0xE7; // set prescaler to allow a slow clock for the ADC and auto-triggering
LED_ON; // turn on the LED just to test the code is running
while(1)
{
delay_10ms(100); // wait a second
lsbyte=ADCL; // read the low byte first
msbyte=ADCH;
anaval=((unsigned int)msbyte)<<8;
anaval=anaval|((unsigned int)lsbyte);
// anaval should now contain a value between 0 and 1023
if (anaval>512)
{
LED_ON; // LED stays lit if the analog value is more than 2.5v
}
else
{
LED_OFF;
}
} // end of while(1) statement, i.e. loop forever
return 0;
}
void byte_write(unsigned char val)
{
unsigned char i;
for (i=0; i<8; i++)
{
if (val & 0x80)
{
PIN_SET_HIGH;
}
else
{
PIN_SET_LOW;
}
usec_delay();
val <<= 1;
}
}
#include <avr/io.h>
#include <stdio.h>
#define F_CPU 8000000UL //frequency = 8MHz
#include <util/delay.h>
#include <avr/interrupt.h>
void ReadADC();
unsigned char lsbyte, msbyte;
unsigned int anaval;
volatile int Scounter = 0;//Sampling Counter
volatile int Dcounter = 0;//Delay Counter
int main (void)
{
DDRB |= (1 << PB0); //LED at PB0 (Pin5) as OUTPUT
DDRB &= (0 << PB5); //ADC0 as analog input
// Set up timer and interrupt,
TCCR0B |= (1<<CS02) | (1<<CS00);// prescaler timer to 1/1024 the clock rate
TIMSK0 |=1<<TOIE0; // enable timer overflow interrupt
sei();
// Timer get increment at 8MHz/1024 = 7812.5
// Clock cycle 256, 8bit timer will over flow 7812.5/256 = 30.5 times per second
// One over flow period takes 32.8ms
// Using counter, increment it x3, 32.8ms*3 = 98ms; x15, 32.8ms*15 = 0.49s
while (1)
{
if(Scounter==2)//Set sampling interval (98ms~100ms)
{
ReadADC();//Read ADC values
//LED test
if (anaval > 512)
{
PORTB |= (1 << PB0); //LED on
}
else
{
PORTB &= (0 << PB0);//LED off
}
//Code for transmission here
//Code for transmission here
Scounter = 0;
}
if(++Dcounter>31)// 1 minute interval, Dcounter = 30.5
{
//sleep function, 10minutes
Dcounter = 0;
}
}
return 0;
}
void ReadADC()
{
ADMUX=0x00; // select channel 0 ADC0 PB5
DIDR0=0x20; // set channel 0 to be analog, not digital pin
ADMUX |= (0 << REFS0); // VCC as Reference
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set prescaler 128; 8MHz/128 = 62.5kHz
ADCSRA |= (1 << ADEN); // Enable ADC
ADCSRA |= (1 << ADSC); // Start A2D Conversions
while((ADCSRA & 0x40) !=0){}; // wait for conversion to be completed, ADSC bit goes to 0
lsbyte=ADCL; // read the low byte first
msbyte=ADCH;
anaval=((unsigned int)msbyte)<<8;
anaval=anaval|((unsigned int)lsbyte);
// anaval now contains a value between 0 and 1023
}
ISR(TIM0_OVF_vect)
{
Scounter++;
Dcounter++;
}
#include <avr/io.h>
#include <stdio.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#define F_CPU 8000000UL //frequency = 8MHz
#include <util/delay.h>
#include <avr/interrupt.h>
void ReadADC();
void wdsetup();
unsigned char lsbyte, msbyte;
unsigned int anaval;
volatile int timer_overflow_count = 0;
volatile int Scounter = 1;//Sampling Counter
volatile int Dcounter = 1;//Delay Counter
volatile int Wcounter = 1;//Watchdog counter
int main (void)
{
DDRB |= (1 << PB0); //LED at PB0 (Pin5) as OUTPUT
DDRB &= (0 << PB5); //ADC0 as analog input
// Set up timer and interrupt,
TCCR0B |= (1<<CS02) | (1<<CS00);// prescaler timer to 1/1024 the clock rate
TIMSK0 |=1<<TOIE0; // enable timer overflow interrupt
sei();
// Timer get increment at 8MHz/1024 = 7812.5
// Clock cycle 256, 8bit timer will over flow 7812.5/256 = 30.5 times per second
// One over flow period takes 32.8ms
// Using counter, increment it x3, 32.8ms*3 = 98ms; x15, 32.8ms*15 = 0.49s
while (1)
{
if(Scounter==4)//Set sampling interval (98ms~100ms)
{
ReadADC();//Read ADC values
//LED test
if (anaval > 512)
{
PORTB |= (1 << PB0); //LED on
}
else
{
PORTB &= (0 << PB0);//LED off
}
//Code for transmission here
//Code for transmission here
}
if(Dcounter>30)// 1.017 minute interval, Dcounter = 30.5times
{
//sleep function, 10minutes
wdsetup();
}
}
return 0;
}
void ReadADC()
{
ADMUX=0x00; // select channel 0 ADC0 PB5
DIDR0=0x20; // set channel 0 to be analog, not digital pin
ADMUX |= (0 << REFS0); // VCC as Reference
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set prescaler 128; 8MHz/128 = 62.5kHz
ADCSRA |= (1 << ADEN); // Enable ADC
ADCSRA |= (1 << ADSC); // Start A2D Conversions
while((ADCSRA & 0x40) !=0){}; // wait for conversion to be completed, ADSC bit goes to 0
lsbyte=ADCL; // read the low byte first
msbyte=ADCH;
anaval=((unsigned int)msbyte)<<8;
anaval=anaval|((unsigned int)lsbyte);
// anaval now contains a value between 0 and 1023
}
ISR(TIM0_OVF_vect)
{
if(Scounter>=4)
{Scounter=1;}
Scounter++;
if(Dcounter>=30)
{Dcounter=1;}
Dcounter++;
}
void wdsetup()
{
cli(); // Disable global interrupts
// Enable Watchdog timer, Prescaler 512, timeout = 4s
WDTCR |= (1<<WDP3); // (1<<WDP2) | (1<<WDP0);
// Enable watchdog timer interrupts
WDTCR |= (1<<WDTIE);
sei(); // Enable global interrupts
// Use the Power Down sleep mode
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
for (;;) {
sleep_mode(); // go to sleep and wait for interrupt...
}
}
ISR(WDT_vect) {
// Toggle Port B pin 4 output state
sleep_disable();
WDTCR &= 0;// Disable Watchdog timer,
Wcounter++;
if(Wcounter>150)
{
MCUSR = 0; //Power on Reset
}
else
{wdsetup();}
}
for (i=0; i<10; i++)
{
if (val & 0x200)
{
PIN_SET_HIGH;
}
else
{
PIN_SET_LOW;
}
usec_delay();
val <<= 1;
//For msbyte
for (i=0; i<2; i++)
{
if (msbyte & 0x02)
{
PIN_SET_HIGH;
}
else
{
PIN_SET_LOW;
}
usec_delay();
msbyte <<= 1;
}
For lsbyte
for (i=0; i<8; i++)
{
if (lsbyte & 0x80)
{
PIN_SET_HIGH;
}
else
{
PIN_SET_LOW;
}
usec_delay();
lsbyte <<= 1;
}
/ ATtiny13
// ADC testing
#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
void ReadADC();
int val=0;
int ADC_val=0;
int main (void)
{
DDRB |= (1 << PB0);// LED1
DDRB |= (1 << PB1);// LED2
DDRB &= ~(1 << PB4);// ADC0 pin
ADMUX |= (0 << REFS0); // VCC as Reference
ADMUX = 0; // ADC0 PB5
ADCSRA |= (1 << ADEN); // Enable ADC
while (1)
{
ReadADC();
if (ADC_val > 1022)
{
PORTB |= (1 << PB0);
PORTB |= (1 << PB1);
}
else if ((ADC_val > 800)&&(ADC_val<=1022))
{
PORTB &= (0 << PB0);
PORTB &= (0 << PB1);
}
else if((ADC_val >= 200)&&(ADC_val<=800))
{
PORTB &= (0 << PB0);
PORTB |= (1 << PB1);
}
else
{
PORTB |= (1 << PB0);
PORTB &= (0 << PB1);
}
}
return 1;
}
void ReadADC()
{
ADCSRA |= (1 << ADSC); // Start ADC Conversion
while((ADCSRA & 0x40) !=0){}; // wait for conversion to be completed, ADSC bit goes to 0
ADC_val = ADCL; // read out ADCL register
ADC_val += (ADCH << 8 );// read out ADCH register
}
ADC_val = ADCL; // read out ADCL register
ADC_val += (ADCH << 8 );// read out ADCH register
unsigned char lsbyte, msbyte;
unsigned int anaval;
lsbyte=ADCL; // read the low byte first
msbyte=ADCH;
anaval=((unsigned int)msbyte)<<8;
anaval=anaval|((unsigned int)lsbyte);
// anaval should now contain a value between 0 and 1023
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?