Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #ifndef F_CPU
  2. #define F_CPU 8000000UL
  3. #endif
  4.  
  5. #include <AVR/IO.h>
  6. #include <AVR/interrupt.h>
  7. #include <math.h>
  8. #include <util/delay.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. #define BAUD 9600
  14. #define ubrr 51
  15. const double pi = 3.14159265358979323846;
  16. int i=1,contor_led,u=0;
  17. volatile int j=1;
  18. volatile long suma=0, suma1=0, suma2=0, vit=0;
  19. //float y,z;
  20. unsigned char r,c;
  21. char buff[200];
  22.  
  23.  
  24. void initserial(void){
  25. SREG=SREG&127;
  26. UCSR0B=(1<<RXEN0)|(1<<TXEN0);//enable transmiter and reciever
  27. UBRR0H=(unsigned char) (ubrr>>8);//set transfer rate
  28. UBRR0L=(unsigned char) ubrr ;
  29. UCSR0C=0x86;//8 data bit+1 stop data
  30. UCSR0B=UCSR0B|192;//enable USART interupt TXCIE/RXCIE
  31.  
  32. //UCSRA=UCSRA|128;//activare recieve complete interupt
  33. SREG=SREG|128;
  34. }
  35.  
  36. int prints(char *string)
  37. {
  38.  
  39. int count =0;
  40. while ((string[count]) != '\0')
  41. {
  42. while ( !( UCSR0A & (1<<UDRE0)) ); // Wait for empty transmit buffer
  43. UDR0 = (char)string[count++];
  44.  
  45. }
  46. //TxByte('_');
  47. UCSR0A=UCSR0A & 32;
  48. return 0;
  49.  
  50. }
  51.  
  52.  
  53.  
  54. ISR (USART_TX_vect){
  55.  
  56. }
  57.  
  58.  
  59. //****************** INTRERUPERE RECIEVE COMPLETE************
  60. ISR (USART_RX_vect){
  61. //ISR(USART_RXC_vect ){
  62.  
  63.  
  64. r=UDR0;
  65. OCR1A=r;
  66. }
  67. ISR(TIMER0_OVF_vect){
  68. //TCNT0=5;
  69.  
  70. contor_led=contor_led+1;
  71.  
  72. if(contor_led>60)
  73. {
  74. sprintf(buff,"%ld,%ld\r",suma2,vit);
  75. prints(buff);
  76. contor_led=0;
  77. }
  78. }
  79. int main(void)
  80. {
  81. DDRD=DDRD|224; // pin 7,6,5 config ca iesiri
  82. DDRB=DDRB|6; //pin pb1, pb2 ca iesiri
  83. DDRC=DDRC|28;///pc2,pc3,pc4
  84. DDRD=DDRD|8;//pd3
  85.  
  86. TCCR1A=TCCR1A|160; //OC1A si OC1B ca pwm
  87. TCCR1B=19;//setare frecventa clock timer 64 prescaler
  88. ICR1=1250;//setare frecventa pwm
  89.  
  90.  
  91. //TCCR0A=(1<<WGM01);// Set CTC Bit
  92. TCCR0B=5;
  93. ;
  94. TIMSK0= TIMSK0|1;
  95.  
  96. TCCR2A |= (1 << COM2B1);
  97. // set none-inverting mode
  98. TCCR2A |= (1 << WGM21) | (1 << WGM20);
  99. // set fast PWM Mode
  100.  
  101. TCCR2B |= (1 << CS21);
  102. // set prescaler to 8 and starts PWM
  103. OCR2B = 0;
  104. // set PWM for 50% duty cycle
  105. initserial();
  106.  
  107. SREG=SREG|128;
  108.  
  109. OCR1A=50;
  110. //OCR1B=140;
  111. sprintf(buff," Buna ziua\n");
  112. prints(buff);
  113.  
  114. ADMUX=1;
  115. ADCSRA= ADCSRA | 128; //ADC enable
  116.  
  117. PORTD=PORTD|32;
  118.  
  119. while(1)
  120. {
  121. TIMSK0= TIMSK0&254;
  122.  
  123. ADMUX=0;
  124. suma=0;
  125. for(i=0;i<10;i++)
  126. {
  127. ADCSRA = ADCSRA | 64; // ADC start conversie
  128. _delay_us(100);
  129. suma=suma+ADC;
  130. _delay_ms(1);
  131. }
  132. suma=suma/10.0;
  133.  
  134.  
  135. ADMUX=1;
  136. suma1=0;
  137. for(i=0;i<10;i++)
  138. {
  139. ADCSRA = ADCSRA | 64; // ADC start conversie
  140. _delay_us(100);
  141. suma1=suma1+ADC;
  142. _delay_ms(1);
  143. }
  144. suma1=suma1/10.0;
  145.  
  146. ADMUX=5;
  147. suma2=0;
  148. for(i=0;i<10;i++)
  149. {
  150. ADCSRA = ADCSRA | 64; // ADC start conversie
  151. _delay_us(100);
  152. suma2=suma2+ADC;
  153. _delay_ms(1);
  154. }
  155. suma2=suma2/10.0;
  156. TIMSK0= TIMSK0|1;
  157. //432 220
  158. //if(suma>425)
  159. vit=(suma1/4)*((suma-220)/212.0);
  160. //vit=(suma1+4*suma-1732)/4;
  161. if(vit>=255)
  162. vit=255;
  163. if(vit<=0)
  164. vit=0;
  165.  
  166. if(suma>425)
  167. PORTC=PORTC&227;
  168. if(suma<420)
  169. PORTC=PORTC|4;
  170. if(suma<370)
  171. PORTC=PORTC|8;
  172. if(suma<310)
  173. PORTC=PORTC|16;
  174. if(suma<250)
  175. {PORTC=PORTC|28;
  176. _delay_ms(100);
  177. PORTC=PORTC&227;
  178. _delay_ms(100);
  179. }
  180. OCR2B=(int)vit;
  181. OCR1A=(int)vit*35/255.0+50;
  182.  
  183.  
  184. // sprintf(buff," %ld, %ld %ld\n",suma,suma1,vit);
  185. // prints(buff);
  186. //_delay_ms(2);
  187. }
  188. return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement