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 CCS Technical Support

How to define a register if it can be used on several pins?

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



Joined: 05 Oct 2020
Posts: 8

View user's profile Send private message AIM Address

How to define a register if it can be used on several pins?
PostPosted: Sun Apr 09, 2023 9:11 am     Reply with quote

Best regard
I'm trying to configure the "CCP1" register for a pwm" with the pic12f1822 and it has CCP1 on 2 pins RA0 and RA5, could you please tell me how to configure it for a certain pin, thanks in advance.
Code:
#INCLUDE <12F1822.h>
#DEVICE ADC=10
//#USE DELAY(CLOCK=4000000) //Crystal Interno 4MHz
//#use delay(clock=4000000,crystal)//Crystal Externo 4MHz
#use delay(internal=8MHz)
#FUSES NOPROTECT,NOWDT,NOBROWNOUT,PUT,NOLVP,nomclr
 
//#INCLUDE <LCD.C>
 
 
 
#pin_select CCP1=PIN_A2
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sun Apr 09, 2023 9:41 am     Reply with quote

Correct your nomenclature. You are not using a register, but a peripheral.
The CCP1 I/O signal can be routed to RA2 or RA5. It defaults to RA2.
The data sheet tells you that this is controlled by APCON (alternative
pin function control), and in particulkr the bit CCP1SEL.
This is not a PPS chip (which pin_select controls). Just a chip allowing
two alternative pins.
So:
Code:

#INCLUDE <12F1822.h>
#DEVICE ADC=10
#use delay(internal=8MHz)
#FUSES NOPROTECT,NOWDT,NOBROWNOUT,PUT,NOLVP,nomclr
 
#bit CCP1SEL=getenv("CCP1SEL")

void main(void)
{
     CCP1SEL=1; //CCP1 will now go to RA5
     //Remember you also have to set the TRIS for the new pin.

     //now your code to usel CCP1

}
 
johenrod



Joined: 05 Oct 2020
Posts: 8

View user's profile Send private message AIM Address

PostPosted: Sun Apr 09, 2023 11:06 am     Reply with quote

Excellent, thank you very much "Ttelmah" that is to say that if "CCP1SEL =0; //CCP1 will now go to RA2" ?
I didn't know this "#bit CCP1SEL=getenv("CCP1SEL")" and it was what was missing.
johenrod



Joined: 05 Oct 2020
Posts: 8

View user's profile Send private message AIM Address

PostPosted: Sun Apr 09, 2023 11:17 am     Reply with quote

I'm trying to do a speed control, I attach the code, it compiles well but it doesn't work for me, I don't get anything at the RC2(PIN17) output, I took it from the "education automatic control" website and the author says it works well, you could be by the version? The only thing I see is that I have version 5015.
Basically what I'm trying to do is pass a code from pic16f887 to a 12f1822, that's why the registry question "CCP1"
Code:

#INCLUDE <16F887.h>
#DEVICE ADC=10
//#USE DELAY(CLOCK=4000000) //Crystal Interno 4MHz
#use delay(clock=4000000,crystal)//Crystal Externo 4MHz
#use delay(crystal=4000000)//Crystal Externo 4MHz
#FUSES XT,NOPROTECT,NOWDT,NOBROWNOUT,PUT,NOLVP
#INCLUDE <LCD.C>

#BYTE PORTA= 5
#byte PORTB= 6
#byte PORTC= 7
#BYTE PORTD= 8

long contador=0;
int16 duty=0;
int Timer2,Poscaler;
double RPM;

//Interrubción por cambio en RB0
#INT_EXT
ext_isr()
{
 contador++;
}
void main()
{
   //Configura los Puertos del PIC
   set_tris_a(0x01);
   set_tris_b(0x01); //RB0 como entrada
   set_tris_d(0);
   
   //Configura la Entrada Analoga
   setup_adc_ports(sAN0);              //Configurar ADC (Lectura de temperatura)
   setup_adc(adc_clock_internal);      //Reloj interno para la conversion analoga digital)
   
   //Configura el PWM
   // Generemos una Señal cuadrada de 1 Khz
   Timer2=249;    //Se carga timer 2 con 249 como lo vimos en la pagina
   //Preescaler=4;  //Preescaler solo puede tomar valores de: 1 - 4 - 16
   //Para el preescaler colocamos "t2_div_by_4"
   Poscaler=1;  //Preescaler solo puede tomar valores de: 1
   
   setup_timer_2(t2_div_by_4,Timer2,Poscaler);   //Configuracion de Timer 2 para establecer frec. PWM a 1kHz
   setup_ccp1(ccp_pwm);                //Configurar modulo CCP1 en modo PWM
   
   //Habilita las Interrupcciones
    enable_interrupts(int_ext); //Activa interrupcion por RB0
    ext_int_edge(L_TO_H);    //Activa interrupción por flanco de subida
    enable_interrupts(global); //Habilita interrupciones de forma global
   
    lcd_init();
    lcd_gotoxy(1,1);
    LCD_PUTC("VELOCIDAD MOTOR");
   
    //Hace por siempre
    while(TRUE)
    {
     
     //Lectura del Potenciometro
     set_adc_channel(0);   //Escoge el canal 0 de los puertos analogos
     delay_us(100);       
     duty=read_adc();      //Almacena en duty el valor del voltaje del pot
     set_pwm1_duty(duty);//10 bits= 1023 y 8bit= 255
     
     //Espera por 1 segundo
     delay_ms(999);
     
     //Despues de 1 segundo multiplica el valor de los pulsos contados en
     //contador y los multiplica por 60 (para pasarlo a minutos) y lo divide
     //por el numero de pulsos que tiene el encoder, en este caso el encoder
     //da 10 pulsos en 1 giro completo
     RPM=contador*60/10;
     
     //Reinicia el contador de Pulsos
     contador=0;
     
     //Visualiza la Velocidad
     lcd_gotoxy(1,2);
     printf(lcd_putc,"RPM: %f   ",RPM);
     
    }
   
}


Last edited by johenrod on Sun Apr 09, 2023 11:20 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sun Apr 09, 2023 11:18 am     Reply with quote

CCP1SEL, defaults to 0. You can set it to this if you want, but you don't
have to.
However there is a caveat, that for most things the compiler will
automatically set TRIS, but for the movable pins you have to go DIY....
johenrod



Joined: 05 Oct 2020
Posts: 8

View user's profile Send private message AIM Address

PostPosted: Sun Apr 09, 2023 1:08 pm     Reply with quote

I saw that you changed 9 to 0, I was crazy looking for that 9 but it already makes sense to me.
#bit CCP1SEL=getenv("CCP1SEL")
it would also be
#bit TXCKSEL=getenv("TXCKSEL")

Gets the value of an environment variable.
So, as you teach me, could I change the variables without so much code difficulty?
Many thanks for everything.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sun Apr 09, 2023 1:44 pm     Reply with quote

#bit creates a bit variable. Most of the control bits have individual
names. You can also use #byte with register name instead to access the
8bit registers. Also #word with a low register like CCP1L, to create a
16bit variable that accesses the whole 16bit register.
For 99% of things it is better to use the internal CCS functions, but
for special things like this, these are the way to give the extra accesses
where they don't exist.
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