Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

nd help in writing the following program...

Status
Not open for further replies.

zak4546

New Member
my 10k pot has a range between 3 to 5 v to control a motor.. i want to compare the analogue values wif the a/d converted results and display it on leds. since i m doin 8 speed, i have divided accordingly and did the 10 bit digital conversion.so this are the expected digital outputs at which i want the respective LEDs to light up:

2.9v - 1001010001
3.2v - 1010001110
3.5v - 1011001100
3.8v - 1100001001
4.1v - 1101000110
4.4v - 1110000100
4.7v - 1111000001
5v - 1111111111

how do i go abt writing the program for this area..pls help! i m using
pic16F876a..
 
Mostly motors are controlling using PWM anyway!

Its better if you use 8bit conversion rather than 10bit.Otherwise you have to write a lot.8 bit is ok in your case to light up 8 led's.

Just read the AD value & compare with your reference values if it is equal just set a bit.This is the bit you sending to display.Don't clear that bit until you get the newer value.
 
Last edited:
how do i convert the values to 8 bit...do i juz read the values from the adreshl (right justified) or is there a calculation for for 8 bit conversionit? btw i would be greatful if u could juz show me a sample code. thx a lot for your reply.really meant a lot..
 
Just configure it justified as 8 bit, there's nothing complicated, if you only want 8 bits you just use the top eight bits, and the PIC is cunningly designed to make that trivial to do.
 
thanx for the help guys.i will write the program and then update u... i am new to this and i really hopes this works...
 
This is the program i wrote.but it doesn't seem to work...i really nd you guys to guide mr thro this...btw its a 8 speed dc motor controller.Any solutions?


#include<p16f876a.inc>
__config _hs_osc & _wdt_off & _pwrte_on & _lvp_off
errorlevel -302 ;Suppress bank warning

;**************** Label Definition ********************
;==========================================================================
;
; Register Definitions
;
;==========================================================================
_wdt_off EQU H'3FFB'
_pwrte_on EQU H'3FF7'
_hs_osc EQU H'3FFE'
_lvp_off EQU H'3F7F'
w EQU H'0000'
f EQU H'0001'

;----- Register Files------------------------------------------------------


status EQU H'0003'
portb EQU H'0006'
intcon EQU H'000B'
pir2 EQU H'000D'
tmr1l EQU H'000E'
tmr1h EQU H'000F'
t1con EQU H'0010'
pir1 EQU H'000C'
pie1 EQU H'008C'
tmr2 EQU H'0011'
t2con EQU H'0012'
ccp1con EQU H'0017'
ccpr2l EQU H'001B'
ccpr2h EQU H'001C'
ccp2con EQU H'001D'
adcon0 EQU H'001F'
trisa EQU H'0085'
trisb EQU H'0086'
trisc EQU H'0087'
pr2 EQU H'0092'
adcon1 EQU H'009F'
ccpr1l EQU H'0015'
pie2 EQU H'008D'
adresh EQU H'001E'
adresl EQU H'009E'
;----- STATUS Bits --------------------------------------------------------


rp0 EQU H'0005'

z EQU H'0002'
c EQU H'0000'




;----- ADCON0 Bits --------------------------------------------------------


go EQU H'0002'



speed1 equ b'01010001'
speed2 equ b'10001110'
speed3 equ b'11001100'
speed4 equ b'00001001'
speed5 equ b'01000110'
speed6 equ b'10000100'
speed7 equ b'11000001'
speed8 equ b'11111111'

;**************** Program Start ***********************
org 0 ;Reset Vector
goto init
org 4 ;Interrupt Vector
goto int

;**************** Initial Process *********************
init

;*** Port initialization
bsf status,rp0 ;Change to Bank1
movlw b'00000001' ;AN0 to input mode
movwf trisa ;Set TRISA register
clrf trisb ;Set TRISB to uotput mode
clrf trisc ;Set TRISC to output mode
bcf status,rp0 ;Change to Bank0

