Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
//Target pic: pic16f877a
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
char *l,*m,n[7],o[7];
unsigned int k,j,i;
unsigned int e,g;
unsigned int maxcurrent;
unsigned int cur_read; //current reading variable
unsigned int pot_read; //potentiometer voltage reading variable
unsigned int vol_read; //voltage reading variable
void LCD_Display(unsigned int value)
{
j=value/10;
i= (int)value%10;
inttostr(j,o);
inttostr(i,n);
l=ltrim(o);
m=ltrim(n);
if ((j<=99)&&(j>9)){
Lcd_out(1,10,l);
Lcd_out(1,12,".");
Lcd_out(1,13,m);
}
else if (j<=9)
{
Lcd_out(1,10,l);
Lcd_out(1,11,".");
Lcd_out(1,12,m);
//Lcd_out(1,13," ");
}
else
{
Lcd_out(1,10,l);
Lcd_out(1,13,".");
Lcd_out(1,14,m);
//Lcd_out(1,15," ");
}
}
void main()
{
short current_duty_2 = 0; // initial value for current_duty_2
PWM2_Init(7800); //7.8kHz frequency
PWM2_Start(); // start PWM2
PWM2_Set_Duty(current_duty_2);
//TRISA=0x07; //analog input at RA0,RA1,RA2
TRISA=0x07; //analog input at RA0,RA1,RA2
TRISC=0x00;
lcd_init(); //initializing LCD
lcd_cmd(_LCD_CURSOR_OFF);
ADCON1=0x02; // reference voltage VDD
ADCON0.ADCS0=0;
ADCON0.ADCS1=1; //Fosc/8
ADCON0.ADON=1; //turn on analog module
while (1)
{
// Current Measurement
ADCON0=0x45; //select RA0 channel
Delay_us(30); //acquisition Delay
cur_read=ADC_Read(0); //reading ADC_Value at channel 0
k= cur_read*0.585339584761;
Lcd_Display(k);
//Potentiometer Voltage Measurement
//Delay_us(30);
ADCON0=0x4D; //select RA1 channel
Delay_us(30); //acquisition Delay
pot_read=ADC_Read(1); //reading ADC_Value at channel 1
e=pot_read*0.127077224;
//Lcd_Display(e);
//Voltage Measurement
//Delay_us(30);
ADCON0=0x55; //select RA2 channel
Delay_us(30); //acquisition Delay
vol_read=ADC_Read(2); //reading ADC_Value at channel 2
g=vol_read*1.5698924731;
//Lcd_Display(g);
/// PWM Generation
maxcurrent=2000/g;
current_duty_2=pot_read/4.02; //maximum value of dutycycle is 1023/4.02 = 254
while((k*0.1)>maxcurrent) // current greater than maxcurrent then decrease the duty cycle
{
current_duty_2--;// decresing duty cycle
}
PWM2_Set_Duty(current_duty_2);//setting pwm for that duty cyclr
Delay_ms(10); //Delay infinte loop
}
}
is this OK?
Hi,
What PWM frequency do you use?
Klaus