#include class Voltimetro{ public: Voltimetro(){ timer =0;}; ~Voltimetro(){}; inline void init(){ mega = (struct LAYOUT * ) IO_MEMORY_OFFSET; mega->ddrb = SET; // prepare leds mega->portb = 0xFF; // mega->sreg = 0x80 ; //global interruption enable bit mega->sreg |= 1<<7 ; //global interruption enable bit mega->sfior = 0; // Set ADTS in SFIOR to 000 - Free running mode mega->adcsra = 0xC8; // Set ADEN in ADCSRA - ADC enable // mega->adcsra |= (1<<3); // Set ADIE in ADCSRA - convertion interrupt enable // mega->adcsra &= ~(0x7); // Set ADSP in ADCSRA to 0 - division by 2 mega->admux = 0x60; //led_test(); }; inline void sleep() { for (unsigned int j=0;j<0xFFF;j++); }; inline void led_test() { mega->portb = 0x00; //debug for (int i=0;i<7;i++) { mega->portb ^= 1=0;i--) { mega->portb ^= 1portb = 0xFF; //debug } inline void enable_int(){ /* mega->admux |= (1<<6); // Set REFS0 to 1 in ADCSRA AVC target mega->admux &= ~(1<<7); // Set REFS1 to 0 in ADCSRA AVC target mega->admux |= (1<<5); // Set ADLAR to 1 in ADCSRA Left adjust mega->admux &= ~(15); // Set MUX4:0 to 0 in ADCSRA ADC0*/ // mega->adcsra |= (1<<5); // Set ADATE in ADCSRA - autotrigger // mega->sfior |= (1); // Set ADTS in SFIOR to 001 - Analog Comparator }; //page 34 stop the timer /* inline void clock_stop(){ mega->tccr0 &= ( 0<<2 | 0<<1 | 0<<0 ); timer=0; }; //page 34 enable the timer clock as CK/1024 inline void clock_start(){ //we neet to set the overflow time mega->tccr0 |= (1<<2) | (1<<0) ; mega->tccr0 &= ~(1<<1); }; inline void key_event() { //BUTTON3 Pressed //mcu bit 3 and 2 //1 0 falling //1 1 rising mega->portb ^= 1;//debug if (mega->mcucr & (1<<2)) { mega->mcucr &= ~(1<<2); clock_stop(); mega->portb ^= 2;//debug mega->portb |= 8; } else { mega->mcucr |= (1<<2); clock_start(); mega->portb ^= 4; //debug } } */ void adc_event() { // led_test(); display(mega->adch); mega->adcsra |= 0x40; } inline void display(unsigned char valor) { mega->portb = ~valor; } // private: LAYOUT *mega; unsigned int timer; }; /******************************************/ Voltimetro k; //wrapper /*void key_int(){ k.key_event(); }; void timer_int(){ k.timer++; if(k.timer == 15){ k.timer = 0; k.clock_stop(); k.mega->portb ^=8; } k.mega->portb ^= 16; };*/ // interruption handlers void adc_int(){ k.adc_event(); } extern "C"{ /* void __vector_2 (void) __attribute__((signal)); void __vector_2 (void) { key_int(); }; */ void __vector_14 (void) __attribute__((signal)); void __vector_14 (void) { adc_int(); }; }; /******************************************/ /* MAIN */ int main(){ k.init(); k.enable_int(); k.led_test(); while(true); /* { k.mega->adcsra |= 1<<6; k.mega->portb=~k.mega->adch; k.mega->adcsra &= ~1<<6; }*/ };