Thursday, July 21, 2005

HPWM: Pot & FREQOUT: speaker


'set pot into AN0
' OSC=Oscirator 4mhz
' It's not necessary but recommended
DEFINE OSC 4
start:
'This is Analog definifition
DEFINE ADC_BITS 10
' Set number of bits in result
DEFINE ADC_CLOCK 3
' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50
' Set sampling time in uS
ADCvar VAR word
' Create variable to store result
TRISA = %11111111
' Set PORTA to all input
ADCON1 = %10000010
' Set PORTA analog and right justify result
' this means switch

input portb.0
' This means information for motor rotation.
output portd.0
output portd.1
'how to know where pot isoutput portb.7
main:
ADCIN 0, ADCvar
' Read channel 0 to adval
' serout2 PORTC.6, 16468, [DEC ADCvar, 13, 10]
' print it to serial out,
' with linefeed and carriage return (10, 13)
ADCVar = ( ADCvar / 17)
'loop
'If the switch is off
'if ADCVar <= 125 then

high portb.7
'make pin 2 high
'These mean for motor

high portd.1
'make pin 1 low
low portd.0
'PWM comment, duty of square wave , cycle of square wave of frequency
'pwm portd.2,ADCVar,100
'Use HPWM instead of PWM, this is HARDWARE PWM.
'Put HPWM to portc.1 (Only either portc.1 or portc.2 of pic can be used for HPWM )
'This means, use HPWM into portc.2, CCP1 and use ADCvar with 1000 frequency.
'(1000 is appropriate, sometimes 200 is possible as low frequency.)
hpwm 1,ADCVar,1000
'HPWM 2,ADCvar, 1000
FREQOUT PORTd.2,2000,ADCvar*100
'else
'low portb.7
'make pin 2 high
'These mean motor stops
'low portd.1
'make pin 1 low
'low portd.0
'endif
'end loop
GoTo main

----------------------------------------------------

0 Comments:

Post a Comment

<< Home