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.

current control using potentiometer

Status
Not open for further replies.

stereo

Newbie level 6
Newbie level 6
Joined
Jun 23, 2009
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,419
here are my programming. this is a programme that read the voltage(max voltage 5v microcontroller) from analog to digital on the LCD screen.

by using potentiometer, the value can be adjusted to 1024(max) on the lcd. and it is = to 5v.

i really appreciate if someone could help me with this.

>instead of displaying in binary, can i display 5v?

>i would like to add on the value of current max 6A. so on the screen there will be two value. by adjusting the potentiometer to the max, there will be 5v and 6A.




thank you


im using mplab c18 and pic 18f4520


#include <p18f4520.h>
#include <delays.h>
#include <stdlib.h>
int result;
void Init_LCD(void);

void W_ctr_4bit(char);
void w_data_4bit(char);

#define LCD_DATA PORTD
#define LCD_RW
#define LCD_RS
#define LCD_E

unsigned char LCD_TEMP, i;
char MESS[11]= "convertion";
char MESS2[4]= " ";

void InterruptHandlerLow(void);
routine
#pragma code InterruptVectorLow= 0x018
void InterruptVectorLow(void) {
_asm
goto InterruptHandlerLow
routine
_endasm
}
#pragma code
#pragma interruptlow InterruptHandlerLow
void InterruptHandlerLow()
{
if(PIR1bits.TMR1IF){
Delay10TCYx(1);
ADCON0bits.GO=1;
while(ADCON0.bits.DONE);
result= ADRESH*256+ADRESL;
//result>>=4;
PORTB=result;
W_ctr_4bit(0xc0);
for(i=0; i<4;i++)
{W_data_4bit(MESS2);
}
W_ctr_4bit(0xc0);
itoa(result,MESS);
i=0;
while(MESS)
{
W_data_4bit(MESS);
i++;}
TMR1H=0x0b;
TMR1L=0xdc;
PIR1bits.TMR1IF=0;
}
}
void main()
{
TRISD=0;
TRISA=0b11110001;
TRISB=0b11110000;
ADCON0=0b00000001;
ADCON1=0b00001110;
ADCON2=0b10000100;

INTCONbits.GIE=0;
Init_LCD();
for(i=o;i<10;i++)
{
W_data_4bit(MESS);
}
PORTB=0b00000110;
RCONbots.IPEN=1;
IPR1bits.TMR1IP=0;
TMR1H=0x0b;
TMR1L=0xdc;
T1CON=0b11110001;
PIR1bits.TMR1IF=0;
PIR1bits.TMR1IE=1;
INCONbits.GIEL=1;
INCONbits.GIE=1;
while(1);
}
void Init_LCD()
{
Delay1kTCYx(15);
W_ctr_4bit(0x03);
Delay1kTCYx(5);
W_ctr_4bit(0x02);
W_ctr_4bit(0b00101000);
W_ctr_4bit(0b00001100);
W_ctr_4bit(0b00000110);
W_ctr_4bit(0b00000001);
}
void W_ctr_4bit(char x)
{
LCD_RW=0;
LCD_RS=0;
LCD_TEMP=x;
LCD_TEMP>>=4;
LCD_E=1;
LCD_DATA=LCD_TEMP;
Delay1KTCYx(1);
LCD_E=0;
Delay1KTCYx(1);
LCD_TEMP=x;
LCD_TEMP &=0x0f;
LCD_E=1;
LCD_DATA=LCD_TEMP;
Delay1KTCYx(1);
LCD_E=0;
Delay1KTCYx(1)
}


void W_ctr_4bit(char x)
{
LCD_RW=0;
LCD_RS=1;
LCD_TEMP=x;
LCD_TEMP>>=4;
LCD_E=1;
LCD_DATA=LCD_TEMP;
Delay1KTCYx(1);
LCD_E=0;
Delay1KTCYx(1);
LCD_TEMP=x;
LCD_TEMP &=0x0f;
LCD_E=1;
LCD_DATA=LCD_TEMP;
Delay1KTCYx(1);
LCD_E=0;
Delay1KTCYx(1)
}
 

vijay s

Full Member level 3
Full Member level 3
Joined
Jun 14, 2008
Messages
159
Helped
27
Reputation
54
Reaction score
9
Trophy points
1,298
Location
Coimbatore, India
Activity points
2,152
i thing you asking for how to convert the voltage in binary to actual voltage to display

