CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Pasar código de MPLAB X IDE XC8 a CCS C Compiler

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
D_Fisher



Joined: 24 Jul 2023
Posts: 3

View user's profile Send private message

Pasar código de MPLAB X IDE XC8 a CCS C Compiler
PostPosted: Tue Feb 06, 2024 8:43 pm     Reply with quote

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
Code:

Pasa el siguiente código a CCS Compiler:

#pragma config PLLDIV = 2, CPUDIV = OSC1_PLL2, USBDIV = 2
#pragma config FOSC = HSPLL_HS, FCMEN = OFF, IESO = OFF
#pragma config PWRT = OFF, BOR = OFF, BORV = 3, VREGEN = OFF
#pragma config WDT = OFF
#pragma config WDTPS = 32768
#pragma config CCP2MX = ON, PBADEN = OFF, LPT1OSC = OFF, MCLRE = ON
#pragma config STVREN = ON, LVP = OFF, ICPRT = OFF, XINST = OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CPB = OFF, CPD = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTC = OFF, WRTB = OFF, WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTRB = OFF

#define _XTAL_FREQ 48000000
#include <xc.h>
#include <stdint.h>

#define CLK_DIR     TRISBbits.RB0
#define DATA1_DIR   TRISBbits.RB1
#define DATA2_DIR   TRISBbits.RB2
#define LATCH_DIR   TRISBbits.RB3

#define CLK_PIN     LATBbits.LB0
#define DATA1_PIN   LATBbits.LB1
#define DATA2_PIN   LATBbits.LB2
#define LATCH_PIN   LATBbits.LB3

uint8_t estado = 0;
uint8_t cont_main = 0;
uint8_t cont_timer_1 = 0;
uint8_t cont_peaton = 0;
uint8_t cont_blink = 0;
uint8_t flag_run = 0;
uint16_t speed_run = 35536;

const uint8_t number_count[20][8] = {
    0x88, 0xea, 0x8a, 0xba, 0x88, 0xff, 0xff, 0xff,    // 20
    0x98, 0xda, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff,    // 19
    0x98, 0xda, 0xd8, 0xda, 0x88, 0xff, 0xff, 0xff,    // 18
    0x98, 0xde, 0xde, 0xde, 0x8e, 0xff, 0xff, 0xff,    // 17
    0x98, 0xdb, 0xd8, 0xda, 0x88, 0xff, 0xff, 0xff,    // 16
    0x98, 0xdb, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff,    // 15
    0x9a, 0xda, 0xd8, 0xde, 0x8e, 0xff, 0xff, 0xff,    // 14
    0x98, 0xde, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff,    // 13
    0x98, 0xde, 0xd8, 0xdb, 0x88, 0xff, 0xff, 0xff,    // 12
    0x99, 0xdd, 0xdd, 0xdd, 0x88, 0xff, 0xff, 0xff,    // 11
    0x98, 0xda, 0xda, 0xda, 0x88, 0xff, 0xff, 0xff,    // 10
    0x88, 0xaa, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff,    // 9
    0x88, 0xaa, 0xa8, 0xaa, 0x88, 0xff, 0xff, 0xff,    // 8
    0x88, 0xae, 0xae, 0xae, 0x8e, 0xff, 0xff, 0xff,    // 7
    0x88, 0xab, 0xa8, 0xaa, 0x88, 0xff, 0xff, 0xff,    // 6
    0x88, 0xab, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff,    // 5
    0x8a, 0xaa, 0xa8, 0xae, 0x8e, 0xff, 0xff, 0xff,    // 4
    0x88, 0xae, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff,    // 3
    0x88, 0xae, 0xa8, 0xab, 0x88, 0xff, 0xff, 0xff,    // 2
    0x89, 0xad, 0xad, 0xad, 0x88, 0xff, 0xff, 0xff     // 1
};

const uint8_t peaton_run[4][8] = {
    0xcf, 0xcf, 0xe7, 0xe7, 0xe7, 0xe7, 0xf7, 0xf7,
    0xcf, 0xcf, 0xe7, 0xe3, 0xc3, 0xc7, 0xdb, 0xfb,
    0xcf, 0xcf, 0xe3, 0x85, 0xe7, 0xdb, 0xdd, 0xdf,
    0xcf, 0xcf, 0xe7, 0xe7, 0xc7, 0xe7, 0xe3, 0xef
};

