Subversion Repositories group.electronics

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
122 pfowler 1
#include <avr/io.h>
2
#include <avr/wdt.h>
3
#include <avr/interrupt.h>
4
#include <avr/pgmspace.h>
5
#include <util/delay.h>
6
#include <stdlib.h>
7
 
123 pfowler 8
#include "avrutil.h"
9
#include "e2p.h"
10
#include "wire.h"
11
#include "mio.h"
12
#include "lcd.h"
122 pfowler 13
#include "config.h"
14
#include "uart.h"
15
 
16
#ifndef NULL
17
#define NULL    ((void *)0)
18
#endif
19
 
20
 
21
/* ------------------------------------------------------------------------- */
22
 
23
#define UART_BAUD_RATE	9600
24
 
25
volatile uint8_t tmr0_ovf = 0;
26
volatile uint32_t systime = 0;
27
 
123 pfowler 28
#define UART_SEND_DELAY 5000
29
#define BUTTON_DELAY 500
30
#define LCD_SEND_DELAY 80
122 pfowler 31
 
123 pfowler 32
volatile uint16_t buttontime = BUTTON_DELAY;
33
volatile uint16_t sendtime = UART_SEND_DELAY;
34
volatile uint8_t lcdTimer = LCD_SEND_DELAY;
122 pfowler 35
 
123 pfowler 36
 
122 pfowler 37
int main(void) {
38
 
39
 
40
  /*
41
  DDR : 1 = Output, 0 = Input
42
  PORT: 1 = Pullup for Input, otherwise set output
43
  PIN : Read input pin
44
  */
45
 
46
  /*
47
        PB0     - 
48
        PB1     - 
49
        PB2     - 
50
        PB3     - 
51
        PB4     - 
52
        PB5     - 
53
        PB6     - 
54
        PB7     - 
55
  */
123 pfowler 56
  DDRB          = 0B00001111;
122 pfowler 57
  PORTB         = 0B00000000;
58
 
59
  /*
60
        PC0     - 
61
        PC1     - 
62
        PC2     - 
63
        PC3     - 
64
        PC4     - 
65
        PC5     - 
66
  */
67
  DDRC          = 0B11111111;
68
  PORTC         = 0B00000000;
69
 
70
  /*
71
        PD0     - 
72
        PD1     - 
73
        PD2     - 
74
        PD3     - 
75
        PD4     - 
76
        PD5     - Input PcInt		- * I/O Interrupt PCINT21
77
        PD6     - Output		- * Status LED
78
        PD7     - Input, Pullup		- * Button
79
  */
80
  DDRD          = 0B01011111;
81
  PORTD         = 0B10000000;
82
 
123 pfowler 83
 
84
 
85
    sysclockInit();   
122 pfowler 86
 
123 pfowler 87
    uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
122 pfowler 88
 
89
    wdt_enable(WDTO_1S);		// Watchdog for 1 sec
90
    sei();				// Enable interrupts
123 pfowler 91
 
92
    i2c_master();
93
    e2p_init();
94
    mio_init();
95
    lcd_init();
96
 
97
    lcd_setCursor(0, 1);
98
    lcd_print("Time:\0");
99
 
100
    char display[10];
101
 
102
    uint16_t address = 0x0003;
122 pfowler 103
 
123 pfowler 104
    sbi(PORTD, PD6); 
105
    _delay_us(100);
106
    cbi(PORTD, PD6);
122 pfowler 107
 
123 pfowler 108
    mio_iodir(0x02);
109
    mio_pullups(0x02);
110
    //mio_writeReg(0x00, 0x02);
111
    //mio_writeReg(0x06, 0x02);
112
    //mio_writeReg(0x0a, 0x01);
113
    //mio_writeReg(0x0a, 0x00);
114
 
122 pfowler 115
 
116
    for(;;){
117
        wdt_reset();
123 pfowler 118
	cbi(PIND, PD6);
122 pfowler 119
 
123 pfowler 120
	if (!rbi(PIND, PD7) && !buttontime) {
121
 
122
	  sbi(PORTD, PD6);
123
	  delay_ms(1);
124
	  cbi(PORTD, PD6);
125
 
126
	  uint8_t lsb = systime & 0x000000ff;
127
 
128
	  e2p_writeByte(address, lsb);
129
	  e2p_flush();	  
130
	  uint8_t rdata = e2p_readByte(address);
131
 
132
	  uart_puts("Write: ");
133
	  utoa(lsb, display, 16);
134
	  uart_puts(display);
135
	  uart_puts("\r\n"); 
136
	  lcd_overprint_right(display, 2, 3, 0);
137
 
138
	  uart_puts("Read: ");
139
	  utoa(rdata, display, 16);
140
	  uart_puts(display);
141
	  uart_puts("\r\n");
142
	  lcd_overprint_right(display, 2, 6, 0);
143
	  buttontime = BUTTON_DELAY;
144
	}
145
 
122 pfowler 146
 
147
	if (!sendtime) {
123 pfowler 148
	  ltoa(systime, display, 10);
149
	  uart_puts(display);
122 pfowler 150
	  uart_puts("\r\n");
123 pfowler 151
 
152
	  sendtime=UART_SEND_DELAY;
153
 
154
 
122 pfowler 155
	}
123 pfowler 156
 
157
	if (!lcdTimer) {
158
	  ultoa(systime, display, 10);
159
	  lcd_overprint_right(display, 10, 6, 1);
160
 
161
	  uint8_t but1 = mio_readPin(1);
162
	  if (but1) {
163
	    cbi(PORTD, PD6);
164
	    mio_latchPin(0, 0x00);
165
	  } else {
166
	    sbi(PORTD, PD6);
167
	    mio_latchPin(0, 0x01);
168
	  }
169
 
170
	  utoa(but1, display, 16);
171
	  lcd_overprint_right(display, 2, 0, 0);
172
 
173
	  lcdTimer = LCD_SEND_DELAY;
174
	}
122 pfowler 175
	_delay_ms(1);
176
 
177
    }
123 pfowler 178
 
122 pfowler 179
    return 0;
180
}
181
 
182
 
183
ISR(TIMER0_OVF_vect) {
184
        tmr0_ovf++;
185
 
186
	// Clk/1 TCCR0B = (1<< CS00);
187
	//20.0Mhz, 1ms = 78ovf
188
	//16.5Mhz, 1ms = 64ovf
189
	//16.0Mhz, 1ms = 62ovf
190
	//12.0Mhz, 1ms = 46ovf
191
 
192
        if (tmr0_ovf>=64) {
193
                systime++;
194
                tmr0_ovf = 0;
195
		if (sendtime)
196
		  sendtime--;
123 pfowler 197
 
198
		if (lcdTimer)
199
		    lcdTimer--;
200
 
201
		if (buttontime)
202
		    buttontime--;
122 pfowler 203
        }
204
}