forked from 004forever/DJPace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
200 lines (176 loc) · 4.03 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*************************************************************
* at328-0.c - Demonstrate simple I/O functions of ATmega328
*
* Program loops turning PC0 on and off as fast as possible.
*
* The program should generate code in the loop consisting of
* LOOP: SBI PORTC,0 (2 cycles)
* CBI PORTC,0 (2 cycles)
* RJMP LOOP (2 cycles)
*
* PC0 will be low for 4 / XTAL freq
* PC0 will be high for 2 / XTAL freq
* A 9.8304MHz clock gives a loop period of about 600 nanoseconds.
*
* Revision History
* Date Author Description
* 09/14/12 A. Weber Initial Release
* 11/18/13 A. Weber Renamed for ATmega328P
*************************************************************/
#include <avr/io.h>
#include <util/delay.h>
#define ClearBit(x,y) x &= ~_BV(y)
#define SetBit(x,y) x |= _BV(y)
#define SWRESET 0x01
#define SLPOUT 0x11
#define DISPOFF 0x28
#define DISPON 0x29
#define CASET 0x2A
#define RASET 0x2B
#define RAMWR 0x2C
#define MADCTL 0x36
#define COLMOD 0x3A
#define FOSC 9830400 // Clock frequency
#define BAUD 9600 // Baud rate used by the LCD
#define MYUBRR FOSC/16/BAUD-1
void OpenSPI()
{
SPCR = 0x50;
}
void HardwareReset()
{
DDRB &= ~_BV(DDB0);
_delay_ms(1);
DDRB |= _BV(DDB0);
_delay_ms(200);
// pull PB0 (digital 8) briefly low
// 1 mS is enough
// return PB0 high
// wait 200 mS for reset to finish
}
void CloseSPI()
{
SPCR = 0x00;
}
uint8_t Xfer(uint8_t data)
{
SPDR = data;
while (!(SPSR & 0x80));
return SPDR;
}
// you can use uint8_t for byte
// initiate transfer
// wait for transfer to complete
void WriteData (uint8_t b)
{
Xfer(b);
}
void WriteCmd (uint8_t cmd)
{
ClearBit(PORTB,1);
Xfer(cmd);
SetBit(PORTB,1);
}
void InitDisplay()
{
HardwareReset();
WriteCmd(SLPOUT);
_delay_ms(150);
WriteCmd(COLMOD);
WriteData(0x05);
WriteCmd(DISPON);
// initialize display controller
// take display out of sleep mode
// wait 150mS for TFT driver circuits
// select color mode:
// mode 5 = 16bit pixels (RGB565)
// turn display on!
}
void sci_num(uint8_t num)
{
char first = (num>>4)&0x0f;
char second = num&0x0f;
first += '0';
second +='0';
if(first > '9')
{
first -= '9';
first -= 1;
first += 'A';
}
if(second > '9')
{
second -= '9';
second -= 1;
second += 'A';
}
sci_out(first);
sci_out(second);
sci_out(' ');
}
void sci_init(void) {
UBRR0 = MYUBRR; // Set baud rate
UCSR0B |= (1 << TXEN0); // Turn on transmitter
UCSR0C = (3 << UCSZ00); // Set for asynchronous operation, no parity,
// one stop bit, 8 data bits
}
/*
sci_out - Output a byte to SCI port
*/
void sci_out(unsigned char ch)
{
while ( (UCSR0A & (1<<UDRE0)) == 0);
UDR0 = ch;
}
unsigned char sci_in()
{
while(!(UCSR0A & (1<<RXC0)) == 0);
return UDR0;
}
/*
sci_outs - Print the contents of the character string "s" out the SCI
port. The string must be terminated by a zero byte.
*/
void sci_outs(unsigned char *s)
{
unsigned char ch;
while ((ch = *s++) != (unsigned char) '\0')
sci_out(ch);
}
int main(void)
{
sci_init(); // Initialize the SCI port
uint8_t x;
int time = 0;
int flag = 0;
DDRD |=(1<<DDD7);
ADMUX |= (1<<REFS0);
ADMUX &=~(1<<REFS1);
ADMUX |= (1<<ADLAR);
//ADMUX |= (3<<MUX0);
//ADMUX &= (0xf0 | (3<<MUX0));
ADCSRA |= (7<<ADPS0);
ADCSRA |= (1<<ADEN);
while (1) {
ADCSRA |= (1 << ADSC); // Start a conversion
while (ADCSRA & (1 << ADSC)); // wait for conversion complete
x = ADCH; // Get converted value
//sci_num(x);
_delay_ms(1);
UDR0 = 0;
if(x < 0xFF && flag == 0)
{
flag = 1;
time = 0;
sci_num(x);
}
if(x < 0xFF && flag == 1)
{
time++;
}
if(x == 0xFF && flag == 1)
{
flag = 0;
}
}
}