// Sys Lib
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Arduino API
#include <MsTimer2.h>
#include <TimerOne.h>
// My Lib
#include <value.h>
#include <My_Arduino_GPIO_Lib_V1_8.h>
//#include <My_Arduino_FND_Lib_V2_7.h>
// PRJ Lib
#define GPIO_IF 1
#define Digit_4 2
//#define Fnd_Dis 3
#define Fnd_Led_Dis 4
//#define En_Font_Dis 5
#include "fnd_dis.h"
#include <My_LCD_User_Font_Lib_V1_4.h>
#define I2C_IF 1
#include <My_Arduino_LiqCry_LCD_Lib_V1_5.h>
#include <My_Arduino_SSD1306_LIB_V1_2.h>
#define Arduino_Mode 1
#define HW_Uart_mode 2
#define Arduino_i2c_Char_LCD 3
#define SSD1306_I2C_OLED 4
extern int out_dev_set_mode;
#include <My_MCU_Printf_Lib_V2_6.h>
#include "key.h"
void tm1_irq()
{
//1ms
fnd_led_dis(ct -> cnt16, map(ct -> cnt16, 1234, 2000, 0, 255));
}
void tm2_irq()
{
// 실습1
//500ms
// if(fg -> run_flag == 1)
// {
// if(mode_cnt == 1) ct-> cnt16 = ((*ct).cnt16 > 2000) ? 1234 : cnt.cnt16 + 1;
// else if(mode_cnt == 2) ct-> cnt16 = ((*ct).cnt16 > 2000) ? 1234 : cnt.cnt16 - 1;
// fg -> t2_flag = 1;
// led_buf2 = ~led_buf2;
// }
}
void setup() {
// put your setup code here, to run once:
// 1. GPIO init
for(int i = 0; i<= 7;)
{ // LED + FND DIR Set
pinMode(LED_pin[i], OUTPUT);
Led_Byte_Out(0xff); // init
i++;
}
pinMode(x1000, OUTPUT);
pinMode(x100, OUTPUT);
pinMode(x10, OUTPUT);
pinMode(x1, OUTPUT);
#if Fnd_Led_Dis
pinMode(led_scan_pin, OUTPUT);
#endif
//--------------------------------------------
// 2. UART init
Serial.begin(115200);
out_dev_set_mode = HW_Uart_IF_Mode;
//-------------------------------------------------
// 3. I2C LCD Init
lcd_init();
// i2c_scan_id();
//Lcd_Test();
// LCD_All_CLr;
// LCD_Home;
// print_xy_str(2,0,"Up Cnt: ");
// print_xy_str(2,1,"Dn Cnt: ");
//------------------------------------------------
// 4. Timer 1, 2 Init
// Timer2 Interrupt
// MsTimer2::set(500, tm2_irq); //1sec
//MsTimer2::start();
// Timer1 Interrupt = IRQ
Timer1.initialize(1000); // 1ms ==fnd display
Timer1.attachInterrupt(tm1_irq);
//----------------------------------------------------
// 5. KEy init
pinMode(sw1, INPUT_PULLUP);
pinMode(sw2, INPUT_PULLUP);
pinMode(sw3, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
key = byte_key_rd();//bit_key_rd();
if(fg -> key_flag == 1) key_chk();
// fnd_led_dis(ct -> cnt16, map(ct -> cnt16, 1234, 2000, 0, 255));
}
//================================================================
#ifndef __KEY__
#define __KEY__
#define sw1 2 // mode
#define sw2 3 // run/stop
#define sw3 17 // up /dn
char bit_key_rd()
{
uint8_t buf = 0;
if(d_in(sw1) == 0) {buf = 1; while(d_in(sw1) == 0){}}
//else if(d_in(sw2) == 0) {buf = 2; while(d_in(sw2) == 0){}}
else if(d_in(sw3) == 0) {buf = 3; while(d_in(sw3) == 0){}}
_delay_ms(40);
if(d_in(sw2) == 0)
{
_delay_ms(20);
if(d_in(sw2) == 0)
{
_delay_ms(10);
if(d_in(sw2) == 0)
{
_delay_ms(5);
if(d_in(sw2) == 0)
{
_delay_ms(2);
buf = 2;
}
}
}
}
/*
while(d_in(sw1) == 0) {}
key = 1;
while(d_in(sw2) == 0) {}
key =2;
*/
// if(d_in(sw1) == 0) buf = 1;
// else if(d_in(sw2) == 0) buf = 2;
// else if(d_in(sw3) == 0) buf = 3;
// while(d_in(sw1) == 0 || d_in(sw2) == 0 || d_in(sw3) == 0)
// {
// //fnd_led_dis(ct -> cnt16, map(ct -> cnt16, 1234, 2000, 0, 255));
// }
fg -> key_flag = (buf != 0) ? 1 : 0;
return buf;
}
char byte_key_rd()
{
uint8_t buf = 0;
if((PIND & 0x0c) != 0x0c)
{
Serial.println((PIND & 0x0c), HEX);
buf = (PIND & 0x0c);
while((PIND & 0x0c) != 0x0c){}
}
else if((PINC & 0x08) != 0x08)
{
Serial.println((PINC & 0x08) | 0x05, HEX);
buf = (PINC & 0x08) | 0x05;
while((PINC & 0x08) != 0x08){}
}
fg -> key_flag = (buf != 0) ? 1 : 0;
return buf;
}
void key_chk()
{
fg -> key_flag = 0;
switch(key)
{
case 1: //bit
case 5: //byte
//mode
//mode_cnt++;
Serial.println("mode");
break;
case 2: // bit
case 4: // byte
ct -> cnt16++;
break;
case 3: // bit
case 8: // byte
ct -> cnt16--;
break;
}
}
#endif