View difference between Paste ID: yf9bFANr and 01WVubqr
SHOW: | | - or go back to the newest paste.
1
// ***********************************************************************
2
//
3
// Demo program pro vyuku predmetu APPS (09/2014)
4
// Petr Olivka, katedra informatiky, FEI, VSB-TU Ostrava
5
// email:petr.olivka@vsb.cz
6
//
7
// Priklad pouzit vyvojoveho nastroje AVR-KIT:
8
// Ovladani zarizeni na sbernici 
9
//
10
// ***********************************************************************
11
12
// nejprve hlavickovy soubor AVR-KITu 
13
#include "../avrkit/avrkit.h"
14
#include "i2c-lib.h"
15
#include "si4735-lib.h"
16
17
#include <stdio.h>
18
#include <avr/io.h>
19
20
// Funkce pro ovladani sbernice I2C, kopie z i2c-lib.h
21
22
// inicializace sbernice
23
void I2C_Init( void );
24
25
// oznameni zacatku komunikace
26
void I2C_Start( void );
27
28
// oznameni ukonceni komunikace
29
void I2C_Stop( void );
30
31
// odeslani potvrzovaci bitu ACK
32
void I2C_Ack( void );
33
34
// odeslani potvrzovaciho bitu NACK
35
void I2C_NAck( void );
36
37
// prijem potvrzovaciho bitu
38
char I2C_getACK( void );
39
40
// odeslani 8 bitove hodnoty, vraci ACK
41
char I2C_Vystup( unsigned char value );
42
43
// precteni 8 bitove hodnoty
44
unsigned char I2C_Vstup( void );
45
46
//************************************************************************
47
48
struct stav_radia {
49
	char S1, S2, RSSI, SNR, MULT, CAP;
50
	int FREQ;
51
};
52
53
typedef struct stav_radia stav_radia;
54
55
#define W	0b00000000
56
#define R	0b00000001
57
#define EXPANDER 0b01001110
58
59
char set_volume(char volume)
60
{
61
	char ack = 0;
62
	
63
	I2C_Start();
64
	
65
	ack |= I2C_Vystup( SI4735_address);
66
	ack |= I2C_Vystup( 0x12 ); //set property
67
	ack |= I2C_Vystup( 0x00 ); //set property
68
	ack |= I2C_Vystup( 0x40 ); //set property
69
	ack |= I2C_Vystup( 0x00 ); //set property
70
	ack |= I2C_Vystup( 0x00 ); //set property
71
	ack |= I2C_Vystup( volume ); //set property	
72
	
73
	I2C_Stop();
74
	
75
	return ack;
76
}
77
char svit_mesicku(char port)
78
{
79
	char ack = 0;
80
	
81
	I2C_Start();
82
	
83
	// adresovani registru PCF8574
84
	// adresa se sklada ze 3 casti!
85
	ack = I2C_Vystup(EXPANDER);
86
	// kontrola ack, navratova hodnota musi byt 0!
87
	// ....
88
	// zapis 8 bitu do registru
89
	ack |= I2C_Vystup( port );
90
	// nyni sviti pozadovane LED
91
92
	// konec komunikace
93
	I2C_Stop();
94
	
95
	return ack;
96
}
97
char nalad_stanici(int frekvence)
98
{
99
	char ack = 0;
100
	
101
	I2C_Start();
102
	ack |= I2C_Vystup( SI4735_address);
103
	ack |= I2C_Vystup( 0x20 );			// FM_TUNE_FREQ
104
	ack |= I2C_Vystup( 0x00 );			// ARG1
105
	ack |= I2C_Vystup( frekvence >> 8 );		// ARG2 - FreqHi   00000000 00011000
106
	ack |= I2C_Vystup( frekvence & 0xff );	// ARG3 - FreqLo
107
	ack |= I2C_Vystup( 0x00 );			// ARG4
108
	I2C_Stop();
109
	
110
	return ack;
111
}
112
char precti_stav_radia(stav_radia * stav)
113
{
114
	char ack = 0;
115
	
116
	// Priklad cteni frekcence aktualne naladene stanice
117
	I2C_Start();
118
	ack |= I2C_Vystup( SI4735_address);
119
	ack |= I2C_Vystup( 0x22 );			// FM_TUNE_STATUS
120
	ack |= I2C_Vystup( 0x00 );			// ARG1
121
	// opakovany start
122
	I2C_Start();
123
	// zmena smeru komunikace
124
	ack |= I2C_Vystup( SI4735_address | R );
125
	// cteni dat
126
	stav->S1 = I2C_Vstup();
127
	I2C_Ack();
128
	stav->S2 = I2C_Vstup();
129
	I2C_Ack();
130
	stav->FREQ = (int) I2C_Vstup() << 8;
131
	I2C_Ack();
132
	stav->FREQ |= I2C_Vstup();
133
	I2C_Ack();
134
	stav->RSSI = I2C_Vstup();
135
	I2C_Ack();
136
	stav->SNR = I2C_Vstup();
137
	I2C_Ack();
138
	stav->MULT = I2C_Vstup();
139
	I2C_Ack();
140
	stav->CAP = I2C_Vstup();
141
	I2C_NAck();
142
	I2C_Stop();
143
	
144
	return ack;
145
}
146
char vyhledej_stanici(char nahoru)
147
{
148
	char ack = 0;
149
	
150
	I2C_Start();
151
	
152
	ack |= I2C_Vystup( SI4735_address);
153
	ack |= I2C_Vystup( 0x21 ); //set property
154
	ack |= I2C_Vystup( 0b00001000); //set property
155
	
156
	I2C_Stop();
157
	
158
	return ack;
159
}
160
161
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
162
int main( void )
163
{
164
	char ack = 0;
165
166
	// inicializace prostredi
167
	avrkit_init();
168
	I2C_Init();
169
170
	// komunikace s 8 bitovym expanderem
171
	/*
172
	// zacatek komunikace
173
	I2C_Start();
174
	
175
	// adresovani registru PCF8574
176
	// adresa se sklada ze 3 casti!
177
	ack = I2C_Vystup( HWADDR_PCF8574 | A012 | R_or_W );
178
	// kontrola ack, navratova hodnota musi byt 0!
179
	// ....
180
	// zapis 8 bitu do registru
181
	I2C_Vystup( libovolne_8_bitove_cislo )
182
	// nyni sviti pozadovane LED
183
184
	// konec komunikace
185
	I2C_Stop();
186
	*/
187
	
188
	if ( ( ack = SI4735_Init() ) != 0 )
189
	{
190
		printf("Chyba (%d) inicializace obvodu SI4735\n", ack );
191
		return 0;
192
	}
193
	else
194
		printf( "Obvod SI4735 inicializovan.\n" );
195
		
196
	printf( "\nLadeni stanice...\n" );
197
	
198
	// Pozadovana frekvence * 100
199
	int freq = 10140;
200
	// Priklad ladeni stanice
201
	nalad_stanici(freq);
202
	// kontrola ack!
203
	// if...
204
	
205
	// probiha ladeni...
206
	delay_ms( 100 );
207
	
208
	if (ack != 0)
209
	{
210
		printf("stanice nebyla naladena\n");
211
	}
212
	else printf( "...stanice naladena.\n\n" );
213
214
	stav_radia stav;
215
	ack = precti_stav_radia(&stav);
216
217
	if ( ack != 0 ) 
218
		printf( "Nastala chyba v komunikaci\n" );
219
	else
220
		printf( "Aktualne naladena fm je: %d.%dMHz\n", stav.FREQ / 100, stav.FREQ % 100 );
221
	
222
	signed char volume = 10;
223
	char led = 1;
224
	svit_mesicku(volume);
225
		
226
	while (1)
227
	{
228
		if (~read_butt() & 0b10000000)
229
		{
230
			volume += 10;
231
			
232
			if (volume > 63)
233
			{
234
				volume = 63;
235
			}
236
			
237
			if (set_volume(volume) != 0)
238
			{
239
				printf("neco je spatne\n");
240
			}
241
			else printf("neco je dobre\n");
242
			
243
			led <<= 1;
244
			svit_mesicku(led);
245
			
246
			stav_radia stav;
247
			precti_stav_radia(&stav);
248
			
249
			printf("RSSI: %d, SNR: %d, MULT: %d, CAP: %d", stav.RSSI, stav.SNR, stav.MULT, stav.CAP);
250
			
251
			delay_ms(200);
252
		}
253
		
254
		if (~read_butt() & 0b01000000)
255
		{
256
			volume -= 10;
257
			
258
			if (volume < 0)
259
			{
260
				volume = 0;
261
			}
262
			
263
			if (set_volume(volume) != 0)
264
			{
265
				printf("neco je spatne\n");
266
			}
267
			else printf("neco je dobre\n");
268
			
269
			led >>= 1;
270
			led |= (1 << 7);
271
			svit_mesicku(led);
272
			
273
			delay_ms(200);
274
		}
275
		
276
		if (~read_butt() & 0b00100000)
277
		{
278
			nalad_stanici(1);
279
			
280
			delay_ms(200);
281
			
282
			stav_radia stav;
283
			precti_stav_radia(&stav);
284
			
285
			printf( "Aktualne naladena fm je: %d.%dMHz\n", stav.FREQ / 100, stav.FREQ % 100 );
286
		}
287
		
288
		if (~read_butt() & 0b00010000)
289
		{
290
			nalad_stanici(0);
291
			
292
			delay_ms(200);
293
			
294
			stav_radia stav;
295
			precti_stav_radia(&stav);
296
			
297
			printf( "Aktualne naladena fm je: %d.%dMHz\n", stav.FREQ / 100, stav.FREQ % 100 );
298
		}
299
	}
300
301
	return 0;
302
}