Home › Forums › Microcontrollers › PIC Microcontroller › Using PWM and ADC at the same time
- This topic has 2 replies, 2 voices, and was last updated 7 years, 5 months ago by mustunsultan.
Viewing 3 posts - 1 through 3 (of 3 total)
-
AuthorPosts
-
September 15, 2015 at 3:05 pm #11756mustunsultanParticipant
Hello,
I am new to PIC Microcontrollers, but have gone through all the tutorials. I can write codes on my own for PWM and ADC seperately. But when I try to use both at the same time I don’t get any output signal at CCP1 *checked with Proteus also*I am trying to use 2 buttons RC4 and RC5 to increase and decrease the PWM duty cycle. And use ADC for lighting up LEDs , but I can get any signal out of CCP1.
Here is the code I tried:
unsigned int ADC; void main() { short duty1 = 16; CMCON = 0x07; ADCON1 = 0x80; TRISA = 0xFF; //input analog signal TRISC = 0x30; //RC4 RC5 input button, RC2 output PWM TRISB = 0x00; //output MSB's of ADC TRISD = 0x00; //output LSB's of ADC //ADC Function do { ADC = ADC_Read(1); PORTB = ADC; PORTD = ADC>>2; } while(1); //////////////////////////////////////////////// //PWM Function PWM1_Init(5000); PWM1_Start(); PWM1_set_duty(duty1); while(1) { if (PORTC.F4 == 0) { Delay_ms(1); duty1++; PWM1_set_duty(duty1); } if (PORTC.F5 == 0) { delay_ms(1); duty1--; PWM1_set_duty(duty1); } delay_ms(100); } }
September 19, 2015 at 7:08 pm #11770Ligo GeorgeKeymasterDear Mustunsultan,
There is no meaning in combining some codes without any logic.
Try like this.
unsigned int ADC; void main() { short duty1 = 16; CMCON = 0x07; ADCON1 = 0x80; TRISA = 0xFF; //input analog signal //PWM Function PWM1_Init(5000); PWM1_Start(); //ADC Function do { ADC = ADC_Read(1); PWM1_set_duty(ADC/4); delay_ms(400); }while(1); }
September 29, 2015 at 12:31 pm #11782mustunsultanParticipantThank you Sir!
It really helped to understand the code well. -
AuthorPosts
Viewing 3 posts - 1 through 3 (of 3 total)
- You must be logged in to reply to this topic.