aboutsummaryrefslogtreecommitdiffstats
path: root/src/testapp1/main.c
blob: b305372eaf090cc30f82589fae5a33a2e2a48f69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <avr/pgmspace.h>
#include <avr/io.h>
#include <avr/interrupt.h>

#include "delay.h"
#include "morse.h"


#define PORTB_PTT (1 << 0)
#define PORTB_LED (1 << 4)

volatile uint8_t Timer;        /* Timer (100Hz increment) */

/*---------------------------------------------------------*/
/* 100Hz timer interrupt generated by OC2                  */
/*---------------------------------------------------------*/
ISR(TIMER1_COMPA_vect)
{
    Timer++;
}

static int inittimer(void)
{
    /* Start 100Hz system timer (TC2.OC) */
    TCCR0B |= (1 << WGM02); // Configure timer for CTC mode
    TIMSK  |= (1 << OCIE1A); // enable overflow interrupt
    OCR1A = (uint8_t)(F_CPU / 64 / 1000 / 100 - 1); // Set CTC compare value to 10ms
    TCCR0B |= ((1 << CS10) | (1 << CS11)); // Start timer at Fcpu/64

    // Enable interrupts
    sei();

    return 0;
}

#define PTT_LOGIC_LOW 0

#if PTT_LOGIC_LOW
void dah(void)
{
    PORTB = PORTB_LED; // LED is non-inverted logic
    delay_ms(3*DIT_DURATION);

    PORTB = PORTB_PTT; // PTT is inverted logic
    delay_ms(DIT_DURATION);
}

void dit(void)
{
    PORTB = PORTB_LED; // LED is non-inverted logic
    delay_ms(DIT_DURATION);

    PORTB = PORTB_PTT; // PTT is inverted logic
    delay_ms(DIT_DURATION);
}
#else
void dah(void)
{
    PORTB = PORTB_PTT | PORTB_LED;
    delay_ms(3*DIT_DURATION);

    PORTB = 0;
    delay_ms(DIT_DURATION);
}

void dit(void)
{
    PORTB = PORTB_PTT | PORTB_LED;
    delay_ms(DIT_DURATION);

    PORTB = 0;
    delay_ms(DIT_DURATION);
}
#endif

int main(void)
{
    /* Enable PTT output on PB0 */
#if PTT_LOGIC_LOW
    PORTB = PORTB_PTT;
#endif
    /* LED is never inverted */
    DDRB  = PORTB_PTT | PORTB_LED;

    /* initialise timer interrupt */
    inittimer();

    delay_ms(12*DIT_DURATION);
    morse("HB9EGM");
    delay_ms(12*DIT_DURATION);

    while (1) {
        // morse code !
        morse("MOH");
        delay_ms(6*DIT_DURATION);
    }

    return 0;
}