Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ***********************************************************************
- //
- // Demo program pro vyuku predmetu APPS (09/2014)
- // Petr Olivka, katedra informatiky, FEI, VSB-TU Ostrava
- // email:petr.olivka@vsb.cz
- //
- // Priklad pouzit vyvojoveho nastroje AVR-KIT:
- // Ovladani zarizeni na sbernici
- //
- // ***********************************************************************
- // nejprve hlavickovy soubor AVR-KITu
- #include "../avrkit/avrkit.h"
- #include "i2c-lib.h"
- #include "si4735-lib.h"
- #include <stdio.h>
- #include <avr/io.h>
- // Funkce pro ovladani sbernice I2C, kopie z i2c-lib.h
- // inicializace sbernice
- void I2C_Init( void );
- // oznameni zacatku komunikace
- void I2C_Start( void );
- // oznameni ukonceni komunikace
- void I2C_Stop( void );
- // odeslani potvrzovaci bitu ACK
- void I2C_Ack( void );
- // odeslani potvrzovaciho bitu NACK
- void I2C_NAck( void );
- // prijem potvrzovaciho bitu
- char I2C_getACK( void );
- // odeslani 8 bitove hodnoty, vraci ACK
- char I2C_Vystup( unsigned char value );
- // precteni 8 bitove hodnoty
- unsigned char I2C_Vstup( void );
- //************************************************************************
- struct stav_radia {
- char S1, S2, RSSI, SNR, MULT, CAP;
- int FREQ;
- };
- typedef struct stav_radia stav_radia;
- #define W 0b00000000
- #define R 0b00000001
- #define EXPANDER 0b01001110
- char set_volume(char volume)
- {
- char ack = 0;
- I2C_Start();
- ack |= I2C_Vystup( SI4735_address);
- ack |= I2C_Vystup( 0x12 ); //set property
- ack |= I2C_Vystup( 0x00 ); //set property
- ack |= I2C_Vystup( 0x40 ); //set property
- ack |= I2C_Vystup( 0x00 ); //set property
- ack |= I2C_Vystup( 0x00 ); //set property
- ack |= I2C_Vystup( volume ); //set property
- I2C_Stop();
- return ack;
- }
- char svit_mesicku(char port)
- {
- char ack = 0;
- I2C_Start();
- // adresovani registru PCF8574
- // adresa se sklada ze 3 casti!
- ack = I2C_Vystup(EXPANDER);
- // kontrola ack, navratova hodnota musi byt 0!
- // ....
- // zapis 8 bitu do registru
- ack |= I2C_Vystup( port );
- // nyni sviti pozadovane LED
- // konec komunikace
- I2C_Stop();
- return ack;
- }
- char nalad_stanici(int frekvence)
- {
- char ack = 0;
- I2C_Start();
- ack |= I2C_Vystup( SI4735_address);
- ack |= I2C_Vystup( 0x20 ); // FM_TUNE_FREQ
- ack |= I2C_Vystup( 0x00 ); // ARG1
- ack |= I2C_Vystup( frekvence >> 8 ); // ARG2 - FreqHi 00000000 00011000
- ack |= I2C_Vystup( frekvence & 0xff ); // ARG3 - FreqLo
- ack |= I2C_Vystup( 0x00 ); // ARG4
- I2C_Stop();
- return ack;
- }
- char precti_stav_radia(stav_radia * stav)
- {
- char ack = 0;
- // Priklad cteni frekcence aktualne naladene stanice
- I2C_Start();
- ack |= I2C_Vystup( SI4735_address);
- ack |= I2C_Vystup( 0x22 ); // FM_TUNE_STATUS
- ack |= I2C_Vystup( 0x00 ); // ARG1
- // opakovany start
- I2C_Start();
- // zmena smeru komunikace
- ack |= I2C_Vystup( SI4735_address | R );
- // cteni dat
- stav->S1 = I2C_Vstup();
- I2C_Ack();
- stav->S2 = I2C_Vstup();
- I2C_Ack();
- stav->FREQ = (int) I2C_Vstup() << 8;
- I2C_Ack();
- stav->FREQ |= I2C_Vstup();
- I2C_Ack();
- stav->RSSI = I2C_Vstup();
- I2C_Ack();
- stav->SNR = I2C_Vstup();
- I2C_Ack();
- stav->MULT = I2C_Vstup();
- I2C_Ack();
- stav->CAP = I2C_Vstup();
- I2C_NAck();
- I2C_Stop();
- return ack;
- }
- char vyhledej_stanici(char nahoru)
- {
- char ack = 0;
- I2C_Start();
- ack |= I2C_Vystup( SI4735_address);
- ack |= I2C_Vystup( 0x21 ); //set property
- ack |= I2C_Vystup( 0b00001000); //set property
- I2C_Stop();
- return ack;
- }
- #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
- int main( void )
- {
- char ack = 0;
- // inicializace prostredi
- avrkit_init();
- I2C_Init();
- // komunikace s 8 bitovym expanderem
- /*
- // zacatek komunikace
- I2C_Start();
- // adresovani registru PCF8574
- // adresa se sklada ze 3 casti!
- ack = I2C_Vystup( HWADDR_PCF8574 | A012 | R_or_W );
- // kontrola ack, navratova hodnota musi byt 0!
- // ....
- // zapis 8 bitu do registru
- I2C_Vystup( libovolne_8_bitove_cislo )
- // nyni sviti pozadovane LED
- // konec komunikace
- I2C_Stop();
- */
- if ( ( ack = SI4735_Init() ) != 0 )
- {
- printf("Chyba (%d) inicializace obvodu SI4735\n", ack );
- return 0;
- }
- else
- printf( "Obvod SI4735 inicializovan.\n" );
- printf( "\nLadeni stanice...\n" );
- // Pozadovana frekvence * 100
- int freq = 10140;
- // Priklad ladeni stanice
- nalad_stanici(freq);
- // kontrola ack!
- // if...
- // probiha ladeni...
- delay_ms( 100 );
- if (ack != 0)
- {
- printf("stanice nebyla naladena\n");
- }
- else printf( "...stanice naladena.\n\n" );
- stav_radia stav;
- ack = precti_stav_radia(&stav);
- if ( ack != 0 )
- printf( "Nastala chyba v komunikaci\n" );
- else
- printf( "Aktualne naladena fm je: %d.%dMHz\n", stav.FREQ / 100, stav.FREQ % 100 );
- signed char volume = 10;
- char led = 1;
- svit_mesicku(volume);
- while (1)
- {
- if (~read_butt() & 0b10000000)
- {
- volume += 10;
- if (volume > 63)
- {
- volume = 63;
- }
- if (set_volume(volume) != 0)
- {
- printf("neco je spatne\n");
- }
- else printf("neco je dobre\n");
- led <<= 1;
- svit_mesicku(led);
- stav_radia stav;
- precti_stav_radia(&stav);
- printf("RSSI: %d, SNR: %d, MULT: %d, CAP: %d", stav.RSSI, stav.SNR, stav.MULT, stav.CAP);
- delay_ms(200);
- }
- if (~read_butt() & 0b01000000)
- {
- volume -= 10;
- if (volume < 0)
- {
- volume = 0;
- }
- if (set_volume(volume) != 0)
- {
- printf("neco je spatne\n");
- }
- else printf("neco je dobre\n");
- led >>= 1;
- led |= (1 << 7);
- svit_mesicku(led);
- delay_ms(200);
- }
- if (~read_butt() & 0b00100000)
- {
- nalad_stanici(1);
- delay_ms(200);
- stav_radia stav;
- precti_stav_radia(&stav);
- printf( "Aktualne naladena fm je: %d.%dMHz\n", stav.FREQ / 100, stav.FREQ % 100 );
- }
- if (~read_butt() & 0b00010000)
- {
- nalad_stanici(0);
- delay_ms(200);
- stav_radia stav;
- precti_stav_radia(&stav);
- printf( "Aktualne naladena fm je: %d.%dMHz\n", stav.FREQ / 100, stav.FREQ % 100 );
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement