Kfir Maymon
Member level 1
- Joined
- Sep 20, 2013
- Messages
- 40
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 8
- Location
- Israel
- Activity points
- 315
i`m building led dimmer with PWM for some reason i get wrong math answer,
when ADC is max (1023)
i get this, temp = (adcval1 * 254)/1024 = 61 it cant be.
Code:
unsigned short duty1=127; // pwm duty cycle
unsigned long temp=0;
unsigned int adcval1=0; //ADC 10 bit value
char ADCTXT[7],TEMPTXT[11]; //debug string
char DUTYTXT[5]; //debug string
void init(){
CM1CON0=0x07;
ANSEL=0x0f;
TRISC=0x00;
TRISB=0xf0;
}
void main() {
init();
UART1_Init(19200);
ADC_Init();
PWM1_Init(20000);
PWM1_Start();
PWM1_Set_Duty(duty1);
while(1){
adcval1 = ADC_Read(0); // read ADC value
delay_ms(100);
IntToStrWithZeros(adcval1, ADCTXT);
UART1_Write_Text("ADC: ");
UART1_Write_Text(ADCTXT);
UART1_Write_Text("\r\n");
temp = (adcval1 * 254)/1024; // make adc value fit short to duty cycle
duty1= temp;
LongWordToStrWithZeros(temp,TEMPTXT);
UART1_Write_Text("TEMP ");
UART1_Write_Text(TEMPTXT);
UART1_Write_Text("\r\n");
ShortToStr(duty1,DUTYTXT);
UART1_Write_Text("duty:");
UART1_Write_Text(DUTYTXT);
UART1_Write_Text("\r\n");
PWM1_Set_Duty(duty1);
delay_ms(1000);
}
}
when ADC is max (1023)
i get this, temp = (adcval1 * 254)/1024 = 61 it cant be.