/* =========================================================================
* Project: GPIO_Setting
* File: main.c
* Description: Set GPIO of PORTB
* 1. PORTB I/O state
* - PB4 set input mode and enable pull-high resistor
* - PB2 set output mode
* - PB1 set input mode and enable pull-low resistor
* - PB0 set open-drain output mode
*
* Author: JasonLee
* Version: V1.1
* Date: 2018/09/07
=========================================================================*/
#include <ny8.h>
#include "ny8_constant.h"
#define UPDATE_REG(x) __asm__("MOVR _" #x ",F")
#define pump PORTBbits.PB2
#define LED PORTBbits.PB1
#define key PORTBbits.PB0
struct
{
unsigned delay1:1;
unsigned delay2:1;
unsigned delay3:1;
unsigned delay4:1;
unsigned delay5:1;
unsigned connection:1;
unsigned button:2;
}flag;
int delay_ms = 0;
int delay2_ms = 0;
int delay3_ms = 0;
int delay4_ms = 0;
int delay5_ms = 0;
char key_count = 0;
int objective = 0;
void isr(void) __interrupt(0)
{
if(INTFbits.PBIF) //按键中断 唤醒睡眠
{
INTF = (unsigned char)~(C_INT_PBKey);
}
if(INTFbits.T1IF)
{
INTF = (unsigned char)~(C_INT_TMR1);
TMR1 = 0xff;
if(flag.delay1)
{
delay_ms++;
if(delay_ms == objective)
{
pump = 0;
flag.delay1 = 0;
delay_ms = 0;
flag.delay2 = 0;
delay2_ms = 0;
LED = 0;
flag.button = 0;
// PCON1 = C_TMR1_Dis;
SLEEP();
// PCON1 = C_TMR1_En; // Enable Timer0
}
}
if(flag.delay2)
{
delay2_ms++;
if(delay2_ms == 488)
{
delay2_ms = 0;
LED ^= 1;
}
}
if(flag.delay3)delay3_ms++;
delay4_ms++;
if(flag.delay5)
{
delay5_ms++;
flag.connection = 1;
if(delay5_ms == 1000)
{
delay5_ms = 0;
flag.delay5 = 0;
flag.connection = 0;
objective = 3883;
flag.delay1 = 1;
}
}
}
}
void delayms(int time)
{
flag.delay3 = 1;
while(1)
{
if(delay3_ms == time)break;
}
flag.delay3 = 0;
delay3_ms = 0;
}
void checkbutton()
{
if(delay4_ms > 1)
{
delay4_ms = 0;
if(key == 0)
{
key_count++;
}
else
{
key_count = 0;
}
if(key_count == 10)
{
if(flag.button == 1 & flag.connection == 1)
{
flag.button = 2;
flag.delay5 = 0;
flag.connection = 0;
pump = 1;
flag.delay2 = 1;
objective = 19531 - delay5_ms;
delay5_ms = 0;
flag.delay1 = 1;
while(key == 0);
}
else if(flag.button == 1 | flag.button == 2)
{
flag.button = 0;
flag.delay2 = 0;
LED = 0;
pump = 0;
while(key == 0);
delayms(100);
SLEEP();
flag.button = 1;
flag.delay5 = 1;
flag.connection = 0;
delay5_ms = 0;
flag.delay1 = 0;
delay_ms = 0;
LED = 1;
pump = 1;
while(key == 0);
}
else if(flag.button == 0)
{
flag.button = 1;
flag.delay5 = 1;
flag.connection = 0;
delay5_ms = 0;
flag.delay1 = 0;
delay_ms = 0;
LED = 1;
pump = 1;
while(key == 0);
}
else;
}
}
}
void main(void)
{
BPHCON = (unsigned char)~(C_PB0_PHB); // Enable PB4 Pull-High Resistor,others disable
IOSTB = C_PB0_Input; // Set PB4 & PB1 to input mode,others set to output mode
PORTB = 0x00; // PB2 & PB0 output high
BWUCON = C_PB0_Wakeup;
// PCON = C_ExtINT_En;
UPDATE_REG(PORTB);
TMR1 = 0xff; // Load 0xFF to TMR1 (Initial Timer1 register)
T1CR1 = C_TMR1_Reload | C_TMR1_En; // Enable Timer1, Initial value reloaded from TMR1, Non-stop mode
T1CR2 = C_TMR1_ClkSrc_Inst | C_PS1_Div4; // Enable Prescaler1, Prescaler1 dividing rate = 1:4, Timer1 clock source is instruction clock
INTE = C_INT_TMR1 | C_INT_PBKey; // Enable Timer0、Timer1、WDT overflow interrupt
INTF = 0;
PCON1 = C_TMR1_En; // Enable Timer0
ENI(); // Enable all unmasked interrupts
SLEEP();
flag.button = 1;
flag.delay5 = 1;
flag.connection = 0;
delay5_ms = 0;
flag.delay1 = 0;
delay_ms = 0;
LED = 1;
pump = 1;
while(key == 0);
while(1)
{
checkbutton();
// if(flag.button == 1)
// {
// LED = 1;
// objective = 5;
// flag.delay1 = 1;
// }
// else if(flag.button == 2)
// {
// flag.delay2 = 1;
// objective = 20;
// flag.delay1 = 1;
// }
// else
// {
// flag.delay2 = 0;
// LED = 0;
// flag.delay1 = 0;
// pump = 0;
//// SLEEP();
// }
}
}