Autor Tema: Pasar código de MPLAB X IDE XC8 a CCS C Compiler  (Leído 409 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado Alinav

  • PIC10
  • *
  • Mensajes: 1
Pasar código de MPLAB X IDE XC8 a CCS C Compiler
« en: 07 de Febrero de 2024, 00:27:33 »
He estado aprendiendo sobre el uso del MPLAB sin embargo me es complicado, por ello me gusta usar el CCS, sin embargo tengo un código XC8, podrían explicarme como en que pueden deferir para terminar de pasar de XC8 a CCS, por favor

Realmente me gustaría ayuda para algunas sentencias que son difíciles de entender así como lo que vendría a ser el uso de los timers y las interrupciones.


Código XC8
Código: C
  1. #pragma config PLLDIV = 2, CPUDIV = OSC1_PLL2, USBDIV = 2
  2. #pragma config FOSC = HSPLL_HS, FCMEN = OFF, IESO = OFF
  3. #pragma config PWRT = OFF, BOR = OFF, BORV = 3, VREGEN = OFF
  4. #pragma config WDT = OFF
  5. #pragma config WDTPS = 32768
  6. #pragma config CCP2MX = ON, PBADEN = OFF, LPT1OSC = OFF, MCLRE = ON
  7. #pragma config STVREN = ON, LVP = OFF, ICPRT = OFF, XINST = OFF
  8. #pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
  9. #pragma config CPB = OFF, CPD = OFF
  10. #pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
  11. #pragma config WRTC = OFF, WRTB = OFF, WRTD = OFF
  12. #pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
  13. #pragma config EBTRB = OFF
  14.  
  15. #define _XTAL_FREQ 48000000
  16. #include <xc.h>
  17. #include <stdint.h>
  18.  
  19. #define CLK_DIR     TRISBbits.RB0
  20. #define DATA1_DIR   TRISBbits.RB1
  21. #define DATA2_DIR   TRISBbits.RB2
  22. #define LATCH_DIR   TRISBbits.RB3
  23.  
  24. #define CLK_PIN     LATBbits.LB0
  25. #define DATA1_PIN   LATBbits.LB1
  26. #define DATA2_PIN   LATBbits.LB2
  27. #define LATCH_PIN   LATBbits.LB3
  28.  
  29. uint8_t estado = 0;
  30. uint8_t cont_main = 0;
  31. uint8_t cont_timer_1 = 0;
  32. uint8_t cont_peaton = 0;
  33. uint8_t cont_blink = 0;
  34. uint8_t flag_run = 0;
  35. uint16_t speed_run = 35536;
  36.  
  37. const uint8_t number_count[20][8] = {
  38.     0x88, 0xea, 0x8a, 0xba, 0x88, 0xff, 0xff, 0xff,    // 20
  39.     0x98, 0xda, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff,    // 19
  40.     0x98, 0xda, 0xd8, 0xda, 0x88, 0xff, 0xff, 0xff,    // 18
  41.     0x98, 0xde, 0xde, 0xde, 0x8e, 0xff, 0xff, 0xff,    // 17
  42.     0x98, 0xdb, 0xd8, 0xda, 0x88, 0xff, 0xff, 0xff,    // 16
  43.     0x98, 0xdb, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff,    // 15
  44.     0x9a, 0xda, 0xd8, 0xde, 0x8e, 0xff, 0xff, 0xff,    // 14
  45.     0x98, 0xde, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff,    // 13
  46.     0x98, 0xde, 0xd8, 0xdb, 0x88, 0xff, 0xff, 0xff,    // 12
  47.     0x99, 0xdd, 0xdd, 0xdd, 0x88, 0xff, 0xff, 0xff,    // 11
  48.     0x98, 0xda, 0xda, 0xda, 0x88, 0xff, 0xff, 0xff,    // 10
  49.     0x88, 0xaa, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff,    // 9
  50.     0x88, 0xaa, 0xa8, 0xaa, 0x88, 0xff, 0xff, 0xff,    // 8
  51.     0x88, 0xae, 0xae, 0xae, 0x8e, 0xff, 0xff, 0xff,    // 7
  52.     0x88, 0xab, 0xa8, 0xaa, 0x88, 0xff, 0xff, 0xff,    // 6
  53.     0x88, 0xab, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff,    // 5
  54.     0x8a, 0xaa, 0xa8, 0xae, 0x8e, 0xff, 0xff, 0xff,    // 4
  55.     0x88, 0xae, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff,    // 3
  56.     0x88, 0xae, 0xa8, 0xab, 0x88, 0xff, 0xff, 0xff,    // 2
  57.     0x89, 0xad, 0xad, 0xad, 0x88, 0xff, 0xff, 0xff     // 1
  58. };
  59.  
  60. const uint8_t peaton_run[4][8] = {
  61.     0xcf, 0xcf, 0xe7, 0xe7, 0xe7, 0xe7, 0xf7, 0xf7,
  62.     0xcf, 0xcf, 0xe7, 0xe3, 0xc3, 0xc7, 0xdb, 0xfb,
  63.     0xcf, 0xcf, 0xe3, 0x85, 0xe7, 0xdb, 0xdd, 0xdf,
  64.     0xcf, 0xcf, 0xe7, 0xe7, 0xc7, 0xe7, 0xe3, 0xef
  65. };
  66.  
  67. const uint8_t peaton_stop[8] = {0xe3, 0xe3, 0xf7, 0xe3, 0xd5, 0xf7, 0xeb, 0xeb};
  68. const uint8_t desp[8] = {0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F};
  69.  
  70. void Send_Byte_Data(uint8_t b_m1, uint8_t b_m2);
  71.  
  72. void main()
  73. {
  74.     ADCON1bits.PCFG = 0x0F;                 // Configura todos los pines como digitales
  75.     TRISAbits.RA0 = 0;                      // Pin RA0 como salida (led verde)
  76.     TRISAbits.RA1 = 0;                      // Pin RA1 como salida (led amarillo)
  77.     TRISAbits.RA2 = 0;                      // Pin RA2 como salida (led rojo)
  78.     CLK_DIR = 0;                            // Pin CLK
  79.     DATA1_DIR = 0;                          // Pin DATA (CI1)
  80.     DATA2_DIR = 0;                          // Pin DATA (CI2)
  81.     LATCH_DIR = 0;                          // Pin LATCH
  82.     LATAbits.LA0 = 0;
  83.     LATAbits.LA1 = 0;
  84.     LATAbits.LA2 = 0;
  85.     CLK_PIN = 0;
  86.     DATA1_PIN = 0;
  87.     DATA2_PIN = 0;
  88.     LATCH_PIN = 0;
  89.     TRISD = 0x00;                           // Puerto D como salida
  90.     LATD = 0x00;                            // Limpia le puerto D
  91.    
  92.     RCONbits.IPEN = 1;                      // Habilita los niveles de interrupcion
  93.     INTCONbits.GIEH = 1;                    // Habilita la prioridad alta en interrupciones
  94.     INTCONbits.GIEL = 1;                    // Habilita la prioridad baja en interrupciones
  95.     T0CON = 0x07;                           // Configuracion del timer 0
  96.     T1CON = 0xF8;                           // Configuracion del timer 1
  97.     INTCONbits.TMR0IE = 1;                  // Habilita la interrupcion del timer 0
  98.     INTCON2bits.TMR0IP = 1;                 // Alta prioridad para la interrupcion del timer 0
  99.     INTCONbits.TMR0IF = 0;                  // Flag 0 (timer 0)
  100.     PIE1bits.TMR1IE = 1;                    // Habilita la interrupcion del timer 1
  101.     IPR1bits.TMR1IP = 0;                    // Baja prioridad para la interrupcion del timer 1
  102.     PIR1bits.TMR1IF = 0;                    // Flag 0 (timer 1)
  103.     TMR0 = 18661;
  104.     TMR1 = 35536;
  105.     T0CONbits.TMR0ON = 1;                   // Habilita el timer 0
  106.     T1CONbits.TMR1ON = 1;                   // Habilita el timer 1
  107.    
  108.     while(1)
  109.     {
  110.         switch(estado)
  111.         {
  112.             case 0:
  113.                 LATAbits.LA0 = 0;
  114.                 LATAbits.LA1 = 0;
  115.                 LATAbits.LA2 = 1;
  116.                 for(uint8_t i=0; i<8; i++){
  117.                     Send_Byte_Data(peaton_stop[i], 0xFF);
  118.                     LATD = desp[i];
  119.                     __delay_us(800);
  120.                 }
  121.                 break;
  122.                
  123.             case 1:
  124.                 for(uint8_t i=0; i<8; i++){
  125.                     Send_Byte_Data(peaton_stop[i], 0xFF);
  126.                     LATD = desp[i];
  127.                     __delay_us(800);
  128.                 }
  129.                 cont_blink++;
  130.                 if(cont_blink == 80){
  131.                     LATAbits.LA2 = !LATAbits.LA2;
  132.                     cont_blink = 0;
  133.                 }
  134.                 break;
  135.                
  136.             case 2:
  137.                 cont_blink = 0;
  138.                 LATAbits.LA0 = 0;
  139.                 LATAbits.LA1 = 1;
  140.                 LATAbits.LA2 = 0;
  141.                 for(uint8_t i=0; i<8; i++){
  142.                     Send_Byte_Data(peaton_stop[i], 0xFF);
  143.                     LATD = desp[i];
  144.                     __delay_us(800);
  145.                 }
  146.                 break;
  147.                
  148.             case 3:
  149.                 LATAbits.LA0 = 1;
  150.                 LATAbits.LA1 = 0;
  151.                 LATAbits.LA2 = 0;
  152.                 for(uint8_t i=0; i<8; i++){
  153.                     Send_Byte_Data(number_count[cont_main][i], peaton_run[cont_peaton][i]);
  154.                     LATD = desp[i];
  155.                     __delay_us(800);
  156.                 }
  157.                 break;
  158.         }
  159.     }
  160. }
  161.  
  162. void __interrupt(high_priority) INT_TMR0()
  163. {
  164.     if(INTCONbits.TMR0IF == 1)
  165.     {
  166.         cont_main++;
  167.         if(cont_main == 5 && estado == 0){
  168.             cont_main = 0;
  169.             estado = 1;
  170.         }
  171.         else if(cont_main == 4 && estado == 1){
  172.             cont_main = 0;
  173.             estado = 2;
  174.         }
  175.         else if(cont_main == 1 && estado == 2){
  176.             cont_main = 0;
  177.             estado = 3;
  178.             flag_run = 1;
  179.         }
  180.         else if(cont_main == 17 && flag_run == 1){
  181.             speed_run = 50536;
  182.             flag_run = 0;
  183.         }
  184.         else if(cont_main == 20 && estado == 3){
  185.             cont_main = 0;
  186.             estado = 0;
  187.             speed_run = 35536;
  188.         }
  189.         TMR0 = 18661;
  190.         INTCONbits.TMR0IF = 0;
  191.     }
  192. }
  193.  
  194. void __interrupt(low_priority) INT_TMR1()
  195. {
  196.     if(PIR1bits.TMR1IF == 1)
  197.     {
  198.         cont_timer_1++;
  199.         if(cont_timer_1 == 9){
  200.             cont_timer_1 = 0;
  201.             cont_peaton++;
  202.             if(cont_peaton > 3){
  203.                 cont_peaton = 0;
  204.             }
  205.         }
  206.         TMR1 = speed_run;
  207.         PIR1bits.TMR1IF = 0;
  208.     }
  209. }
  210.  
  211. void Send_Byte_Data(uint8_t b_m1, uint8_t b_m2)
  212. {
  213.     for(uint8_t i=0x80; i>0; i=i>>1)
  214.     {
  215.         (b_m1 & i) ? (DATA1_PIN = 1) : (DATA1_PIN = 0);
  216.         (b_m2 & i) ? (DATA2_PIN = 1) : (DATA2_PIN = 0);
  217.         CLK_PIN = 1;
  218.         CLK_PIN = 0;
  219.     }
  220.     LATCH_PIN = 1;
  221.     LATCH_PIN = 0;
  222. }





Código CCS en desarrollo
Código: C
  1. #include <16F877A.h>
  2.  
  3. #FUSES HSPLL, NOCPUDIV, NOPBADEN, NODEBUG
  4. #use delay(clock=20M)
  5.  
  6. #define CLK_PIN     PIN_B0
  7. #define DATA1_PIN   PIN_B1
  8. #define DATA2_PIN   PIN_B2
  9. #define LATCH_PIN   PIN_B3
  10.  
  11. int estado = 0;
  12. int cont_main = 0;
  13. int cont_timer_1 = 0;
  14. int cont_peaton = 0;
  15. int cont_blink = 0;
  16. int flag_run = 0;
  17. int speed_run = 35536;
  18.  
  19. char const number_count[20][8] = {
  20.     0x88, 0xea, 0x8a, 0xba, 0x88, 0xff, 0xff, 0xff,    // 20
  21.     0x98, 0xda, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff,    // 19
  22.     0x98, 0xda, 0xd8, 0xda, 0x88, 0xff, 0xff, 0xff,    // 18
  23.     0x98, 0xde, 0xde, 0xde, 0x8e, 0xff, 0xff, 0xff,    // 17
  24.     0x98, 0xdb, 0xd8, 0xda, 0x88, 0xff, 0xff, 0xff,    // 16
  25.     0x98, 0xdb, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff,    // 15
  26.     0x9a, 0xda, 0xd8, 0xde, 0x8e, 0xff, 0xff, 0xff,    // 14
  27.     0x98, 0xde, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff,    // 13
  28.     0x98, 0xde, 0xd8, 0xdb, 0x88, 0xff, 0xff, 0xff,    // 12
  29.     0x99, 0xdd, 0xdd, 0xdd, 0x88, 0xff, 0xff, 0xff,    // 11
  30.     0x98, 0xda, 0xda, 0xda, 0x88, 0xff, 0xff, 0xff,    // 10
  31.     0x88, 0xaa, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff,    // 9
  32.     0x88, 0xaa, 0xa8, 0xaa, 0x88, 0xff, 0xff, 0xff,    // 8
  33.     0x88, 0xae, 0xae, 0xae, 0x8e, 0xff, 0xff, 0xff,    // 7
  34.     0x88, 0xab, 0xa8, 0xaa, 0x88, 0xff, 0xff, 0xff,    // 6
  35.     0x88, 0xab, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff,    // 5
  36.     0x8a, 0xaa, 0xa8, 0xae, 0x8e, 0xff, 0xff, 0xff,    // 4
  37.     0x88, 0xae, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff,    // 3
  38.     0x88, 0xae, 0xa8, 0xab, 0x88, 0xff, 0xff, 0xff,    // 2
  39.     0x89, 0xad, 0xad, 0xad, 0x88, 0xff, 0xff, 0xff     // 1
  40. };
  41.  
  42. char const peaton_run[4][8] = {
  43.     0xcf, 0xcf, 0xe7, 0xe7, 0xe7, 0xe7, 0xf7, 0xf7,
  44.     0xcf, 0xcf, 0xe7, 0xe3, 0xc3, 0xc7, 0xdb, 0xfb,
  45.     0xcf, 0xcf, 0xe3, 0x85, 0xe7, 0xdb, 0xdd, 0xdf,
  46.     0xcf, 0xcf, 0xe7, 0xe7, 0xc7, 0xe7, 0xe3, 0xef
  47. };
  48.  
  49. char const peaton_stop[8] = {0xe3, 0xe3, 0xf7, 0xe3, 0xd5, 0xf7, 0xeb, 0xeb};
  50. char const desp[8] = {0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F};
  51.  
  52. void Send_Byte_Data(int b_m1, int b_m2);
  53.  
  54.  
  55. #int_timer0
  56. void TIMER0_isr()
  57. {
  58.     if(get_timer0() == 0)
  59.     {
  60.         cont_main++;
  61.         if(cont_main == 5 && estado == 0)
  62.         {
  63.             cont_main = 0;
  64.             estado = 1;
  65.         }
  66.         else if(cont_main == 4 && estado == 1)
  67.         {
  68.             cont_main = 0;
  69.             estado = 2;
  70.         }
  71.         else if(cont_main == 1 && estado == 2)
  72.         {
  73.             cont_main = 0;
  74.             estado = 3;
  75.             flag_run = 1;
  76.         }
  77.         else if(cont_main == 17 && flag_run == 1)
  78.         {
  79.             speed_run = 50536;
  80.             flag_run = 0;
  81.         }
  82.         else if(cont_main == 20 && estado == 3)
  83.         {
  84.             cont_main = 0;
  85.             estado = 0;
  86.             speed_run = 35536;
  87.         }
  88.         set_timer0(18661);
  89.         clear_interrupt(INT_TIMER0);
  90.     }
  91. }
  92.  
  93. #int_timer1
  94. void TIMER1_isr()
  95. {
  96.     if(get_timer1() == 0)
  97.     {
  98.         cont_timer_1++;
  99.         if(cont_timer_1 == 9)
  100.         {
  101.             cont_timer_1 = 0;
  102.             cont_peaton++;
  103.             if(cont_peaton > 3)
  104.             {
  105.                 cont_peaton = 0;
  106.             }
  107.         }
  108.         set_timer1(speed_run);
  109.         clear_interrupt(INT_TIMER1);
  110.     }
  111. }
  112.  
  113. void Send_Byte_Data(int b_m1, int b_m2){
  114.     for(int i=0x80; i>0; i=i>>1)
  115.     {
  116.         (b_m1 & i) ? output_high(DATA1_PIN) : output_low(DATA1_PIN);
  117.         (b_m2 & i) ? output_high(DATA2_PIN) : output_low(DATA2_PIN);
  118.         output_high(CLK_PIN);
  119.         output_low(CLK_PIN);
  120.     }
  121.     output_high(LATCH_PIN);
  122.     output_low(LATCH_PIN);
  123. }
  124.  
  125. void main()
  126. {
  127.     setup_adc_ports(NO_ANALOGS);                 // Todos los pins digitales
  128.     set_tris_a(0b111000);                      // RA0, RA1, RA2 Outputs (led green, yellow, red)
  129.     output_a(0);                                // Limpiar A ports
  130.     set_tris_b(0b111000);                      // CLK, DATA1 and DATA2 Outputs
  131.     output_b(0);                                // Limpiar B ports
  132.     set_tris_d(0b00000000);                     // Port D Output
  133.     output_d(0);                                // Limpiar Port D
  134.    
  135.     enable_interrupts(GLOBAL);                  // Permitir high and low priority interrupts
  136.     setup_timer_0( /* confuso */ );         // Setup Timer0
  137.     enable_interrupts(INT_TIMER0);              // Permitir Timer0 interrupt
  138.     setup_timer_1( /* confuso */ );         // Setup Timer1
  139.     enable_interrupts(INT_TIMER1);              // Permitir Timer1 interrupt
  140.     set_timer0(18661);
  141.     set_timer1(35536);
  142.    
  143.     while(true)
  144.     {
  145.         switch(estado)
  146.         {
  147.             case 0:
  148.                 output_high(PIN_A2);
  149.                 output_low(PIN_A0);
  150.                 output_low(PIN_A1);
  151.                 for(uint8_t i=0; i<8; i++){
  152.                     Send_Byte_Data(peaton_stop[i], 0xFF);
  153.                     output_d(desp[i]);
  154.                     delay_us(800);
  155.                 }
  156.                 break;
  157.                
  158.             case 1:
  159.                         // TRABAJANDO
  160.         }
  161.     }
  162. }

Desconectado DominusDRR

  • PIC24H
  • ******
  • Mensajes: 1937
    • Sicoy
Re:Pasar código de MPLAB X IDE XC8 a CCS C Compiler
« Respuesta #1 en: 07 de Febrero de 2024, 12:26:12 »
Deberías indicar que partes "son difíciles" de entender.
Tengo una idea algo difusa sobre MPLAB Harmony, XC32 con PIC32