Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
336 andfri 1
#include <inttypes.h>
2
#include <avr/interrupt.h>
3
#include <bios.h>
4
 
5
#define APP_TYPE    0xf001
6
#define APP_VERSION 0x0001
7
 
8
#define F_SAMP 1000
9
#define PRESCALER 1
10
 
11
/*void can_receive(Can_Message_t *msg) {
12
    printf("candata!\n");
13
}*/
14
 
15
volatile uint8_t overflow_adc;
16
volatile uint8_t overflow_uart;
17
volatile uint8_t adc;
18
 
19
ISR(TIMER1_COMPA_vect) {
20
    if (ADCSRA & _BV(ADSC)) // Previous conversion is not yet ready!
21
        overflow_adc++;
22
    else if (!(UCSRA & _BV(UDRE)))  // Previous data has not been sent!
23
        overflow_uart++;
24
    else ADCSRA |= _BV(ADSC);   // Start a new conversion
25
}      
26
 
27
ISR(ADC_vect) {             // A conversion is done
28
    adc = ADCH;             // Store result
29
    UCSRB |= _BV(UDRIE);    // Enable UART interrupt to send result
30
}
31
 
32
ISR(USART_UDRE_vect) {      // UART is ready for a new byte
33
    UCSRB &= ~_BV(UDRIE);   // Disable this interrupt until a new conversion is done
34
    UDR = adc;              // Send previous result
35
}
36
 
37
int main(void)
38
{
39
    sei();
40
 
41
    UBRRL = (F_CPU / 16 / F_BAUD) - 1;
42
    UCSRB = _BV(TXEN)|_BV(UDRIE);
43
 
44
    ADMUX = _BV(REFS0)|_BV(ADLAR)|_BV(MUX2);
45
    ADCSRA = _BV(ADEN)|_BV(ADSC)|_BV(ADIE)|_BV(ADPS2)|_BV(ADPS1);
46
 
47
    OCR1A = F_CPU / F_SAMP / PRESCALER - 1;
48
    TIMSK |= _BV(OCIE1A);
49
    TCCR1B = _BV(WGM12)|_BV(CS10);
50
 
51
    Can_Message_t txMsg;
52
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
53
    txMsg.DataLength = 4;
54
    txMsg.RemoteFlag = 0;
55
    txMsg.ExtendedFlag = 1;
56
    txMsg.Data.words[0] = APP_TYPE;
57
    txMsg.Data.words[1] = APP_VERSION;
58
 
59
    //set up callback for candata
60
    //bios->can_callback = &can_receive;
61
    // Send CAN_NMT_APP_START
62
    bios->can_send(&txMsg);
63
 
64
    while (1);
65
 
66
    return 0;
67
}