Project

General

Profile

Revision 117

Added by Kevin Woo about 15 years ago

Removed debug leds in the uart module. Can turn relay on and off now.

View differences:

trunk/toolbox/main.c
11 11
#include <util/delay.h>
12 12
#include "uart.h"
13 13

  
14
#define BUT_RED     _BV(PORTB4)
15
#define BUT_BLACK   _BV(PORTB3)
14
#define RELAY       _BV(PORTB5)
15
#define BUT_RED     _BV(PINB4)
16
#define BUT_BLACK   _BV(PINB3)
16 17
#define LED_GREEN   _BV(PORTB2)
17 18
#define LED_YELLOW  _BV(PORTB1)
18 19
#define LED_RED     _BV(PORTB0)
......
22 23
#define DELIM '^'
23 24
#define ADDR '2'
24 25
#define SERVER '1'
26
#define TURNON 'O'
25 27

  
26 28
typedef enum {
27 29
    sd,     // start delimitor
......
36 38
} state_t;
37 39

  
38 40
void init_pins() {
39
    DDRB = _BV(DDB0) | _BV(DDB1) | _BV(DDB2);
41
    DDRB = _BV(DDB0) | _BV(DDB1) | _BV(DDB2) | _BV(DDB5);
40 42

  
41 43
    PORTB = 0x00;
42 44
}
......
49 51
    }
50 52
}
51 53

  
54
void toggle_relay(uint8_t state) {
55
    if (state == ON) {
56
        PORTB |= RELAY;
57
    } else {
58
        PORTB &= ~RELAY;
59
    }
60
}
61

  
52 62
inline uint8_t read_button(uint8_t which) {
53 63
    return (!(PINB & which));
54 64
}
......
82 92
                toggle_led(LED_RED, ON);
83 93
                toggle_led(LED_YELLOW, OFF);
84 94
                toggle_led(LED_GREEN, OFF);
95
                toggle_relay(OFF);
85 96
                while ((uart_get_byte(&r)) < 0);
86 97
                if (r == DELIM) {
87 98
                    state = src;
......
127 138
                while ((uart_get_byte(&r)) < 0);
128 139

  
129 140
                if (r == (packet[0] ^ packet[1] ^ packet[2])) {
130
                    state = ack;
131
                } else {
132
                    packet[0] = 0x00;
133
                    packet[1] = 0x00;
134
                    packet[2] = 0x00;
135
                    state = sd;
141

  
142
                    if (packet[2] = TURNON) {
143
                        state = ack;
144
                        break;
145
                    }
136 146
                }
147

  
148
                packet[0] = 0x00;
149
                packet[1] = 0x00;
150
                packet[2] = 0x00;
151
                state = sd;
137 152
                break;
138 153
            case ack:
139 154
                uart_send_byte(DELIM);
......
151 166
                if (read_button(BUT_BLACK)) {
152 167
                    toggle_led(LED_YELLOW, OFF);
153 168
                    toggle_led(LED_GREEN, ON);
169
                    toggle_relay(ON);
154 170
                    state = toolon;
155 171
                } else if (read_button(BUT_RED)) {
156 172
                    state = sd;
trunk/toolbox/uart.c
58 58

  
59 59
ISR(USART_RX_vect) {
60 60
    received_byte = UDR;
61
    byte_ready = 1;
62
    
63
    if (PORTD & _BV(PORTD3))
64
        PORTD &= ~_BV(PORTD3);
65
    else
66
        PORTD |= _BV(PORTD3);
67
        
61
    byte_ready = 1;    
68 62
}
69 63

  
70 64
ISR(USART_TX_vect) {
71 65
    // Re-enable reads
72 66
    uart_toggle_transmit(UART_TX_OFF);
73
    
74
    if (PORTD & _BV(PORTD4))
75
        PORTD &= ~_BV(PORTD4);
76
    else
77
        PORTD |= _BV(PORTD4);
78

  
79 67
}

Also available in: Unified diff