Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/sleep.h>
- #include <avr/interrupt.h>
- #include <util/atomic.h>
- void flash(void);
- inline void LED(uint8_t on);
- void Error(uint8_t);
- uint8_t getTemp(void);
- void sendRS232Byte(uint8_t);
- //RS232 defines
- #define BAUD 9600
- #define RS232_PAUSE 1000000/BAUD
- volatile uint16_t timeroverflow;
- int main(void){
- //ADC init
- ADMUX=(1<<REFS1)|(1<<MUX3)|(1<<MUX2)|(1<<MUX1)|(1<<MUX0);
- ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //ADC Freq: ~63khz
- ADCSRB=0;
- getTemp(); //init
- DDRB=(1<<PB4)|(1<<PB1); //Enable LED output, SDA, SCL
- PORTB|=(1<<PB1); //PB1 high
- //Timer init
- TCCR1=0xF; //Timer/16384 -> overflow ~0,5s
- GTCCR=0;
- TIMSK=(1<<TOIE1);
- LED(1); //Start
- _delay_ms(1000);
- LED(0);
- timeroverflow=0;
- sei();
- uint16_t temp;
- while(1){
- ATOMIC_BLOCK(ATOMIC_RESTORESTATE){
- temp=timeroverflow;
- if(temp>12){ //~10min
- timeroverflow=0;
- sendRS232Byte(getTemp());
- flash();
- }
- }
- }
- return 0;
- }
- uint8_t getTemp(void){
- ADCSRA|=(1<<ADSC)|(1<<ADIF);
- while(ADCSRA&(1<<ADSC));
- return (ADC>>2);
- }
- inline void LED(uint8_t on){
- switch (on){
- case 0:
- PORTB&=~(1<<PB4);
- break;
- default:
- PORTB|=(1<<PB4);
- }
- }
- void flash(){
- LED(1);
- _delay_us(50);
- LED(0);
- }
- void sendRS232Byte(uint8_t data){
- DDRB|=(1<<PB1);
- PORTB|=(1<<PB1); //Idle
- _delay_us(10*RS232_PAUSE);
- PORTB&=~(1<<PB1); //Start-Bit
- for(uint8_t i=0;i<8;i++){ //8 Bits
- _delay_us(RS232_PAUSE);
- switch((data>>i)&1){
- case 1:
- PORTB|=(1<<PB1);
- break;
- case 0:
- PORTB&=~(1<<PB1);
- break;
- }
- }
- _delay_us(RS232_PAUSE);
- PORTB|=(1<<PB1); //Stop-Bit
- _delay_us(10*RS232_PAUSE);
- }
- ISR(TIMER1_OVF_vect){
- timeroverflow++;
- }
Advertisement
Add Comment
Please, Sign In to add comment