main.c 6.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
//LF Session 3 Task 2: OLED
//main.c
//

#define F_CPU 3333333UL


#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdio.h>
13
#include <avr/interrupt.h>
14
15
16
17

#include "display.h"
#include "driv_OLED.h"

18
const uint8_t OV[512] PROGMEM =
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
{
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0xE0, 0xFF, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x70, 0x38, 0xDE, 0x3F, 0x3E, 0x18, 0x00, 0x00,
	0x00, 0xF0, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0xD8, 0x78, 0xDE, 0x3B, 0x33, 0x1C, 0x00, 0x00,
	0x00, 0xFC, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x8C, 0x79, 0x8E, 0x21, 0x23, 0x1C, 0x00, 0x00,
	0x00, 0xFE, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x8C, 0x79, 0x8E, 0xA9, 0x23, 0x3C, 0x00, 0x00,
	0x00, 0x3F, 0x00, 0xF8, 0x01, 0x00, 0x00, 0x00, 0x8C, 0x79, 0x8F, 0x89, 0x03, 0x3C, 0x00, 0x00,
	0x00, 0x1F, 0x00, 0xF0, 0x01, 0x00, 0x00, 0x00, 0x8C, 0x79, 0x8D, 0x8F, 0x03, 0x36, 0x00, 0x00,
	0x80, 0x0F, 0x00, 0xE0, 0x03, 0x00, 0x00, 0x00, 0x8C, 0xF9, 0x8D, 0x8B, 0x7B, 0x32, 0x00, 0x00,
	0xC0, 0x07, 0x00, 0xC0, 0x07, 0x00, 0x00, 0x00, 0x8C, 0xD9, 0x8D, 0x89, 0x33, 0x3E, 0x00, 0x00,
	0xC0, 0x03, 0x00, 0x80, 0x07, 0x00, 0x00, 0x00, 0x8C, 0xD9, 0x8D, 0xA1, 0x33, 0x72, 0x00, 0x00,
	0xE0, 0x03, 0x00, 0x80, 0x0F, 0x00, 0x00, 0x00, 0x8C, 0xD9, 0x8D, 0xA1, 0x33, 0x72, 0x00, 0x00,
	0xE0, 0xFF, 0xC7, 0xFF, 0x0F, 0x00, 0x00, 0x00, 0xD8, 0xD9, 0x8D, 0x31, 0x33, 0x63, 0x00, 0x00,
	0xE0, 0xFF, 0xC7, 0xFF, 0x0F, 0x00, 0x00, 0x00, 0xF8, 0xFC, 0xDE, 0x3F, 0x3F, 0xF7, 0x00, 0x00,
	0xE0, 0xFF, 0xC7, 0xFF, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0xE0, 0xFF, 0xC7, 0xFF, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0xE0, 0xE1, 0x00, 0x0E, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0xE0, 0xE1, 0x00, 0x0E, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0xE0, 0xC1, 0x01, 0x07, 0x0F, 0x00, 0xC0, 0xF3, 0x7F, 0x3F, 0xBC, 0xE3, 0xFB, 0xF7, 0xEF, 0x07,
	0xE0, 0xC1, 0x01, 0x07, 0x0F, 0x00, 0x80, 0x33, 0x67, 0x6E, 0x38, 0x33, 0xDB, 0xE7, 0xEC, 0x0F,
	0xE0, 0x81, 0x83, 0x03, 0x0F, 0x00, 0x80, 0x33, 0x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54
#define KLAKK 0x00
55
56
#define LED0_bm 0x20
#define LED0_PORT PORTF
57
58
#define FONTSIZE 5
#define ADC_PRINT_START 82
59
60
#define SW0_bm 0x40
#define SW0_PORT PORTF
61
62
63
int main(void)
{
	
64
65
	sei();
	_delay_ms(500);
66
	//DISP_init();
67
	adc_init();
68
	//DISP_write_bitmap(OV); //Printing Bitmap
69
	//DISP_print("Hello World!"); //Printing text
70
71
	LED0_PORT.DIRSET = LED0_bm;
	SW0_PORT.DIRCLR = SW0_bm;
72
73
74
75
	oled_pos(1,12);
	oled_write_d(0xf8);
	oled_pos(2,12);
	oled_write_d(0x1f);
76
	oled_pos(1,115);
77
	oled_write_d(0xf8);
78
	oled_pos(2,115);
79
80
81
82
83
84
85
86
	oled_write_d(0x1f);
	for(uint8_t i=0;i<101;i++)
	{
		oled_pos(1,13+i);
		oled_write_d(0x08);
		oled_pos(2,13+i);
		oled_write_d(0x10);
	}
87
88
	oled_pos(0,12);
	fprintf(FONT5,"OV-POT read: ");
89
90
	
	
91
	
92
93
	uint8_t software_timer_print_adc = 0;
	uint8_t blinkled = 0;
94
95
	uint16_t data;
	uint16_t percent_data;
96
	
97
98
    while (1) 
     {
99

100
101
102
103
104
105
		software_timer_print_adc++;
		blinkled++;
		uint8_t adc_curser_position=82;
		data=adc_read(KLAKK)/4;
		
		
106

107
		if (software_timer_print_adc==2)
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
		{
			oled_pos(0,ADC_PRINT_START);
			//for (uint8_t i=0;i<27;i++)
			//{
			//	oled_write_d(0x00);
			//}
// 			if (data>=1000)
 //			{
 				oled_pos(0,adc_curser_position);
// 			}
			if (data<1000)
			{
				for (uint8_t i=0;i<FONTSIZE;i++)
				{
					oled_write_d(0x00);
				}
				adc_curser_position+=FONTSIZE+1;
				oled_pos(0,adc_curser_position);
				
			}
			if (data<100)
			{
				for (uint8_t i=0;i<FONTSIZE;i++)
				{
					oled_write_d(0x00);
				}
				adc_curser_position+=FONTSIZE+1;
				oled_pos(0,adc_curser_position);
				
			}
			if (data<10)
			{
				for (uint8_t i=0;i<FONTSIZE;i++)
				{
					oled_write_d(0x00);
				}
				adc_curser_position+=FONTSIZE+1;
				oled_pos(0,adc_curser_position);
				
			}
			fprintf(FONT5," %u",data);
			software_timer_print_adc=0;
		}
		
		
		percent_data=data/10;
154
155
156
157
158
159
		for(int i=0; i<percent_data;i++){
			oled_pos(1,13+i);
			oled_write_d(0xf8);
			oled_pos(2,13+i);
			oled_write_d(0x1f);
		}
160
		for(int i=percent_data; i<102;i++){
161
162
163
164
165
			oled_pos(1,13+i);
			oled_write_d(0x08);
			oled_pos(2,13+i);
			oled_write_d(0x10);
		}
166
167
		if (blinkled==15)
		{
168
			PORTF.OUTTGL = LED0_bm;
169
170
171
172
173
174
175
176
177
			blinkled=0;
		}
		
		//Voltage printout
		oled_pos(3,12);
		//uint32_t voltage=
		uint8_t voltage_integer= data*323ul/100000ul;
		uint8_t voltage_fraction= (data*323ul%100000ul)/1000;
		fprintf(FONT5,"Voltage: %u.%02u V",voltage_integer,voltage_fraction);
178
		
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
		if (!(SW0_PORT.IN & (SW0_bm)))
		{
			oled_pos(0,0);
			DISP_write_bitmap(OV);
			while (!(SW0_PORT.IN & (SW0_bm)))
			{
			}
			DISP_clear();
			oled_pos(1,12);
			oled_write_d(0xf8);
			oled_pos(2,12);
			oled_write_d(0x1f);
			oled_pos(1,115);
			oled_write_d(0xf8);
			oled_pos(2,115);
			oled_write_d(0x1f);
			for(uint8_t i=0;i<101;i++)
			{
				oled_pos(1,13+i);
				oled_write_d(0x08);
				oled_pos(2,13+i);
				oled_write_d(0x10);
			}
			oled_pos(0,12);
			fprintf(FONT5,"OV-POT read: ");
		}
205
206
     }
}