refvoltage=5v , resolution= 10bit = 1024 steps
single step resolution = 5/1024 = 4.88mv

so each step size indicates 4.88mv

if you have 1024 as your value then 1024*4.88 = 5

this is the thing you have to displat
 

stereo

Newbie level 6
Newbie level 6
Joined
Jun 23, 2009
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,419
alright noted. so from the code, where should i change? ive tried modify it at
result= ADRESH*256+ADRESL to
result= ADRESH*256+ADRESL/205

and it doesnt make any different. still display in binary.
 

vijay s

Full Member level 3
Full Member level 3
Joined
Jun 14, 2008
Messages
159
Helped
27
Reputation
54
Reaction score
9
Trophy points
1,298
Location
Coimbatore, India
Activity points
2,152
Code:
result= ADRESH*256+ADRESL/205

that is not 205... it should be 5/1024

Code:
int temp,result;

temp   = (ADRESH * 256) + ADRESL;       //Make as 16-bit WORD
result  =  (5/1024) * temp;

still the result is in HEX Code.. to display in LCD you have to convert it to ASCII...

your compiler might be having library routine to convert HEX to ASCII.. convert and display

reply what compiler u are using...

__
click helped button if this helps u
 

    stereo

    Points: 2
    Helpful Answer Positive Rating

kender

Advanced Member level 4
Advanced Member level 4
Joined
Jun 19, 2005
Messages
1,421
Helped
138
Reputation
276
Reaction score
39
Trophy points
1,328
Location
Stanford, SF Bay Peninsula, California, Earth, Sol
Activity points
9,975
vijay s said:
Code:
int temp,result;
result  =  (5/1024) * temp;
Not to be nitpicky, but I suspect that result will always be zero, because integer division 5/1024 is always zero. If that ends up being the case, try:
Code:
result  =  (5 * temp) / 1024;
 

    stereo

    Points: 2
    Helpful Answer Positive Rating

stereo

Newbie level 6
Newbie level 6
Joined
Jun 23, 2009
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,419
alright.
result = (5 * temp) / 1024; will display not in binary anymore. it display from 0 to 4. Thnx kender!

ok...how can i put decimal point to make it more accurate? currently, the max it shows only 4 which i believed it should be 4.8++ close to 5.

thnx
 

vijay s

Full Member level 3
Full Member level 3
Joined
Jun 14, 2008
Messages
159
Helped
27
Reputation
54
Reaction score
9
Trophy points
1,298
Location
Coimbatore, India
Activity points
2,152
multiply with 100 and add decimal point

result = result * 100;

and add decimal point before two decimal value

i hope this work...
 

stereo

Newbie level 6
Newbie level 6
Joined
Jun 23, 2009
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,419
ok ive tried put the value in both line.
still it didnt show any different.


PORTB=result*100;
or
result= ADRESH*256+ADR*100;
so where should i alter?
 

vijay s

Full Member level 3
Full Member level 3
Joined
Jun 14, 2008
Messages
159
Helped
27
Reputation
54
Reaction score
9
Trophy points
1,298
Location
Coimbatore, India
Activity points
2,152
Code:
result  =  (5 * temp) / 1024;

with this result in HEX use

Code:
unsigned char Buf[6];
result = result * 100;
uitoa(result,Buf);          //This converts HEX to ASCII

Now if ur maximum result will be like 488


it will be stored as Buf[0] = 4,Buf[1] = 8, Buf[2] = 8

before displaying this in LCD add decimal point after Buf[0]
 

    stereo

    Points: 2
    Helpful Answer Positive Rating

stereo

Newbie level 6
Newbie level 6
Joined
Jun 23, 2009
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,419
alright!!! almost got it. i can now adjust untill almost 5v. but theres 1 problem. the highest i could go is 499v without the decimal. supposedly 4.99v.

here's my latest program;


include <p18f4520.h>
#include <delays.h>
#include <stdlib.h>
float result;
void Init_LCD(void);

void W_ctr_4bit(char);
void W_data_4bit(char);

#define LCD_DATA PORTD
#define LCD_RW PORTAbits.RA2
#define LCD_RS PORTAbits.RA3
#define LCD_E PORTAbits.RA1

unsigned char LCD_TEMP, i;
char MESS[16]= "VOLTAGE CONTROL";
char MESS2[10]= " . v 0.6A";
unsigned char Buf[6];