const uint8_t peaton_stop[8] = {0xe3, 0xe3, 0xf7, 0xe3, 0xd5, 0xf7, 0xeb, 0xeb};
const uint8_t desp[8] = {0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F};

void Send_Byte_Data(uint8_t b_m1, uint8_t b_m2);

void main()
{
    ADCON1bits.PCFG = 0x0F;                 // Configura todos los pines como digitales
    TRISAbits.RA0 = 0;                      // Pin RA0 como salida (led verde)
    TRISAbits.RA1 = 0;                      // Pin RA1 como salida (led amarillo)
    TRISAbits.RA2 = 0;                      // Pin RA2 como salida (led rojo)
    CLK_DIR = 0;                            // Pin CLK
    DATA1_DIR = 0;                          // Pin DATA (CI1)
    DATA2_DIR = 0;                          // Pin DATA (CI2)
    LATCH_DIR = 0;                          // Pin LATCH
    LATAbits.LA0 = 0;
    LATAbits.LA1 = 0;
    LATAbits.LA2 = 0;
    CLK_PIN = 0;
    DATA1_PIN = 0;
    DATA2_PIN = 0;
    LATCH_PIN = 0;
    TRISD = 0x00;                           // Puerto D como salida
    LATD = 0x00;                            // Limpia le puerto D
   
    RCONbits.IPEN = 1;                      // Habilita los niveles de interrupcion
    INTCONbits.GIEH = 1;                    // Habilita la prioridad alta en interrupciones
    INTCONbits.GIEL = 1;                    // Habilita la prioridad baja en interrupciones
    T0CON = 0x07;                           // Configuracion del timer 0
    T1CON = 0xF8;                           // Configuracion del timer 1
    INTCONbits.TMR0IE = 1;                  // Habilita la interrupcion del timer 0
    INTCON2bits.TMR0IP = 1;                 // Alta prioridad para la interrupcion del timer 0
    INTCONbits.TMR0IF = 0;                  // Flag 0 (timer 0)
    PIE1bits.TMR1IE = 1;                    // Habilita la interrupcion del timer 1
    IPR1bits.TMR1IP = 0;                    // Baja prioridad para la interrupcion del timer 1
    PIR1bits.TMR1IF = 0;                    // Flag 0 (timer 1)
    TMR0 = 18661;
    TMR1 = 35536;
    T0CONbits.TMR0ON = 1;                   // Habilita el timer 0
    T1CONbits.TMR1ON = 1;                   // Habilita el timer 1
   
    while(1)
    {
        switch(estado)
        {
            case 0:
                LATAbits.LA0 = 0;
                LATAbits.LA1 = 0;
                LATAbits.LA2 = 1;
                for(uint8_t i=0; i<8; i++){
                    Send_Byte_Data(peaton_stop[i], 0xFF);
                    LATD = desp[i];
                    __delay_us(800);
                }
                break;
               
            case 1:
                for(uint8_t i=0; i<8; i++){
                    Send_Byte_Data(peaton_stop[i], 0xFF);
                    LATD = desp[i];
                    __delay_us(800);
                }
                cont_blink++;
                if(cont_blink == 80){
                    LATAbits.LA2 = !LATAbits.LA2;
                    cont_blink = 0;
                }
                break;
               
            case 2:
                cont_blink = 0;
                LATAbits.LA0 = 0;
                LATAbits.LA1 = 1;
                LATAbits.LA2 = 0;
                for(uint8_t i=0; i<8; i++){
                    Send_Byte_Data(peaton_stop[i], 0xFF);
                    LATD = desp[i];
                    __delay_us(800);
                }
                break;
               
            case 3:
                LATAbits.LA0 = 1;
                LATAbits.LA1 = 0;
                LATAbits.LA2 = 0;
                for(uint8_t i=0; i<8; i++){
                    Send_Byte_Data(number_count[cont_main][i], peaton_run[cont_peaton][i]);
                    LATD = desp[i];
                    __delay_us(800);
                }
                break;
        }
    }
}

