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.

Using PIC18F4520 TO control a DC 12V FAN.ANYONE hav the CODE

Status
Not open for further replies.

falcoz

Newbie level 6
Joined
Jun 8, 2009
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,386
PLs help me cos i just need this part to complete the whole project.
 

Which language and compiler?

Added after 7 minutes:

Here's a code in C18:
Code:
#include <p18f4520.h>

void interrupt_at_high_vector(void);
void high_isr(void);

#pragma config OSC = XT //Crystal oscillator
#pragma config WDT = OFF //Watchdog Timer off
#pragma config LVP = OFF //LVP off
#pragma config MCLRE = OFF //MCLR off
unsigned char ADCR;

void main(void){
	CMCON = 7; //DISABLE COMPARATOR
	TRISA = 1; //AN0 INPUT
	TRISC = 0; //CCP1 OUTPUT
	PORTA = 0;
	PORTC = 0;
	ADCON0 = 1; //ADC ON
	ADCON1 = 0x0E; //CH0 ANALOG
	ADCON2 = 0x3E; //20TAD, FOSC/64
	PR2 = 64; //APPROX. 1KHZ
	CCPR1L = 32; //START WITH 50% DUTY CYCLE
	CCP1CON = 0x0C; //PWM
	PIR1 = 0;
	T2CON = 0x04; //START TMR2
	while (1){
		ADCON0bits.GO_DONE = 1; //START CONVERSION
		while (ADCON0bits.GO_DONE); //WAIT FOR END OF CONVERSION
		ADCR = ADRESH; //ADC Result
		ADCR >>= 2; //DIVIDE BY 4 TO BRING TO SCALE OF 64
		while (!PIR1bits.TMR2IF); //WAIT FOR TMR2 INTERRUPT
		CCPR1L = ADCR; //UPDATE DUTY CYCLE
	}
}

Added after 6 minutes:

Here's the circuit for the code.
19_1263317184.jpg

Hope it helped.
Tahmid.

Added after 20 minutes:

Hi,
If required, I can provide the code in ASM, mikroBASIC or mikroC.
 

Re: Using PIC18F4520 TO control a DC 12V FAN.ANYONE hav the

Thanks tahmid,
my project is actually on fuel cell power supply,
the fan is to cool the fuel cell stack when it is over a certain temperature.
can show me how to use if...else to control the fan like:
if the fuel cell stack temperature is above 40degrees than the fan will increase the speed,if below 40degree the fan will slow down.
 

Hi,
You have to use a temperature sensor, preferably, something simple like LM35 and use ADC to sense temperature and increase/decrease PWM, like:
Code:
#include <p18f4520.h>

#pragma config OSC = XT //Crystal oscillator
#pragma config WDT = OFF //Watchdog Timer off
#pragma config LVP = OFF //LVP off
#pragma config MCLRE = OFF //MCLR off
unsigned char ADCR, Duty, temp;

void main(void){
	CMCON = 7; //DISABLE COMPARATOR
	TRISA = 1; //AN0 INPUT
	TRISC = 0; //CCP1 OUTPUT
	PORTA = 0;
	PORTC = 0;
	ADCON0 = 1; //ADC ON
	ADCON1 = 0x0E; //CH0 ANALOG
	ADCON2 = 0x3E; //20TAD, FOSC/64
	PR2 = 64; //APPROX. 1KHZ
	CCPR1L = 32; //START WITH 50% DUTY CYCLE
	CCP1CON = 0x0C; //PWM
	PIR1 = 0;
	T2CON = 0x04; //START TMR2
	Duty = 32;
	while (1){
		ADCON0bits.GO_DONE = 1; //START CONVERSION
		while (ADCON0bits.GO_DONE); //WAIT FOR END OF CONVERSION
		ADCR = ADRESH; //ADC Result
		if (ADCR > 20) { //If temperature is greater than 40'C, increase duty cycle
			Duty++;
			if (Duty > 63)
				Duty = 63;
		}
		else { //Else, decrease duty cycle
			Duty--;
			if (Duty < 1)
				Duty = 1;
		}
		while (!PIR1bits.TMR2IF); //WAIT FOR TMR2 overflow
		PIR1bits.TMR2IF = 0;
		CCPR1L = Duty; //UPDATE DUTY CYCLE
		for (temp=0; temp<255; temp++){
			while (!PIR1bits.TMR2IF); //Wait for 10 TMR2 overflows
			PIR1bits.TMR2IF = 0;
		}
	}
}
88_1263809573.jpg
 

Re: Using PIC18F4520 TO control a DC 12V FAN.ANYONE hav the

