TODOPIC

Microcontroladores PIC => Lenguaje C para microcontroladores PIC => Mensaje iniciado por: fanta007 en 23 de Octubre de 2018, 18:02:24

Título: Ayuda con pwm pic18f47j53
Publicado por: fanta007 en 23 de Octubre de 2018, 18:02:24
Buenas tardes, es que tengo un problema con mi codigo, tengo que hacer una incubadora por la cual se controle un bombillo para calentar y un ventilador para enfriar, el bombillo se controla por pwm y el ventilador si se puede simplemente apagando y prendiendo, tras de eso tengo que mostrar en la lcd temperatura actual, la temperatura que quiera el usuario, la frecuencia que quiere el usuario que funcione y el duty, mi unico problema es que por alguna razon no puedo ni si quiera prender un led con pwm en el codigo y no se que sera, es lo unico que me falta pero no se que estoy escribiendo mal, porque al iniciar el codigo el prende pero nunca mas vuelve a prender pase lo que pase, muchas gracias por su ayuda de antemano
Código: CSS
  1. //****************************************************************************************
  2. //            
  3. //      barrer_teclado:    realiza un barrido del teclado y detecta si hay alguna tecla pulsada. La
  4. //      variable "Tecla" retorna con el código de la tecla pulsada o el 0x80 en caso
  5. //      de no haber ninguna.
  6. //
  7. //      esperar_teclado:   Esta funcion espera a que se presione una tecla y no retorna hasta que se halla pulsado una tecla.
  8. //
  9. //
  10. //          
  11. //                               TECLA        CODIGO
  12. //                                     ---------   -----
  13. //          |---|---|---|---       0   11101011 => tecla=PORTA&0b00001111 = 13
  14. //   RA5 -->| 1 | 2 | 3 | A |      1   11101110 => tecla=PORTA&0b00001111 = 14
  15. //          |---|---|---|---       2   11011101 => tecla=PORTA&0b00001111 = 13
  16. //   RE0 -->| 4 | 5 | 6 | B |      3   10111011 => tecla=PORTA&0b00001111 = 11
  17. //          |---|---|---|---       4   11101110 => tecla=PORTA&0b00001111 = 14
  18. //   RE1 -->| 7 | 8 | 9 | C |      5   11011101 => tecla=PORTA&0b00001111 = 13
  19. //          |---|---|---|---       6   10111011 => tecla=PORTA&0b00001111 = 11
  20. //   RE2 -->|0n | 0 | = | D |      7   11101110 => tecla=PORTA&0b00001111 = 14
  21. //          |---|---|---|---       8   01111101 => tecla=PORTA&0b00001111 = 13
  22. //           ^   ^   ^   ^         9   10111011 => tecla=PORTA&0b00001111 = 11
  23. //           |   |   |   |         A   01110111 => tecla=PORTA&0b00001111 = 7
  24. //           RA0 RA1 RA2 RA3       B   01110111 => tecla=PORTA&0b00001111 = 7
  25. //                                 C   01110111 => tecla=PORTA&0b00001111 = 7
  26. //                                 D   01110111 => tecla=PORTA&0b00001111 = 7
  27. //                                 #   10111011 => tecla=PORTA&0b00001111 = 11
  28. //                                 *   11101110 => tecla=PORTA&0b00001111 = 14
  29. //
  30. #include <18F47J53.h>
  31.  
  32.  
  33.  
  34. #device ADC=12
  35.  
  36. #FUSES NOWDT         //WDT disabled (enabled by SWDTEN bit)                    
  37. #FUSES PLL3          //Divide by 3 (12 MHz oscillator input)            
  38. #FUSES NOPLLEN       //PLL Disabled
  39. #FUSES NOSTVREN      //stack overflow/underflow reset enabled                
  40. #FUSES NOXINST       //Extended instruction set disabled            
  41. #FUSES NOCPUDIV      //No CPU system clock divide        
  42. #FUSES NOPROTECT     //Program memory is not code-protected          
  43. #FUSES HSPLL         //HS oscillator, PLL enabled, HSPLL used by USB          
  44. #FUSES SOSC_HIGH     //High Power T1OSC/SOSC circuit selected
  45. #FUSES CLOCKOUT      //CLKO output enabled on the RA6 pin
  46. #FUSES NOFCMEN       //Fail-Safe Clock Monitor disabled
  47. #FUSES NOIESO        //Two-Speed Start-up disabled
  48. #FUSES WDT32768      //Watchdog Postscaler 1:32768
  49. #FUSES DSWDTOSC_INT  //DSWDT uses INTOSC/INTRC as clock
  50. #FUSES RTCOSC_INT    //RTCC uses INTRC as clock
  51. #FUSES NODSBOR       //Zero-Power BOR disabled in Deep Sleep
  52. #FUSES NODSWDT       //Deep Sleep Watchdog Timer Disabled
  53. //#FUSES DSWDT8192     //Deep Sleep Watchdog Postscaler: 1:8,192 (8.5 seconds)  
  54. #FUSES NOIOL1WAY     //IOLOCK bit can be set and cleared
  55. #FUSES ADC12         //ADC 10 or 12 Bit Select:12 - Bit ADC Enabed
  56. #FUSES MSSPMSK7      //MSSP 7 Bit address masking
  57. #FUSES NOWPFP        //Write Protect Program Flash Page 0
  58. #FUSES NOWPCFG       //Write/Erase last page protect Disabled
  59. #FUSES WPDIS         //WPFP[5:0], WPEND, and WPCFG bits ignored
  60. #FUSES WPEND         //Start protection at page 0
  61. #FUSES LS48MHZ       //Low Speed USB mode with 48 MHz System clock at 48 MHz USB CLKEN divide-by is set to 8
  62. #use delay(clock=48000000)
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. #define LOADER_END   0xFFF                        
  70. #build(reset=LOADER_END+1, interrupt=LOADER_END+9)   //Protege posiciones de memoria desde la 0x0000 hasta la 0x1000  
  71. #org 0, LOADER_END {}
  72.  
  73. #bit PLLEN = 0xf9b.6
  74.  
  75. //#use  rs232(baud=9600,parity=N,UART1,bits=8,timeout=30)
  76.  
  77. #include "Configura_LCD_4-8_bits.c"
  78. /********************************************************/
  79. /*------- Espacio para declaracion de constantes  ------*/
  80. /********************************************************/
  81. #define   F1  PIN_A5
  82. #define   F2  PIN_E0
  83. #define   F3  PIN_E1
  84. #define   F4  PIN_E2
  85.                  
  86. #define   C4  PIN_B0
  87. #define   C3  PIN_A1
  88. #define   C2  PIN_A2
  89. #define   C1  PIN_A3
  90.  
  91.  #BIT Data_Pin= 0xF82.0 // PORTC
  92. #BIT Data_Pin_Direction = 0xF94.0 //TRIS C
  93. /********************************************************/
  94. /*--- Espacio para declaracion de variables globales  --*/
  95. /********************************************************/
  96.  
  97.  
  98. //char i=0,n=0 ;
  99. unsigned char tecla=0;
  100. int i=0,menu=0;
  101. int32 td=0,p4=0,frecuencia=0,potenciometro,lecturafinal,duty1=0;
  102. //!unsigned char Check;
  103. //!unsigned char uniT=0;,decT=0,uniHR=0,decHR=0;
  104. //!unsigned char T_byte1,T_byte2,RH_byte1,RH_byte2;
  105. //!unsigned sum;
  106. char message1[] = "Tm=00.0C";
  107. char message2[] = "Td=00.0C";
  108. short Time_out;
  109. unsigned int8 T_byte1, T_byte2, RH_byte1, RH_byte2, CheckSum ;
  110.  
  111.  
  112. //!int32 conconclave=0,clave=0,contiempo=0,contiempo1=0,x;
  113. //!int32 retardo=0,ceros=0000;clave=0,clave=0,contiempo=0,contiempo1=0,x;
  114. //!int32 retardo=0,ceros=0000;
  115. /********************************************************/
  116. /********************************************************/
  117. /*-------------- Espacio para funciones  ---------------*/
  118. /********************************************************/
  119.  
  120. #include <stdlib.h> // for atoi32
  121.  
  122.  
  123.  
  124.  
  125. /********************************************************/
  126. /********************************************************/
  127. unsigned char barrer_teclado (void)
  128. {      
  129.           unsigned char tecla=15;
  130.         delay_ms(30);
  131.  
  132.         output_low(F1);
  133.          
  134.         tecla=  (input_b()&0x01)+(input_a()&0x0e);
  135.         if(tecla!=15){
  136.         output_high(F1);
  137.         delay_ms(200);
  138.         if(tecla==7)return('+');
  139.         if(tecla==11)return(3);
  140.       if(tecla==13)return(2);
  141.       if(tecla==14)return(1);
  142.         }
  143.         output_high(F1);
  144.        
  145.         output_low(F2);
  146.      
  147.        tecla=   (input_b()&0x01)+(input_a()&0x0e);
  148.         if(tecla!=15){
  149.       output_high(F2);
  150.       delay_ms(200);
  151.         if(tecla==7)return('-');
  152.         if(tecla==11)return(6);
  153.       if(tecla==13)return(5);
  154.       if(tecla==14)return(4);
  155.         }
  156.         output_high(F2);
  157.          
  158.       output_low(F3);
  159.      
  160.         tecla=  (input_b()&0x01)+(input_a()&0x0e);
  161.         if(tecla!=15){
  162.       output_high(F3);
  163.        delay_ms(200);
  164.         if(tecla==7)return('/');
  165.         if(tecla==11)return(9);
  166.       if(tecla==13)return(8);
  167.       if(tecla==14)return(7);
  168.         }
  169.         output_high(F3);
  170.        
  171.       output_low(F4);
  172.        
  173.         tecla=  (input_b()&0x01)+(input_a()&0x0e);
  174.          if(tecla!=15){
  175.        output_high(F4);
  176.         delay_ms(200);      
  177.         if(tecla==7)return('X');
  178.         if(tecla==11)return('=');
  179.       if(tecla==13)return(0);
  180.       if(tecla==14)return('.');
  181.         }
  182.         output_high(F4);
  183.  
  184.          
  185.        return(0x80);
  186.        
  187. }
  188. /********************************************************/
  189. /********************************************************/
  190.  
  191. unsigned char esperar_teclado (void)
  192. {      
  193.         unsigned char tecla=0x80;
  194.         while(tecla==0x80)tecla=barrer_teclado ();
  195.         return(tecla);
  196. }
  197. void start_signal(){
  198.   Data_Pin_Direction = 0;              // Configure connection pin as output
  199.   Data_Pin = 0;                        // Connection pin output low
  200.   delay_ms(25);
  201.   Data_Pin = 1;                        // Connection pin output high
  202.   delay_us(30);
  203.   Data_Pin_Direction = 1;              // Configure connection pin as input
  204. }
  205. short check_response(){
  206.   delay_us(40);
  207.   if(!Data_Pin){                     // Read and test if connection pin is low
  208.     delay_us(80);
  209.     if(Data_Pin){                    // Read and test if connection pin is high
  210.       delay_us(50);
  211.       return 1;}
  212.     }
  213. }
  214. unsigned int8 Read_Data(){
  215.   unsigned int8 i, k, _data = 0;     // k is used to count 1 bit reading duration
  216.   if(Time_out)
  217.     break;
  218.   for(i = 0; i < 8; i++){
  219.     k = 0;
  220.     while(!Data_Pin){                          // Wait until pin goes high
  221.       k++;
  222.       if (k > 100) {Time_out = 1; break;}
  223.       delay_us(1);}
  224.     delay_us(30);
  225.     if(!Data_Pin)
  226.       bit_clear(_data, (7 - i));               // Clear bit (7 - i)
  227.     else{
  228.       bit_set(_data, (7 - i));                 // Set bit (7 - i)
  229.       while(Data_Pin){                         // Wait until pin goes low
  230.       k++;
  231.       if (k > 100) {Time_out = 1; break;}
  232.       delay_us(1);}
  233.     }
  234.   }
  235.   return _data;
  236. }
  237.  
  238.  
  239. /******************************************************************************/
  240. /******************************************************************************/
  241. /*--------------------- Espacio de codigo principal --------------------------*/
  242. /******************************************************************************/
  243.  
  244.  
  245. #zero_ram
  246. void main(){
  247. PLLEN = 1;          
  248.    
  249.  lcd_init_4bits();   // Comandos de inicialización del LCD.
  250. setup_adc(ADC_CLOCK_INTERNAL);
  251. setup_adc_ports(sAN0);
  252. set_adc_channel(0);
  253.  setup_timer_2(T2_DIV_BY_16,29,1); // PWM
  254.   setup_ccp4(CCP_PWM);
  255.  for(;;){
  256.  lcd_init_4bits(); // Comandos de inicialización del LCD.
  257.  printf(lcd_putc_4bits,"\fBienvenido\nquerido usuario");
  258.     delay_ms(1000);
  259.    printf(lcd_putc_4bits,"\fIngrese la\ntemperatura");
  260.    delay_ms(1000);
  261.    //Capturar temperatura deseada
  262.  while(menu==0){
  263.  tecla= esperar_teclado ();
  264. delay_ms(100);
  265. if (tecla>=0 && tecla<=9) // condicion para cuando se oprime un numero en el teclado
  266. {
  267. td=(td*10)+tecla; //acumula y va guardando el numero
  268. printf(lcd_putc_4bits,"\f %Lu",td);
  269. delay_ms(200);
  270. }
  271. if (tecla=='/')
  272. {
  273. lcd_putc_4bits(tecla);
  274. menu=1;
  275. delay_ms(200);
  276. }
  277.  }
  278.    printf(lcd_putc_4bits,"\fIngrese la\nfrecuencia");
  279.    delay_ms(1000);
  280.  // Capturar frecuencia
  281.   while(menu==1){
  282.  tecla= esperar_teclado ();
  283. delay_ms(100);
  284. if (tecla>=0 && tecla<=9) // condicion para cuando se oprime un numero en el teclado
  285. {
  286. frecuencia=(frecuencia*10)+tecla; //acumula y va guardando el numero
  287. printf(lcd_putc_4bits,"\f %Lu",frecuencia);
  288. delay_ms(200);
  289. }
  290. if (tecla=='/')
  291. {
  292. lcd_putc_4bits(tecla);
  293. menu=2;
  294. delay_ms(200);
  295. }
  296.  }
  297.  //Control del bombillo y ventilador
  298.  //Mostrar en la LCD
  299.  while(menu==2){
  300.   delay_ms(1000);
  301.     Time_out = 0;
  302.     Start_signal();
  303. //lectura de temperatura
  304.     if(check_response()){                    // If there is response from sensor
  305.       RH_byte1 = Read_Data();                 // read RH byte1
  306.       RH_byte2 = Read_Data();                 // read RH byte2
  307.       T_byte1 = Read_Data();                  // read T byte1
  308.       T_byte2 = Read_Data();                  // read T byte2
  309.       Checksum = Read_Data();
  310.         if(Time_out){                           // If reading takes long time
  311.             lcd_ubicaxy_4bits(1,1);
  312.    printf(lcd_putc_4bits,"\fTime out!");
  313.       }
  314.       else{
  315.       //Control de potencia
  316.       if(T_byte1>td){
  317.       set_pwm4_duty(0);
  318.       output_high(PIN_B3);
  319.       duty1=0;
  320.       }
  321.        if(T_byte1<td){
  322.       output_low(PIN_B3);
  323.        #USE PWM(OUTPUT=PIN_B4,FREQUENCY=10kHz,Duty=100)
  324.        setup_ccp4(CCP_PWM);
  325.       pwm_set_frequency(frecuencia);
  326.       set_pwm4_duty(100);
  327.       duty1=100;
  328.       }
  329.       if(T_byte1==td){
  330.       output_low(PIN_B3);
  331.       set_pwm4_duty(0);
  332.       duty1=0;
  333.       }
  334.       //mostrar mensaje
  335.        if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)){
  336.         message1[3]  = T_Byte1/10  + 48;
  337.         message1[4]  = T_Byte1%10  + 48;
  338.         message2[3]  = td/10 + 48;
  339.         message2[4]  = td%10 + 48;
  340.         message1[7] = 223;                   // Degree symbol
  341.         message2[7] = 223;  
  342.         lcd_ubicaxy_4bits(1,1);
  343.    printf(lcd_putc_4bits,"%s",message1);
  344.    delay_ms(200);
  345.    lcd_ubicaxy_4bits(9,1);
  346.    printf(lcd_putc_4bits,"%s",message2);
  347.    delay_ms(200);
  348.    lcd_ubicaxy_4bits(1,2);
  349.       printf(lcd_putc_4bits,"Fr=%Lu",frecuencia);
  350.    delay_ms(200);
  351.    lcd_ubicaxy_4bits(9,2);
  352.     printf(lcd_putc_4bits,"Dut=%Lu",duty1);
  353.        delay_ms(200);
  354.    lcd_ubicaxy_4bits(16,2);
  355.     printf(lcd_putc_4bits,"%c",37);
  356. //!   printf(lcd_putc_4bits,"\f%s",message2);
  357. //!   delay_ms(500);
  358.        }
  359.        else{
  360.             lcd_ubicaxy_4bits(1,1);
  361.    printf(lcd_putc_4bits,"\fCheck error");
  362.        }
  363.       }
  364.     }
  365.     else{
  366.             lcd_ubicaxy_4bits(1,1);
  367.    printf(lcd_putc_4bits,"\fNo respuesta");
  368.    lcd_ubicaxy_4bits(1,2);
  369.    printf(lcd_putc_4bits,"\fDel sensor");
  370.     }
  371.  }
  372.  }
  373.  
  374.  
  375.  
  376.  
  377.  
  378. }