samedi 13 août 2011

Un exemple avec les timer

Bonjour,
Les Timers sont des registres très utiles dans le pic. Ils peuvent simuler le fonctionnement multitâche grâce aux interruptions qui génèrent.  
Dans cet exemple je vais vous montrer comment créer  un signal PWM grâce aux interruption de timer1 d'une pic 16F628A ,
Le code :
int cnt=0;
void interrupt(){
if(TMR1IF_bit){
     cnt++;
     TMR1IF_bit=0;//l3alam yarja3 lelsfer
     TMR1H=0X00;
     TMR1L=0X00;//nfarghou timer1
     }
}

void main() {
portb=0X00;
trisb=0X00;//portb out
T1CON=0X31;//timer1 ye5dem ==TME1ON_bit=1 plus prescaler 1:8
TMR1IF_bit=0;//l3alam fil sfer
TMR1H=0X00;
TMR1L=0X00;//TIMER1 feregh
TMR1IE_bit=1;//INterruption masmou7a
INTCON=0XC0;//GIE et PEIE masmou7in zeda
while(1){
         if(cnt==100){
           portb=~portb;
           cnt=0;
         }
}//mise en veille
}
Visualiser le résultat sur le port B avec un oscilloscope.

mercredi 3 août 2011

Use the PWM

This is a little demonstration of how to use the pwm library to controle the speed motor (but you can use it in many different ways).
the task:
Read an analogic commande from a potentiometre and transform it to commande the speed motor.
the code:
unsigned int commande=0;//the value delivred by the poteintiometre
unsigned int controle=0;//the puls width that commands the motor speed
void main() {
    
     adc_init();//analogic to digital converter module initialisation
     pwm1_init(10000);//Puls width modulation module initialisation with 10KHz
     pwm1_set_duty(controle);//the motor is stopped
     pwm1_start();
    
     while(1){
        commande=adc_read(0);//read anaolgic value from channel number 0
        controle=(commande/4);//adapt the value of the control to the commande
        pwm1_set_duty(controle);//update the value of the pulse width
     }
}

simulation:
  
enjoy :))