Using ADC & display voltage value to LCD Display:
result= ADRESH*256+ADRESL; // ADC Monitor
x=result/204.6; // Calculate & result in INT (x = ??)
y=result/204.6; // Calculate & result in DOUBLE (y = ??.????)
z=y-x; // Resulting z = ??.???? - ?? = 0.????
a=z*100;

hi tahmid,one last question,thanks alot by the way
i have doubt why i use decimal calculation for my ADC but LCD cant display according to the calculation in theory?
 

Re: Using PIC18F4520 TO control a DC 12V FAN.ANYONE hav the

hey tahmid..thanks alot man i found the way to do the voltage already dun need to reply.thanks man..
last favour from u can?
u mind we chat on msn? cos i need to ask u things or exchange email if u dun mind
 

Hi,
You can PM me or mail me at inferno-rage(at)hotmail.com
It'd be best if you ask over here 'cos there'll be plenty of people to help.
Tahmid.
 

Re: Using PIC18F4520 TO control a DC 12V FAN.ANYONE hav the

hi tahmid, i got a problem.this is my program.
i cannot display the "standby"
this project is to display standby when there is power ,if i turn the power down to 0v the display "FC power on" come out and i can turn the potentiometer to display voltage and current.can u help e to see what wrong with my code

#include <p18f4520.h>
#include <delays.h>
#include <stdlib.h>
int result,x,a,c,w,e;
double y,z,f,g;
void Init_LCD(void);
void PTM(void);
void W_ctr_4bit(char);
void W_data_4bit(char);

#define LCD_DATA PORTD
#define LCD_RW PORTAbits.RC4
#define LCD_RS PORTAbits.RC5
#define LCD_E PORTAbits.RC3

unsigned char LCD_TEMP, i;
char MESS[8]="standby";
char MESS2[10]="FC PWR ON";
char VOLT[3]="V=";
char VOLT2[2]="V";
char DOT[2]=".";
char CURR[3]="C=";
char CURR2[3]="uA";
//char POWR[3]="P=";
//char TEMP[5]="TEM=";
//char TEMP2[2]="C";
char BLANK[5]=" ";
void InterruptHandlerLow(void);
float mps,temp;
#pragma code InterruptVectorLow= 0x018
void InterruptVectorLow(void)

{

_asm
goto InterruptHandlerLow

_endasm

}

#pragma code
#pragma interruptlow InterruptHandlerLow
void InterruptHandlerLow()

{
if (INTCONbits.TMR0IF) // Activate Timer0
{
TMR0H = 0xF8; // Set Timer0's Interval ON for 4m Sec
TMR0L = 0x2F;

w++;
PTM();

INTCONbits.TMR0IF = 0;
}
if(PIR1bits.TMR1IF)
{
if(g>0)//
{
W_ctr_4bit(0x80);
for(i=0; i<9; i++)
{

W_data_4bit(MESS);

}
else if(g=<0)//
{
W_ctr_4bit(0x80);
for(i=0; i<11; i++)
{

W_data_4bit(MESS2);

}
delay10TCYx(1);//
Enable=1;
}
else
{
Enable=0;
}

if(Enable=1)
{
W_ctr_4bit(0x80);
for(i=0; i<4; i++)
{

W_data_4bit(VOLT);

}
W_ctr_4bit(0x82);
itoa(x,BLANK);
i=0;
while(BLANK)
{
W_data_4bit(BLANK);
i++;
}
W_ctr_4bit(0x83);
for(i=0; i<3; i++)
{

W_data_4bit(DOT);
}
W_ctr_4bit(0x84);
itoa(a,BLANK);
i=0;
while(BLANK)
{
W_data_4bit(BLANK);
i++;
}
W_ctr_4bit(0x86);
for(i=0; i<3; i++)
{

W_data_4bit(VOLT2);

}

c=y*1000;
W_ctr_4bit(0x88);
for(i=0; i<4; i++)
{

W_data_4bit(CURR);

}
W_ctr_4bit(0x8A);
itoa(c,BLANK);
i=0;
while(BLANK)
{
W_data_4bit(BLANK);
i++;
}
W_ctr_4bit(0x8E);
for(i=0; i<4; i++)
{

W_data_4bit(CURR2);

}


/*W_ctr_4bit(0xc0);
for(i=0; i<4; i++)
{

W_data_4bit(POWR);

}*/
}
else
{
Enable=0;
}
TMR1H=0x0b;
TMR1L=0xdc;
PIR1bits.TMR1IF=0;
}
}
void main()
{
TRISA=0b11101111;
TRISB=0b11110000;
TRISC=0b10000010;
TRISD=0;
ADCON0bits.ADON = 1; // Enable ADC
ADCON1 = 0x0D; // Enable AN0~AN1 while others are Digital I/O
ADCON2 = 0x84; // Right justified, manual acquistion, Fosc/4

INTCONbits.GIE=0;
Init_LCD();
/*for(i=0;i<15;i++)
{
W_data_4bit(MESS);
}*/
PORTB=0b00000110;
RCONbits.IPEN=1;
TMR0H = 0xF8; // Set Timer0's Interval OFF for 4m Sec
TMR0L = 0x2F;
IPR1bits.TMR1IP=0;
TMR1H=0x0b;
TMR1L=0xdc;
T0CON = 0x80; // Set Timer0's Setup & 16-bit mode, Internal Clk=4/4MHz, Prescaler=2
T1CON=0b11110001;
PIR1bits.TMR1IF=0;
PIE1bits.TMR1IE=1;
INTCON = 0xE4; // Enable Global Interrupt w Enable TMR0's Interrupt W Set TMR0's Flag
while(1);
}
void PTM()
{
if(w == 0)
{
ADCON0bits.CHS0 = 0;
ADCON0bits.CHS1 = 0;
ADCON0bits.CHS2 = 0;
ADCON0bits.CHS3 = 0;
}

else if(w == 1)
{
ADCON0bits.CHS0 = 1;
ADCON0bits.CHS1 = 0;
ADCON0bits.CHS2 = 1;
ADCON0bits.CHS3 = 0;
}
else
w = -1;

ADCON0bits.GO = 1;
while(!ADCON0bits.GO);

if(w == 0) //main supply
{
main = ADRESH * 256 + ADRESL;
e=(5*temp)/1024;
f=(5*temp)/1024;
g=f-e;
}

else if(w == 1) //FC supply
{
temp = ADRESH * 256 + ADRESL;
x=(5*temp)/1024;
y=(5*temp)/1024;
z=y-x;
a=z*100;
}
}

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);
}
 

