Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. /*
  2. * File: seguidor.c
  3. * Author: OrlandoCarvalho
  4. *
  5. * Created on 1 de Novembro de 2019, 20:26
  6. */
  7.  
  8. /*
  9. * File: lightfallow.c
  10. * Author: OrlandoCarvalho
  11. *
  12. * Created on 1 de Novembro de 2019, 19:52
  13. */
  14.  
  15. // CONFIG1H
  16. #pragma config OSC = IRCIO67 // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
  17. #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
  18. #pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
  19.  
  20. // CONFIG2L
  21. #pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
  22. #pragma config BOREN = BOHW // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
  23. #pragma config BORV = 3 // Brown-out Reset Voltage bits (VBOR set to 2.1V)
  24.  
  25. // CONFIG2H
  26. #pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
  27. #pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
  28.  
  29. // CONFIG3H
  30. #pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
  31. #pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
  32. #pragma config MCLRE = OFF // MCLR Pin Enable bit (RE3 input pin enabled; MCLR disabled)
  33.  
  34. // CONFIG4L
  35. #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
  36. #pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
  37. #pragma config BBSIZ = 1024 // Boot Block Size Select bit (1K words (2K bytes) boot block)
  38. #pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
  39.  
  40. // CONFIG5L
  41. #pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
  42. #pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
  43. #pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
  44. #pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)
  45.  
  46. // CONFIG5H
  47. #pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
  48. #pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
  49.  
  50. // CONFIG6L
  51. #pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
  52. #pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
  53. #pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
  54. #pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)
  55.  
  56. // CONFIG6H
  57. #pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
  58. #pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
  59. #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
  60.  
  61. // CONFIG7L
  62. #pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
  63. #pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
  64. #pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
  65. #pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)
  66.  
  67. // CONFIG7H
  68. #pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)
  69.  
  70.  
  71. #include <xc.h>
  72. #include <pic18f4580.h>
  73.  
  74. #define TRUE 1
  75. #define _XTAL_FREQ 20000000 // frequencia 20mhz
  76. #define ADC_RANGE 1024
  77. #define BITS 8
  78.  
  79. float result;
  80. char estado;
  81. char n;
  82.  
  83. void main(void) {
  84. OSCCON = 0x73; // Internal Oscillator Frequency 8MHz
  85. ADCON1bits.PCFG = 0xF; //CONFIGURA TODAS AS SAIDAS COMO DIGITAIS
  86. TRISA = 0b00000010; // Set RA1/AN1 as input
  87. TRISB = 0b00000000;
  88. PORTB=0x00;
  89. LATB = 0x00;
  90.  
  91. ADCON1bits.PCFG = 0b1101; // Set AN1 as analog
  92. ADCON1bits.VCFG = 0b00; // Set Vref+ as Vdd and Vref- as Vss
  93. ADCON0bits.CHS = 0b0001; // Set ADC input channel as AN1
  94. ADCON2bits.ADCS = 0b110; // Set TAD to 3.2us, > 2us.
  95. ADCON2bits.ACQT = 0b010; // Set acquisition time to 4xTAD=12.8us (0 to 50oC).
  96. ADCON2bits.ADFM = 1; // Get the result right formated.
  97. ADCON0bits.ADON = 1; // Turn the ADC ON.
  98. ADCON0bits.GO = 1;
  99.  
  100. while(TRUE);
  101. }
  102.  
  103. /* ligar leds por cast
  104. while(TRUE)
  105. {
  106. __delay_us(100000); // A delay of 100ms will set a sampling
  107. // frequency of 10Hz.
  108. ADCON0bits.GO = 1;
  109. while(ADCON0bits.GO);
  110. result = (ADRESH<<8)+ADRESL;
  111. char i;
  112. char estado = 0xFF;
  113. char n = (char) (result/(ADC_RANGE/(BITS + 1)));
  114.  
  115. for(i=0; i<n; i++){
  116. estado = estado << 1;
  117. }
  118. LATB =~ estado;
  119. */
  120.  
  121. /* Ligacao de led por niveis em ifs
  122. if(result <=10){
  123. PORTB=0b00000000;
  124. }
  125. if(result >10 && result <=128){
  126. PORTB = 0b00000001;
  127. }
  128. if(result >128 && result <=256){
  129. PORTB = 0b00000011;
  130. }
  131. if(result >256 && result <=384){
  132. PORTB = 0b00000111;
  133. }
  134. if(result >384 && result <=512){
  135. PORTB = 0b00001111;
  136. }
  137. if(result >512 && result <=640){
  138. PORTB = 0b00011111;
  139. }
  140. if(result >640 && result <=768){
  141. PORTB = 0b00111111;
  142. }
  143. if(result >768 && result <=896){
  144. PORTB = 0b01111111;
  145. }
  146. if(result >896 && result <=1024){
  147. PORTB = 0b11111111;
  148. }
  149. }
  150. // 1024 / 8 = 128
  151. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement