Friday, August 05, 2005
Final Project = AIR SCREEN SHOW =
'set m-sensor into AN0
'set motor PWM into PORTD.1
'set speaker PWM into PORTD.0
' 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 10
' Set sampling time in uS
' Usually 50 but 10 or 50 not so much different
' Create variable to store result
'motor value, sound value
ADCvar VAR word
motorVal VAR byte
soundVal var byte
' Set PORTA to all input
TRISA = %11111111
' Set PORTA analog and right justify result
ADCON1 = %10000010
'set Lighting Trigger OUTPUT into portb.7
'set LED of FAN OUTPUT into portb.6
OUTPUT portb.7
OUTPUT portb.6
OUTPUT portb.5
OUTPUT portb.4
OUTPUT portb.3
main:
ADCIN 0, ADCvar
' Read channel 0 to adval
'ADCvar = ADCVar/4
serout2 PORTC.6, 16468, [DEC ADCvar, 13, 10]
' print it to serial out, '
' with linefeed and carriage return (10, 13)
'If I stay closer to the FAN, the FAN will start to rotate,
'simultaneouly speaker, lighting trigger too.
if ADCVar >= 100 then
HIGH portb.7
HIGH portb.6
HIGH portb.5
LOW portb.4
LOW portb.3
motorVal = ADCVar/8
soundVal = ADCVar/8
HIGH portd.1
'pwm PORTD.1,motorVal,10
PWM PORTD.0,soundVal,10
else
LOW PORTD.0
LOW PORTD.1
LOW portb.7
LOW portb.6
LOW portb.5
HIGH portb.4
HIGH portb.3
endif
'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 for MOTOR
'(1000 is appropriate, sometimes 200 is possible as low frequency.)
'FREQOUT PORTd.2,2000,ADCvar*100
'This is new syntax for speaker
'This means FREQOUT into PORTd.2 as 2000 and ADCvar X 100
'(FREQOUT needs a lot of number)
GoTo main