-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpov.c
155 lines (132 loc) · 3.39 KB
/
pov.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Copyright 2010 Volts and Bytes
http://voltsandbytes.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define F_CPU 8000000UL
#define FOSC 8000000 // Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
#define STX 0x02
#define ACK 0x06
#define NAK 0x15
#define SIZE 69
#include<avr/io.h>
#include<util/delay.h>
void init_all(void);
void USART_Init(unsigned int);
void USART_Transmit(unsigned char);
unsigned char USART_Receive(void);
void EEPROM_write(unsigned int, unsigned char);
unsigned char EEPROM_read(unsigned int);
unsigned char buffer[SIZE+1];
int main(void) {
unsigned char ctr=0;
unsigned char checker=0;
unsigned char temp;
init_all();
_delay_ms(10);
while(1)
{
if((PIND & 0x20) == 0x20)
{
for(ctr=0;ctr<SIZE;ctr++)
{
temp=EEPROM_read(ctr); //display EEPROM data
PORTB=~temp;
_delay_ms(10);
}
PORTB=0xFF;
}
else
{
PORTB=0xFF;
while(USART_Receive()!=STX); //wait for start byte
USART_Transmit(ACK); //send ack byte when start byte is received
for(ctr=0;ctr<SIZE+1;ctr++)
{
buffer[ctr]=USART_Receive(); //fill buffer
checker+=buffer[ctr]; //find sum of buffer
}
if(checker==0xFF)
{
for(ctr=0;ctr<SIZE;ctr++)
{
EEPROM_write(ctr, buffer[ctr]); //write buffer contents to EEPROM minus the checksum byte
}
USART_Transmit(ACK); //send ack byte
}
else
{
USART_Transmit(NAK); //send no ack byte
}
}
}
}
void init_all(void) {
//initialize pins
DDRB = 0xFF;
PORTB=0xFF;
PORTD=0xDF;
USART_Init(MYUBRR);
}
/*
* pg 183 from datasheet of atmega328
*
* USART Baud Rate Register (UBRRn)
*/
void USART_Init(unsigned int ubrr) {
/* Set baud rate */
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 1stop bit */
UCSR0C = (3<<UCSZ00);
}
void USART_Transmit(unsigned char data) {
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR = data;
}
unsigned char USART_Receive(void) {
/* Wait for data to be received */
while ( !(UCSRA & (1<<RXC)) )
;
/* Get and return received data from buffer */
return UDR;
}
void EEPROM_write(unsigned int uiAddress, unsigned char ucData) {
/* Wait for completion of previous write */
while(EECR & (1<<EEPE))
;
/* Set up address and data registers */
EEAR = uiAddress;
EEDR = ucData;
/* Write logical one to EEMPE */
EECR |= (1<<EEMPE);
/* Start eeprom write by setting EEPE */
EECR |= (1<<EEPE);
}
unsigned char EEPROM_read(unsigned int uiAddress) {
/* Wait for completion of previous write */
while(EECR & (1<<EEPE))
;
/* Set up address register */
EEAR = uiAddress;
/* Start eeprom read by writing EERE */
EECR |= (1<<EERE);
/* Return data from data register */
return EEDR;
}