void __interrupt(high_priority) INT_TMR0()
{
    if(INTCONbits.TMR0IF == 1)
    {
        cont_main++;
        if(cont_main == 5 && estado == 0){
            cont_main = 0;
            estado = 1;
        }
        else if(cont_main == 4 && estado == 1){
            cont_main = 0;
            estado = 2;
        }
        else if(cont_main == 1 && estado == 2){
            cont_main = 0;
            estado = 3;
            flag_run = 1;
        }
        else if(cont_main == 17 && flag_run == 1){
            speed_run = 50536;
            flag_run = 0;
        }
        else if(cont_main == 20 && estado == 3){
            cont_main = 0;
            estado = 0;
            speed_run = 35536;
        }
        TMR0 = 18661;
        INTCONbits.TMR0IF = 0;
    }
}

void __interrupt(low_priority) INT_TMR1()
{
    if(PIR1bits.TMR1IF == 1)
    {
        cont_timer_1++;
        if(cont_timer_1 == 9){
            cont_timer_1 = 0;
            cont_peaton++;
            if(cont_peaton > 3){
                cont_peaton = 0;
            }
        }
        TMR1 = speed_run;
        PIR1bits.TMR1IF = 0;
    }
}

void Send_Byte_Data(uint8_t b_m1, uint8_t b_m2)
{
    for(uint8_t i=0x80; i>0; i=i>>1)
    {
        (b_m1 & i) ? (DATA1_PIN = 1) : (DATA1_PIN = 0);
        (b_m2 & i) ? (DATA2_PIN = 1) : (DATA2_PIN = 0);
        CLK_PIN = 1;
        CLK_PIN = 0;
    }
    LATCH_PIN = 1;
    LATCH_PIN = 0;
}






Código CCS en desarrollo
Code:

#include <16F877A.h>

#FUSES HSPLL, NOCPUDIV, NOPBADEN, NODEBUG
#use delay(clock=20M)

#define CLK_PIN     PIN_B0
#define DATA1_PIN   PIN_B1
#define DATA2_PIN   PIN_B2
#define LATCH_PIN   PIN_B3

int estado = 0;
int cont_main = 0;
int cont_timer_1 = 0;
int cont_peaton = 0;
int cont_blink = 0;
int flag_run = 0;
int speed_run = 35536;

char const number_count[20][8] = {
    0x88, 0xea, 0x8a, 0xba, 0x88, 0xff, 0xff, 0xff,    // 20
    0x98, 0xda, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff,    // 19
    0x98, 0xda, 0xd8, 0xda, 0x88, 0xff, 0xff, 0xff,    // 18
    0x98, 0xde, 0xde, 0xde, 0x8e, 0xff, 0xff, 0xff,    // 17
    0x98, 0xdb, 0xd8, 0xda, 0x88, 0xff, 0xff, 0xff,    // 16
    0x98, 0xdb, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff,    // 15
    0x9a, 0xda, 0xd8, 0xde, 0x8e, 0xff, 0xff, 0xff,    // 14
    0x98, 0xde, 0xd8, 0xde, 0x88, 0xff, 0xff, 0xff,    // 13
    0x98, 0xde, 0xd8, 0xdb, 0x88, 0xff, 0xff, 0xff,    // 12
    0x99, 0xdd, 0xdd, 0xdd, 0x88, 0xff, 0xff, 0xff,    // 11
    0x98, 0xda, 0xda, 0xda, 0x88, 0xff, 0xff, 0xff,    // 10
    0x88, 0xaa, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff,    // 9
    0x88, 0xaa, 0xa8, 0xaa, 0x88, 0xff, 0xff, 0xff,    // 8
    0x88, 0xae, 0xae, 0xae, 0x8e, 0xff, 0xff, 0xff,    // 7
    0x88, 0xab, 0xa8, 0xaa, 0x88, 0xff, 0xff, 0xff,    // 6
    0x88, 0xab, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff,    // 5
    0x8a, 0xaa, 0xa8, 0xae, 0x8e, 0xff, 0xff, 0xff,    // 4
    0x88, 0xae, 0xa8, 0xae, 0x88, 0xff, 0xff, 0xff,    // 3
    0x88, 0xae, 0xa8, 0xab, 0x88, 0xff, 0xff, 0xff,    // 2
    0x89, 0xad, 0xad, 0xad, 0x88, 0xff, 0xff, 0xff     // 1
};

