Advertisement
Guest User

main_RX.c

a guest
Sep 15th, 2014
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.51 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <avr/pgmspace.h>
  4. #include <avr/eeprom.h>
  5. #include <stdlib.h>
  6. #include "global.h"
  7. #include "rf12.h"
  8. #include "uart.h"
  9.  
  10. #include <stdio.h>
  11.  
  12. #include <util/delay.h>
  13. void send(void);
  14. //void USARTInit(uint16_t ubrr_value);
  15. //char USARTReadChar(void);
  16. //void USARTWriteChar(char data);
  17. //void USARTWrite(char*);
  18. //Rx
  19.  
  20.  
  21. int main(void)
  22. {
  23.  
  24.     sei();
  25.  
  26.     UART_Init();
  27.     rf12_init();                // ein paar Register setzen (z.B. CLK auf 10MHz)
  28.     UART_Tx_Str("Init\n", 5);
  29.     rf12_setfreq(RF12FREQ(433.92));     // Sende/Empfangsfrequenz auf 433,92MHz einstellen
  30.     rf12_setbandwidth(4, 1, 4);     // 200kHz Bandbreite, -6dB Verstärkung, DRSSI threshold: -79dBm
  31.     rf12_setbaud(19200);            // 19200 baud
  32.     rf12_setpower(0, 6);            // 1mW Ausgangangsleistung, 120kHz Frequenzshift
  33.    
  34.     DDRA = 0b00000001;
  35.     DDRA &= ~(1 << PINA1);
  36.     PORTA |= 1 << PINA1;
  37.    
  38.     int last_state = 0;
  39.     unsigned char ack_str[]="10000\n";  //ADR D D D D
  40.    
  41.     for (;;)
  42.     {
  43.         unsigned char ret;
  44.         unsigned char test[50];
  45.         unsigned char received_str[50];
  46.         int timeout;
  47.  
  48.         UART_Tx_Str(test, sprintf(test, "Start val: %u\n", rf12_rxstart()));
  49.         rf12_rxstart();
  50.        
  51.         timeout = 2000;
  52.         ret = 255;          //not completed yet
  53.        
  54.         while((ret == 255)&&(timeout>0))    //try if transfer completed
  55.         {  
  56.             timeout--;
  57.             ret = rf12_rxfinish(received_str); //ret = anzahl der Bytes received_str enthält jz Empfangspuffer
  58.             _delay_ms(1);  
  59.         }
  60.        
  61.        
  62.        
  63.         if(timeout>0)
  64.         {
  65.             UART_Tx_Str(test, sprintf(test, "ret: %u\n", ret));
  66.            
  67.             if(ret != 0 && ret != 254 && received_str[0] == '2')                //no CRC error && no old str &&richtige adr
  68.             {
  69.                
  70.                 UART_Tx_Str(received_str,ret); 
  71.                     //Empfangenes ausgeben
  72.                 if(received_str[1] == '1')
  73.                 {
  74.                     PORTA = ( 1 << 0 );
  75.                     ack_str[1] = (char)((PORTA & (1 << PA0))+48);
  76.                 }
  77.                 else if(received_str[1] == '0')
  78.                 {
  79.                     PORTA = ( 0 << 0 );
  80.                     ack_str[1] = (char)((PORTA & (1 << PA0))+48);
  81.                 }
  82.                 else{}
  83.                
  84.                 rf12_allstop();                     //alles zurück
  85.                 _delay_ms(10);
  86.                
  87.                
  88.                 rf12_txstart(ack_str, 6);   //Antwort Senden
  89.                 int timeout2 = 2000;
  90.                 while((rf12_txfinished())&&(timeout2>0))
  91.                 {
  92.                     _delay_ms(1);
  93.                     timeout2--;
  94.                 }
  95.                 if(!(timeout2));
  96.                     //USARTWrite("Timeout2\n\n"); //Warten bis rausgegangen ---evtl. Timeout?
  97.                
  98.                
  99.             }
  100.             else
  101.             {
  102.                 rf12_allstop();
  103.             }
  104.         }
  105.         else
  106.         {
  107.             rf12_allstop();
  108.         }
  109.        
  110.         received_str[0] = '0';
  111.         if (bit_is_clear(PINA,1)) //Stati
  112.         {
  113.                 if(last_state == 0)
  114.                 {
  115.                     last_state = 1;
  116.                     PORTA ^= ( 1 << 0 );
  117.                     ack_str[1] = (char)((PORTA & (1 << PA0))+48);
  118.                    
  119.                 }
  120.                 //USARTWrite("aus\n");
  121.         }
  122.         else
  123.         {
  124.             if(last_state == 1)
  125.                 {
  126.                     last_state = 0;
  127.                     PORTA ^= ( 1 << 0 );
  128.                     ack_str[1] = (char)((PORTA & (1 << PA0))+48);
  129.                 }
  130.                 //USARTWrite("an\n");
  131.                
  132.         }
  133.         //USARTWrite("Ta\n\n");
  134.  
  135. /*blocking sample */   
  136. /*      ret = rf12_rxdata(test);
  137.         if(ret)
  138.             UART_Tx_Str(test,ret);
  139.         else
  140.             UART_Tx_Str("CRC Err\n", 8);
  141. */  }
  142. }
  143. /*void USARTInit(uint16_t ubrr_value)
  144. {
  145.     UBRRL = ubrr_value;
  146.     UBRRH = (ubrr_value>>8);
  147.  
  148.     UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
  149.    
  150.     UCSRB=(1<<RXEN)|(1<<TXEN);
  151. }
  152.  
  153.  
  154. char USARTReadChar()
  155. {
  156.  
  157.    while(!(UCSRA & (1<<RXC)))
  158.    {
  159.      
  160.    }
  161.    
  162.    return UDR;
  163. }
  164.  
  165. void USARTWriteChar(char data)
  166. {
  167.    while(!(UCSRA & (1<<UDRE)))
  168.    {
  169.    }
  170.  
  171.    UDR=data;
  172. }
  173.  
  174. void USARTWrite(char* message)
  175. {
  176.     int i;
  177.     for(i = 0;i<strlen(message);i++)
  178.     {
  179.         USARTWriteChar(*(message+i));
  180.     }
  181. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement