Try this code.
Code C - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
| // LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
unsigned int ADCValue;
unsigned int pwmValue;
char x[17];
[COLOR="#FF0000"]char pwm1[17];[/COLOR]
void main() {
UART1_Init(115200);
ADC_Init();
PWM1_Init(5000);
PORTA=0;
PORTB=0;
TRISA=0x1;
TRISB = 0x00;
TRISC=0b10111001;
PWM1_Start();
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
do
{
// Calculations
unsigned int input;
unsigned int pwm;
ADCValue=ADC_Read(0);
pwm=2.55E2*ADCValue/1024;
pwmValue=1E2*ADCValue/1024;
WordToStr(pwmValue,[COLOR="#FF0000"]pwm1[/COLOR]);
WordToStr(ADCValue,x);
delay_ms(20);
// LCD Display
Lcd_Out(1,1,"Analog V:");
Ltrim(x);
Lcd_Out(1,11,x);
delay_ms(20);
Lcd_Out(2,1,"PWM % :");
Ltrim([COLOR="#FF0000"]pwmValue[/COLOR]);
Lcd_Out(2,11,pwmValue);
PWM1_Set_Duty(pwm);
// Serial port
UART1_Write_Text("Analog Value=");
UART1_Write_Text(x);
UART1_Write(13);
Delay_ms(20);
}while(1);
} |
Thanks for the reply. I made changes highlighted in red, and it now works. The only problem is that once I get above 4 significant figures in the LCD, and reduce the PWM down again, the 4th Significant figure does not disappear. I will attach a pic to illustrate.
<a title="Proteus 8 Pic.png" href="http://obrazki.elektroda.pl/9641453400_1422608492.png"><img src="http://obrazki.elektroda.pl/9641453400_1422608492_thumb.jpg" alt="Proteus 8 Pic.png" /></a>