Step 3The Code
Comments included.
#include <16F887.h>
#include <delay.h>
#include "delay.c"
#include <stdlib.h>
#include <STRING.h>
#use delay(clock = 4000000)
#FUSES INTRC,NOWDT,NOPUT,NOMCLR,NOPROTECT,NOCPD,NOBROWNOUT,NOIESO,NOFCMEN,NOLVP
#byte CCP1CON = 0x17
#byte CCP2CON = 0x1D
#byte PWM1CON = 0x9B
int value = 128;
int p1 = 0;
int p2 = 0;
void my_setup_ccp1(int8 value)
{
output_low(PIN_C2);
CCP1CON = value;
PWM1CON = 0;
}
void my_setup_ccp2(int8 value)
{
output_low(PIN_C1);
CCP2CON = value;
}
//===================================
void main()
{
//A4 = power source for photodiode
output_high(PIN_A4);
output_high(PIN_B1);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
setup_adc_ports(sAN0);
//Timer/Interrupt setup
enable_interrupts(INT_TIMER2);
my_setup_ccp1(CCP_PWM);
my_setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 128, 1);
//setup_compare(2,COMPARE_PWM|COMPARE_TIMER2);
while(1){ // Prevent PIC from going to sleep.
//SET PWM DUTY CYCLE
output_high(PIN_A5);
//Pin A3 is the photodiode connection
if(input(PIN_A3) == 1)
output_high(PIN_A4);
else
output_low(PIN_A4);
//Read value of potentiometer to change color of LED
value = read_adc();
switch (value) {
case 0:
p1 = value;
output_low(PIN_C0);
p2 = value;
break;
case 50:
p1 = value;
output_high(PIN_C0);
p2 = value;
break;
case 100:
p1 = value;
output_high(PIN_C0);
p2 = value;
break;
case 150:
output_high(PIN_C0);
p1 = 50;
p2 = value;
break;
case 200:
output_low(PIN_C0);
p1 = 0;
p2 = value;
break;
case 250:
p1 = 0;
p2 = value;
output_low(PIN_C0);
break;
}
p1 = value;
p2 = 128 - p1;
set_pwm1_duty(p1);
set_pwm2_duty(p2);
}
}
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|







































