Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Driving DC Motor with PIC 16F876/16F877

Status
Not open for further replies.

SASSI Sam

Newbie level 6
Joined
Nov 18, 2013
Messages
11
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,356
Hi every body, I try to drive a DC Motor depending on temperature, this is my program with MikroC, but always the Motor is working with the same speed (duty), could some one pleaaase help me where is the problem!! thanks..



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
67
68
69
70
71
72
73
74
////////
 
sbit LCD_RS at RB1_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D4 at RB4_bit;
 
// Pin direction
sbit LCD_RS_Direction at TRISB1_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB7_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB4_bit;
 
char str [16];
 int temp;
unsigned char duty = 0;
void main() 
{
 
  TRISB = 0x00;
  TRISA = 0xFF;
  adc_init();
  lcd_init();
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Cmd(_LCD_CURSOR_OFF);
 
  PWM1_Init(1500);
  PWM1_Start();
  PWM1_Set_Duty(duty);
  
while (1)
{
    temp = adc_read(0);
    temp = temp/2;
    delay_ms(10);
    inttostr(temp,str);
    lcd_out(1,1,str);
     delay_ms(10);
    /////////////////////
 
 
                 if(5<=temp<15)
                 {
                   delay_ms(40);
                   PORTB = 0x01;
                   Delay_ms(10);
                   duty = 50;
                   PWM1_Set_Duty(duty);
                 }
 
              if(15<=temp<20)
                 {
                   delay_ms(40);
                   PORTB = 0x01;
                   duty = 127;
                   PWM1_Set_Duty(duty);
                 }
         
                if(20<=temp<70)
                 {
                   delay_ms(40);
                   PORTB = 0x01;
                   duty = 255;
                   PWM1_Set_Duty(duty);
                 }
 
         else {    Duty = 0; PWM1_Set_Duty(duty); }
}
 
  }

 
Last edited by a moderator:

Remove

Code C - [expand]
1
ADC_Init();



Change these

Code C - [expand]
1
2
3
if(5<=temp<15)
if(15<=temp<20)
if(20<=temp<70)

to


Code C - [expand]
1
2
3
if((temp >= 5) && (temp < 15))
if((temp >= 15) && (temp < 20))
if((temp >= 20) && (temp < 70))



The adc value you are getting should be between 0 to 140. What adc value are you getting on LCD?


Code C - [expand]
1
char str [17];

 
Hi, thank you for your help: on LCD I get the same value as the sensor (LM35DZ).
 

Your temp calculation is wrong.

If your ADC is 10 bit then you get 1023 for 5V

LM35 max o/p is 1.5V for 150 deg C.

If you are measuring 25 to 40 deg C then

o/p will be 0.25V to 0.4V

adc value will be 51.15 to 81.84

temp / 2 =

approx 25 and 40
 
Last edited:
Hi, (please excuse me for my terrible english!!)
First, I tried your suggestion to my code and it's working!!!! thankyou for help!!!
Concerning ADC 10 bit I am not good at this, the LM35dz (0--->100 degree), Temperature Scale output changes by 10 mV per °C.
1°C-->10mV
100°C--->1V,
Vref=5v, so Step = Vref/1024 , in our case its 4.883 mV, that's the minimum voltage our ADC can read.
could you please explaine what dose it mean "adc value = 51.15 to 81.84 ?"
 

If your ADC is 10 bit then you get 1023 for 5V

LM35 max o/p is 1.5V for 150 deg C.

If you are measuring 25 to 40 deg C then

1023 = 5V

306.9 = 1.5V (150 deg C)

25 deg C = 0.25 V = 51.15 raw adc value

40 deg C = 0.4 V = 81.84 raw adc value

o/p of LM35 will be 0.25V to 0.4V for 25 to 40 deg C

adc value will be 51.15 to 81.84

you are dividing this value by 2, so you get

temp / 2 =

51.15/2 = 25.575 = 25 as int is used

81.84/2 = 40.92 = 40 as int is used

temp will be 25 to 40

So, these two conditions never work


Code C - [expand]
1
2
if((temp >= 5) && (temp < 15))
if((temp >= 15) && (temp < 20))



but this will work as temp will be > 20 and < 70


Code C - [expand]
1
if((temp >= 20) && (temp < 70))

 
Last edited:
Hi, yes thank you I really understand now how calculation is wrong, but I still don't understand why you choosen only 25 and 40 instead of other values ? I would be grateful if you explain to me this point.
do you mean that temp must be " Float " to get true calculation..?
 

I just used that range of temperature as an example. You can change the temparature values in if() conditions as you need. If you use float you get real values. You can also make Vref+ = 1.6V, with that you get raw adc value of 1023 for 1.6V and 959.0625 for 1.5V (150 deg C). Multiply 959.0625 by 0.1564 to get 150 deg C.
 
I think every thing is clear now concerning ADC , but how can I configure Vref+ = 1.6V in my program?
 

You have to configure registers like ADCON1 or VRCON so that Vref+ is used in ADC working. Use a voltage divider to drop 5V to 1.6V and provide this voltage to Vref+ pin.

Edit: ADCON1 has to be configured.


Code C - [expand]
1
ADCON1 = 0b00000101;

 
Last edited:
Hi, thank you very much for help, I have a little idea that ADCON1 is used to configurate a pin as A/D input.
with 16F877, If I want to use RA3 to have Vref+ = 1.6V (with voltage divider) and RA0 as Analog input so can I right:
ADCON1 = 0b00000001;
TRISA = 0b00001000; ?
 

Yes you can use that ADCON1 value.

You can use below ADCON1 values for Vref+ + AN0

0b10000001
0b10000011
0b10000101
0b00000001
0b00000011
0b00000101

TRISA value is wrong. It should be 0b00001001. AN0 and Vref+ both should be analog inputs. Vref+ can have any value from 0 to 5V for 5V uC and 0 to 3.3V for 3.3V uC.
 
Thank you jayanth.devarayanadurga it's clear now!!
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top