fazenfact81
New Member
Hi to you all.
I am stuck with my pwm project. It is a simple pwm project with some button inputs and one ADC input. The problem I sit with is that when the button input initializes the ADC reading will immediately change the PWM as it is an actual reading and has to me executed because the condition is true!
So I try to build in some hysteresis in the ADC reading so that if there is some jitter on the ADC input it will be ignored until it exceeds the threshold. The way I have done it worked for a while like needed and I then expanded the code to do some other things and the ADC became reluctant to react. Then I reprogramed with the code that worked but no change, still very slow. I tried a lot of things and searched the net but did not come across a solution.
This for the background. Sorry for the long description but I try to explain so you could understand my project better and could provide me with better advice.
Attached is my code, please help me overcome the problem and help me learn out of it
I am stuck with my pwm project. It is a simple pwm project with some button inputs and one ADC input. The problem I sit with is that when the button input initializes the ADC reading will immediately change the PWM as it is an actual reading and has to me executed because the condition is true!
So I try to build in some hysteresis in the ADC reading so that if there is some jitter on the ADC input it will be ignored until it exceeds the threshold. The way I have done it worked for a while like needed and I then expanded the code to do some other things and the ADC became reluctant to react. Then I reprogramed with the code that worked but no change, still very slow. I tried a lot of things and searched the net but did not come across a solution.
This for the background. Sorry for the long description but I try to explain so you could understand my project better and could provide me with better advice.
Attached is my code, please help me overcome the problem and help me learn out of it
Code:
sbit UP at RB1_bit;
sbit DOWN at RB0_bit;
sbit OFF at RB2_bit;
sbit Half at RB3_bit;
sbit ON at RB4_bit;
sbit STROBE at RB5_bit;
unsigned short new_DC, current_DC, k;
unsigned int ADR, old_ADR;
void debounce(){
Delay_ms(50);
}
void Button_ON(){
PWM1_Set_Duty(current_DC);
new_DC = 255;
Delay_ms(20);
}
void Button_Half(){
PWM1_Set_Duty(current_DC);
new_DC = 75;
Delay_ms(20);
}
void Button_OFF(){
PWM1_Set_Duty(current_DC);
new_DC = 0;
Delay_ms(20);
}
void main() {
ANSEL = 0x04; // Configure analog pins
ANSELH = 0; // Configure digital pins
TRISD = 0; // Port D pins configured
PORTD = 0x00; // Port D initial state
PORTC = 0x01; // Port C initial state
TRISC = 0; // Port C pins configured
PORTB = 0xFF; // Port B initial state
TRISB = 0xFF; // PORT B set as output
PORTA = 0xFF; // Port A initial state
TRISA = 0x02; // RA input for pot
ADCON1 = 0x82; // A/D voltage reference is VCC
ADC_Init(); // Initializes ADC
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0; //
old_ADR = 0; // Initial value of variable ADR
PWM1_Init(5000); // PWM module initialization (5KHz)
new_DC = 0; // Initial value of variable Duty Cycle
current_DC = 0; // Initial value of variable Duty Cycle
PWM1_Start(); // Start PWM1 module
PWM1_Set_Duty(current_DC);
k=0;
while(k<3)
{
PORTC = 0x01;
Delay_ms(80);
PORTC = 0x00;
Delay_ms(150);
k++;
}
PORTC = 0x00;
Delay_ms(200);
PORTC = 0x01; //Set PORTC
do {
if (!ON){ // If Button is pressed
Button_ON();
}
if (!Half){ // If Button is pressed
Button_Half();
}
if (!OFF){ // If Button is pressed
Button_OFF();
}
if (!UP){ // If the button is pressed
debounce();
if (new_DC < 255) // Don't go above 255
new_DC = new_DC + 1 ; // increment Duty Cycle by 1
}
if (!DOWN) { // If the button is pressed
debounce();
if (new_DC !=0) // Don't go below 0
new_DC= new_DC - 1 ; // Decrement Duty Cycle by 1
}
ADR = ADC_Read(1) >>2; //Get ADC reading and divide reading
if (ADR < (old_ADR - 2)){
new_DC = ADR; // Set new_DC to ADR
}
if (ADR > (old_ADR + 2)){
new_DC = ADR; // Set new_DC to ADR
}
if (old_ADR != ADR){
old_ADR = ADR; // Set new_DC to ADR
}
if (current_DC != new_DC) {
current_DC = new_DC ;
PWM1_Set_Duty(current_DC); // Change the current DC to new value
PORTD = current_DC;
}
if (!STROBE){
PWM1_Set_Duty (0);
k=0;
while(k =! k)
{
PORTC.B1 = 1;
PORTC.B2 = 1;
Delay_ms(20);
PORTC.B1 = 0;
PORTC.B2 = 0;
Delay_ms(45);
k++;
}
PWM1_Start();
PWM1_Set_Duty(current_DC);
}
} while(1);
}