void InterruptHandlerLow(void);
float temp,result;
#pragma code InterruptVectorLow= 0x018
void InterruptVectorLow(void) {
_asm
goto InterruptHandlerLow

_endasm
}
#pragma code
#pragma interruptlow InterruptHandlerLow
void InterruptHandlerLow()
{
if(PIR1bits.TMR1IF){
Delay10TCYx(1);
ADCON0bits.GO=1;
while(ADCON0bits.DONE);

temp = (ADRESH * 256) + ADRESL; //Make as 16-bit WORD
result = (5 * temp) / 1024;
result = result*100 ;

//result>>=4;
PORTB=result;
W_ctr_4bit(0xc0);
for(i=0; i<9; i++)
{W_data_4bit(MESS2);
}
W_ctr_4bit(0xc0);
itoa(result,MESS);
i=0;
while(MESS)
{
W_data_4bit(MESS);
i++;}
TMR1H=0x0b;
TMR1L=0xdc;
PIR1bits.TMR1IF=0;
}
}
void main()
{
TRISD=0;
TRISA=0b11110001;
TRISB=0b11110000;
ADCON0=0b00000001;
ADCON1=0b00001110;
ADCON2=0b10000100;

INTCONbits.GIE=0;
Init_LCD();
for(i=0;i<15;i++)
{
W_data_4bit(MESS);
}
PORTB=0b00000110;
RCONbits.IPEN=1;
IPR1bits.TMR1IP=0;
TMR1H=0x0b;
TMR1L=0xdc;
T1CON=0b11110001;
PIR1bits.TMR1IF=0;
PIE1bits.TMR1IE=1;
INTCONbits.GIEL=1;
INTCONbits.GIE=1;
while(1);
}
void Init_LCD()
{
Delay1KTCYx(15);
W_ctr_4bit(0x03);
Delay1KTCYx(5);
W_ctr_4bit(0x02);
W_ctr_4bit(0b00101000);
W_ctr_4bit(0b00001100);
W_ctr_4bit(0b00000110);
W_ctr_4bit(0b00000001);
}
void W_ctr_4bit(char x)
{
LCD_RW=0;
LCD_RS=0;
LCD_TEMP=x;
LCD_TEMP>>=4;
LCD_E=1;
LCD_DATA=LCD_TEMP;
Delay1KTCYx(1);
LCD_E=0;
Delay1KTCYx(1);
LCD_TEMP=x;
LCD_TEMP &=0x0f;
LCD_E=1;
LCD_DATA=LCD_TEMP;
Delay1KTCYx(1);
LCD_E=0;
Delay1KTCYx(1);
}


void W_data_4bit(char x)
{
LCD_RW=0;
LCD_RS=1;
LCD_TEMP=x;
LCD_TEMP>>=4;
LCD_E=1;
LCD_DATA=LCD_TEMP;
Delay1KTCYx(1);
LCD_E=0;
Delay1KTCYx(1);
LCD_TEMP=x;
LCD_TEMP &=0x0f;
LCD_E=1;
LCD_DATA=LCD_TEMP;
Delay1KTCYx(1);
LCD_E=0;
Delay1KTCYx(1);
}


thnx!
 

stereo

Newbie level 6
Newbie level 6
Joined
Jun 23, 2009
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,419
ok that program could only display 499v max.(supposedly 4.99v)

>before displaying this in LCD add decimal point after Buf[0]


where exactly can i put th decimal point?
 

vijay s

Full Member level 3
Full Member level 3
Joined
Jun 14, 2008
Messages
159
Helped
27
Reputation
54
Reaction score
9
Trophy points
1,298
Location
Coimbatore, India
Activity points
2,152
where exactly can i put the decimal point?

in Main routine you are sending the LCD data byte by byte.. after sending Buf[0] send '.' followed by remaining character....

That program could only display 499v max

the problem might be with arithmetic calculation. 0.01% error is acceptable one
 

stereo

Newbie level 6
Newbie level 6
Joined
Jun 23, 2009
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,419
im still trying to display with decimal point. what a noob i am.

don't mind the voltage now.

ok with some calculation, i would like to display the current as well.
(the amount of voltage displayed / 0.01ohm resistance)

when i adjust the potentiometer, both (voltage and current) will change.
i've tried edit the program in many ways but still couldnt achieved it.
your expertise is well apreciated.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top