Subversion Repositories group.electronics

Rev

Rev 122 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 122 Rev 123
Line 3... Line 3...
3
#include <avr/interrupt.h>
3
#include <avr/interrupt.h>
4
#include <avr/pgmspace.h>
4
#include <avr/pgmspace.h>
5
#include <util/delay.h>
5
#include <util/delay.h>
6
#include <stdlib.h>
6
#include <stdlib.h>
7
 
7
 
-
 
8
#include "avrutil.h"
-
 
9
#include "e2p.h"
-
 
10
#include "wire.h"
-
 
11
#include "mio.h"
-
 
12
#include "lcd.h"
8
#include "config.h"
13
#include "config.h"
9
#include "uart.h"
14
#include "uart.h"
10
 
15
 
11
#ifndef NULL
16
#ifndef NULL
12
#define NULL    ((void *)0)
17
#define NULL    ((void *)0)
Line 18... Line 23...
18
#define UART_BAUD_RATE	9600
23
#define UART_BAUD_RATE	9600
19
 
24
 
20
volatile uint8_t tmr0_ovf = 0;
25
volatile uint8_t tmr0_ovf = 0;
21
volatile uint32_t systime = 0;
26
volatile uint32_t systime = 0;
22
 
27
 
-
 
28
#define UART_SEND_DELAY 5000
-
 
29
#define BUTTON_DELAY 500
-
 
30
#define LCD_SEND_DELAY 80
-
 
31
 
-
 
32
volatile uint16_t buttontime = BUTTON_DELAY;
23
volatile uint16_t sendtime = 1000;
33
volatile uint16_t sendtime = UART_SEND_DELAY;
-
 
34
volatile uint8_t lcdTimer = LCD_SEND_DELAY;
24
 
35
 
25
 
36
 
26
int main(void) {
37
int main(void) {
27
 
38
 
28
 
39
 
Line 40... Line 51...
40
        PB4     - 
51
        PB4     - 
41
        PB5     - 
52
        PB5     - 
42
        PB6     - 
53
        PB6     - 
43
        PB7     - 
54
        PB7     - 
44
  */
55
  */
45
  DDRB          = 0B11111111;
56
  DDRB          = 0B00001111;
46
  PORTB         = 0B00000000;
57
  PORTB         = 0B00000000;
47
 
58
 
48
  /*
59
  /*
49
        PC0     - 
60
        PC0     - 
50
        PC1     - 
61
        PC1     - 
Line 67... Line 78...
67
        PD7     - Input, Pullup		- * Button
78
        PD7     - Input, Pullup		- * Button
68
  */
79
  */
69
  DDRD          = 0B01011111;
80
  DDRD          = 0B01011111;
70
  PORTD         = 0B10000000;
81
  PORTD         = 0B10000000;
71
 
82
 
72
    TIMSK0 = (1<<TOIE0);                    // Enable timer overflow
-
 
73
    TCNT0 = 0x00;                           // Set Timer0 initial value to 0
-
 
74
    TCCR0B = (1<< CS00) ;                   // /1 prescaler
-
 
75
 
83
 
76
    
-
 
77
    
-
 
78
  
84
  
-
 
85
    sysclockInit();   
-
 
86
    
79
  uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
87
    uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
80
 
-
 
81
  
88
  
82
    wdt_enable(WDTO_1S);		// Watchdog for 1 sec
89
    wdt_enable(WDTO_1S);		// Watchdog for 1 sec
83
    sei();				// Enable interrupts
90
    sei();				// Enable interrupts
-
 
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;
84
 
103
 
85
    sbi(PIND, PD6);
104
    sbi(PORTD, PD6); 
86
    _delay_ms(500);
105
    _delay_us(100);
87
    cbi(PIND, PD6);
106
    cbi(PORTD, PD6);
88
    
107
    
89
  char buffer[7];
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
   
90
 
115
 
91
    for(;;){
116
    for(;;){
92
        wdt_reset();
117
        wdt_reset();
-
 
118
	cbi(PIND, PD6);
93
	
119
	
94
	if (rbi(PIND, PD7))
120
	if (!rbi(PIND, PD7) && !buttontime) {
-
 
121
	  
95
	   cbi(PORTD, PD6);
122
	  sbi(PORTD, PD6);
96
	else
123
	  delay_ms(1);
97
	   sbi(PORTD, PD6);
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);
98
	
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;
99
 
144
	}
-
 
145
	   
100
	
146
	
101
	if (!sendtime) {
147
	if (!sendtime) {
102
	  ltoa(systime, buffer, 10);
148
	  ltoa(systime, display, 10);
103
	  uart_puts(buffer);
149
	  uart_puts(display);
104
	  uart_puts("\r\n");
150
	  uart_puts("\r\n");
-
 
151
	  
105
	  sendtime=1000;
152
	  sendtime=UART_SEND_DELAY;
-
 
153
	  
-
 
154
 
-
 
155
	}
-
 
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) {
106
	  xbi(PORTD, PD6);
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;
107
	}
174
	}
108
	_delay_ms(1);
175
	_delay_ms(1);
109
	
176
	
110
    }
177
    }
111
 
178
    
112
    return 0;
179
    return 0;
113
}
180
}
114
 
181
 
115
 
182
 
116
ISR(TIMER0_OVF_vect) {
183
ISR(TIMER0_OVF_vect) {
Line 125... Line 192...
125
        if (tmr0_ovf>=64) {
192
        if (tmr0_ovf>=64) {
126
                systime++;
193
                systime++;
127
                tmr0_ovf = 0;
194
                tmr0_ovf = 0;
128
		if (sendtime)
195
		if (sendtime)
129
		  sendtime--;
196
		  sendtime--;
-
 
197
		
-
 
198
		if (lcdTimer)
-
 
199
		    lcdTimer--;
-
 
200
		
-
 
201
		if (buttontime)
-
 
202
		    buttontime--;
130
        }
203
        }
131
}
204
}