|
|
View previous topic :: View next topic |
Author |
Message |
Louati
Joined: 26 Mar 2012 Posts: 16
|
Réaliser un signal PWM 500hz |
Posted: Wed May 30, 2012 2:48 am |
|
|
Bonjour
Je suis en train de réaliser un signal PWM de fréquence 500hz dont le rapport cyclique est variable à l'aide d'un potentiomètre.
Sur ISIS le programme fonctionne parfaitement mais en pratique il coince :/
Voici le code de mon programme :
#include <18f2550.h>
#device adc=10
#FUSES NOWDT
#FUSES WDT128
#FUSES PLL1
#FUSES CPUDIV1
#FUSES NOUSBDIV
#FUSES INTRC_IO
#FUSES NOFCMEN
#FUSES NOIESO
#FUSES BORV27
#FUSES NOVREGEN
#FUSES NOPBADEN
#FUSES NOLPT1OSC
#FUSES NOMCLR
#FUSES NOLVP
#FUSES NOXINST
#use delay(clock=8000000)
#byte porta = 0xF80
#byte portb = 0xF81
#byte portc = 0xF82
#byte portd = 0xF83
#byte trisa = 0xF92
#byte trisb = 0xF93
#byte trisc = 0xF94
#byte trisd = 0xF95
char a=0;
char i;
int16 valeur=0,j,p;
void main(void){
portc=0b00000000;
trisc=0b01111111;
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL|ADC_CLOCK_DIV_2);
set_adc_channel(0);
delay_ms(5000);
valeur=read_adc();
j=(int16)(2000.0*(valeur/1023));
p=2000-j;
while(1){
valeur=read_adc();
j=(int16)(2000.0*(valeur/1023));
p=2000-j;
for(i=1;i;i++){
output_high(pin_C7);
delay_us(j);
output_low(pin_C7);
delay_us(p);
}
}
}
Si quelqu'un de vous a un code plus fiable, prière de me le proposer et merci d'avance |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed May 30, 2012 4:28 am |
|
|
Please use English on this forum.
Get rid if PROTEUS. It's full of bugs.
Use REAL hardware from the start.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19541
|
|
Posted: Wed May 30, 2012 6:17 am |
|
|
Agree about ISIS/PROTEUS.
Also _use the code buttons_
Code: |
#include <18f2550.h>
#device adc=10
#FUSES NOWDT, INTRC_IO, NOFCMEN, NOIESO, BORV27, NOVREGEN, NOPBADEN, NOLPT1OSC, NOMCLR, NOLVP, NOXINST
#use delay(internal=8000000)
char a=0;
char i;
int16 valeur=0,j,p;
void main(void){
output_c(0);
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_2); //Only one clock can be used
set_adc_channel(0);
delay_ms(5000);
//j=(int16)(2000.0*(valeur/1023)); This won't work. (0 to 1023)/1023
//using int16 arithmetic=0
//j=(2000*(int32)valeur)/1023;
//p=2000-j;
//No point in doing it all first anyway, since it is done in the loop
while(1){
valeur=read_adc();
j=(2000*(int32)valeur)/1023;
//Use int32, since 1023*2000 is too large to fit in int16. Divide _after_
//multiplication.
p=2000-j;
for(i=1;i;i++) {
output_high(pin_C7);
delay_us(j);
output_low(pin_C7);
delay_us(p);
} //will loop 255 times. 0.51 seconds between updates
}
}
|
Big mistake is in the arithmetic.
All the port stuff is unwanted.
Best Wishes |
|
|
|
|
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
|