![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
johenrod
Joined: 05 Oct 2020 Posts: 8
|
How to define a register if it can be used on several pins? |
Posted: Sun Apr 09, 2023 9:11 am |
|
|
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 |
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Sun Apr 09, 2023 9:41 am |
|
|
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
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
johenrod
Joined: 05 Oct 2020 Posts: 8
|
|
Posted: Sun Apr 09, 2023 11:06 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
johenrod
Joined: 05 Oct 2020 Posts: 8
|
|
Posted: Sun Apr 09, 2023 11:17 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Sun Apr 09, 2023 11:18 am |
|
|
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.... |
|
![](templates/subSilver/images/spacer.gif) |
johenrod
Joined: 05 Oct 2020 Posts: 8
|
|
Posted: Sun Apr 09, 2023 1:08 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Sun Apr 09, 2023 1:44 pm |
|
|
#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. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|