char const peaton_run[4][8] = {
    0xcf, 0xcf, 0xe7, 0xe7, 0xe7, 0xe7, 0xf7, 0xf7,
    0xcf, 0xcf, 0xe7, 0xe3, 0xc3, 0xc7, 0xdb, 0xfb,
    0xcf, 0xcf, 0xe3, 0x85, 0xe7, 0xdb, 0xdd, 0xdf,
    0xcf, 0xcf, 0xe7, 0xe7, 0xc7, 0xe7, 0xe3, 0xef
};

char const peaton_stop[8] = {0xe3, 0xe3, 0xf7, 0xe3, 0xd5, 0xf7, 0xeb, 0xeb};
char const desp[8] = {0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F};

void Send_Byte_Data(int b_m1, int b_m2);


#int_timer0
void TIMER0_isr()
{
    if(get_timer0() == 0)
    {
        cont_main++;
        if(cont_main == 5 && estado == 0)
        {
            cont_main = 0;
            estado = 1;
        }
        else if(cont_main == 4 && estado == 1)
        {
            cont_main = 0;
            estado = 2;
        }
        else if(cont_main == 1 && estado == 2)
        {
            cont_main = 0;
            estado = 3;
            flag_run = 1;
        }
        else if(cont_main == 17 && flag_run == 1)
        {
            speed_run = 50536;
            flag_run = 0;
        }
        else if(cont_main == 20 && estado == 3)
        {
            cont_main = 0;
            estado = 0;
            speed_run = 35536;
        }
        set_timer0(18661);
        clear_interrupt(INT_TIMER0);
    }
}

#int_timer1
void TIMER1_isr()
{
    if(get_timer1() == 0)
    {
        cont_timer_1++;
        if(cont_timer_1 == 9)
        {
            cont_timer_1 = 0;
            cont_peaton++;
            if(cont_peaton > 3)
            {
                cont_peaton = 0;
            }
        }
        set_timer1(speed_run);
        clear_interrupt(INT_TIMER1);
    }
}

void Send_Byte_Data(int b_m1, int b_m2){
    for(int i=0x80; i>0; i=i>>1)
    {
        (b_m1 & i) ? output_high(DATA1_PIN) : output_low(DATA1_PIN);
        (b_m2 & i) ? output_high(DATA2_PIN) : output_low(DATA2_PIN);
        output_high(CLK_PIN);
        output_low(CLK_PIN);
    }
    output_high(LATCH_PIN);
    output_low(LATCH_PIN);
}

void main()
{
    setup_adc_ports(NO_ANALOGS);                 // Todos los pins digitales
    set_tris_a(0b111000);                      // RA0, RA1, RA2 Outputs (led green, yellow, red)
    output_a(0);                                // Limpiar A ports
    set_tris_b(0b111000);                      // CLK, DATA1 and DATA2 Outputs
    output_b(0);                                // Limpiar B ports
    set_tris_d(0b00000000);                     // Port D Output
    output_d(0);                                // Limpiar Port D
   
    enable_interrupts(GLOBAL);                  // Permitir high and low priority interrupts
    setup_timer_0( /* confuso */ );         // Setup Timer0
    enable_interrupts(INT_TIMER0);              // Permitir Timer0 interrupt
    setup_timer_1( /* confuso */ );         // Setup Timer1
    enable_interrupts(INT_TIMER1);              // Permitir Timer1 interrupt
    set_timer0(18661);
    set_timer1(35536);
   
    while(true)
    {
        switch(estado)
        {
            case 0:
                output_high(PIN_A2);
                output_low(PIN_A0);
                output_low(PIN_A1);
                for(uint8_t i=0; i<8; i++){
                    Send_Byte_Data(peaton_stop[i], 0xFF);
                    output_d(desp[i]);
                    delay_us(800);
                }
                break;
               
            case 1:
         // TRABAJANDO
        }
    }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19231

View user's profile Send private message

PostPosted: Wed Feb 07, 2024 10:51 am     Reply with quote

Your original code was on a different chip. The 16F877A does not support
interrupt priorities. Also no PLL, so there is no HSPLL fuse. You don't need
to test if get_timer is zero, since the interrupts will only be called if this is the
case. Also the compiler automatically clears the interrupts unless you ask
it not to. You need to work out what the values given to the timer setups
in the XC8 program 'do' (data sheet), and then just use the same settings
in the CCS code.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group