Hi falcoz,
I'm not very comfortable with C coding. I can do it in bits and pieces but for bigger codes, I use mikroBASIC.
Tahmid.
 

Re: Using PIC18F4520 TO control a DC 12V FAN.ANYONE hav the

Tahmid said:
Which language and compiler?

Added after 7 minutes:

Here's a code in C18:
Code:
#include <p18f4520.h>

void interrupt_at_high_vector(void);
void high_isr(void);

#pragma config OSC = XT //Crystal oscillator
#pragma config WDT = OFF //Watchdog Timer off
#pragma config LVP = OFF //LVP off
#pragma config MCLRE = OFF //MCLR off
unsigned char ADCR;

void main(void){
	CMCON = 7; //DISABLE COMPARATOR
	TRISA = 1; //AN0 INPUT
	TRISC = 0; //CCP1 OUTPUT
	PORTA = 0;
	PORTC = 0;
	ADCON0 = 1; //ADC ON
	ADCON1 = 0x0E; //CH0 ANALOG
	ADCON2 = 0x3E; //20TAD, FOSC/64
	PR2 = 64; //APPROX. 1KHZ
	CCPR1L = 32; //START WITH 50% DUTY CYCLE
	CCP1CON = 0x0C; //PWM
	PIR1 = 0;
	T2CON = 0x04; //START TMR2
	while (1){
		ADCON0bits.GO_DONE = 1; //START CONVERSION
		while (ADCON0bits.GO_DONE); //WAIT FOR END OF CONVERSION
		ADCR = ADRESH; //ADC Result
		ADCR >>= 2; //DIVIDE BY 4 TO BRING TO SCALE OF 64
		while (!PIR1bits.TMR2IF); //WAIT FOR TMR2 INTERRUPT
		CCPR1L = ADCR; //UPDATE DUTY CYCLE
	}
}

Added after 6 minutes:

Here's the circuit for the code.
19_1263317184.jpg

Hope it helped.
Tahmid.

Added after 20 minutes:

Hi,
If required, I can provide the code in ASM, mikroBASIC or mikroC.
pl provide me it ASM or hex code in 16f676. pl
 

Re: Using PIC18F4520 TO control a DC 12V FAN.ANYONE hav the

its ok tahmid..now project is to use the fan to cool the fuel cell stack when the temperature is high.and the LCD display is to monitor the voltage,current,air pressure and temperature.after thaT I NEED to interface with the computer to display the readings of those four.u learn usart b4?if u have the schemetic diagram of pic18f4520 mcu board pls send me.thanks
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top