Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. /**********************************************************
  2. Projet : nv_simu
  3. Fichier(s) : nv_simu.c
  4. Objet :
  5.  
  6. Cible : Atmega8
  7. frequ : 8MHz
  8.  
  9. Date création : 18/05/2010
  10. **********************************************************/
  11. // Fichiers à inclure :
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <avr/io.h>
  15. #include <avr/pgmspace.h>
  16. #include <avr/interrupt.h>
  17.  
  18. // Equivalences :
  19. #define BIT0 0b00000001
  20. #define BIT1 0b00000010
  21. #define BIT2 0b00000100
  22. #define BIT3 0b00001000
  23. #define BIT4 0b00010000
  24. #define BIT5 0b00100000
  25. #define BIT6 0b01000000
  26. #define BIT7 0b10000000
  27.  
  28.  
  29. #define nv_lon_1h 24
  30. #define nv_lon_1l 21
  31. #define nv_lon_2h 24
  32.  
  33. #define nv_ron_1h 23
  34. #define nv_ron_1l 78
  35. #define nv_ron_2h 40
  36.  
  37. #define nv_loff_1h 43
  38.  
  39. #define nv_roff_1h 24
  40. #define nv_roff_1l 46
  41. #define nv_roff_2h 31
  42.  
  43. #define nv_off_1h 68
  44. #define nv_off_1l 450
  45. #define nv_off_2h 68
  46.  
  47. #define nv_darktime 500
  48.  
  49. #define nv_nbrpuls_l 4
  50. #define nv_nbrpuls_r 3
  51. #define nv_nbrpuls_off 2
  52.  
  53. //function prototypes :
  54. void out_left(void);
  55. void out_right(void);
  56. void out_off(void);
  57.  
  58. // constantes :
  59.  
  60. // definition des entrées/sorties :
  61. #define DDC_in ((PIND & BIT2) >> 2) //ddc in sur PORTD.2 (INT0)
  62. #define off_in ((PIND & BIT3) >> 3) //off_in sur PORTD.3
  63.  
  64. //variables globales :
  65. volatile unsigned char state_ddc_in;
  66. volatile unsigned int t_ddc_rise;
  67. volatile unsigned int t_ddc_fall;
  68. volatile unsigned int per_high;
  69. volatile unsigned int delay;
  70.  
  71. volatile unsigned int t[7];
  72. volatile unsigned char no_pulse=0;
  73. volatile unsigned char nbr_pulses=0;
  74.  
  75. // ============================== ZONE PROGRAMME ==============================
  76.  
  77. int main(void)
  78. {
  79. //Config portB
  80. DDRB=0b00000001; //PB0 en sortie
  81. PORTB=0x01;
  82.  
  83. //config timer1
  84. TCCR1A=0b00000000; //Mode normal
  85. TCCR1B=0b00000010; //clk timer1=clk/8 Fmin = 62Hz
  86.  
  87. //Configuration interruption externes
  88. MCUCR=0b00000001; //INT0 actif sur changement d'état
  89.  
  90. //Autorisation interruptions
  91. GICR=0b01000000; //Autorisation INT0
  92. TIMSK=0b00011000; //Autorisation interruption T2OVF, T1compare1A,T1compare1B, T0_OVF
  93. sei (); //Valide toutes les interruptions
  94.  
  95. //initialisation des variables
  96.  
  97. //message serie
  98.  
  99. while (1)
  100. {
  101. delay = per_high - 1000; //Avance de 1ms
  102. }
  103. }
  104.  
  105. void out_left(void)
  106. {
  107. //right off
  108. t[0]= nv_roff_1h-10; //pulse1 etat haut
  109. t[1]= nv_roff_1l-10; //pulse1 etat bas
  110. t[2]= nv_roff_2h-10; //pulse2 etat haut
  111. //dark time
  112. t[3]= nv_darktime-10;
  113. //lefton
  114. t[4]= nv_lon_1h-10; //pulse1 etat haut
  115. t[5]= nv_lon_1l-10; //pulse1 etat bas
  116. t[6]= nv_lon_2h-10; //pulse2 etat haut
  117. nbr_pulses= (nv_nbrpuls_l*2)-1;
  118.  
  119. PORTB &= ~BIT0; //IR_out à 1
  120. OCR1B=TCNT1+t[0]; //charger compare1B
  121. }
  122.  
  123. void out_right(void)
  124. {
  125. //left off
  126. t[0]= nv_loff_1h-10; //pulse1 etat haut
  127. //dark_time
  128. t[1]= nv_darktime-10;
  129. //right on
  130. t[2]= nv_ron_1h-10; //pulse1 etat haut
  131. t[3]= nv_ron_1l-10; //pulse1 etat bas
  132. t[4]= nv_ron_2h-10; //pulse2 etat haut
  133. nbr_pulses= (nv_nbrpuls_r*2)-1;
  134.  
  135. PORTB &= ~BIT0; //IR_out à 1
  136. OCR1B=TCNT1+t[0]; //charger compare1B
  137. }
  138.  
  139. void out_off(void)
  140. {
  141. t[0]= nv_off_1h-10; //pulse1 etat haut
  142. t[1]= nv_off_1l-10;
  143. t[2]= nv_off_2h-10; //pulse1 etat haut
  144. nbr_pulses= (nv_nbrpuls_off*2)-1;
  145.  
  146. PORTB &= ~BIT0; //IR_out à 1
  147. OCR1B=TCNT1+t[0]; //charger compare1B
  148. }
  149.  
  150. // Interruption INT0
  151. SIGNAL(SIG_INTERRUPT0)
  152. {
  153. if(DDC_in) //si front montant de DDC
  154. {
  155. t_ddc_rise = TCNT1;
  156. OCR1A = t_ddc_rise + delay;
  157. state_ddc_in = 1;
  158. }
  159. else //front descendant de DDC
  160. {
  161. t_ddc_fall = TCNT1;
  162.  
  163. if (t_ddc_rise > t_ddc_fall)
  164. per_high = (0xFFFF - t_ddc_rise) + t_ddc_fall;
  165.  
  166. else
  167. per_high = t_ddc_fall - t_ddc_rise;
  168.  
  169. OCR1A = t_ddc_fall + delay;
  170. state_ddc_in = 0; //delai sur front descendant
  171. }
  172. }
  173.  
  174. //compare Timer1A
  175. SIGNAL(SIG_OUTPUT_COMPARE1A)
  176. {
  177. if (delay != 0)
  178. {
  179. if(off_in == 0)
  180. out_off();
  181. else if(state_ddc_in)
  182. out_right();
  183. else
  184. out_left();
  185. }
  186. }
  187.  
  188. //compare Timer1B
  189. SIGNAL(SIG_OUTPUT_COMPARE1B)
  190. {
  191. if(no_pulse < nbr_pulses)
  192. {
  193. switch(no_pulse % 2)
  194. {
  195. case 0: //fin de pulse
  196. PORTB |= BIT0; //mettre PORTB.1 à 0
  197. no_pulse++;
  198. OCR1B=TCNT1+t[no_pulse]; //charger compare1B
  199. break;
  200.  
  201. case 1: //debut de pulse
  202. PORTB &= ~BIT0; //mettre PORTB.1 à 1
  203. no_pulse++;
  204. OCR1B=TCNT1+t[no_pulse]; //charger compare1B
  205. break;
  206.  
  207. default:
  208. break;
  209. }
  210. }
  211.  
  212. if( no_pulse == nbr_pulses) //émission terminée
  213. {
  214. nbr_pulses=0;
  215. no_pulse=0;
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement