PDA

View Full Version : 12f683 comparator and pwm



Automan
- 23rd May 2007, 16:31
HI,
excuse me for my poor english.
I use :

PIC : 12F683
PBP : 2.46
programmer : labtool 48UXP

I'm trying to use, on 12F683, the comparator (mode comparator without output) and PWM simultaneously.
It seem to be possible but i'm not sure.
Here's my code :


'-------------------------------------------------------------------------------
' Configuration des fusibles
'-------------------------------------------------------------------------------

@ device pic12f683, fcmen_off, ieso_off, intrc_osc, wdt_off
@ device pic12f683, pwrt_on, mclr_off, protect_off

'-------------------------------------------------------------------------------
' Configuration des ports
'-------------------------------------------------------------------------------

GPIO = %00000000 ' toutes les sorties = low
CMCON0 = 2 ' Comparateur en mode 'comparator without output'
TRISIO = %00011011 ' GP0, GP1, GP3 et GP4 en entrée GP2 et GP5 en sortie
ANSEL = %00010011 ' GP0, GP1 et GP4 en entrée analogique
WPU = %00001000 ' Pull-ups on sur l'entrée GP3
OPTION_REG.7 = 0 ' Enable internal pull-ups

' Pour mémoire :
' gpio.0 ==> entrée comparateur tension proportionelle
' à I consommée
' gpio.1 ==> entrée comparateur tension de seuil
' gpio.2 ==> sortie PWM de commande pour IRL2203N (moteur)
' gpio.3 ==> entrée interrupteur "option"
' gpio.4 ==> entrée tension de réglage vitesse
' gpio.5 ==> LED pour test 1 = LED allumée

'-------------------------------------------------------------------------------
' Définition des paramètres de ADCIN
'-------------------------------------------------------------------------------

DEFINE OSC 4
Define ADC_BITS 10 ' résolution
Define ADC_CLOCK 3 ' Source de l'horloge (3=rc)
Define ADC_SAMPLEUS 50 ' Temps d'échantillonage en uS
Define CCP1_REG GPIO ' PWM Output on gpio
Define CCP1_BIT 2 ' Bit 2

'-------------------------------------------------------------------------------
' Déclaration des variables et alias
'-------------------------------------------------------------------------------

VITESSE Var Word ' Lecture de la vitesse sur 10 bits
V_PWM var byte ' valeur pour le signal PWM
SURCHARGE var bit ' Sortie du comparateur
surcharge = CMCON0.6
INVERSE_COMP var bit ' Bit d'inversion du comparateur
inverse_comp = CMCON0.4

'-------------------------------------------------------------------------------
' Déclaration des E/S
'-------------------------------------------------------------------------------

LED var gpio.5
INTER var gpio.3

'-------------------------------------------------------------------------------
'pour test
' if inter = 1 then inverse_comp = 0 ' sortie non inversée
' if inter = 0 then inverse_comp = 1 ' sortie inversée
'-------------------------------------------------------------------------------

'-------------------------------------------------------------------------------
' Programme
'-------------------------------------------------------------------------------

Pause 100

DEBUT:
ADCIN 0, vitesse ' Lecture de la vitesse
v_pwm = vitesse / 4
HPWM 1,v_pwm,500 ' PWM entre 0-255 , 500Hz sur GP2 , fréquence de test

' if surcharge = 1 then goto blocage ' Détection moteur bloqué = surcomsomation
pause 100
Goto DEBUT

BLOCAGE:
HPWM 1,0,500 ' PWM = 0 , 500Hz sur GP2 ==> arrêt
high led
pause 300
low led
pause 300
goto blocage

End

'-------------------------------------------------------------------------------

I haven't any PWM signal on pin 5

i don't understand, i have probably make a big mistake but y don't see it!
could somebody help me?

skimask
- 23rd May 2007, 16:44
I haven't any PWM signal on pin 5

First off...how do you know that the PIC is running in the first place?
Have you tried the ol' 'Blinky LED' program yet?
Break it down, small steps, independant small steps...Get an LED to blink, get the comparator to light an LED for you, get the PWM to do what you want it to do...THEN tie it all together. Can't go wrong with that methodology.

Automan
- 23rd May 2007, 17:02
HI, thanks for the reply,

I have got make some test with the LED output, all get right before the PWM instruction (i make the LED blinking) but it seems that the program freeze after. I can post all the test i have made but it's only a bigger program.
I have never got any PWM signal.
I have posted the entire program to let you see what i want to do.
I want to drive a motor with variable speed and verifying if it stops by controling his current.

mister_e
- 23rd May 2007, 17:03
modify your code as bellow.


Define ADC_BITS 10 ' résolution
Define ADC_CLOCK 3 ' Source de l'horloge (3=rc)
Define ADC_SAMPLEUS 50 ' Temps d'échantillonage en uS
;Define CCP1_REG GPIO ' PWM Output on gpio
;Define CCP1_BIT 2 ' Bit 2
ADCON0.7=1

mister_e
- 23rd May 2007, 17:39
and


SURCHARGE var CMCON0.6
INVERSE_COMP var CMCON0.4

Those above create aliases to the CMCON0 bits you want to monitor or write to.

Bonne chance!

Automan
- 23rd May 2007, 18:05
Thanks for your help!

I really do a big mistake!!
I wrote :

' gpio.4 ==> entrée tension de réglage vitesse

so i do use ADCIN 3 and not ADCIN 0

Now my PWM works and i'm going to test my current detector.

see you later!