#include <16F684.h>
#device ADC=8
#fuses NOWDT,INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, BROWNOUT
#use delay(clock=8000000)
// Camera Mode
#define LED_MODE 1
#define NON_LED_MODE 0
unsigned int8 Camera_Mode;
// Adc
unsigned int8 value[2];
unsigned int16 value_16[2];
unsigned int8 value_count[2];
void Key_Adc(void)
{
if(++value_count[0] >= 32)
{
value_16[0] /= 32;
if(value[0] > 250)
{
output_high(PIN_C0); // OSD_UP
output_high(PIN_C1); // OSD_RIGHT
output_high(PIN_C2); // OSD_DOWN
output_high(PIN_C3); // OSD_ENTER
output_high(PIN_C4); // OSD_LEFT
}
else if(value[0] > 230)
{
output_low(PIN_C2); // OSD_DOWN
}
else if(value[0] > 200)
{
output_low(PIN_C0); // OSD_UP
}
else if(value[0] > 150)
{
output_low(PIN_C1); // OSD_RIGHT
}
else if(value[0] > 100)
{
output_low(PIN_C4); // OSD_LEFT
}
else
{
output_low(PIN_C3); // OSD_ENTER
}
value_count[0] = 0;
}
else
{
value_16[0] += value[0];
}
value[0] = read_adc(); // Built-in A/D read function
}
#define CDS_TIME 50 // * 64
#define DAY 0
#define NIGHT 1
unsigned int8 Day_Night;
void Cds_Adc(void)
{
static unsigned int16 Day_Time;
static unsigned int16 Night_Time;
if(++value_count[1] >= 32)
{
value_16[1] /= 32;
if(value[1] > 47) // NIGHT -> DAY (0.6V)
{
if(++Day_Time >= CDS_TIME)
{
Day_Time = CDS_TIME + 1;
if(Day_Night != DAY)
{
Day_Night = DAY;
// Night Out Low
output_low(PIN_A1);
output_low(PIN_A5);
// Motor R
output_high(PIN_A0); // MOTOR_R
output_low(PIN_C5); // MOTOR_L
delay_ms(150);
output_low(PIN_A0); // MOTOR_R
}
}
Night_Time = 0;
}
else if(value[1] < 31) // DAY -> NIGHT (0.4V)
{
if(++Night_Time >= CDS_TIME)
{
Night_Time = CDS_TIME + 1;
if(Day_Night != NIGHT)
{
Day_Night = NIGHT;
// Night Out High
output_high(PIN_A1);
output_high(PIN_A5);
// Motor L
output_low(PIN_A0); // MOTOR_R
output_high(PIN_C5); // MOTOR_L
delay_ms(150);
output_low(PIN_C5); // MOTOR_L
}
}
Day_Time = 0;
}
else
{
Night_Time = 0;
Day_Time = 0;
}
value_count[1] = 0;
}
else
{
value_16[1] += value[1];
}
value[1] = read_adc(); // Built-in A/D read function
}
void Motor_Control(unsigned int8 Command)
{
static unsigned int8 Command_Old;
if(Command == Command_Old) return;
switch(Command)
{
case 0 : // NIGHT -> DAY
// Motor R
output_high(PIN_A0); // MOTOR_R
output_low(PIN_C5); // MOTOR_L
delay_ms(150);
output_low(PIN_A0); // MOTOR_R
break;
case 1 : // DAY -> NIGHT
output_low(PIN_A0); // MOTOR_R
output_high(PIN_C5); // MOTOR_L
delay_ms(150);
output_low(PIN_C5); // MOTOR_L
break;
}
Command_Old = Command;
}
void main(void)
{
static unsigned int8 Adc_Select;
// PORT Init
set_tris_a(0x1C);
set_tris_c(0x00);
// output_a(0x00);
// output_c(0xFF);
// ADC Init
setup_adc(ADC_CLOCK_INTERNAL); // Built-in A/D setup function
// Key_Adc
/*
setup_adc_ports(sAN2, VSS_VDD); // Built-in A/D setup function
set_adc_channel(2); // Built-in A/D setup function
*/
// Cds_Adc
/*
setup_adc_ports(sAN4, VSS_VDD); // Built-in A/D setup function
set_adc_channel(3); // Built-in A/D setup function
*/
// GPIO Init
output_high(PIN_C0); // OSD_UP
output_high(PIN_C1); // OSD_RIGHT
output_high(PIN_C2); // OSD_DOWN
output_high(PIN_C3); // OSD_ENTER
output_high(PIN_C4); // OSD_LEFT
// Start Up Pin Out
output_high(PIN_A0); // MOTOR_R
output_low(PIN_C5); // MOTOR_L
// Start Up Delay
delay_ms(100);
output_low(PIN_A0); // MOTOR_R
Camera_Mode = input(PIN_A3); // 0 = LED_MODE, 1 = NON_LED_MODE
if(Camera_Mode == NON_LED_MODE)
{
set_tris_a(0x3C); // RA5 == Input
}
while(1)
{
delay_ms(1);
Adc_Select ^= 1;
if(Adc_Select)
{
Key_Adc();
// Key -> Cds
setup_adc_ports(sAN4, VSS_VDD);
set_adc_channel(3);
}
else
{
if(Camera_Mode == LED_MODE)
{
Cds_Adc();
}
else if(Camera_Mode == NON_LED_MODE)
{
Motor_Control(input(PIN_A5));
}
// Cds -> Key
setup_adc_ports(sAN2, VSS_VDD);
set_adc_channel(2);
}
}
}
'마이컴 & 프로테우스 VSM > PIC16F684' 카테고리의 다른 글
- PIC16F684 ADC (0) | 2016.04.25 |
---|