void init_pic (void)
{
RBPU = 1; // Pull up désactivés
// Timer 0 (8 bits) utilisé pour le wachtdog a 2,3 s.
// T0CS = 0 ; // internal Clock
// PSA = 1 ; // timer0 = Wachtdog
// PS2 = 1 ; // \
// PS1 = 1 ; // => 111 + WDT => 18 ms x 128 = 2,304 s
// PS0 = 1 ; // /
// T0IE = 1;
ADCON1= 6 ; // Passe le port A en mode digital
TRISA = ConfigIOPortA ; // Configuration du port A
TRISB = ConfigIOPortB ; // Configuration du port B
TRISC = 0 ; // Configuration du port C
PORTA = 0x3F ; // Toutes les sorties à 5V
PORTB = 0xFF ; // Toutes les entrées à 5V
PORTC = 0 ; // Toutes les sorties à 0V
GIE = 1; // Valide toutes les interruptions
PEIE = 1 ;
CCP1IE = 1 ; // Interruption en mode comparaison autorisée
CCP2IE = 1 ;
TMR1IE = 1 ; // Interruption du Timer1 autorisée
}
static void interrupt inter(void) // Gestion du timer1 en mode compare
{
if (TMR1IF)
{
RC6 = 1 ;
RC7 = 1 ;
TMR1H = 0xEC ;
TMR1L = 0x77 ;
TMR1IF = 0 ;
}
if (CCP1IF)
{
CCP1IF = 0 ;
RC6 = 0 ;
// CCP1IF = 0 ;
}
if (CCP2IF)
{
CCP2IF = 0 ;
RC7 = 0 ;
// CCP2IF = 0 ;
}
}
void Init_compare (void)
{
{
TMR1H = 0xEC ; //on fixe la fréquence 65535-20000µs/4µs = 0xEC77
TMR1L = 0x77 ;
CCP1CON = 0b00001010 ;
CCP2CON = 0b00001010 ;
T1CON = 0b00100001 ; //Prescaler=1:4 Timer1=on
}
}
void Config_rapport_cycliqueV(unsigned char)
{
unsigned int Val1_tmp ;
Val1_tmp = (139*Vpos)/100 ;
Val1_tmp += 60785 ;
CCPR1L = Val1_tmp ;
CCPR1H = Val1_tmp >> 8 ;
}
void Config_rapport_cycliqueH(unsigned char)
{
unsigned int Val2_tmp ;
Val2_tmp = (139*Hpos)/100 ;
Val2_tmp += 60785 ;
CCPR2L = Val2_tmp ;
CCPR2H = Val2_tmp >> 8 ;
}
/* *
* set Pulse to some value between 400 and 2400 (usecs) *
* *
* setup the TMR1 prescaler for 1.0-usec 'ticks' *
* */
unsigned int Pulse = 1500; // 1-usec TMR1 'ticks'
unsigned int Period = 20000; // 1-usec TMR1 'ticks'
/*****************************************************************
* setup interrupt vectors *
*****************************************************************/
#pragma code high_interrupt = 0x08
void high_interrupt (void)
{ _asm goto isr_hi _endasm
}
#pragma code
/*****************************************************************
* ISR (high) *
*****************************************************************/
#pragma interrupt isr_hi
void isr_hi ()
{ if (PIR1bits.CCP1IF == 1) // CCP1 interrupt?
{ PIR1bits.CCP1IF = 0; // yes, clear int flag
if (CCP1CONbits.CCP1M0 == 0) // CCP1 is hi, make it go
{ CCP1CONbits.CCP1M0 = 1; // lo next match interrupt
CCPR1H += (Pulse/256); // setup Pulse on time
CCPR1L += (Pulse%256); // match/interrupt value
}
else // CCP1 is lo, make it go
{ CCP1CONbits.CCP1M0 = 0; // hi next match interrupt
CCPR1H += ((Period-Pulse)/256); // setup Pulse off time
CCPR1L += ((Period-Pulse)%256); // match/interrupt value
}
}
}
#include <pic.h>
#include "joystick.h"
#include "ad16f87x.h"
__CONFIG(XT & WDTDIS & BORDIS & LVPDIS & UNPROTECT);
// Varibles declaration
unsigned int val ;
unsigned char Vpos ;
unsigned char Hpos ;
unsigned int Val1_tmp ;
unsigned int Val2_tmp ;
unsigned int Period ;
#define Period 20000
# define Time 5
#define Clign 10
#define time_led 10
#define time2 40
// prototypes functions
void init_pic (void);
void Delais_Milli (int val) ;
void PosLed (void) ;
void Test_Leds (void) ;
void Reset (void) ;
static void interrupt inter(void) ;
void Init_compare(void) ;
void Config_rapport_cycliqueV(unsigned char) ;
void Config_rapport_cycliqueH(unsigned char) ;
// PIC initialization
void init_pic (void)
{
RBPU = 1; // Pull up off
ADCON1= 6 ; // port A digital
TRISA = ConfigIOPortA ; // Config port A
TRISB = ConfigIOPortB ; // Config port B
TRISC = 0 ; // Config port C
PORTA = 0x3F ; // output to 5V
PORTB = 0xFF ; // input to 5V
PORTC = 0 ; // output to 0V
GIE = 1; // global interrupt enable
PEIE = 1 ;
CCP1CON = 0b00001010 ; // compare mode CCP1
CCP2CON = 0b00001010 ; // compare mode CCP2
T1CON = 0b00000001 ; //Prescaler=1:1 Timer1=on
CCP1IE = 1 ; // Interupt mode compare CCPx enable
CCP2IE = 1 ;
TMR1IE = 1 ; // Interupt Timer1 enable
}
main()
{
Val1_tmp = 0 ; // variables init
Val2_tmp = 0 ;
init_pic() ;
Vpos = 90 ; // position centrale servo Vertical
Hpos = 90 ; // position centrale servo Horizontal
Config_rapport_cycliqueV (Vpos) ;
Config_rapport_cycliqueH (Hpos) ;
Test_Leds() ; // Test des leds au démarrage
while(1)
{ ..............
.............
}
static void interrupt inter(void)
{
if (CCP1IF)
{
CCP1IF = 0;
if (CCP1M0 == 0)
{
RC6 = 1 ;
CCP1M0 = 1 ;
CCPR1H += (Val1_tmp / 256) ;
CCPR1L += (Val1_tmp % 256) ;
}
else
{
RC6 = 1 ;
CCP1M0 = 0 ;
CCPR1H += ((Period - Val1_tmp) / 256) ;
CCPR1L += ((Period - Val1_tmp) % 256) ;
}
}
if (CCP2IF)
{
CCP2IF = 0;
if (CCP1M0 == 0)
{
RC7 = 1 ;
CCP1M0 = 1 ;
CCPR2H += (Val2_tmp / 256) ;
CCPR2L += (Val2_tmp % 256) ;
}
else
{
RC7 = 0 ;
CCP1M0 = 0 ;
CCPR2H += ((Period - Val2_tmp) / 256) ;
CCPR2L += ((Period - Val2_tmp) % 256) ;
}
}
}
void Test_Leds (void)
{ .......
..........
}
void Reset (void) // Reset servos positions
{
...........
}
void PosLed(void)
{
...........
}
void Delais_Milli (int val)
{
static int val1 = 0 ;
static int val2 = 0 ;
for (val2=0 ; val2 < val ; val2 ++)
{
for (val1=0 ; val1 < 200 ; val1++) ;
}
}
void Config_rapport_cycliqueV(unsigned char)
{
unsigned int Val1_tmp ;
Val1_tmp = (555*Vpos)/100 ;
Val1_tmp += 1000 ;
// if Vpos=0 => Val1_tmp=1000/ if Vpos=180 => Val1_tmp= 2000
}
void Config_rapport_cycliqueH(unsigned char)
{
unsigned int Val2_tmp ;
Val2_tmp = (555*Hpos)/100 ;
Val2_tmp += 1000 ;
}
#include <pic.h>
#include "joystick.h"
#include "ad16f87x.h"
__CONFIG(XT & WDTDIS & BORDIS & LVPDIS & UNPROTECT);
unsigned int val ;
unsigned char Vpos ;
unsigned char Hpos ;
unsigned int PulseV ;
unsigned int PulseH ;
unsigned char fact ;
#define Time 10
#define time2 5
#define periode 21500
void init_pic (void) ;
void init_var (void) ;
void Delais_Milli (int val) ;
void PosLed (void) ;
static void interrupt inter(void) ;
void Config_rapport_cycliqueV(unsigned char) ;
void Config_rapport_cycliqueH(unsigned char) ;
void init_pic (void)
{
RBPU = 1; // Pull up désactivés
ADCON1= 6 ; // Passe le port A en mode digital
TRISA = ConfigIOPortA ; // Configuration du port A
TRISB = ConfigIOPortB ; // Configuration du port B
TRISC = 0 ; // Configuration du port C
PORTA = 0x3F ; // Toutes les sorties à 5V
PORTB = 0xFF ; // Toutes les entrées à 5V
PORTC = 0 ; // Toutes les sorties à 0V
GIE = 1; // Valide toutes les interruptions
PEIE = 1 ;
CCP1CON = 0b00001011 ;
CCP2CON = 0b00001011 ;
T1CON = 0b00000001 ; //Prescaler=1:1 Timer1=on
CCP1IE = 1 ; // Interruption en mode comparaison autorisée
CCP2IE = 1 ;
TMR1IE = 1 ; // Interruption du Timer1 activée
}
void init_var()
{
fact = 5 ;
Vpos = 100 ;
Hpos = 100 ;
}
main()
{
init_pic() ;
init_var() ;
Config_rapport_cycliqueV (Vpos) ;
Config_rapport_cycliqueH (Hpos) ;
while(1) // Le programme tourne dans cette boucle principale
{
while ((JOY12H == 0) && (Vpos < 200)) // Gestion des positions en fonctions des actions sur le joystick
{
Delais_Milli(time2) ;
Vpos = Vpos++ ;
Config_rapport_cycliqueV(Vpos) ;
Delais_Milli(Time) ;
PosLed() ;
}
while ((JOY18H == 0) && (Vpos > 0))
{
Delais_Milli(time2) ;
Vpos = Vpos-- ;
Config_rapport_cycliqueV(Vpos) ;
Delais_Milli(Time) ;
PosLed() ;
}
while ((JOY15H == 0) && (Hpos < 200)) // Gestion des positions en fonctions des actions sur le joystick
{
Delais_Milli(time2) ;
Hpos = Hpos++ ;
Config_rapport_cycliqueH(Hpos) ;
Delais_Milli(Time) ;
PosLed() ;
}
while ((JOY21H == 0) && (Hpos > 0))
{
Delais_Milli(time2) ;
Hpos = Hpos-- ;
Config_rapport_cycliqueH(Hpos) ;
Delais_Milli(Time) ;
PosLed() ;
}
}
}
static void interrupt inter(void) // Gestion du timer1 en mode compare
{
if (CCP1IF)
{
CCP1IF = 0 ;
if (CCP1M0 == 1)
{
CCP1M0 = 0 ;
RC6 = 1 ;
CCPR1H = (PulseV / 256) ;
CCPR1L = (PulseV % 256) ;
}
else
{
CCP1M0 == 1 ;
RC6 = 0 ;
CCPR1H = ((periode - PulseV) / 256) ;
CCPR1L = ((periode - PulseV) % 256) ;
}
}
if (CCP2IF)
{
CCP2IF = 0 ;
if (CCP2M0 == 1)
{
CCP2M0 = 0 ;
RC7 = 1 ;
CCPR2H = (PulseH / 256) ;
CCPR2L = (PulseH % 256) ;
}
else
{
CCP2M0 == 1 ;
RC7 = 0 ;
CCPR2H = ((periode - PulseH) / 256) ;
CCPR2L = ((periode - PulseH) % 256) ;
}
}
}
void Config_rapport_cycliqueV(unsigned char)
{
PulseV = (fact * Vpos) ; //Valeurs adaptées à la maquette et au servo
PulseV += 1000 ;
}
void Config_rapport_cycliqueH(unsigned char)
{
PulseH = (fact * Hpos) ; //Valeurs adaptées à la maquette et au servo
PulseH += 1000 ;
}
void PosLed(void) // Fonction de gestion des leds de direction
{
if (Vpos > 100)
{
LED_BAS = 1 ;
LED_HAUT = 0 ;
}
if (Vpos < 80)
{
LED_HAUT = 1 ;
LED_BAS = 0 ;
}
if (Hpos > 100)
{
LED_GAUCHE = 1 ;
LED_DROITE = 0 ;
}
if (Hpos < 80)
{
LED_GAUCHE = 0 ;
LED_DROITE = 1 ;
}
}
void Delais_Milli (int val)
{
static int val1 = 0 ;
static int val2 = 0 ;
for (val2=0 ; val2 < val ; val2 ++)
{
for (val1=0 ; val1 < 200 ; val1++) ;
}
}
void Config_rapport_cycliqueV(unsigned char)
{
PulseV = (fact * Vpos) ; //Valeurs adaptées à la maquette et au servo
PulseV += 1000 ;
}
Yes, that's correct. "Set output on match" uses the RC2/CCP1 and the RC1/CCP2 pins.POWERMAN said:"Set output on match" means that i must used RC1 and RC2?
static unsigned char n = 0;
static unsigned int Qarray [] = { 1500, 1500, 1500, 1500,
1500, 1500, 1500, 1500, 20000 };
static unsigned char Servo = 1; // Port B shadow register
/*****************************************************************
* K8LH Crazy-8 'Soft' 8-channel (Port B) Servo Algorithm *
* */
void isr_hi ()
{ if (PIR1bits.CCP1IF == 1) // if CCP "compare" interrupt
{
LATB = Servo; // output new Servo pulse
CCPR1H += (Qarray[n]/256); // update "match" period value
CCPR1L += (Qarray[n]%256); // for this Servo pulse
PIR1bits.CCP1IF = 0; // clear CCP1 interrupt flag
Qarray[8] -= Qarray[n]; // adjust end-of-period time
Servo <<= 1; // prep for next channel
n++; // increment array index
if (n = 9) // if end-of-cycle
{ n = 0; // reset array index
Qarray[8] = 20000; // reset the 20.0-msec period
Servo = 1; // reset the Servo to Servo 1
}
}
}
LATB = (Servo & 0b11000000);
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?