;*** A/D converter initialization
movlw b'10000001' ;ADCS=10 CHS=AN0 ADON=ON
movwf adcon0 ;Set ADCON0 register
bsf status,rp0 ;Change to Bank1
movlw b'00001110' ;ADFM=0 PCFG=1110
movwf adcon1 ;Set ADCON1 register
bcf status,rp0 ;Change to Bank0

;*** PWM initialization
clrf tmr2 ;Clear TMR2 register
movlw b'01111101' ;duty cylcle(50%)
movwf ccpr1l ;Set CCPR1L register
bsf status,rp0 ;Change to Bank1
movlw d'61' ;
movwf pr2 ;Set PR2 register
bcf status,rp0 ;Change to Bank0
movlw b'00000110' ;Pst=1:1 TMR2=ON Pre=1:16
movwf t2con ;Set T2CON register
movlw b'00001100' ;CCP1XY=0 CCP1M=110(PWM)
movwf ccp1con ;Set CCP1CON register

;*** Compare mode initialization
clrf tmr1h ;Clear TMR1H register
clrf tmr1l ;Clear TMR1L register
movlw h'61' ;H'61A8'=25000
movwf ccpr2h
movlw h'A8'
movwf ccpr2l
movlw b'00000001' ;Pre=1:1 TMR1=Int TMR1=ON
movwf t1con ;Set T1CON register
movlw b'00001011' ;CCP2M=1011(Compare)
movwf ccp2con ;Set CCP2CON register

;*** Interruption control
clrf pir1
bsf status,rp0 ;Change to Bank1
movlw b'00000001' ;CCP2IE=Enable
movwf pie2 ;Set PIE2 register
bcf status,rp0 ;Change to Bank0
movlw b'11000000' ;GIE=ON PEIE=ON
movwf intcon ;Set INTCON register

wait
goto $ ;Interruption wait

;*************** Interruption Process *****************
int
clrf pir2 ;Clear interruption flag
ad_check
btfsc adcon0,go ;A/D convert end ?
goto ad_check ;No. Again
bsf status,rp0
movf adresl,w ;Read ADRESH register
sublw speed1,w ;Ref speed - Detect speed
btfss status,z ;Reference < Detect ?
goto led2 ;No. Jump to > or = check
bcf status,rp0
movlw b'00000001'
goto int_end
led2
bsf status,rp0
movf adresl,w ;Read ADRESH register
sublw speed2,w ;Ref speed - Detect speed
btfss status,z ;Reference < Detect ?
goto led3
bcf status,rp0
movlw b'00000011'
goto int_end
led3
bsf status,rp0
movf adresl,w ;Read ADRESH register
sublw speed3,w ;Ref speed - Detect speed
btfss status,z ;Reference < Detect ?
goto led4
bcf status,rp0
movlw b'00000111'
goto int_end
led4
bsf status,rp0
movf adresl,w ;Read ADRESH register
sublw speed4,w ;Ref speed - Detect speed
btfss status,z ;Reference < Detect ?
goto led5
bcf status,rp0
movlw b'00001111'
goto int_end
led5
bsf status,rp0
movf adresl,w ;Read ADRESH register
sublw speed5,w ;Ref speed - Detect speed
btfss status,z ;Reference < Detect ?
goto led6
bcf status,rp0
movlw b'00011111'
goto int_end
led6
bsf status,rp0
movf adresl,w ;Read ADRESH register
sublw speed6,w ;Ref speed - Detect speed
btfss status,z ;Reference < Detect ?
goto led7
bcf status,rp0
movlw b'00111111'
goto int_end
led7
bsf status,rp0
movfw adresl ;Read ADRESH register
sublw speed7,w ;Ref speed - Detect speed
btfss status,z ;Reference < Detect ?
goto led8
bcf status,rp0
movlw b'01111111'
goto int_end
led8
bsf status,rp0
movf adresl,w ;Read ADRESH register
sublw speed8,w ;Ref speed - Detect speed
btfss status,z ;Reference < Detect ?
goto ad_check
movlw b'11111111'
goto int_end

;************ END of Interruption Process **************
;int_end
; movwf portb ;Set PROTB
; retfie

;********************************************************
; END of DC motor speed controller
;********************************************************

end
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top