Heat - Fan Controller Project (PIC16F88)

Thread Starter

Refugees

Joined Nov 19, 2012
1
Hi there.

I am currently in the progress of of making myself a stand with a fan for my laptop.
I am using a PIC16F88, Thernistor 4k7 and a 5V Fan, with a home made plastic base.
I am using a Pickit 3.

My board is finished and the layout is as follows:
* Terminator --> RA2 / AN2 Port
* Fan ----> RB0 / CCP1 Port

MPLAB programming and I am struggling heavily.
Here is my code so far, I cannot go any further as I don't know the rest:

Rich (BB code):
;List all PIC16F88 Files that are needed to run the program

List p=PIC16F88
#INCLUDE <P16F88.INC>

__CONFIG    _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_OFF & _WDT_OFF & _INTRC_IO
__CONFIG    _CONFIG2, _IESO_OFF & _FCMEN_OFF

	ORG 0x0000					;Force program to start at address 0
	goto start					;Go to the start loop

;-----------------------------------------------------------------------------	
;Interrupt - Motor Speed Control

ISR:							;Interrupt

	bcf	PIR1,ADIF			;Clear AD interrupt flag
	bsf 	STATUS,RP0			;Go to bank 1
	movlw 	b'00000000'			;Making all TRISB pins outputs
	movwf 	TRISB				;Get to TRISB
	moVLW 	ADRESH				;ADC result byte
	movwf 	PR2					;Period register
        bcf 	STATUS,RP0 			;Go to back 0
        movlw	b'00001100'			;Set control register pins
	movwf	CCP1CON				;PWM control register
	bsf 	T2CON, TMR2ON		;Swtches on timer
	movlw	.100				;Set the speed of the motor
	movwf 	CCPR1L				;PWM low byte
	
retfie

;-----------------------------------------------------------------------------	
;Start of the inital program

start:							;Begin the start loop

	bsf 	INTCON,PEIE			;Set interrupt enable
	bsf	PIE1,ADIE			;Set AD interrupt enable
        nop							;No operation
	bsf 	STATUS,RP0			;Go to bank 1 
        movlw 	b'00000001'			;Set TRISA pins
        movwf 	TRISA				;Set pins in TRISA
        movlw 	b'10111111'			;Set TRISB pins
	movwf 	TRISB				;Set pins in TRISB
	bsf	INTCON,GIE			;Global interrupt enable

;-----------------------------------------------------------------------------
;Analog to Digital Converter

	clrf 	ANSEL				;Clear analog select

	movlw 	b'00000100'			;Set pin to analog
	movlw 	ANSEL				;Analog pin select register

	movlw 	b'10000000' 		;Reference voltage to external pins
	movwf 	ADCON1				;ADC control register

	movlw 	b'10000101' 		;Reference voltage to external pins
	movwf 	ADCON0				;ADC control register

;-----------------------------------------------------------------------------	
;Motor Speed Control
 
;    btfss	ADCON0,GO_DONE      ;This bit will change to zero when the conversion is complete   
;    goto	$-1                 ;So loop till it does. 

;-----------------------------------------------------------------------------
;End of Program

	goto $						;Endless loop

	end							;End the program
Can someone please help me to finish my code
I would love to see my fan in action

Thanks so much!
Refugees
 

tshuck

Joined Oct 18, 2012
3,534
I can tell you one thing after briefly looking at your code...

Your A/D result should not be going into the PR2 register. The PR2 register sets your PWM frequency and should be left alone after initialization, unless you mean to be changing your frequency. If you are trying to change your frequency, then you probably need to go back through PWM again. ;)

Your result should be going to the CCPR1L register and to the CCP1CON<5:4> bits as stated in the PIC16F88 datasheet pg. 86.

Also, you are going to need to setup your CCP module, look at pages 86 & 87....
 
Last edited:

John P

Joined Oct 14, 2008
2,025
I'm very reluctant to get into someone else's assembly code, even if you have used the <code> tags and written a literate message. This would be a lot easier if you started using a compiler.

But one thing I noticed immediately--shouldn't the interrupt start with
ORG 0x0004

?
 

tshuck

Joined Oct 18, 2012
3,534
I'm very reluctant to get into someone else's assembly code, even if you have used the <code> tags and written a literate message. This would be a lot easier if you started using a compiler.
I have to agree with this. The only reason I even looked at your code, OP, is because you named the registers so as to be readable... :)

The XC8 compiler from Microchip has a free version based off of the older HiTech compiler. It is quite nice and allows you to program your μC in C as opposed to assembly...

But one thing I noticed immediately--shouldn't the interrupt start with
ORG 0x0004
?
Yes, it should.. I missed that when I looked at this...
 
Top